为什么我可以将 Float 转换为张量,如下所示:
tensor = convert(TensorFlow.Tensor{Float32}, 0.05)
<Tensor Cast_8:1 shape=() dtype=Float32>
但不是 float 的张量。以下命令:
convert(Float32, tensor)
返回以下错误:
ERROR: MethodError: Cannot `convert` an object of type TensorFlow.Tensor{Float32} to an object of type Float32
This may have arisen from a call to the constructor Float32(...),
since type constructors fall back to convert methods.
如何在变量或数组中使用张量的值?
谢谢
最佳答案
在 TensorFlow 中(与实际数学不同)
张量并不是真正(仅)包装一些数字的东西。
它位于计算图中的符号值中,它可能恰好包含常量(如您的情况),但也可能包含变量和占位符。因此,一般来说,转换
为数字是没有意义的——变量和占位符在您刚刚定义它们时没有具体值。
您需要做的是在 session
中运行
张量:
sess = Session(Graph())
run(sess, tensor) # plus necessary initializers and placeholders
这将评估计算图,直到可以确定张量
的值。看看 basic examples ;所有 TensorFlow 代码都是此方案的重复:定义图形,然后使用为占位符和初始值提供的实际数据在 session 中运行它。
关于TensorFlow、Julia - 张量和 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50465195/