码迷,mamicode.com
首页 > 其他好文 > 详细

使用线性回归识别手写阿拉伯数字mnist数据集

时间:2018-08-24 20:44:20      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:调用   rand   asi   abs   type   reg   closed   ESS   ges   

学习了tensorflow的线性回归。

首先是一个sklearn中makeregression数据集,对其进行线性回归训练的例子。来自腾讯云实验室

技术分享图片
import tensorflow as tf
import numpy as np
class linearRegressionModel:
    def __init__(self,x_dimen):
        self.x_dimen=x_dimen
        self._index_in_epoch=0
        self.constructModel()
        self.sess=tf.Session()
        self.sess.run(tf.global_variables_initializer())
    #权重初始化
    def weight_variable(self,shape):
        initial=tf.truncated_normal(shape,stddev=0.1)
        return tf.Variable(initial)
    #偏置项初始化
    def bais_variable(self,shape):
        initial=tf.constant(0.1,shape=shape)
        return tf.Variable(initial)
    #获取数据块,每次选100个样本,如果选完,则重新打乱
    def next_batch(self,batch_size):
        start=self._index_in_epoch
        self._index_in_epoch+=batch_size
        if self._index_in_epoch>self._num_datas:
            perm=np.arange(self._num_datas)
            np.random.shuffle(perm)
            self._datas=self._datas[perm]
            self._labels=self._labels[perm]
            start=0
            self._index_in_epoch=batch_size
            assert batch_size<=self._num_datas
        end=self._index_in_epoch
        return self._datas[start:end],self._labels[start:end]
    def constructModel(self):
        self.x=tf.placeholder(tf.float32,[None,self.x_dimen])
        self.y=tf.placeholder(tf.float32,[None,1])
        self.w=self.weight_variable([self.x_dimen,1])
        self.b=self.bais_variable([1])
        self.y_prec=tf.nn.bias_add(tf.matmul(self.x,self.w),self.b)
        mse=tf.reduce_mean(tf.squared_difference(self.y_prec,self.y))
        l2=tf.reduce_mean(tf.square(self.w))
        #self.loss=mse+0.15*l2
        self.loss=mse
        self.train_step=tf.train.AdamOptimizer(0.1).minimize(self.loss)
    def train(self,x_train,y_train,x_test,y_test):
        self._datas=x_train
        self._labels=y_train
        self._num_datas=x_train.shape[0]
        for i in range(5000):
            batch=self.next_batch(100)
            self.sess.run(self.train_step,
                          feed_dict={
                              self.x:batch[0],
                              self.y:batch[1]
                          })
            if i%10==0:
                train_loss=self.sess.run(self.loss,feed_dict={
                    self.x:batch[0],
                    self.y:batch[1]
                })
                print("setp %d,test_loss %f"%(i,train_loss))
    def predict_batch(self,arr,batchsize):
            for i in range(0,len(arr),batchsize):
                yield arr[i:i+batchsize]
    def predict(self,x_predict):
        pred_list=[]
        for x_test_batch in self.predict_batch(x_predict,100):
            pred =self.sess.run(self.y_prec,{self.x:x_test_batch})
            pred_list.append(pred)
        return np.vstack(pred_list)
View Code

仿照这个代码,联系使用线性回归的方法对mnist进行训练。开始选择学习率为0.1,结果训练失败,调节学习率为0.01.正确率在0.91左右

给出训练类:

技术分享图片
import tensorflow as tf
import numpy as np
class myLinearModel:
    def __init__(self,x_dimen):
        self.x_dimen=x_dimen
        self.epoch=0
        self._num_datas=0
        self.datas=None
        self.lables=None
        self.constructModel()
    def get_weiInit(self,shape):
        weiInit=tf.truncated_normal(shape)
        return tf.Variable(weiInit)
    def get_biasInit(self,shape):
        biasInit=tf.constant(0.1,shape=shape)
        return tf.Variable(biasInit)
    def constructModel(self):
        self.x = tf.placeholder(dtype=tf.float32,shape=[None,self.x_dimen])
        self.y=tf.placeholder(dtype=tf.float32,shape=[None,10])
        self.weight=self.get_weiInit([self.x_dimen,10])
        self.bias=self.get_biasInit([10])
        self.y_pre=tf.nn.softmax(tf.matmul(self.x,self.weight)+self.bias)
        self.correct_mat=tf.equal(tf.argmax(self.y_pre,1),tf.argmax(self.y,1))
        #self.loss=tf.reduce_mean(tf.squared_difference(self.y_pre,self.y))
        self.loss=-tf.reduce_sum(self.y*tf.log(self.y_pre))
        self.train_step = tf.train.GradientDescentOptimizer(0.01).minimize(self.loss)
        self.accuracy=tf.reduce_mean(tf.cast(self.correct_mat,"float"))
    def next_batch(self,batchsize):
        start=self.epoch
        self.epoch+=batchsize
        if self.epoch>self._num_datas:
            perm=np.arange(self._num_datas)
            np.random.shuffle(perm)
            self.datas=self.datas[perm,:]
            self.lables=self.lables[perm,:]
            start=0
            self.epoch=batchsize
        end=self.epoch
        return self.datas[start:end,:],self.lables[start:end,:]
    def train(self,x_train,y_train,x_test,y_test):
        self.datas=x_train
        self.lables=y_train
        self._num_datas=(self.lables.shape[0])
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            for i in range(5000):
                batch=self.next_batch(100)
                sess.run(self.train_step,feed_dict={
                    self.x:batch[0],
                    self.y:batch[1]
                })
                if 1:
                    train_loss = sess.run(self.loss, feed_dict={
                        self.x: batch[0],
                        self.y: batch[1]
                    })
                    print("setp %d,test_loss %f" % (i, train_loss))
                    #print("y_pre",sess.run(self.y_pre,feed_dict={                        self.x: batch[0],
                    #    self.y: batch[1]}))
                    #print("*****************weight********************",sess.run(self.weight))
            print(sess.run(self.accuracy,feed_dict={self.x:x_test,self.y:y_test}))
View Code

然后是调用方法,包括了对这个mnist数据集的下载

from myTensorflowLinearModle import myLinearModel as mlm
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

if __name__==__main__:

    x_train,x_test,y_train,y_test=mnist.train.images,mnist.test.images,mnist.train.labels,mnist.test.labels
    linear = mlm(len(x_train[1]))
    linear.train(x_train,y_train,x_test,y_test)

下载方法来自tensorflow的官方文档中文版

使用线性回归识别手写阿拉伯数字mnist数据集

标签:调用   rand   asi   abs   type   reg   closed   ESS   ges   

原文地址:https://www.cnblogs.com/superxuezhazha/p/9531783.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!