Reputation: 321
I have built up a NN for classification, but when trying to compile I get problems with the dimensions of input and output:
from keras.models import Sequential
from keras.layers import Dense
# data splited into input (X) and output (y) variables
model = Sequential()
model.add(Dense(12, input_dim=456, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(8, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
Here are the dimensions of my y
and X
print(y.shape, X.shape)
(8000, 1) (8000, 456, 3)
I have 8000 sub sets which contain 456 particles(x,y,z); and I have labels which are in y ranging from 0 to 7; this is also why my output layer has 8 nodes.
But when I fit with
model.fit(X, y, epochs=15, batch_size=10)
I do not get why this error occurs:
ValueError: Error when checking input: expected dense_26_input to have 2 dimensions, but got array with shape (8000, 456, 3)
Any suggestions?
Upvotes: 2
Views: 36
Reputation: 1290
To answer your question, you can achieve what you want by doing :
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
model = Sequential()
model.add(Dense(12, input_shape=(456,3), activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(8, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
Edit:
I think what you're looking for is that type of architecture :
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Flatten
model = Sequential()
model.add(Dense(12, input_shape=(456,3), activation='relu'))
model.add(Flatten())
model.add(Dense(8, activation='relu'))
model.add(Dense(8, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
So that it only output the 8 labels
Upvotes: 1