9951 explained code solutions for 126 technologies


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:

  1. 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
  2. Building the model:

    • Defining the layers of the CNN
    • Compiling the model with a suitable optimizer and loss function
  3. Training the model:

    • Feeding the prepared data into the model
    • Training the model on the data
  4. 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

Edit this code on GitHub