Reputation: 21
I changed the data type but I could not resolve the error.
I tried One-Hot Encoding but it doesn't work too.
I don't know what's wrong:(
seed = 0
np.random.seed(seed)
tf.set_random_seed(seed)
df = pd.read_csv('HW01_dataset_tae.txt', sep=',' ,header=None, names = ["Native", "Instructor", "Course", "Semester", "Class Size", "Evaluation"])
dataset = df.values # dataframe to int64
X = dataset[:,0:5] # attribute
Y_Eva = dataset[:,5] # class
e = LabelEncoder()
e.fit(Y_Eva)
Y = e.transform(Y_Eva)
K = 10
kFold = StratifiedKFold(n_splits=K, shuffle=True, random_state=seed)
accuracy = []
for train_index, test_index in kFold.split(X,Y):
model = Sequential()
model.add(Dense(16, input_dim=5, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(3, activation='softmax'))
model.compile(loss='mean_squared_error',
optimizer='adam',
metrics=['accuracy'])
model.fit(X[train_index], Y[train_index], epochs=100, batch_size=2)
the error ; Error when checking target: expected dense_2 to have shape (3,) but got array with shape (1,)
is detected at here ; model.fit(X[train_index], Y[train_index], epochs=100, batch_size=2).
What shout I do?
Upvotes: 1
Views: 259
Reputation: 21
I solved the problem.
At this code,
model.fit(X[train_index], Y[train_index], epochs=100, batch_size=2)
the number of rows in 'Y[train_index]' must be three because the classes are three.
The error came out since each Y[train_index] has only one row.
So, I used One-Hot Encoding and changed the code like this.
e = LabelEncoder()
e.fit(Y_Eva)
Y = e.transform(Y_Eva)
Y_encoded = np_utils.to_categorical(Y) # changed code
K = 10
kFold = StratifiedKFold(n_splits=K, shuffle=True, random_state=seed)
accuracy = []
for train_index, test_index in kFold.split(X,Y):
model = Sequential()
model.add(Dense(32, input_dim=5, activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(X[train_index], Y_encoded[train_index], epochs=100, batch_size=2) # changed code
Finally, I was able to run the code.
Upvotes: 1
Reputation: 392
TensorFlow has made some documenation on the dense layer, and if you then instead of saying input_dim says input_shape you can specify the prefered shape.
https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense
model = Sequential()
model.add(Dense(16, input_shape=(5,))) # Then your data has to be of shape (batch x 5)
When you then are adding another dense layer, you actaully don't have to provide the input_sahpe
model.add(Dense(10))
Upvotes: 0