list, ndarray, tensor间的相互转化

list 2 darray

In [39]: list=[1,2,3,4,5,6]

In [40]: type(list)
Out[40]: list

In [42]: array=np.array(list) //creare array by list

In [43]: type(array)
Out[43]: numpy.ndarray
 

ndarray to list

In [44]: list=array.tolist()

In [45]: list
Out[45]: [1, 2, 3, 4, 5, 6]

In [32]: n1 = np.array([1,2, 3, 4]).reshape(2, 2)                            

In [33]: n1                                                                  
Out[33]:
array([[1, 2],
       [3, 4]])

In [34]: n1.tolist()                                                         
Out[34]: [[1, 2], [3, 4]] 并不是 [1,2, 3, 4]

np.save("valid", [r_s, result_labels])

ValueError: could not broadcast input array from shape (610,3920) into shape (610)

ndarray to tensor

In [50]: t1 = tf.convert_to_tensor(np.array([1,2,3,4,5,6]), dtype=tf.float32)

In [51]: t1
Out[51]: <tf.Tensor: id=10, shape=(6,), dtype=float32, numpy=array([1., 2., 3., 4., 5., 6.], dtype=float32)>

tensor to ndarray

In [51]: t1
Out[51]: <tf.Tensor: id=10, shape=(6,), dtype=float32, numpy=array([1., 2., 3., 4., 5., 6.], dtype=float32)>

In [52]: t1.numpy()
Out[52]: array([1., 2., 3., 4., 5., 6.], dtype=float32)

在2.0中EeagerTensor的 eval()不行,但错误提示给出方法

/usr/local/lib/python2.7/dist-packages/tensorflow_core/python/framework/ops.pyc in eval(self, feed_dict, session)
   1113   def eval(self, feed_dict=None, session=None):
   1114     raise NotImplementedError(
-> 1115         "eval is not supported when eager execution is enabled, "
   1116         "is .numpy() what you're looking for?")
   1117 

NotImplementedError: eval is not supported when eager execution is enabled, is .numpy() what you're looking for?
 

好多地方或者说所有的地方 ndarray 和tensor都是一样的不用转型

interpreter.set_tensor(input_index, np.reshape(x_train_rs[100],(1,128,3,1)))

tf.data.Dataset.from_tensor_slices((x_test_rs, y_test)).batch(1)

也许这也是说tensorflow2.0像 numpy的原因之一

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值