Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Negative dimension size error in tensorflow

so I have 2 images, img1 and img2 both with shape=(20,20), to which I expand_dims to (1,20,20) 1 being batch size and feed them to the network, but I get the following error:

ValueError: Negative dimension size caused by subtracting 3 from 1 for '{{node conv2d/Conv2D}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[], padding="VALID", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true](Placeholder, conv2d/Conv2D/ReadVariableOp)' with input shapes: [?,1,20,20], [3,3,20,32]. ```

    def mean_squared_error(y_true, y_pred):
        return tf.keras.metrics.mean_squared_error(y_true, y_pred)
    
    model = Sequential()
    model.add(Conv2D(32, kernel_size=(3, 3),
                     activation='relu',
                     input_shape=(1,20,20)))
    model.add(Conv2D(1, kernel_size=(3, 3),
                     activation='relu'))
    
    model.compile(optimizer='adam', loss=mean_squared_error, metrics=[mean_squared_error, 'accuracy'])
    
    # Train
    
    model.fit(img1, img2)


>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The convolution layers reduce your input’s dimensions, but IIUC, you are trying to apply mse to the model’s output and img2. So try something like this:

import tensorflow as tf

model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(32, kernel_size=(2, 2),
                  activation='relu',
                  input_shape=(20, 20, 1)))
model.add(tf.keras.layers.Conv2DTranspose(1, kernel_size=(2, 2),
                  activation='relu'))
model.compile(optimizer='adam', loss='mse', metrics=['mae'])

# Train
img1 = tf.random.normal((1, 20, 20))
img2 = tf.random.normal((1, 20, 20))
model.fit(img1, img2)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading