LoslegenKostenlos loslegen

Fit neural net with custom loss function

Now we'll use the custom loss function we just created. This will enable us to alter the model's behavior in useful ways particular to our problem -- it's going to try to force the model to learn how to at least predict price movement direction correctly. All we need to do now is set the loss argument in our .compile() function to our function name, sign_penalty. We'll examine the training loss again to make sure it's flattened out.

Diese Übung ist Teil des Kurses

Machine Learning for Finance in Python

Kurs anzeigen

Anleitung zur Übung

  • Set the input_dim of the first neural network layer to be the number of columns of scaled_train_features with the .shape[1] property.
  • Use the custom sign_penalty loss function to .compile() our model_2.
  • Plot the loss from the history of the fit. The loss is under history.history['loss'].

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

# Create the model
model_2 = Sequential()
model_2.add(Dense(100, input_dim=____, activation='relu'))
model_2.add(Dense(20, activation='relu'))
model_2.add(Dense(1, activation='linear'))

# Fit the model with our custom 'sign_penalty' loss function
model_2.compile(optimizer='adam', loss=____)
history = model_2.fit(scaled_train_features, train_targets, epochs=25)
plt.plot(____)
plt.title('loss:' + str(round(history.history['loss'][-1], 6)))
plt.show()
Code bearbeiten und ausführen