Get startedGet started for free

Build and fit a simple neural net

The next model we will learn how to use is a neural network. Neural nets can capture complex interactions between variables, but are difficult to set up and understand. Recently, they have been beating human experts in many fields, including image recognition and gaming (check out AlphaGo) -- so they have great potential to perform well.

To build our nets we'll use the keras library. This is a high-level API that allows us to quickly make neural nets, yet still exercise a lot of control over the design. The first thing we'll do is create almost the simplest net possible -- a 3-layer net that takes our inputs and predicts a single value. Much like the sklearn models, keras models have a .fit() method that takes arguments of (features, targets).

This exercise is part of the course

Machine Learning for Finance in Python

View Course

Exercise instructions

  • Create a dense layer with 20 nodes and the ReLU ('relu') activation as the 2\(^{nd}\) layer in the neural network.
  • Create the last dense layer with 1 node and a linear activation (activation='linear').
  • Fit the model to the scaled_train_features and train_targets.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

from keras.models import Sequential
from keras.layers import Dense

# Create the model
model_1 = Sequential()
model_1.add(Dense(100, input_dim=scaled_train_features.shape[1], activation='relu'))
model_1.add(Dense(____, activation=____))
model_1.add(Dense(____, activation=____))

# Fit the model
model_1.compile(optimizer='adam', loss='mse')
history = model_1.fit(____, ____, epochs=25)
Edit and Run Code