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.
Este ejercicio forma parte del curso
Machine Learning for Finance in Python
Instrucciones del ejercicio
- Set the
input_dim
of the first neural network layer to be the number of columns ofscaled_train_features
with the.shape[1]
property. - Use the custom
sign_penalty
loss function to.compile()
ourmodel_2
. - Plot the loss from the
history
of the fit. The loss is underhistory.history['loss']
.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
# 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()