-
Notifications
You must be signed in to change notification settings - Fork 0
/
mNistNumberReader.py
47 lines (26 loc) · 1.23 KB
/
mNistNumberReader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import tensorflow as tf
import numpy as np
model = tf.keras.models.Sequential([ # 3 st layers,128, 64 och 10 är antal neurons /layer
tf.keras.layers.Dense(128, activation= "relu"),
tf.keras.layers.Dense(64, activation= "relu"),
tf.keras.layers.Dense(10, activation= "softmax"),
])
#ladda MNIST
(x_train,y_train), (x_test,y_test) = tf.keras.datasets.mnist.load_data()
# återskapar, formar och konverterar inputen till float32
x_train = (x_train / 255.).reshape([-1,784]).astype(np.float32)
x_test = (x_test / 255.).reshape([-1,784]).astype(np.float32)
#convert labebls till one hot vectors
y_train = tf.one_hot(y_train,10)
y_test = tf.one_hot(y_test,10)
#fixar själva träningen
train_data = tf.data.Dataset.from_tensor_slices((x_train,y_train))
train_data = train_data.shuffle(500).batch(32)
model.compile(optimizer="adam", loss="categorical_crossentropy")
model.fit(train_data)
#kollar accuary av lästa siffran
def accuracy(y_pred,y_true):
correct_prediction = tf.equal(tf.argmax(y_pred,-1), tf.argmax(y_true,-1))
return tf.reduce_mean(tf.cast(correct_prediction,tf.float32), axis = -1)
pred = model(x_test)
print(f"Test accuracy of read number: {accuracy(pred, y_test)}") #bör ha ~96% accuracy