Tensorflow错误“unhashable type:'numpy.ndarray'”

时间:2023-01-25 21:21:16
    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))

The code is returning Error

代码返回错误

TypeError: unhashable type: 'numpy.ndarray'

TypeError:不可用类型:'numpy.ndarray'

Here I am trying to implement XOR gate with neural network but can't find error.

在这里,我试图用神经网络实现异或门,但无法找到错误。

1 个解决方案

#1


1  

First you define result to be a placeholder, but later redefine it as result = data_output[j]. This is when it gets wrong, because you can no longer feed the value to feed_dict.

首先,您将结果定义为占位符,但稍后将其重新定义为result = data_output [j]。这是出错的时候,因为您无法再将该值提供给feed_dict。

#1


1  

First you define result to be a placeholder, but later redefine it as result = data_output[j]. This is when it gets wrong, because you can no longer feed the value to feed_dict.

首先,您将结果定义为占位符,但稍后将其重新定义为result = data_output [j]。这是出错的时候,因为您无法再将该值提供给feed_dict。