tf.ones公司(数据类型)=tf.32浮动)或者tf.ones公司(数据类型)=tf.int32型)表现不同

2024-06-17 14:52:54 发布

您现在位置:Python中文网/ 问答频道 /正文

当我使用dtype=tf.32浮动

import tensorflow as tf

z = tf.Variable(tf.ones(5, dtype=tf.float32), name='z')
init = tf.global_variables_initializer()
with tf.Session() as session:

        session.run(init)
        print("z=" , session.run(z))
        for i in range(5):
            z = z +1 
            print(session.run(z))

结果是这样的:

('z=', array([0., 0., 0., 0., 0.], dtype=float32))
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]

但是,当我使用数据类型=tf.int32型

z = tf.Variable(tf.ones(5, dtype=tf.int32), name='z')

结果是:

('z=', array([1, 1, 1, 1, 1], dtype=int32))
[2 2 2 2 2]
[3 3 3 3 3]
[4 4 4 4 4]
[5 5 5 5 5]
[6 6 6 6 6]

你能帮我解释一下为什么使用tf.32浮动?你知道吗


Tags: runnameimportinitsessiontftensorflowas