tensorflow2.0,自定义损失函数

def customized_mse(y_true, y_pred):
    return tf.reduce_mean(tf.square(y_pred - y_true))

model = keras.models.Sequential([
    					keras.layers.Dense(30, activation='relu', input_shape=x_train.shape[1:]),
    					keras.layers.Dense(1),])
model.summary()
model.compile(loss=customized_mse, optimizer="sgd", metrics=["mean_squared_error"])
callbacks = [keras.callbacks.EarlyStopping(patience=5, min_delta=1e-2)]

history = model.fit(x_train_scaled, y_train,
                    validation_data = (x_valid_scaled, y_valid),
                    epochs = 100,
                    callbacks = callbacks)

你可能感兴趣的:(tensorflow)