如何在int32张量流float32中投射张量。我不明白有什么tf.cast作用。它似乎没有做任何事情。
import tensorflow as tf
import numpy as np
tf.enable_eager_execution()
a = tf.constant([[1, 2, 3, 4], [1, 2, 3, 4]])
b = tf.cast(a, dtype=tf.float32)
print(tf.shape(a))
print(tf.shape(b))
Run Code Online (Sandbox Code Playgroud)
输出;
tf.Tensor([2 4], shape=(2,), dtype=int32) #a
tf.Tensor([2 4], shape=(2,), dtype=int32) #b
Run Code Online (Sandbox Code Playgroud)
如果你只是使用;
print(a)
print(b)
Run Code Online (Sandbox Code Playgroud)
你会得到正确的结果;
tf.Tensor(
[[1 2 3 4]
[1 2 3 4]], shape=(2, 4), dtype=int32) #a
tf.Tensor(
[[1. 2. 3. 4.]
[1. 2. 3. 4.]], shape=(2, 4), dtype=float32) #b
Run Code Online (Sandbox Code Playgroud)
所以tf.cast()按预期工作!
随着tf.shape()你得到解释输入的形状细节的结果。
返回: 类型为 out_type 的张量。
out_type:(可选)操作的指定输出类型(int32 或 int64)。默认为 tf.int32
因此,dtype的tf.shape()结果,是dtype由此产生的“的形状,详细说明张量”,而不是a,或b
| 归档时间: |
|
| 查看次数: |
2065 次 |
| 最近记录: |