Home > Software design >  TypeError: fit() got an unexpected keyword argument 'metrics'
TypeError: fit() got an unexpected keyword argument 'metrics'

Time:05-05

model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv1D(64, kernel_size=64, padding="same", activation='relu', input_shape=(x_train.shape[1], 1)))
model.add(tf.keras.layers.Conv1D(64, kernel_size=64, padding="same", activation='relu'))
model.add(tf.keras.layers.Dropout(0.6))
model.add(tf.keras.layers.MaxPooling1D(pool_size=2))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(20, activation='relu'))
model.add(tf.keras.layers.Dense(10))
opt = tf.keras.optimizers.SGD(lr=0.01)
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['acc'])
print(model.summary())
model.fit(x_train, y_train, epochs=epochs,validation_data=(x_test,y_test), 
          batch_size=batch_size, metrics=['acc'], verbose=1)

Having this error:

TypeError: fit() got an unexpected keyword argument 'metrics'

I am using a dataframe which was originally in csv format. The shape of the dataframe after splitting it into 80:20 ratio using:

x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

is (206138, 198) (51535, 198) (206138,) (51535,)

CodePudding user response:

since metrics is a parameter to model.compile, not model.fit as mentioned by @Rahul, the quick fix was to remove the metrics from model.compile

CodePudding user response:

as it is with the error code The fit method of the keras sequential does not support the metric factor.

Please refer to the Tensorflow document

compile(
optimizer='rmsprop',
loss=None,
metrics=None,
loss_weights=None,
weighted_metrics=None,
run_eagerly=None,
steps_per_execution=None,
jit_compile=None,
**kwargs

)

and fit

fit(
x=None,
y=None,
batch_size=None,
epochs=1,
verbose='auto',
callbacks=None,
validation_split=0.0,
validation_data=None,
shuffle=True,
class_weight=None,
sample_weight=None,
initial_epoch=0,
steps_per_epoch=None,
validation_steps=None,
validation_batch_size=None,
validation_freq=1,
max_queue_size=10,
workers=1,
use_multiprocessing=False

)

  • Related