Keras: OverflowError: Range exceeds valid bounds

Created on 10 May 2016  Â·  11Comments  Â·  Source: keras-team/keras

My Convolutional neural net is giving me an error on the first DenseLayer, saying "OverflowError: Range exceeds valid bounds". My code looks correct given the other examples that I have consulted, but I'm not really sure.

IMAGE_HEIGHT = 6
IMAGE_WIDTH = 200
NUM_PEOPLE = 18

def gen_model():
    """
    Generates the model to be used
    :return: the model, untrained
    """
    model = Sequential()

    model.add(Convolution2D(5, 5, 5, input_shape=(1, IMAGE_HEIGHT, IMAGE_WIDTH)))
    model.add(AveragePooling2D(pool_size=(2, 2)))
    model.add(Convolution2D(5, 5, 5))
    model.add(AveragePooling2D(pool_size=(2, 2)))
    model.add(Flatten())
    model.add(Dense(output_dim=20))
    model.add(Activation('relu'))
    model.add(Dense(output_dim=18))
    model.add(Activation('softmax'))
    model.compile(optimizer="adagrad", loss="categorical_crossentropy", metrics=['accuracy'])
    return model

And here is the full traceback error:
Traceback (most recent call last):
File "/home/chris/Desktop/KerasCNN/model.py", line 63, in <module>
main()
File "/home/chris/Desktop/KerasCNN/model.py", line 52, in main
model = gen_model()
File "/home/chris/Desktop/KerasCNN/model.py", line 31, in gen_model
model.add(Dense(output_dim=20))
File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 142, in add
output_tensor = layer(self.outputs[0])
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 458, in __call__
self.build(input_shapes[0])
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 596, in build
name='{}_W'.format(self.name))
File "/usr/local/lib/python2.7/dist-packages/keras/initializations.py", line 59, in glorot_uniform
return uniform(shape, s, name=name)
File "/usr/local/lib/python2.7/dist-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:16656)
OverflowError: Range exceeds valid bounds

UPDATE

Resolved issue by adding border_mode='same' to Convolutional layers. Now receiving error:

Exception: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 arrays but instead got the following list of 540 arrays

UPDATE 2

Resolved previous issue by doing train_concat = np.arry(train). Now receiving error:

ValueError: ('Bad input argument to theano function with name "/usr/local/lib/python3.5/dist-packages/keras/backend/theano_backend.py:514" at index 0(0-based)', 'setting an array element with a sequence.')

stale

Most helpful comment

I could solve the problem by adding the following two lines at the top of my program

from keras import backend as K
K.set_image_dim_ordering('th')

All 11 comments

Same problem here! I transfered my scripts to a different machine and the error popped up. On my old machine it is working flawlessly.

My code causing the problem:

model = Sequential()

model.add(Convolution2D(nb_conv_filters, conv_kernel_size, conv_kernel_size, border_mode='valid', input_shape=(1, img_rows, img_cols)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(pool_kernel_size, pool_kernel_size)))

model.add(Flatten())

model.add(Dense(nb_dense_neurons)) #128
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(nb_classes))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy',
                  optimizer='adadelta',
                  metrics=['accuracy'])

The error is the following:

Traceback (most recent call last):
  File "sensitivityAnalysis.py", line 14, in <module>
    cnn.buildModelLargeKernels( [4])
  File "/home/user/CNN.py", line 341, in buildModelLargeKernels
    model.add(Dense(nb_dense_neurons)) #128
  File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line 308, in add
    output_tensor = layer(self.outputs[0])
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py", line 487, in __call__
    build(input_shapes[0])
  File "/usr/local/lib/python3.5/dist-packages/keras/layers/core.py", line 695, in build
    name='{}_W'.format(self.name))
  File "/usr/local/lib/python3.5/dist-packages/keras/initializations.py", line 59, in glorot_uniform
    return uniform(shape, s, name=name)
  File "/usr/local/lib/python3.5/dist-packages/keras/initializations.py", line 32, in uniform
    return K.random_uniform_variable(shape, -scale, scale, name=name)
  File "/usr/local/lib/python3.5/dist-packages/keras/backend/theano_backend.py", line 140, in random_uniform_variable
    return variable(np.random.uniform(low=low, high=high, size=shape),
  File "mtrand.pyx", line 1565, in mtrand.RandomState.uniform (numpy/random/mtrand/mtrand.c:17311)
OverflowError: Range exceeds valid bounds

It could be different software version. Can you compare them?.

On Thu, Sep 15, 2016 at 10:37 AM, jagiella [email protected] wrote:

Same problem here! I transfered my scripts to a different machine and the
error popped up. On my old machine it is working flawlessly.

My code causing the problem:

model = Sequential()

model.add(Convolution2D(self.nb_conv_filters, self.conv_kernel_size, self.conv_kernel_size, border_mode='valid', input_shape=(1, self.img_rows, self.img_cols)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(self.pool_kernel_size, self.pool_kernel_size)))

model.add(Flatten())

model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(self.nb_classes))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['accuracy'])

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/fchollet/keras/issues/2681#issuecomment-247347006,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AALC-_nSdzBgWywIVZR85J0lj5HSz3Cnks5qqVgmgaJpZM4Iawx-
.

I updated my docker with the latest build just now and i get this too. I wasn't getting this before.

<ipython-input-6-95e4eaaa23f7> in train(BATCH_SIZE)
      3     X_train = (X_train.astype(np.float32) - 127.5)/127.5
      4     X_train = X_train.reshape((X_train.shape[0], 1) + X_train.shape[1:])
----> 5     discriminator = discriminator_model()
      6     generator = generator_model()
      7     discriminator_on_generator =         generator_containing_discriminator(generator, discriminator)

<ipython-input-3-57fe563bb1fd> in discriminator_model()
     11     model.add(MaxPooling2D(pool_size=(2, 2)))
     12     model.add(Flatten())
---> 13     model.add(Dense(1024))
     14     model.add(Activation('tanh'))
     15     model.add(Dense(1))

/opt/conda/lib/python2.7/site-packages/keras/models.pyc in add(self, layer)
    306                  output_shapes=[self.outputs[0]._keras_shape])
    307         else:
--> 308             output_tensor = layer(self.outputs[0])
    309             if type(output_tensor) is list:
    310                 raise Exception('All layers in a Sequential model '

/opt/conda/lib/python2.7/site-packages/keras/engine/topology.pyc in __call__(self, x, mask)
    485                                     '`layer.build(batch_input_shape)`')
    486             if len(input_shapes) == 1:
--> 487                 self.build(input_shapes[0])
    488             else:
    489                 self.build(input_shapes)

/opt/conda/lib/python2.7/site-packages/keras/layers/core.pyc in build(self, input_shape)
    693 
    694         self.W = self.init((input_dim, self.output_dim),
--> 695                            name='{}_W'.format(self.name))
    696         if self.bias:
    697             self.b = K.zeros((self.output_dim,),

/opt/conda/lib/python2.7/site-packages/keras/initializations.pyc in glorot_uniform(shape, name, dim_ordering)
     57     fan_in, fan_out = get_fans(shape, dim_ordering=dim_ordering)
     58     s = np.sqrt(6. / (fan_in + fan_out))
---> 59     return uniform(shape, s, name=name)
     60 
     61 

/opt/conda/lib/python2.7/site-packages/keras/initializations.pyc in uniform(shape, scale, name)
     30 
     31 def uniform(shape, scale=0.05, name=None):
---> 32     return K.random_uniform_variable(shape, -scale, scale, name=name)
     33 
     34 

/opt/conda/lib/python2.7/site-packages/keras/backend/theano_backend.pyc in random_uniform_variable(shape, low, high, dtype, name)
    138 
    139 def random_uniform_variable(shape, low, high, dtype=_FLOATX, name=None):
--> 140     return variable(np.random.uniform(low=low, high=high, size=shape),
    141                     dtype=dtype, name=name)
    142 

mtrand.pyx in mtrand.RandomState.uniform (numpy/random/mtrand/mtrand.c:13528)()

OverflowError: Range exceeds valid bounds

I have the same problem, but when I typed model.summary() it looked like I have negatief shapes

I could solve this by changing
input_shape=(1, IMAGE_HEIGHT, IMAGE_WIDTH)
To
input_shape=(IMAGE_HEIGHT, IMAGE_WIDTH,1)

I could solve the problem by adding the following two lines at the top of my program

from keras import backend as K
K.set_image_dim_ordering('th')

check your ~/.keras/keras.json
if "image_dim_ordering": is "th" and "backend": "theano", your input_shape must be (channels, height, width)
if "image_dim_ordering": is "tf" and "backend": "tensorflow", your input_shape must be (height, width, channels)

anujshah1003 suggestion was good. K needs to be imported explicitly.

@lizhiyuanUSTC what if my "image_dim_ordering" is "th" and "backend" is "tensorflow"? Thanks!

@qilicun just change the image_dim_ordering to tf or backend to theano

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vinayakumarr picture vinayakumarr  Â·  3Comments

kylemcdonald picture kylemcdonald  Â·  3Comments

fredtcaroli picture fredtcaroli  Â·  3Comments

farizrahman4u picture farizrahman4u  Â·  3Comments

amityaffliction picture amityaffliction  Â·  3Comments