import tensorflow as tf
import numpy as np
layer1_weight = tf.Variable(tf.zeros([2 , 3]))
layer1_bias = tf.Variable(tf.zeros([3 , 1]))
layer2_weight = tf.Variable(tf.zeros([3, 1]))
layer2_bias = tf.Variable(tf.constant([[0.]]))
input = tf.placeholder(tf.float32 , [2 , 1] )
result = tf.placeholder(tf.float32 ,[1 , 1] )
data_input = [np.float32([[0.],[0.]]) , np.float32([[0.],[1.]]) ,
np.float32([[1.],[0.]]) , np.float32([[1.],[1.]])]
data_output = [np.float32([[0.]]) , np.float32([[1.]]) ,
np.float32([[1.]]) , np.float32([[0.]])]
layer1_output = tf.add(tf.matmul(tf.transpose(layer1_weight) , input) ,
layer1_bias )
layer2_output = tf.add(tf.matmul(tf.transpose(layer2_weight) ,
layer1_output) , layer2_bias)
print (data_input[0])
loss = tf.square(tf.subtract(result , layer2_output))
optimizer = tf.train.GradientDescentOptimizer(0.0001)
train_step = optimizer.minimize(loss)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for i in range(30) :
j = int(i % 4)
result = data_output[j]
sess.run(train_step , feed_dict= {input : data_input[j] , result :
data_output[j]})
print(str(layer2_output))
代码返回错误
TypeError:不可用类型:' numpy.ndarray'
这里我试图用神经网络实现XOR门,但是找不到错误。
答案 0 :(得分:2)
首先,您将result
定义为占位符,但稍后将其重新定义为
result = data_output[j]
。这是出错的时候,因为您无法再将值提供给feed_dict
。