TensorFlow从入门到入门

记录损失函数并运维

sess = tf.Session()
merged = tf.summary.merge_all()
# 分别记录训练集的loss和测试集的loss值,目的是为了能够对比训练集和测试集中得拟合情况
train_writer = tf.summary.FileWriter("D:/todel/data/tensorflow/train", sess.graph)
test_writer = tf.summary.FileWriter("D:/todel/data/tensorflow/test", sess.graph)

init = tf.global_variables_initializer()
sess.run(init)

for i in range(500):
    sess.run(train_step, feed_dict={xs:X_train, ys:y_train})
    if i % 50 == 0:
        # 分别用训练集和测试集数据获得损失函数值
        train_result = sess.run(merged, feed_dict={xs:X_train, ys: y_train})
        train_writer.add_summary(train_result, i)

        test_result = sess.run(merged, feed_dict={xs:X_test, ys: y_test})
        test_writer.add_summary(test_result, i)
  1. 轻便线性回归

     import tensorflow as tf
     import numpy 
    
     # 创造数据
     x_data = numpy.random.rand(100).astype(numpy.float32)
     y_data = x_data*0.1 + 0.3
    
     print(x_data,y_data)
    
     Weights = tf.Variable(tf.random_uniform([1],-1.0,1))
     biases = tf.Variable(tf.zeros([1]))
     y = Weights*x_data+biases
    
     loss = tf.reduce_mean(tf.square(y-y_data))
    
     optimizer = tf.train.GradientDescentOptimizer(0.5)
     train = optimizer.minimize(loss)
     init = tf.global_variables_initializer()
    
     sess = tf.Session()
     sess.run(init)
     for step in range(201):
         sess.run(train)
         if step%20 == 0:
             print(step,sess.run(Weights),sess.run(biases))
    
  2. 矩阵相乘,和Session()的三种采纳格局

     import tensorflow as tf
    
     #创建两个矩阵
     matrix1 = tf.constant([[3,3]])
     matrix2 = tf.constant([[2],[2]])
     product = tf.matmul(matrix1,matrix2)
     #到此都是在准备计算关系,并没有实际计算
    
     #启动session并计算的第一种形式
     sess = tf.Session()
     result = sess.run(product)
     print(result)
     sess.close()
    
     #启动session并计算的第二种方法
     with tf.Session() as sess:
         result = sess.run(product)
         print(result)
    
  3. 变量定义,常量定义,步骤定义,操作定义 ,Session
    本身对意况保存的表征

     # Tensorflow中必须定义变量,添加到构建的流图中  基本语法  state = tensorflow.Variable()  
    
     import tensorflow as tf
     #定义变量
     state = tf.Variable(0, name='counter')
     #定义常量
     one = tf.constant(1)
     #定义步骤
     new_value = tf.add(state,one)
     #定义赋值操作
     update = tf.assign(state, new_value)
    
     #定义变量以后初始化变量就是必须的
     init = tf.global_variables_initializer()
    
     #启动Session
     with tf.Session() as sess:
         sess.run(init)
         for _ in range(3):
             sess.run(update)
             print(sess.run(state))
    
  4. placeholder

     #placeholder 有时候会出现一些量我们不想在,定义流图阶段就把这些量写成常量,而是想计算的时候再输入。此时就要用到placeholder,定义流图的时候占位
     import tensorflow as tf
     #在 Tensorflow 中需要定义 placeholder 的 type ,一般为 float32 形式
     input1 = tf.placeholder(tf.float32)
     input2 = tf.placeholder(tf.float32)
    
     # mul = multiply 是将input1和input2 做乘法运算,并输出为 output 
     ouput = tf.multiply(input1, input2)
     with tf.Session() as sess:
         print(sess.run(ouput, feed_dict={input1: [7.], input2: [2.]}))
    
  5. 勉力函数 (Activation
    Function),人工智能领域为了适应复杂多变的切实可行世界非常找到的一对形状奇特的函数。特点或然须要:1.必得是非线性函数,因为要适应非线性难题。2.须假设可微分的,backpropagation基值误差反向传递要运用到可微分本性。

  6. 增多层函数

     # 神经网络层的构建
     import tensorflow as tf
    
     #定义添加层的操作,新版的TensorFlow库中自带层不用手动怼
     def add_layer(inputs, in_size, out_size, activation_function = None):
         Weights = tf.Variable(tf.random_normal([in_size, out_size]))
         biases = tf.Variable(tf.zeros(1,out_size))+0.1
         Wx_plus_b = tf.matmul(inputs, Weights)+biases
         if activation_function is None:
             outputs = Wx_plus_b
         else:
             outputs = activation_function(Wx_plus_b)
         return outputs
    
  7. 多少可视化

     #结果可视化, 数据转换成图像
     # 1. matplotlib 
     import tensorflow as tf
     import numpy as np
     import matplotlib.pyplot as plt
    
     x_data = np.linspace(-1,1,300, dtype=np.float32)[:,np.newaxis]
     noise = np.random.normal(0,0.05,x_data.shape).astype(np.float32)
     y_data = np.square(x_data) - 0.5 +noise
    
     plt.figure(1, figsize=(8, 6))
    
     plt.subplot(111)
     plt.plot(x_data, y_data, c='red', label='relu')
     plt.ylim((-1, 5))
     plt.legend(loc='best')
    
     plt.show()
    

    动画片进程

      # 神经网络建造,训练过程
     import tensorflow as tf
     import numpy as np
     import matplotlib.pyplot as plt
    
     def add_layer(inputs, in_size, out_size, activation_function=None):
         Weights = tf.Variable(tf.random_normal([in_size, out_size]))
         biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
         Wx_plus_b = tf.matmul(inputs, Weights) + biases
         if activation_function is None:
             outputs = Wx_plus_b
         else:
             outputs = activation_function(Wx_plus_b)
         return outputs
    
     x_data = np.linspace(-1,1,300, dtype=np.float32)[:,np.newaxis]
     noise = np.random.normal(0,0.05,x_data.shape).astype(np.float32)
     y_data = np.square(x_data) - 0.5 +noise
    
     xs = tf.placeholder(tf.float32,[None,1])
     ys = tf.placeholder(tf.float32,[None,1])
    
     #开始搭建神经网络
     #1个输入,10个输出 激励函数为tf.nn.relu
     l1 = add_layer(xs,1,10,activation_function=tf.nn.relu)
     #输出层定义
     prediction = add_layer(l1,10,1,activation_function=None)
     #误差计算 二者差的平方求和再取平均
     loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1]))
     #学习效率参数 学习效率 0-1
     train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
    
     #初始化变量
     init = tf.global_variables_initializer()
    
     #准备显示数据
     fig = plt.figure()
     ax = fig.add_subplot(1,1,1)
     ax.scatter(x_data, y_data)
     plt.ion()
     plt.show()
    
     #启动Session 开始训练
     with tf.Session() as sess:
         sess.run(init)
         for i in range(1000):
             sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
             #每过50步输出状态
             if i%50 == 0 :
                 # to visualize the result and improvement
                 try:
                     ax.lines.remove(lines[0])
                 except Exception:
                     pass
                 prediction_value = sess.run(prediction, feed_dict={xs: x_data})
                 # plot the prediction
                 lines = ax.plot(x_data, prediction_value, 'r-', lw=5)
                 plt.pause(0.1)
    
  8. 加快神经互联网陶冶 富含以下两种格局:

    1. Stochastic Gradient Descent (SGD)
    2. Momentum
    3. AdaGrad
    4. RMSProp
    5. Adam
  9. 计算可视化(tensorboard)

     # TensorFlow 中自带一个流图可视化工具tensorboard 可以用图的方式显示定义的流图
     # 神经网络建造,训练过程
     import tensorflow as tf
     import numpy as np
     import matplotlib.pyplot as plt
    
     def add_layer(inputs, in_size, out_size, activation_function=None):
         #都放到命名空间内
         with tf.name_scope('layer'):
             with tf.name_scope('weights'):
                 Weights = tf.Variable(
                 tf.random_normal([in_size, out_size]), 
                 name='W')
             with tf.name_scope('biases'):
                 biases = tf.Variable(
                 tf.zeros([1, out_size]) + 0.1, 
                 name='b')
             with tf.name_scope('Wx_plus_b'):
                 Wx_plus_b = tf.add(
                 tf.matmul(inputs, Weights), 
                 biases)
             if activation_function is None:
                 outputs = Wx_plus_b
             else:
                 outputs = activation_function(Wx_plus_b, )
             return outputs
    
     x_data = np.linspace(-1,1,300, dtype=np.float32)[:,np.newaxis]
     noise = np.random.normal(0,0.05,x_data.shape).astype(np.float32)
     y_data = np.square(x_data) - 0.5 +noise
    
     #图结构分层 把两个placeholder放在一个方框中
     with tf.name_scope('inputs'):
         #站位名称给定 以前没有name参数
         xs= tf.placeholder(tf.float32, [None, 1],name='x_in') 
         ys= tf.placeholder(tf.float32, [None, 1],name='y_in')
    
     #开始搭建神经网络
     #1个输入,10个输出 激励函数为tf.nn.relu
     l1 = add_layer(xs,1,10,activation_function=tf.nn.relu)
     #输出层定义
     prediction = add_layer(l1,10,1,activation_function=None)
    
     with tf.name_scope('loss'):
         #误差计算 二者差的平方求和再取平均
         loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1]))
    
     with tf.name_scope('train'):
         #学习效率参数 学习效率 0-1
         train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
    
     #初始化变量
     init = tf.global_variables_initializer()
    
     #准备显示数据
     fig = plt.figure()
     ax = fig.add_subplot(1,1,1)
     ax.scatter(x_data, y_data)
     plt.ion()
     plt.show()
    
     #启动Session 开始训练
     with tf.Session() as sess:
         sess.run(init)
         #手动建立logs文件夹,运行后没有错误 再执行tensorboard --logdir logs
         writer = tf.summary.FileWriter("logs/", sess.graph)
         for i in range(1000):
             sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
             #每过50步输出状态
             if i%50 == 0 :
                 # to visualize the result and improvement
                 try:
                     ax.lines.remove(lines[0])
                 except Exception:
                     pass
                 prediction_value = sess.run(prediction, feed_dict={xs: x_data})
                 # plot the prediction
                 lines = ax.plot(x_data, prediction_value, 'r-', lw=5)
                 plt.pause(0.1)
    
  10. 磨炼可视化,在测算结构中著录变量tf.summary.histogram(layer_name+'/weights',Weights)笔录标量tf.summary.scalar('loss', loss)。Seesion起头化未来merged = tf.summary.merge_all()约等于起初化,通过rs = sess.run(merged,feed_dict={xs:x_data,ys:y_data}),writer.add_summary(rs, i)举行步进记录

    # TensorFlow 中自带一个流图可视化工具tensorboard 可以用图的方式显示定义的流图
    # 神经网络建造,训练过程
    import tensorflow as tf
    import numpy as np
    import matplotlib.pyplot as plt
    
    def add_layer(inputs, in_size, out_size,layer_n, activation_function=None):
        #都放到命名空间内
        layer_name = 'layer%s'%layer_n
        with tf.name_scope('layer'):
            with tf.name_scope('weights'):
                Weights = tf.Variable(
                tf.random_normal([in_size, out_size]), 
                name='W')
            with tf.name_scope('biases'):
                biases = tf.Variable(
                tf.zeros([1, out_size]) + 0.1, 
                name='b')
            with tf.name_scope('Wx_plus_b'):
                Wx_plus_b = tf.add(
                tf.matmul(inputs, Weights), 
                biases)
            if activation_function is None:
                outputs = Wx_plus_b
            else:
                outputs = activation_function(Wx_plus_b, )
            #添加分析数据
            tf.summary.histogram(layer_name+'/weights',Weights)
            tf.summary.histogram(layer_name+'/biase',biases)
            tf.summary.histogram(layer_name+'/outputs',outputs)
            return outputs
    
    x_data = np.linspace(-1,1,300, dtype=np.float32)[:,np.newaxis]
    noise = np.random.normal(0,0.05,x_data.shape).astype(np.float32)
    y_data = np.square(x_data) - 0.5 +noise
    
    #图结构分层 把两个placeholder放在一个方框中
    with tf.name_scope('inputs'):
        #站位名称给定 以前没有name参数
        xs= tf.placeholder(tf.float32, [None, 1],name='x_in') 
        ys= tf.placeholder(tf.float32, [None, 1],name='y_in')
    
    #开始搭建神经网络
    #1个输入,10个输出 激励函数为tf.nn.relu
    l1 = add_layer(xs,1,10,1,activation_function=tf.nn.relu)
    #输出层定义
    prediction = add_layer(l1,10,1,2,activation_function=None)
    
    with tf.name_scope('loss'):
        #误差计算 二者差的平方求和再取平均
        loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1]))
        #添加分析数据
        tf.summary.scalar('loss', loss)
    
    with tf.name_scope('train'):
        #学习效率参数 学习效率 0-1
        train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
    
    #初始化变量
    init = tf.global_variables_initializer()
    
    #准备显示数据
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    ax.scatter(x_data, y_data)
    plt.ion()
    plt.show()
    
    #启动Session 开始训练
    with tf.Session() as sess:
        sess.run(init)
        #数据分析初始化
        merged = tf.summary.merge_all()
        #手动建立logs文件夹,运行后没有错误 再执行tensorboard --logdir logs
        writer = tf.summary.FileWriter("logs/", sess.graph)
        for i in range(1000):
            sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
            #每过50步输出状态
            if i%50 == 0 :
                #图标统计
                rs = sess.run(merged,feed_dict={xs:x_data,ys:y_data})
                writer.add_summary(rs, i)
                # to visualize the result and improvement
                try:
                    ax.lines.remove(lines[0])
                except Exception:
                    pass
                prediction_value = sess.run(prediction, feed_dict={xs: x_data})
                # plot the prediction
                lines = ax.plot(x_data, prediction_value, 'r-', lw=5)
                plt.pause(0.1)
    
  11. 分类器:利用MNIST数据完结测量试验分类器,程序中至关心尊崇要新的就学点有1.
    MNIST数据运用。2.优化指标函数中的交叉熵函数 3.
    演练方法选取梯度下跌法

    import tensorflow as tf
    
    def add_layer(inputs, in_size, out_size,layer_n, activation_function=None):
        #都放到命名空间内
        layer_name = 'layer%s'%layer_n
        with tf.name_scope('layer'):
            with tf.name_scope('weights'):
                Weights = tf.Variable(
                tf.random_normal([in_size, out_size]), 
                name='W')
            with tf.name_scope('biases'):
                biases = tf.Variable(
                tf.zeros([1, out_size]) + 0.1, 
                name='b')
            with tf.name_scope('Wx_plus_b'):
                Wx_plus_b = tf.add(
                tf.matmul(inputs, Weights), 
                biases)
            if activation_function is None:
                outputs = Wx_plus_b
            else:
                outputs = activation_function(Wx_plus_b, )
            #添加分析数据
            tf.summary.histogram(layer_name+'/weights',Weights)
            tf.summary.histogram(layer_name+'/biase',biases)
            tf.summary.histogram(layer_name+'/outputs',outputs)
            return outputs
    
    def compute_accuracy(v_xs, v_ys):
        global prediction
        y_pre = sess.run(prediction, feed_dict={xs: 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={xs: v_xs, ys: v_ys})
        return result
    
    from tensorflow.examples.tutorials.mnist import input_data
    mnist = input_data.read_data_sets('MNIST_data',one_hot = True)
    
    xs = tf.placeholder(tf.float32,[None,784])
    ys = tf.placeholder(tf.float32,[None,10])
    
    prediction = add_layer(xs,784,10,1,activation_function=tf.nn.softmax)
    
    #loss函数(即最优化目标函数)选用交叉熵函数。交叉熵用来衡量预测值和真实值的相似程度,如果完全相同,它们的交叉熵等于零
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),reduction_indices=[1]))
    #train方法(最优化算法)采用梯度下降法。
    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
    
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for i in range(1000):
            batch_xs,batch_ys = mnist.train.next_batch(100)
            sess.run(train_step,feed_dict={xs:batch_xs,ys:batch_ys})
            if i%50 == 0:
                print(compute_accuracy(mnist.test.images, mnist.test.labels))
    
  12. 过分拟合(Overfitting),过度学习。在拍卖具体题材的时候,数据来自是不可控的。总会出现对机械学习神经互联网不利的数量,来源首要能够分成,度量测量误差,文化背景,外部烦恼。总括起来就是机械学习的神经互联网自设计之初就管理不了的数额。对于由于过于拟合大家找到了优化神经互联网的思路。

    1. 充实数据量,机器学习的结果来自数据的合计。数据量大了,有一个半个的不行数据也就不算什么了,或是出现对峙的数来抵消(小可能率数据)。未有增进神经网络的质量。
    2. 正规化。
      1.修改标称误差总括函数,使得神经互连网得到不一样档案的次序的报告。原始的 cost
      绝对误差是如此总计, cost = 预测值-真实值的平方。假设 W 变得太大,
      大家就让 cost 也跟着变大, 产生一种惩罚机制. 所以大家把 W
      本人思索进来. 这里 abs 是纯属值. 这一种样式的 正规化, 叫做 l1
      正规化. L2 专门的学问和 l1 类似, 只是纯属值换到了平方. 其余的l3, l4
      也都以换来了立方和4次方等等. 格局类似.
      用那些办法,我们就能够确认保障让学出来的线条不会超负荷扭曲.(援引https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/5-02-A-overfitting/)

      1. dropout。一种极度用在神经网络的正规的方法。Dropout
        的做法是从根本上让神经互联网没机缘过度信赖.消息存在网络中并非最首要节点。
  13. overfitting和dropout 效果相比较,dropout
    对于不另行的数目很实用不过假如数额有限,过度磨炼的情事下效果会反弹。

    import tensorflow as tf
    import numpy as np
    import matplotlib.pyplot as plt
    
    tf.set_random_seed(1)
    np.random.seed(1)
    
    # Hyper parameters
    N_SAMPLES = 20
    N_HIDDEN = 300
    LR = 0.01
    
    # training data
    x = np.linspace(-1, 1, N_SAMPLES)[:, np.newaxis]
    y = x + 0.3*np.random.randn(N_SAMPLES)[:, np.newaxis]
    
    # test data
    test_x = x.copy()
    test_y = test_x + 0.3*np.random.randn(N_SAMPLES)[:, np.newaxis]
    
    # show data
    plt.scatter(x, y, c='magenta', s=50, alpha=0.5, label='train')
    plt.scatter(test_x, test_y, c='cyan', s=50, alpha=0.5, label='test')
    plt.legend(loc='upper left')
    plt.ylim((-2.5, 2.5))
    plt.show()
    
    # tf placeholders
    tf_x = tf.placeholder(tf.float32, [None, 1])
    tf_y = tf.placeholder(tf.float32, [None, 1])
    tf_is_training = tf.placeholder(tf.bool, None)  # to control dropout when training and testing
    
    # overfitting net
    o1 = tf.layers.dense(tf_x, N_HIDDEN, tf.nn.relu)
    o2 = tf.layers.dense(o1, N_HIDDEN, tf.nn.relu)
    o_out = tf.layers.dense(o2, 1)
    o_loss = tf.losses.mean_squared_error(tf_y, o_out)
    o_train = tf.train.AdamOptimizer(LR).minimize(o_loss)
    
    # dropout net
    d1 = tf.layers.dense(tf_x, N_HIDDEN, tf.nn.relu)
    d1 = tf.layers.dropout(d1, rate=0.5, training=tf_is_training)   # drop out 50% of inputs
    d2 = tf.layers.dense(d1, N_HIDDEN, tf.nn.relu)
    d2 = tf.layers.dropout(d2, rate=0.5, training=tf_is_training)   # drop out 50% of inputs
    d_out = tf.layers.dense(d2, 1)
    d_loss = tf.losses.mean_squared_error(tf_y, d_out)
    d_train = tf.train.AdamOptimizer(LR).minimize(d_loss)
    
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    
    plt.ion()   # something about plotting
    
    for t in range(5000):
        sess.run([o_train, d_train], {tf_x: x, tf_y: y, tf_is_training: True})  # train, set is_training=True
    
        if t % 50 == 0:
            # plotting
            plt.cla()
            o_loss_, d_loss_, o_out_, d_out_ = sess.run(
                [o_loss, d_loss, o_out, d_out], {tf_x: test_x, tf_y: test_y, tf_is_training: False} # test, set is_training=False
            )
            plt.scatter(x, y, c='magenta', s=50, alpha=0.3, label='train'); 
            plt.scatter(test_x, test_y, c='cyan', s=50, alpha=0.3, label='test')
            plt.plot(test_x, o_out_, 'r-', lw=3, label='overfitting'); 
            plt.plot(test_x, d_out_, 'b--', lw=3, label='dropout(50%)')
            plt.text(0, -1.2, 'overfitting loss=%.4f' % o_loss_, fontdict={'size': 20, 'color':  'red'}); 
            plt.text(0, -1.5, 'dropout loss=%.4f' % d_loss_, fontdict={'size': 20, 'color': 'blue'})
            plt.legend(loc='upper left'); 
            plt.ylim((-2.5, 2.5)); 
            plt.pause(0.1)
    
    plt.ioff()
    plt.show()
    
  14. 卷积神经互联网,非常消耗总计能源,pc机器已经展现慢了。参谋:https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/5-03-A-CNN/

    import tensorflow as tf
    from tensorflow.examples.tutorials.mnist import input_data
    import numpy as np
    import matplotlib.pyplot as plt
    
    tf.set_random_seed(1)
    np.random.seed(1)
    
    BATCH_SIZE = 50
    LR = 0.001              # learning rate
    
    mnist = input_data.read_data_sets('./mnist', one_hot=True)  # they has been normalized to range (0,1)
    test_x = mnist.test.images[:2000]
    test_y = mnist.test.labels[:2000]
    
    # plot one example
    print(mnist.train.images.shape)     # (55000, 28 * 28)
    print(mnist.train.labels.shape)   # (55000, 10)
    plt.imshow(mnist.train.images[0].reshape((28, 28)), cmap='gray')
    plt.title('%i' % np.argmax(mnist.train.labels[0])); plt.show()
    
    tf_x = tf.placeholder(tf.float32, [None, 28*28]) / 255.
    image = tf.reshape(tf_x, [-1, 28, 28, 1])              # (batch, height, width, channel)
    tf_y = tf.placeholder(tf.int32, [None, 10])            # input y
    
    # CNN
    conv1 = tf.layers.conv2d(   # shape (28, 28, 1)
        inputs=image,
        filters=16,
        kernel_size=5,
        strides=1,
        padding='same',
        activation=tf.nn.relu
    )           # -> (28, 28, 16)
    pool1 = tf.layers.max_pooling2d(
        conv1,
        pool_size=2,
        strides=2,
    )           # -> (14, 14, 16)
    conv2 = tf.layers.conv2d(pool1, 32, 5, 1, 'same', activation=tf.nn.relu)    # -> (14, 14, 32)
    pool2 = tf.layers.max_pooling2d(conv2, 2, 2)    # -> (7, 7, 32)
    flat = tf.reshape(pool2, [-1, 7*7*32])          # -> (7*7*32, )
    output = tf.layers.dense(flat, 10)              # output layer
    
    loss = tf.losses.softmax_cross_entropy(onehot_labels=tf_y, logits=output)           # compute cost
    train_op = tf.train.AdamOptimizer(LR).minimize(loss)
    
    accuracy = tf.metrics.accuracy(          # return (acc, update_op), and create 2 local variables
        labels=tf.argmax(tf_y, axis=1), predictions=tf.argmax(output, axis=1),)[1]
    
    sess = tf.Session()
    init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) # the local var is for accuracy_op
    sess.run(init_op)     # initialize var in graph
    
    # following function (plot_with_labels) is for visualization, can be ignored if not interested
    from matplotlib import cm
    try: from sklearn.manifold import TSNE; HAS_SK = True
    except: HAS_SK = False; print('nPlease install sklearn for layer visualizationn')
    def plot_with_labels(lowDWeights, labels):
        plt.cla(); X, Y = lowDWeights[:, 0], lowDWeights[:, 1]
        for x, y, s in zip(X, Y, labels):
            c = cm.rainbow(int(255 * s / 9)); plt.text(x, y, s, backgroundcolor=c, fontsize=9)
        plt.xlim(X.min(), X.max()); plt.ylim(Y.min(), Y.max()); plt.title('Visualize last layer'); plt.show(); plt.pause(0.01)
    
    plt.ion()
    for step in range(600):
        b_x, b_y = mnist.train.next_batch(BATCH_SIZE)
        _, loss_ = sess.run([train_op, loss], {tf_x: b_x, tf_y: b_y})
        if step % 50 == 0:
            accuracy_, flat_representation = sess.run([accuracy, flat], {tf_x: test_x, tf_y: test_y})
            print('Step:', step, '| train loss: %.4f' % loss_, '| test accuracy: %.2f' % accuracy_)
    
            if HAS_SK:
                # Visualization of trained flatten layer (T-SNE)
                tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000); plot_only = 500
                low_dim_embs = tsne.fit_transform(flat_representation[:plot_only, :])
                labels = np.argmax(test_y, axis=1)[:plot_only]; plot_with_labels(low_dim_embs, labels)
    plt.ioff()
    
    # print 10 predictions from test data
    test_output = sess.run(output, {tf_x: test_x[:10]})
    pred_y = np.argmax(test_output, 1)
    print(pred_y, 'prediction number')
    print(np.argmax(test_y[:10], 1), 'real number')
    
  15. 神经网络的保留或提取。

    1. 保存,本质是session的保存。

      import tensorflow as tf
      import numpy as np

      ## Save to file
      # remember to define the same dtype and shape when restore
      W = tf.Variable([[1,2,3],[3,4,5]], dtype=tf.float32, name=’weights’)
      b = tf.Variable([[1,2,3]], dtype=tf.float32, name=’biases’)

      # 替换到上边包车型地铁写法:
      init = tf.global_variables_initializer()

      saver = tf.train.Saver()

      with tf.Session() as sess:

       sess.run(init)
       save_path = saver.save(sess, "my_net/save_net.ckpt")
       print("Save to path: ", save_path)
      
    2. 提取,session的恢复

    import tensorflow as tf
    import numpy as np
    
    # 先建立 W, b 的容器
    W = tf.Variable(np.arange(6).reshape((2, 3)), dtype=tf.float32, name="weights")
    b = tf.Variable(np.arange(3).reshape((1, 3)), dtype=tf.float32, name="biases")
    
    # 这里不需要初始化步骤 init= tf.initialize_all_variables()
    
    saver = tf.train.Saver()
    with tf.Session() as sess:
        # 提取变量
        saver.restore(sess, "my_net/save_net.ckpt")
        print("weights:", sess.run(W))
        print("biases:", sess.run(b))
    
  16. 循环神经互联网。参考:https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/5-07-A-RNN/

图中深灰蓝曲线是正规模型,深紫灰曲线便是overfitting模型。就算鲜绿曲线很规范的分化了拥有的教练多少,不过并不曾描述数据的一体化特点,对新测验数据的适应性相当差。

在机械学习中只怕会存在过拟合的题目,表现为在教练集上显现很好,但在测量检验集中展现不及训练集中的那么好。

图片 1

打消过拟合

为了祛除过拟合,大家选拔dropout格局来打开。
第一设置多个保留概率的placeholder,那样在运维时能够因而参数来张开安装

# 设置保留概率,即我们要保留的结果所占比例,它作为一个placeholder,在run时传入, 当keep_prob=1的时候,相当于100%保留,也就是dropout没有起作用。
keep_prob = tf.placeholder(tf.float32)

然后在add_layer函数中调用dropout作用:

    # 调用dropout功能
    Wx_plus_b = tf.nn.dropout(Wx_plus_b, keep_prob)

最终在教练时设置保留的概率,但在获取损失值时用任何的数码来进行获取:

for i in range(500):
    sess.run(train_step, feed_dict={xs:X_train, ys:y_train, keep_prob: 0.7})
    if i % 50 == 0:
        # 分别用训练集和测试集数据获得损失函数值
        train_result = sess.run(merged, feed_dict={xs:X_train, ys: y_train, keep_prob:1})
        train_writer.add_summary(train_result, i)

        test_result = sess.run(merged, feed_dict={xs:X_test, ys: y_test, keep_prob:1})
        test_writer.add_summary(test_result, i)

这么全体代码为:

from sklearn.datasets import load_digits
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split

digits = load_digits()
X = digits.data
y = digits.target

# 把数值转换成one hot格式,例如:数字4就会被转换成:[0 0 0 0 1 0 0 0 0 0]
y = LabelBinarizer().fit_transform(y)
# 拆分数据集,以总量的30%作为测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

import tensorflow as tf
def add_layer(inputs, in_size, out_size, activation_function=None):
    """
    添加层
    :param inputs: 输入数据
    :param in_size: 输入数据的列数
    :param out_size: 输出数据的列数
    :param activation_function: 激励函数
    :return:
    """

    # 定义权重,初始时使用随机变量,可以简单理解为在进行梯度下降时的随机初始点,这个随机初始点要比0值好,因为如果是0值的话,反复计算就一直是固定在0中,导致可能下降不到其它位置去。
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    # 偏置shape为1行out_size列
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
    # 建立神经网络线性公式:inputs * Weights + biases,我们大脑中的神经元的传递基本上也是类似这样的线性公式,这里的权重就是每个神经元传递某信号的强弱系数,偏置值是指这个神经元的原先所拥有的电位高低值
    Wx_plus_b = tf.matmul(inputs, Weights) + biases
    # 调用dropout功能
    Wx_plus_b = tf.nn.dropout(Wx_plus_b, keep_prob)

    if activation_function is None:
        # 如果没有设置激活函数,则直接就把当前信号原封不动地传递出去
        outputs = Wx_plus_b
    else:
        # 如果设置了激活函数,则会由此激活函数来对信号进行传递或抑制
        outputs = activation_function(Wx_plus_b)
    return outputs

# 定义placeholder
# 输入的手写数字大小为8*8单位的数据
xs = tf.placeholder(tf.float32, [None, 8*8])
# 输出值为one hot结构的数据
ys = tf.placeholder(tf.float32, [None, 10])
# 设置保留概率,即我们要保留的结果所占比例,它作为一个placeholder,在run时传入, 当keep_prob=1的时候,相当于100%保留,也就是dropout没有起作用。
keep_prob = tf.placeholder(tf.float32)

# 添加层
# 第一层输入为8*8单位的手写输入数字图像,输出设定为100个神经元的层(为了能够看出是overfitting的问题),激活函数一般用tanh比较好
l1 = add_layer(xs, 8*8, 100, activation_function=tf.nn.tanh)
# 输出层因为最终是一个one hot的结构,因此输出的大小为10,激活函数用softmax
prediction = add_layer(l1, 100, 10, activation_function=tf.nn.softmax)

# 定义损失函数
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction), axis=1))
# 在tensorboard中记录损失函数值
tf.summary.scalar('loss', cross_entropy)
# 用梯度下降优化器进行训练
train_step = tf.train.GradientDescentOptimizer(0.6).minimize(cross_entropy)

sess = tf.Session()
merged = tf.summary.merge_all()
# 分别记录训练集的loss和测试集的loss值,目的是为了能够对比训练集和测试集中得拟合情况
train_writer = tf.summary.FileWriter("D:/todel/data/tensorflow/train", sess.graph)
test_writer = tf.summary.FileWriter("D:/todel/data/tensorflow/test", sess.graph)

init = tf.global_variables_initializer()
sess.run(init)

for i in range(500):
    sess.run(train_step, feed_dict={xs:X_train, ys:y_train, keep_prob: 0.7})
    if i % 50 == 0:
        # 分别用训练集和测试集数据获得损失函数值
        train_result = sess.run(merged, feed_dict={xs:X_train, ys: y_train, keep_prob:1})
        train_writer.add_summary(train_result, i)

        test_result = sess.run(merged, feed_dict={xs:X_test, ys: y_test, keep_prob:1})
        test_writer.add_summary(test_result, i)

运维后输出tensorboard图形为(记得把在此之前的文件或目录进行删减并运行tensorboard实行展现图形):
图片 2

如此磨炼集和测量试验集的损失值就比较周边了。

常常用于化解过拟合的方法有扩展权重的惩罚机制,比方L2正规化,但在本处大家运用tensorflow提供的dropout方法,在教练的时候,
大家随意忽略掉一部分神经元和神经联结 , 使那个神经网络变得”不完全”.
用多少个不完全的神经互连网操练一回.
到首次再轻便忽略另一部分, 产生另三个不完整的神经网络. 有了这么些随机 drop
掉的条条框框, 大家得以想象其实每一次磨炼的时候,
大家都让每三次预测结果都不会凭仗于在那之中某部分特定的神经元. 像l1,
l2正式一样, 过度看重的 W , 也正是陶冶参数的数值会极大, l1,
l2会处以那么些大的 参数. Dropout 的做法是从根本上让神经互联网没机遇过度正视.

发表评论

电子邮件地址不会被公开。 必填项已用*标注