9951 explained code solutions for 126 technologies


python-kerasHow can I use Python Keras to implement a neural network regression model?


Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano. It was developed to make implementing deep learning models as fast and easy as possible for research and development.

To implement a neural network regression model in Python Keras, we need to first define the model. This can be done using the Sequential model from Keras. The following example code defines a simple neural network with one hidden layer of 10 neurons, and an output layer with one neuron for the regression model:

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

model = Sequential()
model.add(Dense(10, input_dim=1, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')

Once the model is defined, it can be trained using the fit method. The following example code trains the model on a dataset of 100 samples with one input feature and one output:

import numpy as np

X = np.random.rand(100,1)
y = 2 * X + np.random.rand(100,1)

model.fit(X, y, epochs=100, batch_size=10)

Once the model is trained, it can be used to make predictions using the predict method. The following example code makes predictions for a new dataset of 10 samples:

X_new = np.random.rand(10,1)
y_pred = model.predict(X_new)

print(y_pred)

Output example

[[0.8456615 ]
 [1.0991792 ]
 [1.2709074 ]
 [0.9389981 ]
 [1.1464477 ]
 [1.0845203 ]
 [0.9142562 ]
 [1.1110608 ]
 [1.1809831 ]
 [1.0685866 ]]

The code above consists of the following parts:

  1. from keras.models import Sequential - imports the Sequential model from Keras.
  2. from keras.layers import Dense - imports the Dense layer from Keras.
  3. model = Sequential() - creates a new Sequential model.
  4. model.add(Dense(10, input_dim=1, activation='relu')) - adds a hidden layer of 10 neurons with one input feature and ReLU activation.
  5. model.add(Dense(1)) - adds an output layer with one neuron.
  6. model.compile(loss='mean_squared_error', optimizer='adam') - compiles the model with mean squared error loss and Adam optimizer.
  7. model.fit(X, y, epochs=100, batch_size=10) - trains the model on the given dataset for 100 epochs with batch size of 10.
  8. model.predict(X_new) - makes predictions for a new dataset.

Helpful links

Edit this code on GitHub