/
day3_mnist_train_ex4.py
44 lines (35 loc) · 1.35 KB
/
day3_mnist_train_ex4.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
# noinspection PyUnresolvedReferences
import tensorflow as tf
import os
import numpy as np
np.set_printoptions(threshold = np.inf)
mnist = tf.keras.datasets.mnist
(x_train,y_train),(x_test,y_test) = mnist.load_data()
x_train,x_test = x_train/255.,x_test/255.
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128,activation = "relu"),
tf.keras.layers.Dense(10,activation = "softmax")
])
model.compile(optimizer = "adam",
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),
metrics = ["sparse_categorical_accuracy"])
checkpoint_save_path = "./checkpoint/mnist.ckpt"
if os.path.exists(checkpoint_save_path+".index"):
print("-----------------load Data---------------")
model.load_weights(checkpoint_save_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(
filepath = checkpoint_save_path,
save_weights_only = True,
save_best_only = True
)
history = model.fit(x_train,y_train,batch_size = 2048,epochs = 5,validation_data = (x_test,y_test),
validation_freq = 1,callbacks = [cp_callback])
model.summary()
# print((model.trainable_variables))
file = open("./weights_variables.txt","w")
for v in model.trainable_variables:
file.write(str(v.name)+"\n")
file.write(str(v.shape)+"\n")
file.write(str(v.numpy())+"\n")
file.close()