layer=Dense(d, activation='relu', init='glorot_normal', bias=True)
layer=Dense(d, init='glorot_normal', bias=True)
layer=BatchNormalization()(layer)
layer=Activation('relu')(layer)
layer=Dense(d, init='glorot_normal', bias=True)(x)
layer=BatchNormalization()(layer)
layer=Activation('relu')(layer)
x = Input(shape=(input_dim,), name='x')
y = Input(shape=(input_dim,), name='y')
h1_dim = ....
wx_h1 = Dense(h1_dim, activation='relu', init='glorot_normal', bias=True, name='wx_h1')
wy_h1 = Dense(h1_dim, activation='relu', init='glorot_normal', bias=True, name='wy_h1')
h1_1 = merge([wx_h1(x), wy_h1(y)], mode='sum', name='h1_1')
h1_2 = merge([wy_h1(x), wx_h1(y)], mode='sum', name='h1_2')
I don't understand, layers have to be connected to some input (except output layers), so what you said is not possible. What are the inputs to these Dense layers?
output_layer ...
model = Model(input=[x, y], output=output_layer)
Please provide the complete model, you are only giving us parts and it is very hard to see the whole picture. I asked how the Dense layers are connected to the input and still I don't understand that.
On Thursday, 19 January 2017 07:55:37 GMT h.ros...@gmail.com wrote:
> the lines of code are the beginning of the model
> I have more layers connected (after the h1_1, h1_2 layers)
> and finally I have an output layer and the model is finished
> output_layer ...
> model = Model(input=[x, y], output=output_layer)
>
> so you can see the inputs are perfectly defined using the x and y
> definitions of Input() layers.
> the model works very well as is.
> I was looking to improve its training speed and robustness with
> BatchNormalization.
>
> So the question remains:
> How do I insert BatchNormalization layers in my model?
>
> thanks
>
> בתאריך יום חמישי, 19 בינואר 2017 בשעה 17:41:09 UTC+2, מאת Matias Valdenegro:
> > I don't understand, layers have to be connected to some input (except
> > output layers), so what you said is not possible. What are the inputs to
> > these Dense layers?
> >
> >
> >
> > On Thursday, 19 January 2017 07:30:58 GMT h.ros...@gmail.com <javascript:>
x = Input(shape=(5,), name='x')
y = Input(shape=(5,), name='y')
h_dim = 64
wx_h = Dense(h_dim, activation='relu', init='glorot_normal', bias=True, name='wx_h')
wy_h = Dense(h_dim, activation='relu', init='glorot_normal', bias=True, name='wy_h')
h_1 = Dropout(0.1)(merge([wx_h(x), wy_h(y)], mode='sum', name='h_1'))
h_2 = Dropout(0.1)(merge([wy_h(x), wx_h(y)], mode='sum', name='h_2'))
wh1_o1 = Dense(1, activation='tanh', init='glorot_uniform', bias=True, name='wh1_o1')
wh2_o1 = Dense(1, activation='tanh', init='glorot_uniform', bias=True, name='wh2_o1')
o1 = merge([wh1_o1(h_1), wh2_o1(h_2)], mode='sum', name='out_1')
o2 = merge([wh2_o1(h_1), wh1_o1(h_2)], mode='sum', name='out_2')
output = merge([o1, o2], mode=softmax2, output_shape=(2,), name='output')
model = Model(input=[x, y], output=output)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['categorical_accuracy'])
def softmax2(two_tensors):
return K.softmax(K.concatenate(two_tensors))
wx_h = Dense(h_dim, init='glorot_normal', bias=True, name='wx_h')
wx_h = BatchNormalization()(wx_h)
wx_h = Activation('relu')(wx_h)
wy_h = Dense(h_dim, init='glorot_normal', bias=True, name='wy_h')
wy_h = BatchNormalization()(wy_h)
wy_h = Activation('relu')(wy_h)
Exception: ('Not a Keras tensor:', <keras.layers.core.Dense object at 0x......>)
Ok, the code is slightly obfuscated as you instantiate the layers and "call" them later, that is fine.
To use Batch Normalization, just replace your dropout layers with Batch Normalization, like:
h_1 = BatchNormalization()(merge([wx_h(x), wy_h(y)], mode='sum', name='h_1'))
h_2 = BatchNormalization()(merge([wy_h(x), wx_h(y)], mode='sum', name='h_2'))
> > On 19 January 2017 at 13:47, hanan <h.ros...@gmail.com <javascript:>>
Is there any way to accomplish that?
Yes, of course that can be done, you just need to refactor your code a little:
x = Input(shape=(5,), name='x')
y = Input(shape=(5,), name='y')
h_dim = 64
wx_h = Dense(h_dim, activation='relu', init='glorot_normal', bias=True, name='wx_h')
wy_h = Dense(h_dim, activation='relu', init='glorot_normal', bias=True, name='wy_h')
wx_hx = BatchNormalization()(wx_h(x))
wx_hy = BatchNormalization()(wx_h(y))
wy_hx = BatchNormalization()(wy_h(x))
wy_hy = BatchNormalization()(wy_h(y))
h_1 = Dropout(0.1)(merge([wx_hx, wy_hy], mode='sum', name='h_1'))
h_2 = Dropout(0.1)(merge([wy_hx, wx_hy], mode='sum', name='h_2'))
Also, maybe you should drop the Dropout layers, they are not needed if you use Batch Normalization.
Yes, of course that can be done, you just need to refactor your code a little: