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

时间:2016-05-14 00:16:22

标签: tensorflow

TF中的新高阶函数详见:

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

特别是,map函数看起来很有用。以下是他们为本教程编写的内容:

  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。

2 个答案:

答案 0 :(得分:1)

您的代码似乎没问题,但我可以使用>>> from googlefinance import getQuotes >>> fra_bmw = getQuotes('FRA:BMW') >>> fra_bmw[0]["Index"] u'FRA' 这样的代码修复它:

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 :(得分:0)

你必须使用np.array作为elems,但我不明白为什么

以下代码:

// max exclusive (not included!)
public static BigInteger GetRandom(RNGCryptoServiceProvider rng, BigInteger min, BigInteger max)
{
    // shift to 0...max-min
    BigInteger max2 = max - min;

    int bits = max2.bitCount();

    // 1 bit for sign (that we will ignore, we only want positive numbers!)
    bits++;

    // we round to the next byte
    int bytes = (bits + 7) / 8;

    int uselessBits = bytes * 8 - bits;

    var bytes2 = new byte[bytes];

    while (true)
    {
        rng.GetBytes(bytes2);

        // The maximum number of useless bits is 1 (sign) + 7 (rounding) == 8
        if (uselessBits == 8)
        {
            // and it is exactly one byte!
            bytes2[0] = 0;
        }
        else
        {
            // Remove the sign and the useless bits
            for (int i = 0; i < uselessBits; i++)
            {
                //Equivalent to
                //byte bit = (byte)(1 << (7 - (i % 8)));
                byte bit = (byte)(1 << (7 & (~i)));

                //Equivalent to
                //bytes2[i / 8] &= (byte)~bit;
                bytes2[i >> 3] &= (byte)~bit;
            }
        }

        var bi = new BigInteger(bytes2);

        // If it is too much big, then retry!
        if (bi >= max2)
        {
            continue;
        }

        // unshift the number
        bi += min;
        return bi;
    }
}