A sequential model can be built in Keras using the ‘Sequential’ method. The number and type of layers are specified inside this method.
Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?
We will be using the abalone dataset, which contains a set of measurements of abalone. Abalone is a type of sea snail. The goal is to predict the age based on other measurements.
We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.
print("The sequential model is being built") abalone_model = tf.keras.Sequential([ layers.Dense(64), layers.Dense(1) ]) abalone_model.compile(loss = tf.losses.MeanSquaredError(),optimizer = tf.optimizers.Adam()) print("The data is being fit to the model") abalone_model.fit(abalone_features, abalone_labels, epochs=10)
Code credit: https://www.tensorflow.org/tutorials/load_data/csv
Output
The sequential model is being built The data is being fit to the model Epoch 1/10 104/104 [==============================] - 0s 963us/step - loss: 84.2213 Epoch 2/10 104/104 [==============================] - 0s 924us/step - loss: 16.0268 Epoch 3/10 104/104 [==============================] - 0s 860us/step - loss: 9.4125 Epoch 4/10 104/104 [==============================] - 0s 898us/step - loss: 8.9159 Epoch 5/10 104/104 [==============================] - 0s 912us/step - loss: 7.9076 Epoch 6/10 104/104 [==============================] - 0s 936us/step - loss: 6.8316 Epoch 7/10 104/104 [==============================] - 0s 992us/step - loss: 7.1021 Epoch 8/10 104/104 [==============================] - 0s 1ms/step - loss: 7.0550 Epoch 9/10 104/104 [==============================] - 0s 1ms/step - loss: 6.2762 Epoch 10/10 104/104 [==============================] - 0s 883us/step - loss: 6.5584 <tensorflow.python.keras.callbacks.History at 0x7fda82a35160>
Explanation
- A regression model is built to predict the 'age' column of the abalone dataset.
- A sequential model is built, since there is a single input tensor.
- The model is compiled (trained), and then the features and labels are passed to the 'Model.fit' method.