简体   繁体   English

如何创建 0 维 tf.Variable?

[英]How to create a 0 dimension tf.Variable?

dimension tf.Variable.尺寸 tf.Variable。 Before I did that via:在我这样做之前:

tensor = tf.zeros([0,], tf.float32) * (rank + 1)
w = tf.Variable(tensor.numpy().tolist())

however it says that AttributeError: 'Tensor' object has no attribute 'numpy' when I put these two lines in a new project.但是它说AttributeError: 'Tensor' object has no attribute 'numpy'当我将这两行放在一个新项目中时。 I think it is because eager_mode is disabled in that project i am working on?我认为这是因为在我正在从事的项目中禁用了 eager_mode? Is there any way I can create the same Viriable directly without numpy() ?有什么方法可以在没有numpy()的情况下直接创建相同的 Viriable ?

To create a tensor of dimension 0, run the following code要创建维度为 0 的张量,请运行以下代码

## rank 0
# Default name
rank_0_tensor = tf.constant(4, tf.int16) 
print(rank_0_tensor)
        
Output: tf.Tensor(4, shape=(), dtype=int16)

Reference: https://www.tensorflow.org/guide/tensor参考: https://www.tensorflow.org/guide/tensor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM