识别手写集,cpu跑的代码,很慢~
__author__ = 'Administrator'
#tensorflow.keras.__version__=2.2.4-tf,tensorflow.__version__=2.1.0
import numpy as np
from tensorflow import keras
import matplotlib.pyplot as plt
#load the dataSet
(train_image,train_label),(test_image,test_label)=keras.datasets.fashion_mnist.load_data()
#print(train_image.shape)
#(60000, 28, 28)
#normalize
train_image=train_image/255
test_image=test_image/255
#build model
model=keras.Sequential()
#the first layer is used to make the input shape be suitable for the keras input format:60000*784 from 60000*28*28
model.add(keras.layers.Flatten(input_shape=(28,28)))
#the hide layer with 128 hidden neures
model.add(keras.layers.Dense(128,activation='relu'))
#dropout some data to avoid overriding
model.add(keras.layers.Dropout(0.5))
#model.add(keras.layers.Dense(128,activation='relu'))
#model.add(keras.layers.Dense(128,activation='relu'))
#the full connective layer to output the results with softmax as its activation
model.add(keras.layers.Dense(10,activation='softmax'))
#softmax:let 10 outputs' probability are between [0,1], and their sum is 1
'''
#sparse_categorical_crossentropy:0,1,2,3,4,5,6,7……, label is order encoded, if the label is one-hot encoded, it is used
#to use categorical_crossentropy
#if you would like to use order code to decode the label, you should use the following code:
model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['acc'])
#sparse_categorical_crossentropy:0,1,2,3,4,5,6,7……, label is order encoded, if the label is one-hot encoded, it is used
#to use categorical_crossentropy
model.fit(train_image,train_label,epochs=5,verbose=2)
model.evaluate(test_image,test_label)
'''
#if you'd like to use onehot to decode the label, transform it to onehot code and use categorical_crossentropy as loss method
train_label_onehot=keras.utils.to_categorical(train_label)
test_label_onehot=keras.utils.to_categorical(test_label)
model.compile(loss='categorical_crossentropy',optimizer=keras.optimizers.Adam(learning_rate=0.01),metrics=['acc'])
#start training, model.fit(train data,train label, train times, verbose=2:don't show the detail)
#model.fit(train_image,train_label_onehot,epochs=5,verbose=1)
#validation_data=(test_image,test_label_onehot) will show val_loss and val_acc, meanwhile the return in history also including those two values
h=model.fit(train_image,train_label_onehot,epochs=5,verbose=2,validation_data=(test_image,test_label_onehot))
#evaluate the predicted label with test label, the return are loss and accuracy
model.evaluate(test_image,test_label_onehot)
#predict the tested image, the return are the result of tested image with 10 outputs for each tested image
predict=model.predict(test_image)
#to store the wrong predicted numbers
count=0
for i in range(predict.shape[0]):
#x is to store the index of each predicted images's max probability
x=(np.argmax(predict[i]))
y=(test_label[i])
#if the predicted value is not equal with the true value, print the log
if x!=y:
count+=1
#print(count," the ",i,"is wrongly predicted, the correct one is ",y," predicted one is ",x)
print("the correct rate is ",(predict.shape[0]+0.0-count)/predict.shape[0])
print(h.history.keys())
plt.plot(h.epoch,h.history.get('loss'))
plt.plot(h.epoch,h.history.get('val_loss'))
plt.legend(['loss','val_loss'])
plt.show()
结果
中间有许多预测错误的log
N久之后,继续学习
__author__ = 'pc'
import os
os.environ['TF_CPP_MIN_LOG_LEVLE'] ='2'
import tensorflow as tf
from tensorflow.keras import datasets,layers,optimizers,Sequential,metrics
assert tf.__version__.startswith('2.')
#数据预处理
def preprocess(x,y):
#转换x,y的数据类型为张量
x = tf.cast(x,dtype=tf.float32)/255.
y = tf.cast(y,dtype=tf.int32)
return x,y
(x,y),(x_test,y_test) = datasets.fashion_mnist.load_data()#从官网上下载数据集,并且返回值格式为(x_train,y_train),(x_test,y_test)
print(x.shape,y.shape,"x_test,y_test",x_test.shape,y_test.shape)#(60000, 28, 28) (60000,) x_test,y_test (10000, 28, 28) (10000,)
batch_size = 128
#tf.data.Dataset.from_tensor_slices((x,y)):
#对传入的(x,y)进行切分,如果训练集为(5,3)最终产生的db中有5条数据,一条数据形状都是一个(3,),那么在手写数据集中,
# 每一个数据对应一个标签:数据(28,28,)对应(1,),把每一条数据和其标签对应起来了,之前是数据是数据 ,标签是标签,
db = tf.data.Dataset.from_tensor_slices((x,y))
#map(function,iterable,...):
#map(None,[2,4,6],[3,2,1])
# 结果如下
#[(2,3),(4,2),(6,1)]
#db.map(preprocess)之后对应的结果是[(x1,y1),(x2,y2)……]
#shuffle(11000):定义随机打乱数据时buffer的大小,按照顺序每11000条打乱一次
#batch(batch_size):按照顺序将数据长度划分为N*batch_size,每次取batch_size大小的数据,直到取完,取到最后一条batch大小可能会小于batch_size
db = db.map(preprocess).shuffle(11000).batch(batch_size)
print('db',len(db))#db 60000条数据被分为469条抽样数据
db_test = tf.data.Dataset.from_tensor_slices(((x_test,y_test)))
db_test = db_test.map(preprocess).batch(batch_size)
print('db_test',len(db_test)) #db_test 79,10000条数据被分为79条抽样数据
db_iter = iter(db)
sample = next(db_iter)#指向下一条抽样数据
#定义模型
model = Sequential([
layers.Dense(256,activation=tf.nn.relu),
layers.Dense(128,activation=tf.nn.relu),
layers.Dense(64,activation=tf.nn.relu),
layers.Dense(32,activation=tf.nn.relu),
layers.Dense(10),
])
#模型的输入层为可变尺寸
model.build(input_shape=[None,28*28])
model.summary()#输出图形结构
#优化函数
optimizer = optimizers.Adam(lr = 1e-3)
#tf.keras.utils.plot_model(model,'model_image.png',show_shapes=True)#另一种可视化模型的方法,但是没有跑出来,额
if __name__=='__main__':
for epoch in range(30):
for step ,(x,y) in enumerate(db):
x = tf.reshape(x,[-1,28*28])
with tf.GradientTape() as tape:
y_predict = model(x)
#print("y_predict",y_predict.shape) #y_predict (128, 10)
y_true = tf.one_hot(y,depth=10)
#print("y onehot",y_onehot.shape) #y onehot (128, 10)
loss_mse = tf.reduce_mean(tf.losses.MSE(y_true,y_predict))
loss_ce = tf.reduce_mean(tf.losses.categorical_crossentropy(y_true,y_predict,from_logits=True))
#MSE均方差损失函数适合做二分类,手写是多分类(10个),
#输出可以看到均方差和交叉熵损失函数categorical_crossentropy(),均方差的损失增大结果不收敛,而交叉熵的损失是降低的,正确率增大结果收敛,交叉熵损失函数更适合多分类
#另外根据标签不同,交叉熵损失函数可选择不同的函数:sparse_categorical_crossentropy函数要求标签是 数字编码:2, 0, 1
#categorical_crossentropy函数要求标签是 [0, 0, 1], [1, 0, 0], [0, 1, 0],适合本算法
#计算梯度
grads = tape.gradient(loss_ce,model.trainable_variables)
#梯度grads通过优化函数计算后,再赋值给模型中对应的参数model.trainable_variables
optimizer.apply_gradients(zip(grads,model.trainable_variables))
if step%100 ==0:
print(epoch,step,'loss',float(loss_ce),'loss mse',float(loss_mse))
#测试,计算争取个数和总的数据数
correct_num,all_num = 0,0
for x,y in db_test:
x = tf.reshape(x,[-1,28*28])
logits = model(x)
prob = tf.nn.softmax(logits,axis=1)
pred = tf.argmax(prob,axis=1)
pred = tf.cast(pred,dtype=tf.int32)
#比较pred,y的数值,返回bool值的张量
correct = tf.equal(pred,y)
#将bool值转换为0,1的常数
correct = tf.reduce_sum(tf.cast(correct,dtype=tf.int32))
#统计在一个样本中,正确预测的个数
correct_num += int(correct)
all_num += x.shape[0]
acc = correct_num/all_num
print("正确率为",acc)