如果我想在TensorFlow中获取一个普通张量的形状,并将这些值存储在一个列表中,我会使用以下代码
a_shape=[a.shape[0].value , a.shape[1].value]
如果我没记错的话,使用.value可以将张量中的元素转换为实际的数字。
对于稀疏张量,我输入以下代码
a_sparse_shape=[a.dense_shape[0].value, a.dense_shape[1].value]
然而,我得到了错误信息:“’Tensor’ object has no attribute ‘value’”
有没有其他解决方案?
回答:
是的,有一个替代方案:
import tensorflow as tftensor = tf.random_normal([2, 2, 2, 3])tensor_shape = tensor.get_shape().as_list()print(tensor_shape)# [2, 2, 2, 3]
对于稀疏张量也是如此:
sparse_tensor = tf.SparseTensor(indices=[[0,0], [1, 1]], values=[1, 2], dense_shape=[2, 2])sparse_tensor_shape = sparse_tensor.get_shape().as_list()print(sparse_tensor_shape)# [2, 2]