minist_tensorflow实现

 识别手写集,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)

MNIST是一个手写数字图像数据集,其中包含60,000个训练数据和10,000个测试数据。BP神经网络可以用来对MNIST数据集中的手写数字进行分类。 下面是BP神经网络实现MNIST分类的一般步骤: 1. 数据预处理:将MNIST数据集中的手写数字图像转换为数字矩阵,并进行归一化处理。 2. 数据分割:将数据集分为训练集和测试集,一般比例为7:3。 3. 网络构建:构建BP神经网络,设置输入层、隐藏层和输出层的节点数和激活函数。 4. 神经网络训练:使用训练集对神经网络进行训练,通过反向传播算法对网络权重进行更新,直到达到预设的训练次数或误差精度。 5. 神经网络测试:用测试集对训练好的神经网络进行测试,计算分类准确率。 6. 模型优化:根据测试结果对神经网络进行优化,调整网络结构、调整超参数等。 下面是一个Python实现的示例代码: ``` import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # 加载MNIST数据集 mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # 设置神经网络模型参数 learning_rate = 0.01 training_epochs = 100 batch_size = 100 display_step = 1 # 定义输入和输出的占位符 x = tf.placeholder(tf.float32, [None, 784]) y = tf.placeholder(tf.float32, [None, 10]) # 定义神经网络模型 W1 = tf.Variable(tf.random_normal([784, 256])) b1 = tf.Variable(tf.random_normal([256])) layer1 = tf.nn.sigmoid(tf.matmul(x, W1) + b1) W2 = tf.Variable(tf.random_normal([256, 10])) b2 = tf.Variable(tf.random_normal([10])) pred = tf.nn.softmax(tf.matmul(layer1, W2) + b2) # 定义损失函数和优化器 cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred), reduction_indices=1)) optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) # 初始化所有变量 init = tf.global_variables_initializer() # 启动TensorFlow会话 with tf.Session() as sess: sess.run(init) # 训练神经网络 for epoch in range(training_epochs): avg_cost = 0. total_batch = int(mnist.train.num_examples/batch_size) for i in range(total_batch): batch_xs, batch_ys = mnist.train.next_batch(batch_size) _, c = sess.run([optimizer, cost], feed_dict={x: batch_xs, y: batch_ys}) avg_cost += c / total_batch if (epoch+1) % display_step == 0: print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost)) print("Optimization Finished!") # 测试神经网络 correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels})) ``` 这个示例代码定义了一个具有一个隐藏层的BP神经网络,使用sigmoid作为激活函数,采用交叉熵作为损失函数,并使用梯度下降法进行优化。最后输出了神经网络的分类准确率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值