【问题标题】:How to prevent Tensorflow unpack() method casting to float64如何防止 TensorFlow unpack() 方法转换为 float64
【发布时间】:2016-04-05 12:54:01
【问题描述】:

我正在尝试使用 seq2seq.rnn_decoder() 在 Tensorflow 中设置顺序 RNN。 rnn_decoder() 想要的输入是张量列表,因此为了生成它,我传入了一个 rank-3 张量并使用 tf.unpack() 将其放入列表中。当我传入的 float32 数组通过 tf.unpack() 变成 float64 张量时,就会出现问题,使其与模型的其余部分不兼容。这是我整理的代码,以使我相信罪魁祸首是 tf.unpack():

inputDat = loader.getSequential(BATCH_SIZE)
print(inputDat.shape)

输出(BATCH_SIZE 为 5,序列长度为 10):

(10, 5, 3)

然后我可以在 Tensorflow 会话中加载这些数据:

sess = tf.InteractiveSession()
input_tensor = tf.constant(inputDat.astype('float32'), dtype=tf.float32)
print "Input tensor type: " + str(type(input_tensor.eval()[0,0,0]))
input_tensor = tf.unpack(inputDat)
print "Input tensor shape: " + str(len(input_tensor)) + "x" + str(input_tensor[0].eval().shape)
print "Input tensor type: " + str(type(input_tensor[0].eval()[0,0]))

输出:

Input tensor type: <type 'numpy.float32'>
Input tensor shape: 10x(5, 3)
Input tensor type: <type 'numpy.float64'>

这里发生了什么?使用 FOR 循环遍历每个顺序条目并重新转换它似乎是错误的方法,而且我无法在 Tensorflow 中找到一个方法来转换列表的每个成员。

【问题讨论】:

    标签: python numpy tensorflow


    【解决方案1】:

    您不需要 for 循环:您可以使用 tf.cast()。

    例子:

    input_tensor = tf.unpack(inputDat) # result is 64-bit
    input_tensor = tf.cast(input_tensor, tf.float32) # now it's 32-bit
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-14
      • 2020-10-28
      • 1970-01-01
      • 2020-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      相关资源
      最近更新 更多