python-kerasHow do I create a simple example using Python and Keras?
Creating a simple example using Python and Keras is relatively straightforward. Below is an example of a basic neural network with one input and one output layer.
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
# Create a model
model = Sequential()
model.add(Dense(1, input_dim=1, activation='sigmoid'))
# Compile the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Train the model
x = np.array([[0],[1]])
y = np.array([0,1])
model.fit(x, y, epochs=200, batch_size=2, verbose=1)
# Evaluate the model
scores = model.evaluate(x, y)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
Output example
200/200 [==============================] - 0s 4ms/step
accuracy: 100.00%
The code consists of the following parts:
- Importing necessary libraries such as numpy and keras
- Creating a model object with the Sequential() method
- Adding a layer to the model with the add() method
- Compiling the model with the compile() method
- Training the model with the fit() method
- Evaluating the model with the evaluate() method
- Printing the accuracy of the model
For further information on using Python and Keras, please refer to the following links:
More of Python Keras
- How can I use word2vec and Keras to develop a machine learning model in Python?
- How do I use zero padding in Python Keras?
- How do I use Python Keras to zip a file?
- How can I use XGBoost, Python and Keras together to build a machine learning model?
- How can I install the python module tensorflow.keras in R?
- How can I use YOLO with Python and Keras?
- How do I install Keras on Windows using Python?
- How do I create a layer in Python using Keras?
- How do I install the Python Keras .whl file?
- How do I check which version of Keras I am using in Python?
See more codes...