LoslegenKostenlos starten

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

<Kurs>Machine Learning for Finance in Python</Kurs>
Kurs ansehen

Übungsanweisungen

  • 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 praktische Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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