title: Matrix size-incompatible: In[0]: [32,6], In[1]: [128,1][[node gradient_tape/sequential/dense_1/MatMul
body: <p>I am new to Machine Learning and Deep learning. I followed a tutorial for Convolutional Neural Network. But the tutorial was for binary classification. Now I tried my own for a categorical dataset with a few things changed and getting this error.</p>
  <p>My code:</p>
  <pre><code>import tensorflow as tf
  from keras.preprocessing import image
  
  train_datagen = image.ImageDataGenerator(
      rescale=1./255,
      shear_range=0.2,
      zoom_range=0.2,
      horizontal_flip=True
  )
  
  training_set = train_datagen.flow_from_directory(
      'datasets/training_data/',
      target_size=(64,64),
      batch_size=32,
      class_mode='categorical'
  )
  
  test_datagen = image.ImageDataGenerator(
      rescale=1./255,
  )
  
  test_set = test_datagen.flow_from_directory(
      'datasets/testing_data/',
      target_size=(64,64),
      batch_size=32,
      class_mode='categorical'
  )
  
  cnn = tf.keras.models.Sequential()
  
  cnn.add(tf.keras.layers.Conv2D(
      filters = 32,
      kernel_size = 3,
      activation = 'relu',
      input_shape = [64,64,3]
  ))
  
  cnn.add(tf.keras.layers.MaxPool2D(
      pool_size = 2,
      strides = 2
  ))
  
  cnn.add(tf.keras.layers.Conv2D(
      filters = 32,
      kernel_size = 3,
      activation = 'relu'
  ))
  
  cnn.add(tf.keras.layers.MaxPool2D(
      pool_size = 2,
      strides = 2
  ))
  
  cnn.add(tf.keras.layers.Flatten())
  
  cnn.add(tf.keras.layers.Dense(
      units = 128,
      activation = 'relu'
  ))
  
  cnn.add(tf.keras.layers.Dense(
      units = 1,
      activation = 'softmax'
  ))
  
  cnn.compile(
      optimizer='adam',
      loss = 'categorical_crossentropy',
      metrics=['accuracy']
  )
  
  cnn.fit(x = training_set,
          validation_data = test_set,
          epochs = 25
  )
  </code></pre>
  <p>Error:</p>
  <pre><code>InvalidArgumentError                      Traceback (most recent call last)
  &lt;ipython-input-17-52da1b2b0cd1&gt; in &lt;module&gt;
  ----&gt; 1 cnn.fit(x = training_set,
        2         validation_data = test_set,
        3         epochs = 25)
  
  ~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in _method_wrapper(self, *args, **kwargs)
      106   def _method_wrapper(self, *args, **kwargs):
      107     if not self._in_multi_worker_mode():  # pylint: disable=protected-access
  --&gt; 108       return method(self, *args, **kwargs)
      109 
      110     # Running inside `run_distribute_coordinator` already.
  
  ~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
     1096                 batch_size=batch_size):
     1097               callbacks.on_train_batch_begin(step)
  -&gt; 1098               tmp_logs = train_function(iterator)
     1099               if data_handler.should_sync:
     1100                 context.async_wait()
  
  ~\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in __call__(self, *args, **kwds)
      778       else:
      779         compiler = &quot;nonXla&quot;
  --&gt; 780         result = self._call(*args, **kwds)
      781 
      782       new_tracing_count = self._get_tracing_count()
  
  ~\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in _call(self, *args, **kwds)
      805       # In this case we have created variables on the first call, so we run the
      806       # defunned version which is guaranteed to never create variables.
  --&gt; 807       return self._stateless_fn(*args, **kwds)  # pylint: disable=not-callable
      808     elif self._stateful_fn is not None:
      809       # Release the lock early so that multiple threads can perform the call
  
  ~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in __call__(self, *args, **kwargs)
     2827     with self._lock:
     2828       graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
  -&gt; 2829     return graph_function._filtered_call(args, kwargs)  # pylint: disable=protected-access
     2830 
     2831   @property
  
  ~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _filtered_call(self, args, kwargs, cancellation_manager)
     1841       `args` and `kwargs`.
     1842     &quot;&quot;&quot;
  -&gt; 1843     return self._call_flat(
     1844         [t for t in nest.flatten((args, kwargs), expand_composites=True)
     1845          if isinstance(t, (ops.Tensor,
  
  ~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
     1921         and executing_eagerly):
     1922       # No tape is watching; skip to running the function.
  -&gt; 1923       return self._build_call_outputs(self._inference_function.call(
     1924           ctx, args, cancellation_manager=cancellation_manager))
     1925     forward_backward = self._select_forward_and_backward_functions(
  
  ~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in call(self, ctx, args, cancellation_manager)
      543       with _InterpolateFunctionError(self):
      544         if cancellation_manager is None:
  --&gt; 545           outputs = execute.execute(
      546               str(self.signature.name),
      547               num_outputs=self._num_outputs,
  
  ~\anaconda3\lib\site-packages\tensorflow\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
       57   try:
       58     ctx.ensure_initialized()
  ---&gt; 59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
       60                                         inputs, attrs, num_outputs)
       61   except core._NotOkStatusException as e:
  
  InvalidArgumentError:  Matrix size-incompatible: In[0]: [32,6], In[1]: [128,1]
       [[node gradient_tape/sequential/dense_1/MatMul (defined at &lt;ipython-input-16-c714df782bf1&gt;:1) ]] [Op:__inference_train_function_798]
  
  Function call stack:
  train_function
  </code></pre>
  <p>If anyone can help me find the solution that would be awesome. Thankyou</p>