TensorFlow 中的高阶函数 - 如何使用?

Higher Order Functions in TensorFlow - How to use?

此处详细介绍了 TF 中新的高阶函数:

https://www.tensorflow.org/versions/r0.8/api_docs/python/functional_ops.html#map_fn

特别是地图功能看起来很有用。以下是他们为教程编写的内容:

  elems = [1, 2, 3, 4, 5, 6]
  squares = map_fn(lambda x: x * x, elems)
  # squares == [1, 4, 9, 16, 25, 36]

因此我创建了一个空的 python 文件:

import tensorflow as tf
elems = [1, 2, 3, 4, 5, 6]
squares = tf.map_fn(lambda x: x * x, elems)

运行 这给出了这个错误:

/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.pyc in make_tensor_proto(values, dtype, shape)
    323   else:
    324     if values is None:
--> 325       raise ValueError("None values not supported.")
    326     # if dtype is provided, forces numpy array to be the type
    327     # provided if possible.

ValueError: None values not supported.

有人知道这是怎么回事吗?谢谢!

编辑:我使用的是 TensorFlow 版本 0.8。

您的代码似乎没问题,但我能够使用 numpy array 修复它,如下所示:

import tensorflow as tf
import numpy as np

elems = np.array([1, 2, 3, 4, 5, 6], dtype="float32")
squares = tf.map_fn(lambda x: x * x, elems)

sess = tf.Session()
sess.run(squares)

这输出:

[  1.   4.   9.  16.  25.  36.]