Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Negative dimension size caused by subtracting 3 from 2 for 'conv2d_5/convolution #5

Open
shehzi-khan opened this issue Sep 5, 2018 · 1 comment

Comments

@shehzi-khan
Copy link

I'm trying to run GA for mnist but getting Negative Dimension error randomly in different network combinations. Some models would compile fine and some would not.

Your code for compiling CNN model remains same as given below.

def compile_model_cnn(genome, nb_classes, input_shape):
    """Compile a sequential model.

    Args:
        genome (dict): the parameters of the genome

    Returns:
        a compiled network.

    """
    # Get our network parameters.
    nb_layers  = genome.geneparam['nb_layers' ]
    nb_neurons = genome.nb_neurons()

    activation = genome.geneparam['activation']
    optimizer  = genome.geneparam['optimizer' ]

    logging.info("Architecture:%s,%s,%s,%d" % (str(nb_neurons), activation, optimizer, nb_layers))

    model = Sequential()
    # Add each layer.
    for i in range(0,nb_layers):
        # Need input shape for first layer.
        if i == 0:
            model.add(Conv2D(nb_neurons[i], kernel_size = (3, 3), activation = activation, padding='same', input_shape = input_shape))
        else:
            model.add(Conv2D(nb_neurons[i], kernel_size = (3, 3), activation = activation))
        
        if i < 2: #otherwise we hit zero
            model.add(MaxPooling2D(pool_size=(2, 2)))
        
        model.add(Dropout(0.2))

    model.add(Flatten())
    # always use last nb_neurons value for dense layer
    model.add(Dense(nb_neurons[len(nb_neurons) - 1], activation = activation))
    model.add(Dropout(0.5))
    model.add(Dense(nb_classes, activation = 'softmax'))

    #BAYESIAN CONVOLUTIONAL NEURAL NETWORKS WITH BERNOULLI APPROXIMATE VARIATIONAL INFERENCE
    #need to read this paper

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

    return model

Below is the error report.

Getting Keras datasets
Compling Keras model
Architecture:[64, 16, 128, 16, 64, 128],relu,nadam,5
  7%|▋         | 1/15 [00:17<04:09, 17.81s/it]Traceback (most recent call last):
  File "/home/shehzikhan/Projects/DeepWork/generaldeepevolution/main.py", line 377, in <module>
    main(dataset,nb_classes,batch_size,epochs,mode,population,generations,network,project_dir=project_dir)
  File "/home/shehzikhan/Projects/DeepWork/generaldeepevolution/main.py", line 339, in main
    generate(evolution_params, dataset,nb_classes,batch_size,epochs,run,network,project_dir=project_dir)
  File "/home/shehzikhan/Projects/DeepWork/generaldeepevolution/main.py", line 232, in generate
    train_genomes(genomes, dataset,i+1,run,nb_classes,batch_size,epochs,mode,network,project_dir=project_dir)
  File "/home/shehzikhan/Projects/DeepWork/generaldeepevolution/main.py", line 31, in train_genomes
    genome.train(dataset,gen,run,nb_classes,batch_size,epochs,mode,network,project_dir=project_dir)
  File "/home/shehzikhan/Projects/DeepWork/generaldeepevolution/genome.py", line 123, in train
    self.training_history,self.test_score,self.model_name = train_and_score(self, dataset,mode,gen,run,nb_classes,batch_size,epochs,network,project_dir=project_dir)
  File "/home/shehzikhan/Projects/DeepWork/generaldeepevolution/train.py", line 407, in train_and_score
    model = compile_model_cnn(genome, nb_classes, input_shape,mode)
  File "/home/shehzikhan/Projects/DeepWork/generaldeepevolution/train.py", line 259, in compile_model_cnn
    model.add(Conv2D(nb_neurons[i], kernel_size = (3, 3), activation = activation))
  File "/home/shehzikhan/pythonenvs/deepwork/local/lib/python2.7/site-packages/keras/engine/sequential.py", line 185, in add
    output_tensor = layer(self.outputs[0])
  File "/home/shehzikhan/pythonenvs/deepwork/local/lib/python2.7/site-packages/keras/engine/base_layer.py", line 457, in __call__
    output = self.call(inputs, **kwargs)
  File "/home/shehzikhan/pythonenvs/deepwork/local/lib/python2.7/site-packages/keras/layers/convolutional.py", line 168, in call
    dilation_rate=self.dilation_rate)
  File "/home/shehzikhan/pythonenvs/deepwork/local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 3565, in conv2d
    data_format=tf_data_format)
  File "/home/shehzikhan/pythonenvs/deepwork/local/lib/python2.7/site-packages/tensorflow/python/ops/nn_ops.py", line 780, in convolution
    return op(input, filter)
  File "/home/shehzikhan/pythonenvs/deepwork/local/lib/python2.7/site-packages/tensorflow/python/ops/nn_ops.py", line 868, in __call__
    return self.conv_op(inp, filter)
  File "/home/shehzikhan/pythonenvs/deepwork/local/lib/python2.7/site-packages/tensorflow/python/ops/nn_ops.py", line 520, in __call__
    return self.call(inp, filter)
  File "/home/shehzikhan/pythonenvs/deepwork/local/lib/python2.7/site-packages/tensorflow/python/ops/nn_ops.py", line 204, in __call__
    name=self.name)
  File "/home/shehzikhan/pythonenvs/deepwork/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 956, in conv2d
    data_format=data_format, dilations=dilations, name=name)
  File "/home/shehzikhan/pythonenvs/deepwork/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/home/shehzikhan/pythonenvs/deepwork/local/lib/python2.7/site-packages/tensorflow/python/util/deprecation.py", line 454, in new_func
    return func(*args, **kwargs)
  File "/home/shehzikhan/pythonenvs/deepwork/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3155, in create_op
    op_def=op_def)
  File "/home/shehzikhan/pythonenvs/deepwork/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1731, in __init__
    control_input_ops)
  File "/home/shehzikhan/pythonenvs/deepwork/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1579, in _create_c_op
    raise ValueError(str(e))
ValueError: Negative dimension size caused by subtracting 3 from 2 for 'conv2d_5/convolution' (op: 'Conv2D') with input shapes: [?,2,2,16], [3,3,16,64].
Exception KeyError: KeyError(<weakref at 0x7f249b920e68; to 'tqdm' at 0x7f242b4391d0>,) in <object repr() failed> ignored
@shehzi-khan
Copy link
Author

I thought it may be due to many other changes done in the code but I just cloned the fresh copy from git repo and tried to run it for mnist and it gave the same error.

For reference, I'm using
virtualenv of Python 2.7
Keras 2.2.2
Tensorflow-gpu 1.10.1
Numpy 1.14.5
Cuda 9.0
Pycharm Professional 2018.2.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant