Noori
Noori

Reputation: 3

Calculate average of tensor and use this value as the tensor value

I don't have good knowledge about Tensors so forgive me if this is a silly question.

I am trying to use Lambda layer from Keras to add a special layer for my model. I want this layer to calculate the mean value of the tensor and I want this mean value to be used as the tensor value.

To make it clearer, this is what I need but in numpy, I don't know how to do it with tensor: (using NumPy is not working with tensors)

   colInput ## this should be my tensor
   avg = np.mean(colInput)
   colInput =[avg for n in range(colInput.shape[0])]

I did try the following:

avg = tf.reduce_mean(colInput)

I also did try :

avLayer= Lambda(lambda x: tf.reduce_mean(x, axis=1, keepdims=False))(visible)

where visible has my colInput as input layer but I couldn't manage to make the result set for each element in the tensor, and use this new tensor.

My tensor shape is (?,1,27) and I want it to have this 27, but they will be equal to the average value.

Thanks in advance

Upvotes: 0

Views: 1730

Answers (1)

sai
sai

Reputation: 1784

I'm not sure if this is what you want, but let me know-

xx = tf.constant([1., 0., 1., 0.])
tf.expand_dims(tf.repeat(tf.reduce_mean(xx), xx.shape[0]), axis=1)
Output would be
<tf.Tensor: shape=(4, 1), dtype=float32, numpy=
array([[0.5],
       [0.5],
       [0.5],
       [0.5]], dtype=float32)>

Upvotes: 1

Related Questions