未调用张量流映射函数 [英] tensorflow map function not being invoked
本文介绍了未调用张量流映射函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
根据我对 tensorflow 中 map 函数的理解,我预计 my_map 会被调用 60,000 次,但它只被调用了一次.
based on my understanding of map function in tensorflow, I expected that my_map would be invoked 60,000 times but it was invoked only once.
输出
使用 TensorFlow 后端.(60000, 28, 28)Tensor("map/while/TensorArrayReadV3:0", shape=(28, 28), dtype=uint8)
Using TensorFlow backend. (60000, 28, 28) Tensor("map/while/TensorArrayReadV3:0", shape=(28, 28), dtype=uint8)
进程结束,退出代码 0
Process finished with exit code 0
代码:
import tensorflow as tf
from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
def my_map(elem):
print(elem)
return elem
print(train_images.shape)
tf_map = tf.map_fn(fn=my_map, elems=train_images)
with tf.Session() as sess:
sess.run(tf_map)
我做错了什么?任何帮助,将不胜感激.
What did I do wrong? Any help would be appreciated.
推荐答案
my_map 中的打印将无法用于打印.请测试一下:
The print in the my_map won't work for printing. Please, test this:
import tensorflow as tf
from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
def my_map(elem):
#print(elem)
elem = elem + 1
return elem
print(train_images.shape)
tf_map = tf.map_fn(fn=my_map, elems=train_images)
with tf.Session() as sess:
print(sess.run(tf_map[0,0]))
这篇关于未调用张量流映射函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文