Home > Mobile >  Addition of unequal sized tensors in Keras
Addition of unequal sized tensors in Keras

Time:04-21

I am working in Keras with a tensor of the form

A = <tf.Tensor 'lambda_87/strided_slice:0' shape=(?, 40, 2) dtype=float32>

Now, I would like to add, for each of the 40 "rows" the index 0 row of the a Tensor with dimensions

B = <tf.Tensor 'lambda_92/mul:0' shape=(?, 2, 2) dtype=float32>

For short, for the second tensor I only need for the present step B[:,0,:]. So, excluding the first dimension, this would be the first "row" of the matrix B.

The Add() layer seems to work only with equally-sized tensors. Any suggestion on how I could specify a Lambda function that does the job?

Thanks for reading!

CodePudding user response:

Maybe try something like this:

import tensorflow as tf

samples = 1
A = tf.random.normal((samples, 40, 2))
B = tf.random.normal((samples, 2, 2))
B = tf.expand_dims(B[:, 0, :], axis=1) # or just B = B[:, 0, :]

C = A   B
print(C.shape)
# (1, 40, 2)

Or with a Lambda layer:

import tensorflow as tf

samples = 1
A = tf.random.normal((samples, 40, 2))
B = tf.random.normal((samples, 2, 2))

lambda_layer = tf.keras.layers.Lambda(lambda x: x[0]   x[1][:, 0, :])

print(lambda_layer([A, B]))
  • Related