title: Keras: ValueError: logits and labels must have the same shape ((None, 2) vs (None, 1))
body: <p>I have been using the famous dogs-vs-cats kaggle dataset and trying to come up with my own CNN Model. I'm new to using the <code>image_dataset_from_directory</code> method to import the dataset after configuring it into two folders that contain the cat and dog images separately.</p>
  <p>Here is the code for the model.</p>
  <pre><code>from keras.models import Sequential
  from keras.layers import Conv2D,MaxPooling2D,\
       Dropout,Flatten,Dense,Activation,\
       BatchNormalization
  model=Sequential()
  model.add(Conv2D(32,(3,3),activation='relu',input_shape=(128,128,3)))
  model.add(BatchNormalization())
  model.add(MaxPooling2D(pool_size=(2,2)))
  model.add(Dropout(0.25))
  model.add(Conv2D(64,(3,3),activation='relu'))
  model.add(BatchNormalization())
  model.add(MaxPooling2D(pool_size=(2,2)))
  model.add(Dropout(0.25))
  model.add(Conv2D(128,(3,3),activation='relu'))
  model.add(BatchNormalization())
  model.add(MaxPooling2D(pool_size=(2,2)))
  model.add(Dropout(0.25))
  model.add(Flatten())
  model.add(Dense(512,activation='relu'))
  model.add(BatchNormalization())
  model.add(Dropout(0.5))
  model.add(Dense(2,activation='sigmoid'))
  model.compile(loss = 'binary_crossentropy',
    optimizer='adam',metrics=['accuracy'])
  </code></pre>
  <p>And here's the code for the dataset:</p>
  <pre><code>Dataset = tf.keras.preprocessing.image_dataset_from_directory(
      TRAIN_DIR,
      labels=&quot;inferred&quot;,
      label_mode=&quot;binary&quot;,
      class_names=None,
      color_mode=&quot;rgb&quot;,
      batch_size=32,
      image_size=(128, 128),
      shuffle=True,
      seed=None,
      validation_split=None,
      subset=None,
      interpolation=&quot;bilinear&quot;,
      follow_links=False,
  )
  </code></pre>
  <p>After running the fit function to train my CNN. I was shown this error:</p>
  <pre><code>ValueError: in user code:
  
      C:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\training.py:806 train_function  *
          return step_function(self, iterator)
      C:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\training.py:796 step_function  **
          outputs = model.distribute_strategy.run(run_step, args=(data,))
      C:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1211 run
          return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
      C:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2585 call_for_each_replica
          return self._call_for_each_replica(fn, args, kwargs)
      C:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2945 _call_for_each_replica
          return fn(*args, **kwargs)
      C:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\training.py:789 run_step  **
          outputs = model.train_step(data)
      C:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\training.py:748 train_step
          loss = self.compiled_loss(
      C:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:204 __call__
          loss_value = loss_obj(y_t, y_p, sample_weight=sw)
      C:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\losses.py:149 __call__
          losses = ag_call(y_true, y_pred)
      C:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\losses.py:253 call  **
          return ag_fn(y_true, y_pred, **self._fn_kwargs)
      C:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
          return target(*args, **kwargs)
      C:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\losses.py:1605 binary_crossentropy
          K.binary_crossentropy(y_true, y_pred, from_logits=from_logits), axis=-1)
      C:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
          return target(*args, **kwargs)
      C:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\backend.py:4823 binary_crossentropy
          return nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
      C:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
          return target(*args, **kwargs)
      C:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\ops\nn_impl.py:173 sigmoid_cross_entropy_with_logits
          raise ValueError(&quot;logits and labels must have the same shape (%s vs %s)&quot; %
  
      ValueError: logits and labels must have the same shape ((None, 2) vs (None, 1))
  </code></pre>
  <p>How can I fix this?</p>