Rishavv
Rishavv

Reputation: 293

Unable to get output using CNN model

I am trying to use cnn-lstm model on this dataset. I've stored this dataset in dataframe named as df. there are totally 11 column in this dataset but i am just mentioning 9 columns here. All columns have numerical values only

Area     book_hotel  votes  location    hotel_type  Total_Price   Facilities   Dine      rate
6             0        0      1           163          400             22        7        4.4
19            1        2      7           122          220             28        11       4.6


X=df.drop(['rate'],axis=1) 
Y=df['rate']

x_train, x_test, y_train, y_test = train_test_split(np.asarray(X), np.asarray(Y), test_size=0.33, shuffle= True)

x_train has shape (3350,10) and x_test has shape (1650, 10)

# The known number of output classes.
num_classes = 10

# Input image dimensions
input_shape = (10,)

# Convert class vectors to binary class matrices. This uses 1 hot encoding.
y_train_binary = keras.utils.to_categorical(y_train, num_classes)
y_test_binary = keras.utils.to_categorical(y_test, num_classes)

x_train = x_train.reshape(3350, 10,1)
x_test = x_test.reshape(1650, 10,1)





input_layer = Input(shape=(10, 1))
conv1 = Conv1D(filters=32,
               kernel_size=8,
               strides=1,
               activation='relu',
               padding='same')(input_layer)
lstm1 = LSTM(32, return_sequences=True)(conv1)
output_layer = Dense(1, activation='sigmoid')(lstm1)
model = Model(inputs=input_layer, outputs=output_layer)

model.summary()
model.compile(loss='mse',optimizer='adam')

Finally when i am trying to fit the model with input

 model.fit(x_train,y_train)

ValueError                                Traceback (most recent call last)
<ipython-input-170-4719cf73997a> in <module>()
----> 1 model.fit(x_train,y_train)

2 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    133                         ': expected ' + names[i] + ' to have ' +
    134                         str(len(shape)) + ' dimensions, but got array '
--> 135                         'with shape ' + str(data_shape))
    136                 if not check_batch_axis:
    137                     data_shape = data_shape[1:]

ValueError: Error when checking target: expected dense_2 to have 3 dimensions, but got array with shape (3350, 1)

Can someone please help me resolving this error

Upvotes: 2

Views: 100

Answers (1)

Marco Cerliani
Marco Cerliani

Reputation: 22031

I see some problem in your code...

the last dimension output must be equal to the number of class and with multiclass tasks you need to apply a softmax activation: Dense(num_classes, activation='softmax')

you must set return_sequences=False in your last lstm cell because you need a 2D output and not a 3D

you must use categorical_crossentropy as loss function with one-hot encoded target

here a complete dummy example...

num_classes = 10
n_sample = 1000
X = np.random.uniform(0,1, (n_sample,10,1))
y = tf.keras.utils.to_categorical(np.random.randint(0,num_classes, n_sample))

input_layer = Input(shape=(10, 1))
conv1 = Conv1D(filters=32,
               kernel_size=8,
               strides=1,
               activation='relu',
               padding='same')(input_layer)
lstm1 = LSTM(32, return_sequences=False)(conv1)
output_layer = Dense(num_classes, activation='softmax')(lstm1)

model = Model(inputs=input_layer, outputs=output_layer)

model.compile(loss='categorical_crossentropy',optimizer='adam')
model.fit(X,y, epochs=5)

Upvotes: 2

Related Questions