在使用x.get_shape().as_list()的前提:x必须为tensor类型
x.get_shape()返回的是一个<‘TensorShape’>的元组类型,as_list()将结果转化为list类型
import tensorflow as tf
import numpy as np
a_array = np.array([[1, 2, 3], [4, 5, 6]])
b_list = [[1, 2, 3], [3, 4, 5]]
c_tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
d=c_tensor.get_shape()
print("d:",type(d))
print("ctens:",type(c_tensor))
print(type(c_tensor.get_shape().as_list()))
sess=tf.Session()
print(sess.run(tf.shape(a_array)))
print(sess.run(tf.shape(b_list)))
print(sess.run(tf.shape(c_tensor)))
print(sess.run(c_tensor))
运行结果:
d: <class 'tensorflow.python.framework.tensor_shape.TensorShape'>
ctens: <class 'tensorflow.python.framework.ops.Tensor'>
<class 'list'>
[2 3]
[2 3]
[2 3]
[[1 2 3]
[4 5 6]]