• TensorFlow机器学习实战指南之第二章2


    TensorFlow实现反向传播

    本节先举个简单的回归算法的例子。这里先举一个简单的例子,从均值1,标准差为0.1的正态分布中随机抽样100个数,然后乘以变量A,损失函数L2正则函数,也就是实现函数X*A=target,X为100个随机数,target为10,那么A的最优结果也为10

    第二个例子是一个简单的二值分类算法。从两个正态分布(N(-1,1)和N(3,1))生成100个数。
    所有从正态分布N(-1,1)生成的数据标为目标类0;从正态分布N(3,1)生成的数据标为目标类1,模型
    算法通过sigmoid函数将这些生成的数据转换成目标类数据。换句话讲,模型算法是sigmoid(x+A),其中,
    A是要拟合的变量,理论上A=-1。假设,两个正态分布的均值分别是m1和m2,则达到A的取值时,它们通
    过-(m1+m2)/2转换成到0等距的值。后面将会在TensorFlow中见证怎样取到相应的值。

    同时,指定一个合适的学习率对机器学习算法的收敛是有帮助的。优化器类型也需要指定,前面的两个
    例子会使用标准梯度下降法,它在TensorFlow中的实现是GradientDescentOptimizer()函数。

    这里是回归算法例子:

    1.导入Python的数值计算模块,numpy和tensorflow:

    In [15]:
    import numpy as np
    import tensorflow as tf
    
     

    2.创建计算图会话:

    In [16]:
    sess = tf.Session()
    
     

    3.生成数据,创建占位符和变量A:

    In [17]:
    x_vals = np.random.normal(1, 0.1, 100)
    x_vals
    
    Out[17]:
    array([0.85473643, 0.96126079, 0.99987964, 0.94898843, 1.04117097,
           0.97721906, 1.17807261, 0.83860367, 1.28056141, 1.02976099,
           0.90844363, 1.05311543, 1.10732355, 0.94467634, 0.97918689,
           0.94916167, 0.87431717, 1.04365034, 0.9653559 , 0.9738876 ,
           0.94834554, 1.04800372, 0.97612144, 0.97875486, 1.08076762,
           0.89620432, 0.82966182, 1.01347914, 1.00655594, 1.00972554,
           1.0956883 , 1.01281699, 0.88992947, 1.04429882, 1.01027622,
           0.91045714, 1.10571857, 1.0064056 , 1.09069858, 0.91892655,
           0.99566244, 0.96414187, 1.10456956, 1.03746805, 1.05676228,
           1.05400922, 0.91619416, 1.00368318, 1.01889345, 1.01920683,
           0.9712843 , 0.99061975, 0.98477408, 1.02996796, 0.95895593,
           0.94575059, 0.89801272, 1.06555307, 0.85761454, 1.13257007,
           1.13296022, 0.96402961, 1.10022208, 0.99971843, 0.98802702,
           0.94654868, 1.08425381, 0.84186499, 0.95389053, 1.01410783,
           0.91944571, 1.1104405 , 1.04115229, 1.02436364, 1.03605459,
           1.06967948, 1.1200382 , 1.08068316, 0.89911599, 1.0328783 ,
           1.19179204, 1.10538897, 1.07498215, 1.13399276, 1.08425489,
           1.25083017, 1.05845486, 1.07359734, 1.18477225, 0.74738841,
           1.14564339, 1.071004  , 0.80177086, 0.88481168, 1.13268239,
           1.0493438 , 1.06613515, 0.89849749, 0.99410046, 0.99045711])
    In [18]:
    y_vals = np.repeat(10., 100)
    y_vals
    
    Out[18]:
    array([10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10.,
           10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10.,
           10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10.,
           10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10.,
           10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10.,
           10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10.,
           10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10.,
           10., 10., 10., 10., 10., 10., 10., 10., 10.])
    In [19]:
    x_data = tf.placeholder(shape=[1], dtype=tf.float32)
    y_target = tf.placeholder(shape=[1], dtype=tf.float32)
    A = tf.Variable(tf.random_normal(shape=[1]))
    A
    
    Out[19]:
    <tf.Variable 'Variable_2:0' shape=(1,) dtype=float32_ref>
     

    4.增加乘法操作:

    In [20]:
    my_output = tf.multiply(x_data, A)
    
     

    5.增加L2正则损失函数:

    In [21]:
    loss = tf.square(my_output - y_target)
    
     

    6.在运行之前,需要初始化变量:

    In [22]:
    init = tf.global_variables_initializer()
    sess.run(init)
    
     

    7.现在声明变量的优化器,使用的是标准梯度下降算法,学习率为0.02

    In [23]:
    my_opt = tf.train.GradientDescentOptimizer(learning_rate=0.02)
    train_step = my_opt.minimize(loss)
    
     

    8.最后一步是训练算法。选择一个随机的x和y,传入计算图中。TensorFlow将自动地计算损失,调整A偏差来最小化损失:

    In [25]:
    for i in range(100):
        rand_index = np.random.choice(100)
        rand_x = [x_vals[rand_index]]
        rand_y = [y_vals[rand_index]]
        sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
        if (i + 1) % 25 == 0:
            print('Step #' + str(i + 1) + ' A = ' + str(sess.run(A)))
            print('Loss = ' + str(sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})))
    
     
    Step #25 A = [10.019832]
    Loss = [0.2743014]
    Step #50 A = [9.841655]
    Loss = [0.00076162]
    Step #75 A = [9.859462]
    Loss = [0.15839773]
    Step #100 A = [9.82074]
    Loss = [0.08536417]
    
     
     
     
     
  • 相关阅读:
    模板引擎
    MongoDB基础操作
    node异步编程
    关于bootstrap table 的可编辑列表的实例
    weblogic 补丁步骤
    BIZ中model.getSql源码分析
    windows切换 jdk的坑!!!
    Oracle 给予访问其他用户包的权限
    关于解决Tomcat服务器Connection reset by peer 导致的宕机
    查找多余逗号的正则表达式
  • 原文地址:https://www.cnblogs.com/xinmomoyan/p/10794793.html
Copyright © 2020-2023  润新知