獲取array和tensor的shape


list、array、tensor之間的轉換

list轉array:np.array(list)
numpy數組轉tensor:t = tf.convert_to_tensor(array, tf.float32, name='t')或者t = tf.cast(array, tf.float32)
tensor轉numpy數組:array = sess.run(tensor) 或者 array = tensor.eval()

如何獲取shape

1、獲取numpy數組形狀使用:a_array.shape[0]
2、獲取tensor形狀使用:b_tensor.shape[0]或b_tensor.get_shape().as_list()

示例

import numpy as np
import tensorflow as tf

# numpy數組類型
print("==== numpy.ndarray ====")
a_array = np.array([[1,2,3],[4,5,6]])
print(type(a_array))   # <class 'numpy.ndarray'>
row,column = a_array.shape[0],a_array.shape[1]
print("The rows and columns of a_array is %d, %d" %(row,column))

# tensor 類型
print("==== tensorflow.python.framework.ops.Tensor ====")
b_tensor = tf.constant([[1,2,3],[4,5,6]])
print(type(b_tensor)) # <class 'tensorflow.python.framework.ops.Tensor'>
row,column = b_tensor.shape[0],b_tensor.shape[1]
print("The rows and columns of b_tensor is %d, %d" %(row,column))
row,column = b_tensor.get_shape().as_list()
print("The rows and columns of b_tensor is %d, %d" %(row,column))


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM