美文网首页
nndl:chapter4-simple neural netw

nndl:chapter4-simple neural netw

作者: jianshuqwerty | 来源:发表于2019-04-02 18:31 被阅读0次

    《神经网络与深度学习》作业github
    来复习一遍

    full connection numpy

    # -*- coding: utf-8 -*-
    
    import numpy as np
    from tensorflow.examples.tutorials.mnist import input_data
    
    class Model():
        def __init__(self):
            print("init...")
            mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
            self.n_in = 784  # 每张图片是 28 *28 像素
            self.n_out = 10  # 总共有 10个类
            self.max_epochs = 10000  # 最大训练步数 1000步
            self.Weights = np.random.rand(self.n_in,self.n_out) # initialize W 0
    
            self.biases = np.zeros(self.n_out)  # initialize bias 0
            for i in range(self.max_epochs):
                batch_xs, batch_ys = mnist.train.next_batch(100)
                batch_xs = np.array(batch_xs)
                batch_ys = np.array(batch_ys)
    
                self.train(batch_xs, batch_ys, 0.0001)
                if i % 500 == 0: 
                    accuracy_test = self.compute_accuracy(np.array(mnist.test.images[:500]), np.array(mnist.test.labels[:500]))
                    print("#"*30)
                    print("compute_accuracy:",accuracy_test)
                    print("cross_entropy:",self.cross_entropy(batch_ys,self.output(batch_xs) )) # 输出交叉熵损失函数
                    
        def train(self,batch_x,batch_y, learning_rate):# 训练数据 更新权重
            #在下面补全(注意对齐空格)
            y_pred = self.output(batch_x)
            grad = np.dot(batch_x.T,(y_pred-batch_y))
    #         print(batch_x.shape)
    #         print(batch_y.shape)
    #         print(grad.shape)
    #         print(self.Weights.shape)
            self.Weights = self.Weights - grad*learning_rate
        
        def output(self, batch_x):# 输出预测值
            # 注意防止 上溢出和下溢出
            def softmax(x):
                e_x = np.exp(x-np.max(x))
                return e_x / (e_x.sum(axis=0)) +1e-30  #
            prediction = np.add(np.dot(batch_x, self.Weights),self.biases)
            result =[]
            for i in range(len(prediction)):
                result.append(softmax(prediction[i]))
            return np.array(result)
            
        def cross_entropy(self,batch_y,prediction_y): #交叉熵函数
            cross_entropy = - np.mean(
                np.sum(batch_y * np.log(prediction_y),axis=1))
            return cross_entropy
            
        def compute_accuracy(self,xs, ys):# 计算预测精度
            pre_y = self.output(xs)
            pre_y_index = np.argmax(pre_y,axis =1)
            y_index = np.argmax(ys,axis =1)
            count_equal = np.equal(y_index,pre_y_index)
            count = np.sum([1 for e in count_equal if e ])
            sum_count =len(xs)
            return  count * 1.0 / sum_count
    
    m = Model()
    
    init...
    WARNING:tensorflow:From <ipython-input-2-1fbc6a3520ac>:4: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
    Instructions for updating:
    Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
    WARNING:tensorflow:From /Users/gaohanning/.pyenv/versions/3.6.6/envs/dl/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
    Instructions for updating:
    Please write your own downloading logic.
    WARNING:tensorflow:From /Users/gaohanning/.pyenv/versions/3.6.6/envs/dl/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
    Instructions for updating:
    Please use tf.data to implement this functionality.
    Extracting MNIST_data/train-images-idx3-ubyte.gz
    WARNING:tensorflow:From /Users/gaohanning/.pyenv/versions/3.6.6/envs/dl/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
    Instructions for updating:
    Please use tf.data to implement this functionality.
    Extracting MNIST_data/train-labels-idx1-ubyte.gz
    WARNING:tensorflow:From /Users/gaohanning/.pyenv/versions/3.6.6/envs/dl/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
    Instructions for updating:
    Please use tf.one_hot on tensors.
    Extracting MNIST_data/t10k-images-idx3-ubyte.gz
    Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
    WARNING:tensorflow:From /Users/gaohanning/.pyenv/versions/3.6.6/envs/dl/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
    Instructions for updating:
    Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
    ##############################
    compute_accuracy: 0.084
    cross_entropy: 5.217768517589507
    ##############################
    compute_accuracy: 0.496
    cross_entropy: 1.3667549956884744
    ##############################
    compute_accuracy: 0.696
    cross_entropy: 0.9100604725017135
    ##############################
    compute_accuracy: 0.756
    cross_entropy: 0.5283512200394022
    ##############################
    compute_accuracy: 0.786
    cross_entropy: 0.5764147291756632
    ##############################
    compute_accuracy: 0.804
    cross_entropy: 0.37392200139605103
    ##############################
    compute_accuracy: 0.832
    cross_entropy: 0.6398413262851023
    ##############################
    compute_accuracy: 0.834
    cross_entropy: 0.5282240686661552
    ##############################
    compute_accuracy: 0.846
    cross_entropy: 0.37446538466513324
    ##############################
    compute_accuracy: 0.848
    cross_entropy: 0.7063916594615859
    ##############################
    compute_accuracy: 0.854
    cross_entropy: 0.4532571558888631
    ##############################
    compute_accuracy: 0.866
    cross_entropy: 0.5824548984274599
    ##############################
    compute_accuracy: 0.864
    cross_entropy: 0.4608331339898249
    ##############################
    compute_accuracy: 0.866
    cross_entropy: 0.3215719108943983
    ##############################
    compute_accuracy: 0.866
    cross_entropy: 0.47378736687496287
    ##############################
    compute_accuracy: 0.87
    cross_entropy: 0.407303310977041
    ##############################
    compute_accuracy: 0.872
    cross_entropy: 0.342605986543631
    ##############################
    compute_accuracy: 0.874
    cross_entropy: 0.29502702721525553
    ##############################
    compute_accuracy: 0.874
    cross_entropy: 0.5693335527019046
    ##############################
    compute_accuracy: 0.876
    cross_entropy: 0.41044814982575467
    

    full connection tensorflow

    输入:

    import tensorflow as tf
    from tensorflow.examples.tutorials.mnist import input_data
    # number 1 to 10 data
    mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
    

    输出:

    WARNING:tensorflow:From <ipython-input-1-e544549f6194>:4: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
    Instructions for updating:
    Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
    WARNING:tensorflow:From /Users/gaohanning/.pyenv/versions/3.6.6/envs/dl/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
    Instructions for updating:
    Please write your own downloading logic.
    WARNING:tensorflow:From /Users/gaohanning/.pyenv/versions/3.6.6/envs/dl/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
    Instructions for updating:
    Please use tf.data to implement this functionality.
    Extracting MNIST_data/train-images-idx3-ubyte.gz
    WARNING:tensorflow:From /Users/gaohanning/.pyenv/versions/3.6.6/envs/dl/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
    Instructions for updating:
    Please use tf.data to implement this functionality.
    Extracting MNIST_data/train-labels-idx1-ubyte.gz
    WARNING:tensorflow:From /Users/gaohanning/.pyenv/versions/3.6.6/envs/dl/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
    Instructions for updating:
    Please use tf.one_hot on tensors.
    Extracting MNIST_data/t10k-images-idx3-ubyte.gz
    Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
    WARNING:tensorflow:From /Users/gaohanning/.pyenv/versions/3.6.6/envs/dl/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
    Instructions for updating:
    Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
    

    输入:

    def compute_accuracy(v_xs, v_ys):
        global prediction
        y_pre = sess.run(prediction, feed_dict={inputs: v_xs})
        correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        result = sess.run(accuracy, feed_dict={inputs: v_xs, outputs: v_ys})
        return result
    
    #定义相关参数
    in_size = 784        # 输入的尺寸
    out_size = 10        # 输出的尺寸
    learning_rate = 0.1  #学习率
    max_epochs = 5000    # 最大训练步数
    
    inputs = tf.placeholder(tf.float32, [None, in_size]) # 28x28
    outputs = tf.placeholder(tf.float32, [None, out_size])# 10
    
                             
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1,)
    
    prediction = tf.nn.softmax(tf.matmul(inputs, Weights) + biases)
    #################################################################
    # 请在下面补全交叉熵 和 训练两个节点。
    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=outputs,logits=prediction)
    
    train_step = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cross_entropy)
    
    #################################################################
    sess = tf.Session()
    init = tf.global_variables_initializer() # 注意tensorflow 版本在 0.12 以后,才能执行这句命令
    sess.run(init)
    for i in range(max_epochs):
        batch_xs, batch_ys = mnist.train.next_batch(100)
        sess.run(train_step, feed_dict={inputs: batch_xs, outputs: batch_ys})
        if i % 500 == 0:
            print(compute_accuracy(
                mnist.test.images, mnist.test.labels))
    

    输出:

    0.15
    0.7789
    0.8174
    0.8241
    0.8288
    0.8327
    0.8326
    0.8336
    0.8285
    0.8362
    

    相关文章

      网友评论

          本文标题:nndl:chapter4-simple neural netw

          本文链接:https://www.haomeiwen.com/subject/vccnbqtx.html