tensorflow中显示 list 无法使用tf.cast()

num_examples = 10000
num_iter = int(math.ceil(num_examples / batch_size))
true_count = 0
total_samples_count = num_iter * batch_size
step = 0
while step < num_iter:
    image_batch,label_batch = sess.run([images_test,labels_test])
    predictions = sess.run([top_k_op],feed_dict={image_holder: image_batch, label_holder:label_batch})
    true_count += np.sum(predictions)
    step += 1
precision = true_count/total_samples_count
print('precision @ 1 = %.3f' %precision)

其中

1.情况1

    predictions = sess.run(top_k_op, feed_dict={image_holder: image_batch,label_holder:label_batch})
    print(type(predictions))

打印值为<class ‘numpy.ndarray’>,tensor经过Session.run()自动生成numpy数组

参考:https://stackoverflow.com/questions/34097281/how-can-i-convert-a-tensor-into-a-numpy-array-in-tensorflow

2.情况2

    predictions = sess.run([top_k_op],  feed_dict={image_holder: image_batch,label_holder:label_batch})
    print(type(predictions))

打印值为<class ‘list’>
当使用 tf.cast()predictions进行变换时,出现错误:

ValueError: Argument must be a dense tensor: [array([False, True, False.......])]- got shape [1,128], but wanted [1].

问题
[ ] 使得原本的array变成了list(这种[array([False, True, False…])]),这种list+array的混搭模式是list。
解决方法
再将其转换为array就可以了:predictions = np.array(predictions)然后再使用 tf.cast() 就没有问题了。或者像情况1一样去除 [top_k_op] 外的 [ ]

参考: https://blog.csdn.net/u010698086/article/details/78175933
(不知道理解的有没有问题。。)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值