Concatenate input with constant vector in keras

て烟熏妆下的殇ゞ 提交于 2019-12-06 09:08:34

Dimensions in keras work like this:

  • When you define them in layers, building your model, you never define "batch_size".
  • Internally, using backend functions, in loss functions and in any tensor operation, the batch dimension is the first

Keras shows you a None to represent the batch size in summaries, errors and others.

That means that:

  • a's shape is (None, 10, 5)
  • a1's shape just (10,5). You cannot concatenate them.

There are a few workarounds you can do, such as creating a1 with shape (1,10,5) and then repeating it's values in the batch dimension:

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

I was totally unable to use Input(tensor=...) because the constant's dimension is fixed, and the input's dimension is None, so I worked it around with a lambda layer:

b = Lambda(lambda x: K.concatenate([x,constant],axis=1),output_shape=(20,5))(a)

But I can't at all understand what you want to achieve with x += [b] and the rest.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!