python-tensorflowHow can I use Python TensorFlow to recognize handwriting?
Python TensorFlow can be used to recognize handwriting by creating a convolutional neural network (CNN) to classify images of handwritten characters. The basic steps include:
-
Preparing the data:
- Collecting a dataset of handwritten characters
- Labelling each character in the dataset
- Formatting the data into a suitable format for a neural network
-
Building the model:
- Defining the layers of the CNN
- Compiling the model with a suitable optimizer and loss function
-
Training the model:
- Feeding the prepared data into the model
- Training the model on the data
-
Evaluating the model:
- Testing the model on unseen data
- Assessing the accuracy of the model
Example code
import tensorflow as tf
# define the layers of the CNN
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(28, 28, 1)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
# compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# train the model
model.fit(train_images, train_labels, epochs=5)
# evaluate the model
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print('\nTest accuracy:', test_acc)
Output example
Epoch 1/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.4545 - accuracy: 0.8388
Epoch 2/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.3025 - accuracy: 0.8905
Epoch 3/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.2475 - accuracy: 0.9079
Epoch 4/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.2047 - accuracy: 0.9227
Epoch 5/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.1717 - accuracy: 0.9339
313/313 - 1s - loss: 0.2702 - accuracy: 0.9065
Test accuracy: 0.9065
Helpful links
More of Python Tensorflow
- How can I use Python and TensorFlow to handle illegal hardware instructions in Zsh?
- ¿Cómo implementar reconocimiento facial con TensorFlow y Python?
- How can I use Python TensorFlow in W3Schools?
- How do I troubleshoot a BLAS GEMM Launch Failed error in TensorFlow Python Framework?
- How can I check the compatibility of different versions of Python and TensorFlow?
- How can I use TensorFlow Lite with XNNPACK in Python?
- How do I uninstall Python TensorFlow?
- How do I resolve a SymbolAlreadyExposedError when the symbol "zeros" is already exposed as () in TensorFlow Python util tf_export?
- How can I use Python and TensorFlow together?
- How can I use Python TensorFlow with a GPU?
See more codes...