Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neural Network model

I have sample data with 6 columns and 100 rows (all values are integers). there are 20 classes that the input data is classified into. This is the model that I tried building:

model = Sequential()
model.add(Dense(50,input_shape=X.shape[1:],activation='relu'))

model.add(Dense(20,activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', 
              metrics=['accuracy'])
model.summary()
model.fit(X, Y, epochs=1000, verbose=0)
predictions=model.predict(test_data)

However, I get an error:

Error when checking target: expected dense_2 to have shape (20,) but got array with shape (1,)

I have two questions:

  1. What am I doing wrong?
  2. Can you give me a proper architecture for this?
like image 615
RASHMI K A Avatar asked Sep 18 '26 10:09

RASHMI K A


1 Answers

You need to convert Y to a binary class matrix using to_categorical (docs).

import sklearn.datasets
X,Y = sklearn.datasets.make_classification(n_samples=100, n_features=6, n_redundant=0,n_informative=6, n_classes=20)

import numpy as np
from keras import Sequential
from keras.layers import Dense
from keras.utils import to_categorical
from keras import backend as K
K.clear_session()

model = Sequential()
model.add(Dense(50,input_dim=X.shape[1],activation='softmax'))
model.add(Dense(20,activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', 
              metrics=['accuracy'])
model.summary()
model.fit(X, to_categorical(Y), epochs=1000, verbose=1) # <---

Also you can use sklearn for that too.

like image 171
Arkady. A Avatar answered Sep 20 '26 22:09

Arkady. A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!