DeepLearner
DeepLearner

Reputation: 95

Converting sparse tensor dense shape into an integer value in tensorflow

If I want to get the shape of a normal tensor in tensorflow, and store the values in a list, I would use the following

a_shape=[a.shape[0].value , a.shape[1].value]

If I'm not mistaken, using .value converts the element in the tensor to a real number.

With sparse tensors, I type the following

a_sparse_shape=[a.dense_shape[0].value, a.dense_shape[1].value]

However, I get the error message " 'Tensor' object has no attribute 'value' "

Does anyone have any alternate solutions?

Upvotes: 4

Views: 297

Answers (1)

Vlad
Vlad

Reputation: 8585

Yes, there is an alternative:

import tensorflow as tf

tensor = tf.random_normal([2, 2, 2, 3])
tensor_shape = tensor.get_shape().as_list()
print(tensor_shape)
# [2, 2, 2, 3]

Same for sparse tensors:

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]

Upvotes: 1

Related Questions