Untitled

mail@pastecode.io avatar
unknown
plain_text
19 days ago
1.1 kB
1
Indexable
Never
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()

train_images=train_images/255
test_images=test_images/255

for i in range(0, len(train_images)):
  for j in range(0,28):
    for k in range(0,28):
      if train_images[i][j][k] > 0.6:
         train_images[i][j][k] = 1
      else:
         train_images[i][j][k] = 0

train_labels = tf.keras.utils.to_categorical(train_labels, 10)

model = tf.keras.Sequential([
        tf.keras.Input(shape=(28, 28, 1)),
        tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),
        tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
        tf.keras.layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
        tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dropout(0.5),
        tf.keras.layers.Dense(10, activation="softmax"),
    ])

model.compile(optimizer="adam",loss="categorical_crossentropy", metrics=["accuracy"])
model.fit(train_images, train_labels, epochs=10)