0
votes

I'm using Python 3.7.7 and Tensorflow 2.1.0.

I have this tensor:

tf.Tensor([...], shape=(5, 512), dtype=float32)

And I want to compute its mean on each of its elements, getting a tensor with shape (1, 512) but I don't know how. I have tried tf.math.reduce_mean but it returns a number.

This is what I have tried:

support_set_embeddings = encoder(X_train) # tf.Tensor([...], shape=(5, 512), dtype=float32)
class_prototype = tf.math.reduce_mean(support_set_embeddings, axis=1) # tf.Tensor([...], shape=(5,), dtype=float32)

If I change the axis in reduce_mean I get a tensor with shape (512,):

support_set_embeddings = encoder(X_train) # tf.Tensor([...], shape=(5, 512), dtype=float32)
class_prototype = tf.math.reduce_mean(support_set_embeddings, axis=0) # tf.Tensor([...], shape=(512,), dtype=float32)

How can I compute the mean of each elements of a Tensor like I have explained above?

I'm looking for a tensor with shape (1,512).

1
Did you try changing dimensions of your (512,) shaped tensor? Is it not what you need?S4rt-H4K
Doing a tf.expand_dims(class_prototype,axis=0) gives you a tensor of shape (1,512)S4rt-H4K

1 Answers

1
votes

Just reshape.. tf.reshape(tf.reduce_mean(your_tensor, axis=0), (1,512))