<p>If you apply a convolution with <code>kernel_size</code> > 1 and <code>strides</code> > 1 the output is going to have a smaller dimension than the input.</p>

<p>For example:</p>

<pre><code>Conv2D(filters=6, kernel_size=5, stride=2)
</code></pre>

<p>Would take an input of dimension <code>(32,32,1)</code> and give an output of dimension <code>(28,28,6)</code>.
This causes a problem if you try to add this to a <a href="https://arxiv.org/abs/1512.03385" rel="nofollow noreferrer">ResNet</a> style shortcut block because it isn't clear how add to tensors of different dimensions.</p>

<p>There are several ways to deal with this.</p>

<ul>
<li>Do not reduce the dimension from the convolution (keep stride=1)</li>
<li>Reduce the size of the shortcut block by using a 1x1 convolution kernel with the same stride as used in <code>Conv2D</code> </li>
<li>Change the number of output channels of the shortcut block to be
the same as the number of    filters in <code>Conv2D</code></li>
</ul>
Tensorflow ValueError: Operands could not be broadcast together with shapes (5, 5, 160) (19, 19, 80)
<p>I was creating a CNN with a size of 80 for the first hidden layer, 160 for the rest of the conv layers, and 128 for the last hidden layer. But I keep running into an error message and I don't really know what it means. The input data shape is (80, 80, 1) which is what I feed into the neural network. </p>

<p>Here is the code to create the CNN:</p>

<pre><code>    if start_model is not None:
        model = load_model(start_model)
    else:
        def res_net_block(input_layers, conv_size, hm_filters, hm_strides):
            x = Conv2D(conv_size, kernel_size=hm_filters, strides=hm_strides, activation="relu", padding="same")(input_layers)
            x = BatchNormalization()(x)
            x = Conv2D(conv_size, kernel_size=hm_filters, strides=hm_strides, activation=None, padding="same")(x)
            x = Add()([x, input_layers])  # Creates resnet block
            x = Activation("relu")(x)
            return x

        input = keras.Input(i_shape)
        x = Conv2D(80, kernel_size=8, strides=4, activation="relu")(input)
        x = BatchNormalization()(x)

        for i in range(3):
            x = res_net_block(x, 160, 4, 2)

        x = Conv2D(160, kernel_size=4, strides=2, activation="relu")(x)
        x = BatchNormalization()(x)

        x = Flatten(input_shape=(np.prod(window_size), 1, 1))(x)

        x = Dense(128, activation="relu")(x)

        output = Dense(action_space_size, activation="linear")(x)

        model = keras.Model(input, output)

        model.compile(optimizer=Adam(lr=0.01), loss="mse", metrics=["accuracy"])
</code></pre>

<p>BTW the error message is located at <code>x = Add()([x, input_layers])</code> in the code</p>