TeenyTinySparkles
TeenyTinySparkles

Reputation: 419

How to create a tensor with an unknown dimension

I have a layer in my neural network with an output vector x of size [?, N]. (with first dimension for the batch size). I want declare a tensor of ones of the same size in the next layer (Lambda layer). I see that I cannot use y = keras.backend.ones(x.shape) as the batch size is only available in runtime. How can I create this tensor?

Upvotes: 1

Views: 1542

Answers (1)

Agost Biro
Agost Biro

Reputation: 2839

As suggested by today in the comments, K.ones_like works:

from keras import backend as K
a = K.placeholder(shape=(None, 5))
b = K.ones_like(a)
print(b.shape)

>> TensorShape([Dimension(None), Dimension(5)])

Depending on the type of operation you're doing, you can also make a ones tensor of shape [N] and rely on broadcasting to save memory:

from keras import backend as K
a = K.placeholder(shape=(None, 5))
b = K.ones(a.shape[-1])
print(a + b)

>> <tf.Tensor 'add:0' shape=(?, 5) dtype=float32>

Upvotes: 2

Related Questions