获取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