tensorflow中出现{TypeError}unhashable type: 'numpy.ndarray'

本人实验中使用feed的方式填充数据,sess处的代码如下:

1 with tf.Session() as sess:
2     init = tf.global_variables_initializer()
3     sess.run(init)
4     for epoch in range(a.epochs):
5         input, target = load_batch_data(batch_size=16, a=a)
6         batch_input = input.astype(np.float32)
7         batch_target = target.astype(np.float32)
8         sess.run(predict_real, feed_dict={input: batch_input, target: batch_target})

运行的时候出现:{TypeError}unhashable type: ‘numpy.ndarray’

后  来  发  现:

在session外边定义input和target的时候是这么写的:

1 input = tf.placeholder(dtype=tf.float32, shape=[None, image_size, image_size, num_channels])
2 target = tf.placeholder(dtype=tf.float32, shape=[None, image_size, image_size, num_channels])

然而,我在开启session后又定义了input,target。这导致我在运行下面这行代码的时候,

1 sess.run(predict_real, feed_dict={input: batch_input, target: batch_target})

出现了{TypeError}unhashable type: ‘numpy.ndarray’这样的错误。然而此input和target非session外面的input和target。知道是这个原因后,改正的话就很简单了,修改session内input和target的名称即可,如下:

 1     with tf.Session() as sess:
 2         init = tf.global_variables_initializer()
 3         sess.run(init)
 4         if a.mode == 'train':
 5             for epoch in range(a.epochs):
 6                 batch_input, batch_target = load_batch_data(a=a)
 7                 batch_input = batch_input.astype(np.float32)
 8                 batch_target = batch_target.astype(np.float32)
 9                 sess.run(model, feed_dict={input: batch_input, target: batch_target})
10                 print('epoch' + str(epoch) + ':')
11             saver.save(sess, 'model_parameter/train.ckpt')
12             print('training finished!!!')
13         elif a.mode == 'test':
14             #ceshi
15             ckpt = tf.train.latest_checkpoint(a.checkpoint)
16             saver.restore(sess, ckpt)
17             # 获取测试时候的图像,然后添加标签
18             batch_input, _ = load_batch_data(a=a)
19             # batch_input = batch_input / 255.
20             batch_input = batch_input.astype(np.float32)
21             generator_output = sess.run(test_output, feed_dict={input: batch_input})
22             # 对结果进行处理,图像通道上减去3,得到rgb图像
23             result = process_generator_output(generator_output)
24             if result:
25                 print('测试完成!')
26         else:
27             print('the MODE is not avaliable...')

 

    原文作者:MySummerRain
    原文地址: https://www.cnblogs.com/ZT-SummerRain/p/9252673.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞