python-kerasHow do I use the fit() function to train a Keras model in Python?
The fit()
function is used to train a Keras model in Python. It takes the model, the training data, and the number of epochs as arguments and returns a history object.
Example code
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# Create a model
model = Sequential()
model.add(Dense(2, activation='relu', input_dim=3))
model.add(Dense(1, activation='sigmoid'))
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train the model
training_data = [[1,2,3],[4,5,6]]
target_data = [[0],[1]]
history = model.fit(training_data, target_data, epochs=10)
Output (if any):
Epoch 1/10 1/1 [==============================] - 0s 2ms/step - loss: 0.6931 - accuracy: 0.5000 Epoch 2/10 1/1 [==============================] - 0s 2ms/step - loss: 0.6931 - accuracy: 0.5000 Epoch 3/10 1/1 [==============================] - 0s 2ms/step - loss: 0.6931 - accuracy: 0.5000 ... Epoch 10/10 1/1 [==============================] - 0s 2ms/step - loss: 0.6931 - accuracy: 0.5000
Code explanation
from tensorflow.keras.models import Sequential
: This imports the Sequential model from the Keras library.from tensorflow.keras.layers import Dense
: This imports the Dense layer from the Keras library.model = Sequential()
: This creates an empty sequential model.model.add(Dense(2, activation='relu', input_dim=3))
: This adds a dense layer with two neurons and a ReLU activation function to the model. The input dimension is set to 3.model.add(Dense(1, activation='sigmoid'))
: This adds a dense layer with one neuron and a sigmoid activation function to the model.model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
: This compiles the model, using the Adam optimizer, the binary cross entropy loss function, and accuracy as the metric.history = model.fit(training_data, target_data, epochs=10)
: This trains the model using the training data and target data, for 10 epochs. The history object is returned.
Helpful links
More of Python Keras
- How do I install the Python Keras .whl file?
- How do I use validation_data when creating a Keras model in Python?
- How can I visualize a Keras model using Python?
- How can I use YOLO with Python and Keras?
- How do I check which version of Keras I am using in Python?
- How do I use Python Keras to create a Zoom application?
- How do I install Keras on Windows using Python?
- How can I use Python and Keras to create a backend for my application?
- How do I use Python Keras to zip a file?
- How can I use Python and Keras together?
See more codes...