保存
python
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import gzip
# fashion_mnist=tf.keras.datasets.fashion_mnist
# (train_images,train_labels),(test_images,test_labels)=fashion_mnist.load_data()
#数据在个人资源里面,放到该文件目录中即可
def load_data():
# dirname = os.path.join('datasets', 'fashion-mnist')
# base = 'https://storage.googleapis.com/tensorflow/tf-ke ras-datasets/'
files = [
'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',
't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'
]
paths = []
for fname in files:
paths.append(fname)
with gzip.open(paths[0], 'rb') as lbpath:
y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)
with gzip.open(paths[1], 'rb') as imgpath:
x_train = np.frombuffer(
imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28)
with gzip.open(paths[2], 'rb') as lbpath:
y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)
with gzip.open(paths[3], 'rb') as imgpath:
x_test = np.frombuffer(
imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28)
return (x_train, y_train), (x_test, y_test)
(x_train, y_train), (x_test, y_test)=load_data()
x_train=np.expand_dims(x_train,-1)
y_train_one_hot=tf.one_hot(y_train,10).numpy()
x_train=np.float32(x_train)
model=tf.keras.Sequential([
tf.keras.layers.Conv2D(1,3,1),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(256,activation="relu"),
tf.keras.layers.Dense(128,activation="relu"),
tf.keras.layers.Dense(64,activation="relu"),
tf.keras.layers.Dense(32,activation="relu"),
tf.keras.layers.Dense(10,activation="softmax")
])
model.build(input_shape=[None,28,28,1])
model.summary()
model.compile(optimizer=tf.keras.optimizers.Adam(),loss=tf.keras.losses.CategoricalCrossentropy(),metrics=[tf.keras.losses.CategoricalCrossentropy()])
import os
checkpoint_path="training_1/cp.ckpt"
cp_callback=tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,save_weights_only=True,verbose=1)
history=model.fit(x_train,y_train_one_hot,epochs=10,callbacks=[cp_callback])
LOSS=history.history["loss"]
plt.plot(LOSS)
plt.show()
导入
python
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import gzip
# fashion_mnist=tf.keras.datasets.fashion_mnist
# (train_images,train_labels),(test_images,test_labels)=fashion_mnist.load_data()
#数据在个人资源里面,放到该文件目录中即可
def load_data():
# dirname = os.path.join('datasets', 'fashion-mnist')
# base = 'https://storage.googleapis.com/tensorflow/tf-ke ras-datasets/'
files = [
'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',
't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'
]
paths = []
for fname in files:
paths.append(fname)
with gzip.open(paths[0], 'rb') as lbpath:
y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)
with gzip.open(paths[1], 'rb') as imgpath:
x_train = np.frombuffer(
imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28)
with gzip.open(paths[2], 'rb') as lbpath:
y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)
with gzip.open(paths[3], 'rb') as imgpath:
x_test = np.frombuffer(
imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28)
return (x_train, y_train), (x_test, y_test)
(x_train, y_train), (x_test, y_test)=load_data()
x_test=np.expand_dims(x_test,-1)
model=tf.keras.Sequential([
tf.keras.layers.Conv2D(1,3,1),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(256,activation="relu"),
tf.keras.layers.Dense(128,activation="relu"),
tf.keras.layers.Dense(64,activation="relu"),
tf.keras.layers.Dense(32,activation="relu"),
tf.keras.layers.Dense(10,activation="softmax")
])
model.build(input_shape=[None,28,28,1])
model.summary()
model.compile(optimizer=tf.keras.optimizers.Adam(),loss=tf.keras.losses.CategoricalCrossentropy(),metrics=[tf.keras.losses.CategoricalCrossentropy()])
checkpoint_path="training_1/cp.ckpt"
model.load_weights(checkpoint_path)
x_test=np.array(x_test,dtype=np.float32)
print(np.argmax(model.predict(x_test),axis=1))
print(y_test)
np.sum((y_test==np.argmax(model.predict(x_test),axis=1))*1)/y_test.shape[0]