3

As a follow-up from this question:

Concatenate input with constant vector in keras

I am trying to use the suggested solution:

constant=K.variable(np.ones((1,10, 5)))
constant = K.repeat_elements(constant,rep=batch_size,axis=0)

And got the following Error:

NameError: name 'batch_size' is not defined

I do not see how one define within the keras model the batch_size [not explicitly] so that one can concatenate a symbolic layer and a constant layer in order to use them as an input layer.

jnd0
  • 356
  • 3
  • 11
YoavEtzioni
  • 85
  • 10

1 Answers1

2

To get the dynamic batch size:

batch_size = K.shape(your_tensor)[0]

But K.repeat_elements() doesn't accept Tensor values for rep. You can however produce the same result using K.tile():

from keras.models import *
from keras import backend as K
import numpy as np

a = Input(shape=(10, 5))
batch_size = K.shape(a)[0]
constant = K.variable(np.ones((1,10, 5)))
constant = K.tile(constant, (batch_size, 1, 1))
print(constant)
# Tensor("Tile:0", shape=(?, 10, 5), dtype=float32)
benjaminplanche
  • 14,689
  • 5
  • 57
  • 69
  • However now there is a problem with : AttributeError: 'Tensor' object has no attribute '_keras_history' when calling Model. This is the same as in: https://github.com/keras-team/keras/issues/7362 with no clear solution.... – YoavEtzioni May 09 '18 at 14:13
  • It's difficult to address your problem without further information (what are you trying to do with this code?). I'd suggest you have a llok at this other [question](https://stackoverflow.com/questions/47887288/attributeerror-tensor-object-has-no-attribute-keras-history-during-implem), and if it doesn't help, open a new thread. – benjaminplanche May 09 '18 at 14:23
  • Thanks! WIth this help and other threads I got it working. However I would like to add that following this solution one need to add a new Input layer: F = Input(tensor=k_constant) and define it in the model model = Model(inputs=[X,F], outputs=outputs) However no such definition is needed in the model.fit(X,... – YoavEtzioni May 10 '18 at 11:22
  • This answer may also provide help to others seeking a similar solution: https://stackoverflow.com/a/46466275/220338 – hyperspasm Aug 30 '19 at 15:11
  • I created a similar question mostly because I could not map this answer to my question, probably because of my inexperience. I would greatly appreciate it if you could look at my question. https://stackoverflow.com/questions/58158620/concatenate-input-tensor-with-multiple-of-neg-1-tensors – grabbag Sep 30 '19 at 01:54