如何将张量变量传递给feed_dict?

时间:2019-08-27 13:59:18

标签: python numpy tensorflow neural-network

我有一个基本的神经网络,它具有功能run()。它接受输入并提供神经网络的输出。在函数主体中,我想在运行会话以计算输出时权重feed_dict。由于这些权重是tf.compat.v1.variable,因此我遇到了错误ValueError: Setting an array element with sequence

我最初以为是引起错误的输入,因此,我确保使用与tf.multiply相同的权重尺寸,并且由于错误仍然存​​在,因此我确保转换了numpy数组进入可能导致错误的张量对象。但是ti仍然不起作用。

def run(self, carollis_input):
        self.normalization(carollis_input)
        c_in = np.array(carollis_input)
        c_input = tf.compat.v1.convert_to_tensor(c_in, tf.float64)
        #'finding the output of the input layer'
        #with tf.Session() as sess1_2:
        input_weight = tf.compat.v1.placeholder(tf.float64, shape=(self.neurons, 1))
        input_bias = tf.compat.v1.placeholder(tf.float64, shape=(self.neurons, 1))
        hidden_weight = tf.compat.v1.placeholder(tf.float64, shape=(self.neurons, 1))
        hidden_bias =tf.compat.v1.placeholder(tf.float64, shape=(self.neurons, 1))
        output_weight = tf.compat.v1.placeholder(tf.float64, shape=(4, 1))
        output_bias = tf.compat.v1.placeholder(tf.float64, shape=(self.neurons, 1))

        knowledge_input = tf.add(tf.multiply(c_input, input_weight), input_bias)
        with tf.Session() as ses1:
            ses1.run(knowledge_input, feed_dict={input_weight: self.weight_in, input_bias:self.bias_in})
        knowledge_hidden = tf.nn.leaky_relu(knowledge_input, alpha=0.01) 
        #'calculating the output of hidden layer'
        knowledge_hidden_output = 3.14*(tf.add(tf.multiply(knowledge_hidden, hidden_weight), hidden_bias))#input function of hidden layer
        knowledge_hidden_out = tf.nn.leaky_relu(knowledge_hidden_output, alpha=0.01, name='leaky_relu')

        with tf.Session() as ses2:
            ses2.run(knowledge_hidden_out, feed_dict={hidden_weight: self.weight_hid, hidden_bias:self.bias_hid})
        #'calculating the input of output layer'
        tf.reshape(knowledge_hidden_out, [4, 2])#for quadrant method
        in_out = tf.add(tf.multiply(knowledge_hidden_out, output_weight), output_bias)
        with tf.Session() as ses3:
            ses3.run(in_out, feed_dict={output_weight: self.weight_out, output_bias: self.bias_out})
        #'finding the softmax output of the neurons'
        softmax_output = np.array(4)
        softmax_output = self.out_softmax(in_out)  # this gives the softmax output and stores it in the newly created array
        return softmax_output

经过其他答案之后,我认为可能是由于我试图输入一个张量变量(即权重和feed_dict的偏差)导致的错误。如果那是问题,我该如何解决,请帮助我。确切的错误是

File "/home/microbot/catkin_ws/src/spider/spider_control/knowledge_transfer.py", line 85, in run
    ses1.run(knowledge_input, feed_dict={input_weight: self.weight_in, input_bias:self.bias_in})
  File "/home/microbot/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 950, in run
    run_metadata_ptr)
  File "/home/microbot/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1142, in _run
    np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
  File "/home/microbot/.local/lib/python3.6/site-packages/numpy/core/_asarray.py", line 85, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence

编辑: 权重在类初始化器内部进行初始化,即

def __init__(self, neurons, layers):
        self.neurons = neurons
        self.layers =  layers
        self.weight_initer = tf.truncated_normal_initializer(mean=0.0, stddev=0.01)
        self.weight_in = tf.get_variable(name="Weight_input", dtype=tf.float64, shape=[self.neurons, 1], initializer=self.weight_initer)
        self.weight_initer1 = tf.truncated_normal_initializer(mean=1.0, stddev=0.01)
        self.weight_hid = tf.get_variable(name="Weight_hidden", dtype=tf.float64, shape=[self.neurons, 1], initializer=self.weight_initer1)
        self.weight_initer2 = tf.truncated_normal_initializer(mean=2.0, stddev=0.01)
        self.weight_out = tf.get_variable(name="Weight_output", dtype=tf.float64, shape=[4, 2], initializer=self.weight_initer2)
        self.bias_initer =tf.truncated_normal_initializer(mean=0.1, stddev=0.01)
        self.bias_in  =tf.get_variable(name="Bias_input", dtype=tf.float64, shape=[self.neurons, 1], initializer=self.bias_initer)
        self.bias_initer1 =tf.truncated_normal_initializer(mean=0.2, stddev=0.01)
        self.bias_hid = tf.get_variable(name="Bias_hidden", dtype=tf.float64, shape=[self.neurons, 1], initializer=self.bias_initer1)
        self.bias_initer2 =tf.truncated_normal_initializer(mean=0.3, stddev=0.01)
        self.bias_out = tf.get_variable(name="Bias_output", dtype=tf.float64, shape=[4, 1], initializer=self.bias_initer2)

尽管我现在有两种不同的困惑,一种与图形范围有关,另一种与权重初始化有关。由于我已经在初始化程序中定义了权重张量,因此这将是默认图,而我尝试在运行函数内部构建的图是否是次要图?

0 个答案:

没有答案