Repository navigation
Expand file tree
/
Copy pathsentdex.py
More file actions
75 lines (53 loc) · 2.18 KB
/
Copy pathsentdex.py
File metadata and controls
75 lines (53 loc) · 2.18 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import tensorflow as tf
import tensorflow.keras as keras
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# print(tf.__version__)
minst=tf.keras.datasets.mnist #28x28 image of hand written digits 0-9
# load data from minst and split into train and test data
(x_train,y_train),(x_test,y_test)=minst.load_data()
# normalize the number
x_train=keras.utils.normalize(x_train,axis=1)
x_test=keras.utils.normalize(x_test,axis=1)
print("start model")
# created model squ
# The first line creates a Sequential model This is the simplest kind of Keras model
model=keras.models.Sequential()
# we build the first layer and add it to the model. It is a Flatten layer whose role is simply to convert each input image into a 1D array
model.add(keras.layers.Flatten())
#hidden layer one and 128 nodes or neuron and activate functions relu
model.add(keras.layers.Dense(128, activation=tf.nn.relu))
#hidden layer two /// /////////////////
model.add(keras.layers.Dense(128,activation=tf.nn.relu))
# Finally, we add a Dense output layer with 10 neurons (one per class), using the softmax activation function (because the classes are exclusive).
model.add(keras.layers.Dense(10,activation=tf.nn.softmax))
# compile: method to specify the loss function and the optimizer to use
# sgd - adam
model.compile(
optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"]
)
history=model.fit(x_train,y_train,epochs=3)
pd.DataFrame(history.history).plot(figsize=(8, 5))
plt.grid(True)
plt.gca().set_ylim(0, 1) # set the vertical range to [0-1]
plt.show()
# # evaluate model
val_loss,val_acc=model.evaluate(x_test,y_test)
print(f"loss is {val_loss} accuracy {val_acc}\n","*"*40)
print("end model")
# plt.imshow(x_train[0], cmap=plt.cm.binary)
# print(x_train[0])
# plt.show()
# #save moedel
# model.save('epic_num_reader_model')
# new_model=tf.keras.models.load_model('epic_num_reader_model')
# #predicts
# predictions=new_model.predict([x_test])
# print(predictions)
# print(np.argmax(predictions[0]))
# plt.imshow(x_test[0])
# print(x_train[0])
# plt.show()