I was given an assignment at the university to write a classifier of simple text dataset. I have 5 classes: [‘0’, ‘1’, ‘2’, ‘3’, ‘4’]. I don’t understand neural networks well and using internet i wrote something =)
BUT
if i use categorical_crossentropy it gives me an error
Arguments 'target and 'output' must have the same rank (ndim). Received: target. shape=(None,), output.shape=(None,6)
If i use ‘sparse_categorical_crossentropy’ it gives me 2d array in output.
x_train, x_test, y_train, y_test = train_test_split(X_scaled, y_encoded, test_size=0.2, random_state=42)
x_train = x_train / 255
x_test = x_test / 255
def b_m(hp):
model = Sequential()
activation_choice = hp.Choice('activation', values=['relu', 'sigmoid', 'tanh', 'elu', 'selu'])
model.add(Dense(units=hp.Int('units_input',
min_value=512,
max_value=1024,
step=32),
input_dim=2,
activation=activation_choice))
model.add(Dense(units=hp.Int('units_hidden',
min_value=128,
max_value=600,
step=32),
activation=activation_choice))
model.add(Dense(5, activation='softmax'))
model.compile(
optimizer=hp.Choice('optimizer', values=['adam','rmsprop','SGD']),
loss='sparse_categorical_crossentropy',
#loss='categorical_crossentropy',
metrics=['accuracy'])
return model
tuner = kt.Hyperband(b_m,
objective='val_accuracy',
max_epochs=10,
factor=3,
directory='test_dir')
stop_early = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5)
tuner.search(x_train, y_train, epochs=50, validation_split=0.2, callbacks=[stop_early])
best_hps=tuner.get_best_hyperparameters(num_trials=1)[0]
model = tuner.hypermodel.build(best_hps)
history = model.fit(x_train, y_train, epochs=50, validation_split=0.2)
val_acc_per_epoch = history.history['val_accuracy']
best_epoch = val_acc_per_epoch.index(max(val_acc_per_epoch)) + 1
hypermodel = tuner.hypermodel.build(best_hps)
hypermodel.fit(x_train, y_train, epochs=best_epoch, validation_split=0.2)
eval_result = hypermodel.evaluate(x_test, y_test)
print("[loss, test]:", eval_result)
y_pred = hypermodel.predict(x_test)
So, i have three questions: 1. what type of loss is best to use in this situation?
2. if it is better to use categorical_crossentropy, then how to fix the error that occurs
3. if it is better to use sparse_categorical_crossentropy, then how to convert the output from a 2d array to a one-dimensional array with classes
please help
I tried to reshape input data and change parameters of my model but nothing happend
randomname is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.