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]

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的原因之一

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章