Tensorflow Bug :got shape [1, 7], but wanted [1].

import numpy as np
import tensorflow as tf

arg =np.array([-1.0560990e-03 , 1.4153031e+00 -1.0698825e-01 , 1.9480248e-01,
  1.2305677e-03 , 2.4234470e-02, 0.0000000e+00 , 0.0000000e+00])
arg = [arg]
arg = tf.convert_to_tensor(arg)  #, dtype=tf.float32
print(arg)

error:

Traceback (most recent call last):
  File "/Users/closer./PycharmProjects/tensorflow_3.6/Youtube/Policy/func_test.py", line 12, in <module>
    arg = tf.convert_to_tensor(arg)  #, dtype=tf.float32
  File "/Users/closer./opt/miniconda3/envs/tensorflow_3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 1184, in convert_to_tensor
    return convert_to_tensor_v2(value, dtype, preferred_dtype, name)
  File "/Users/closer./opt/miniconda3/envs/tensorflow_3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 1242, in convert_to_tensor_v2
    as_ref=False)
  File "/Users/closer./opt/miniconda3/envs/tensorflow_3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 1297, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/Users/closer./opt/miniconda3/envs/tensorflow_3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py", line 286, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/Users/closer./opt/miniconda3/envs/tensorflow_3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py", line 227, in constant
    allow_broadcast=True)
  File "/Users/closer./opt/miniconda3/envs/tensorflow_3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py", line 265, in _constant_impl
    allow_broadcast=allow_broadcast))
  File "/Users/closer./opt/miniconda3/envs/tensorflow_3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/tensor_util.py", line 458, in make_tensor_proto
    _GetDenseDimensions(values)))
ValueError: Argument must be a dense tensor: [array([-1.05609900e-03,  1.30831485e+00,  1.94802480e-01,  1.23056770e-03,
        2.42344700e-02,  0.00000000e+00,  0.00000000e+00])] - got shape [1, 7], but wanted [1].

Process finished with exit code 1

修改一下array

import numpy as np
import tensorflow as tf

arg =np.array([-1.0560990e-03 , 1.4153031e+00 -1.0698825e-01 , 1.9480248e-01,
  1.2305677e-03 , 2.4234470e-02, 0.0000000e+00 , 0.0000000e+00])
arg = [arg]
arg = np.array(arg)  # 添加这一句!
arg = tf.convert_to_tensor(arg)  #, dtype=tf.float32
print(arg)

完美运行

Tensor("Const:0", shape=(1, 7), dtype=float64)

Process finished with exit code 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值