1

My input shape is (150,10,1) and my output has the same shape (150,10,1). My problem is multi-classification (3 classes). After using np_utils.to_categorical(Ytrain) the output shape will be (150,10,3) which is perfect. However during the process of modelling with GlobalAvgPool1D(), it gives the error : "A target array with shape (150, 10, 3) was passed for an output of shape (None, 3) while using as loss categorical_crossentropy. This loss expects targets to have the same shape as the output". How should I fix it?

My codes:

nput_size = (150, 10, 1)
Xtrain = np.random.randint(0, 100, size=(150, 10, 1))

Ytrain = np.random.choice([0,1, 2], size=(150, 10,1))
Ytrain = np_utils.to_categorical(Ytrain)

input_shape = (10, 1)
input_layer = tf.keras.layers.Input(input_shape)
conv_x = tf.keras.layers.Conv1D(filters=32, kernel_size=10, strides = 1, padding='same')(input_layer)

conv_x = tf.keras.layers.BatchNormalization()(conv_x)
conv_x = tf.keras.layers.Activation('relu')(conv_x)
g_pool = tf.keras.layers.GlobalAvgPool1D()(conv_x)
output_layer = tf.keras.layers.Dense(3, activation='softmax')(g_pool)
model = tf.keras.models.Model(inputs= input_layer, outputs = output_layer) 
model.summary()

model.compile(loss='categorical_crossentropy', optimizer= tf.keras.optimizers.Adam(), 
          metrics='accuracy'])
hist = model.fit(Xtrain, Ytrain, batch_size= 5, epochs= 10, verbose= 0)
Nume
  • 421
  • 1
  • 4
  • 8

1 Answers1

0

When I ran your code in Tensorflow Version 2.2.0 in Google colab, I got the following error - ValueError: Shapes (5, 10, 3) and (5, 3) are incompatible.

You are getting this error because, the labels Ytrain data is having the shape of (150, 10, 3) instead of (150, 3).

As your labels are having shape of (None,3), your input also should be same .i.e. (Number of records, 3). I was able to run your code successfully after modifying,

Ytrain = np.random.choice([0,1, 2], size=(150, 10,1))

to

Ytrain = np.random.choice([0,1, 2], size=(150, 1))

np_utils.to_categorical adds the 3 columns for labels thus making the shape of (150,3) which our model expects.

Fixed Code -

import tensorflow as tf
print(tf.__version__)
import numpy as np
from tensorflow.keras import utils as np_utils

Xtrain = np.random.randint(0, 100, size=(150, 10, 1))

Ytrain = np.random.choice([0,1, 2], size=(150, 1))
Ytrain = np_utils.to_categorical(Ytrain)

print(Ytrain.shape)

input_shape = (10, 1)
input_layer = tf.keras.layers.Input(input_shape)
conv_x = tf.keras.layers.Conv1D(filters=32, kernel_size=10, strides = 1, padding='same')(input_layer)

conv_x = tf.keras.layers.BatchNormalization()(conv_x)
conv_x = tf.keras.layers.Activation('relu')(conv_x)
g_pool = tf.keras.layers.GlobalAvgPool1D()(conv_x)
output_layer = tf.keras.layers.Dense(3, activation='softmax')(g_pool)
model = tf.keras.models.Model(inputs= input_layer, outputs = output_layer) 
model.summary()

model.compile(loss='categorical_crossentropy', optimizer= tf.keras.optimizers.Adam(), 
          metrics=['accuracy'])
hist = model.fit(Xtrain, Ytrain, batch_size= 5, epochs= 10, verbose= 0)

print("Ran Successfully")

Output -

2.2.0
(150, 3)
Model: "model_13"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_21 (InputLayer)        [(None, 10, 1)]           0         
_________________________________________________________________
conv1d_9 (Conv1D)            (None, 10, 32)            352       
_________________________________________________________________
batch_normalization_15 (Batc (None, 10, 32)            128       
_________________________________________________________________
activation_9 (Activation)    (None, 10, 32)            0         
_________________________________________________________________
global_average_pooling1d_9 ( (None, 32)                0         
_________________________________________________________________
dense_14 (Dense)             (None, 3)                 99        
=================================================================
Total params: 579
Trainable params: 515
Non-trainable params: 64
_________________________________________________________________
Ran Successfully

Hope this answers your question. Happy Learning.

  • @Nazanin Fouladgar - Hope we have answered your question. Can you please accept and upvote the answer if you are satisfied with the answer. –  Jun 09 '20 at 03:42
  • Thanks! What if my input_shape = (150, 10) and I consider every 2 rows for conv1D? I mean I wanna convolve the "first two rows" of matrix (150,10) with a filter (2,10) and stride=1, "second two rows" with the same filter, "starting from second row of matrix till third row of matrix" and so on. – Nume Jun 09 '20 at 10:42
  • I modify "..till fourth* row of matrix and so on" , implying for considering previous time step with the current time step for convolution on a time series matrix. – Nume Jun 09 '20 at 13:32
  • Conv1D kernel_size must be an integer or tuple/list of a single integer, specifying the length of the 1D convolution window. –  Jun 09 '20 at 13:39
  • Assuming the kernel = 2, it will slide over 10 features, right? If so, that is what I meant. However, gives error of "ValueError: Input 0 of layer conv1d_46 is incompatible with the layer: expected axis -1 of input shape to have value 10 but received input with shape [None, 10, 1]" – Nume Jun 09 '20 at 14:09
  • I fixed the problem now by reshaping the Xtrain to (1, 150, 10), Ytrain to (1, 150, 1), input_shape (150, 10), kernel (2), without GlobalAvgPool1D. However, there is still error if this layer is added.. – Nume Jun 09 '20 at 17:08