Skip to content
Snippets Groups Projects
Commit 141a5ebf authored by Devon Harstrom's avatar Devon Harstrom
Browse files

Part 1 done. Made a complete.py file to make a complete.keras model

reaches 95+

Also made a bonus model that achieves 98%+
parent 32ec886d
No related branches found
No related tags found
1 merge request!1Working_branch
File added
import tensorflow as tf
from tensorflow import keras
tf.random.set_seed(1234)
print("--Get data--")
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print("--Process data--")
x_train, x_test = x_train / 255.0, x_test / 255.0
print("--Make model--")
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28, 1)),
tf.keras.layers.Dense(30, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(20, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
print("--Fit model--")
model.fit(x_train, y_train, epochs=10, verbose=2)
print("--Evaluate model--")
model_loss1, model_acc1 = model.evaluate(x_train, y_train, verbose=2)
model_loss2, model_acc2 = model.evaluate(x_test, y_test, verbose=2)
print(f"Train / Test Accuracy: {model_acc1*100:.1f}% / {model_acc2*100:.1f}%")
#save - complete
model.save("MNIST-Complete.keras")
#load complete
# loaded_model = tf.keras.models.load_model("MNIST-Complete.keras")
\ No newline at end of file
File added
import tensorflow as tf
from tensorflow import keras # whats wrong?
tf.random.set_seed(1234)
print("--Get data--")
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print("--Process data--")
x_train, x_test = x_train / 255.0, x_test / 255.0
print("--Make model--")
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(28, 28, 1), padding='same', strides=(2, 2)),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPooling2D((2, 2)),
# added kernel_regularizer to help
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same', strides=(2, 2)),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Flatten(input_shape=(28, 28, 1)),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
print("--Fit model--")
model.fit(x_train, y_train, epochs=20, verbose=2)
print("--Evaluate model--")
model_loss1, model_acc1 = model.evaluate(x_train, y_train, verbose=2)
model_loss2, model_acc2 = model.evaluate(x_test, y_test, verbose=2)
print(f"Train / Test Accuracy: {model_acc1*100:.1f}% / {model_acc2*100:.1f}%")
#save - complete
model.save("MNIST-Complete98+.keras")
#load complete
# loaded_model = tf.keras.models.load_model("MNIST-Complete.keras")
\ No newline at end of file
#Original Author: Jonathan Hudson
#CPSC 501 W25
import tensorflow as tf
tf.random.set_seed(1234)
print("--Get data--")
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print("--Process data--")
x_train, x_test = x_train / 255.0, x_test / 255.0
print("--Make model--")
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='sgd', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
print("--Fit model--")
model.fit(x_train, y_train, epochs=1, verbose=2)
print("--Evaluate model--")
model_loss1, model_acc1 = model.evaluate(x_train, y_train, verbose=2)
model_loss2, model_acc2 = model.evaluate(x_test, y_test, verbose=2)
print(f"Train / Test Accuracy: {model_acc1*100:.1f}% / {model_acc2*100:.1f}%")
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment