Python,打印执行两次

时间:2020-07-18 11:38:36

标签: python printing

我已经开始使用Tariq Rashid的书来研究Python和神经网络。仔细阅读并尝试了所有步骤。有一点,我应该测试一种方法,如果它执行正确。所以我做了什么,我使用print强制转换了该方法(该方法具有返回值)。现在我的问题是,它工作正常,但是尽管输出应为单个数组,但它们是具有不同值的2个数组。当我尝试调试文件时,它也通过print命令执行了2次,其中只有1次。

我唯一的区别是,我将 init 用作主类(来自Java),并在其中创建了神经网络作为对象。

还可以提及的是,如果我在神经网络文件中执行相同的过程,则可以正常工作。

from src2.neuralNetwork import NeuralNetwork

network = NeuralNetwork(3, 3, 3, 0.3)
print(network.query([1.0, 0.5, -1.5]))

这是__初始化__.py文件。

import numpy
import scipy.special


class NeuralNetwork:

    def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
        self.inodes = inputnodes
        self.hnodes = hiddennodes
        self.onodes = outputnodes

        self.lr = learningrate

        self.wih = (numpy.random.rand(self.hnodes, self.inodes) - 0.5)
        self.who = (numpy.random.rand(self.onodes, self.hnodes) - 0.5)

        self.activation_function = lambda x: scipy.special.expit(x)
        pass

    def train(self):
        pass

    def query(self, inputs_list):
        inputs = numpy.array(inputs_list, ndmin=2).T

        hidden_inputs = numpy.dot(self.wih, inputs)
        hidden_outputs = self.activation_function(hidden_inputs)

        final_inputs = numpy.dot(self.who, hidden_outputs)
        final_outputs = self.activation_function(final_inputs)
        return final_outputs

那是神经网络的文件。

感谢您的回答。

编辑: 根据要求,输出。它们不是随机值的一致原因。

This is the output when printing from the __ init __ file

This is the output when printing from the neural network file

1 个答案:

答案 0 :(得分:0)

我用两个文件对此进行了测试。这是每个代码。 testinit.py

import numpy
import scipy.special


class NeuralNetwork:

    def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
        self.reset(inputnodes, hiddennodes, outputnodes, learningrate)
        self.inodes = inputnodes
        self.hnodes = hiddennodes
        self.onodes = outputnodes

        self.lr = learningrate

        self.wih = (numpy.random.rand(self.hnodes, self.inodes) - 0.5)
        self.who = (numpy.random.rand(self.onodes, self.hnodes) - 0.5)

        self.activation_function = lambda x: scipy.special.expit(x)
        
    
        pass
    def reset(self, inputnodes, hiddennodes, outputnodes, learningrate):
        self.inodes = inputnodes
        self.hnodes = hiddennodes
        self.onodes = outputnodes

        self.lr = learningrate

        self.wih = (numpy.random.rand(self.hnodes, self.inodes) - 0.5)
        self.who = (numpy.random.rand(self.onodes, self.hnodes) - 0.5)
    def train(self):
        pass

    def query(self, inputs_list):
        inputs = numpy.array(inputs_list, ndmin=2).T

        hidden_inputs = numpy.dot(self.wih, inputs)
        hidden_outputs = self.activation_function(hidden_inputs)

        final_inputs = numpy.dot(self.who, hidden_outputs)
        final_outputs = self.activation_function(final_inputs)
        return final_outputs


network = NeuralNetwork(3, 3, 3, 0.3)
print(network.query([1.0, 0.5, -1.6]))

和importingclass.py

from testinit import NeuralNetwork #only consists of this line

问题似乎是当您在原始文件中定义网络并执行print命令,然后将该文件导入新文件“ importingclass.py”时,即使没有代码,importingclass.py也将执行原始“ testinit.py”文件中定义的打印命令。

您可以使用我提供的文件代码自己尝试一下,您将看到执行“ importingclass.py”文件将打印一次且不带代码,前提是您使用给定的“ testinit.py”。 / p>

在两个文件中只有一个打印命令实例仅会产生一个打印输出,而在两个文件中都有一个打印命令实例(共两个)将产生两个输出。这是我得出的结论。

这是我的初始化文件的输出(条件是导入类的文件中没有打印命令) enter image description here

因此,解决方案是不实例化对象并将其打印在要导入的init文件中,因为将创建该对象并从导入中一次打印该对象,并使用导入从文件中打印一次(我相信Python在导入文件的行中运行一次)。

相关问题