首页 > 解决方案 > tensorflow tf.cast 的奇怪行为

问题描述

tf.cast有没有人在整数和无符号整数之间有奇怪的行为?我运行以下示例(在 上https://colab.research.google.com):

import tensorflow as tf

c = tf.constant([5, 6, 7, 8, 9, 10], dtype=tf.int32)
x = tf.cast(c, dtype=tf.uint32)
with tf.Session() as sess:
    x_raw = sess.run(x)
    print(x_raw.dtype)
    print(x_raw)

print(tf.__version__)

并得到结果:

uint32
[0 0 0 0 0 0]
1.14.0-rc1

我是否以错误的方式使用演员表?或者 TF 中的类型转换是否存在一些错误?

- - - - - - - - - 更新 - - - - - - - - -

有些人可能已经运行了上面的代码并得到了正确的结果。实际上,当我只运行一次(在笔记本单元中)时,我也得到了正确的,但如果你再次运行单元,它会让你大吃一惊。重新创建事件的更好方法可能是运行以下命令:

import tensorflow as tf

c = tf.constant([5, 6, 7, 8, 9, 10], dtype=tf.int32)
d = tf.constant([5, 6, 7, 8, 9, 10], dtype=tf.int32)
x = tf.cast(c, dtype=tf.uint32)
y = tf.cast(c, dtype=tf.uint32)
with tf.Session() as sess:
    x_raw, y_raw = sess.run([x, y])
    print(x_raw.dtype, y_raw.dtype)
    print(x_raw)
    print(y_raw)

print(tf.__version__)

在我的情况下,这将保证一个错误。似乎 TF 运行时发生了一些非常臭的事情......

标签: pythontensorflow

解决方案


推荐阅读