keras: Error creating shared node when using multiple Convolutional layers

I am trying to run following script:

import numpy as np
from keras.models import Model
from keras.layers import Input, Dense, merge
from keras.layers import Convolution2D, MaxPooling2D, Flatten

input1 = Input((64, 64, 3))
input2 = Input((64, 64, 3))

conv_1 = Convolution2D(32, 3, 3, activation='relu')
conv_2 = Convolution2D(64, 3, 3, activation='relu')
fl_3 = Flatten()
fc_4 = Dense(64, activation='relu')
fc_5 = Dense(32, activation='relu')

rep1 = fc_5(fc_4(fl_3(conv_2(conv_1(input1)))))
rep2 = fc_5(fc_4(fl_3(conv_2(conv_1(input2)))))
#rep1 = fc_5(fc_4(fl_3(conv_1(input1))))
#rep2 = fc_5(fc_4(fl_3(conv_1(input2))))

combined_vec = merge([rep1, rep2], mode='concat')

fc_6 = Dense(64)(combined_vec)
prediction = Dense(100, activation='softmax')(fc_6)

model = Model([input1, input2], prediction)
model.compile('sgd', 'categorical_crossentropy', metric=['accuracy'])

I get following error:

Using Theano backend.
Traceback (most recent call last):
  File "shared-example.py", line 15, in <module>
    rep1 = fc_5(fc_4(fl_3(conv_2(conv_1(input1)))))
  File "/usr/lib/python3.5/site-packages/keras/engine/topology.py", line 458, in __call__
    self.build(input_shapes[0])
  File "/usr/lib/python3.5/site-packages/keras/layers/core.py", line 589, in build
    name='{}_W'.format(self.name))
  File "/usr/lib/python3.5/site-packages/keras/initializations.py", line 59, in glorot_uniform
    return uniform(shape, s, name=name)
  File "/usr/lib/python3.5/site-packages/keras/initializations.py", line 30, in uniform
    return K.variable(np.random.uniform(low=-scale, high=scale, size=shape),
  File "mtrand.pyx", line 1565, in mtrand.RandomState.uniform (numpy/random/mtrand/mtrand.c:17303)
OverflowError: Range exceeds valid bounds

But when I remove one of the convolutional layers (as done in commented code in above script), I do not get the error. I am guessing this error arises due to non integer value of scale passed to np.random.uniform. How can I solve this issue ?

Thanks.

PS: I am using Keras using Theano backend on arch linux x64 with latest versions of all libraries.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 20 (7 by maintainers)

Most upvoted comments

from keras import backend as K K.set_image_dim_ordering(‘th’) Try this, this may work