IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]AI学习之路(3): 牛刀小试之线性回归

    caimouse发表于 2017-02-22 15:18:18
    love 0

    当阿法狗在围棋界展开大规模战斗之后,所有人都倒下了。可见人工智能的威力无限,正像《射雕英雄传》里的九阴真经,谁掌握了它,那么谁就是华山论剑里的第一人物了。我们也能学习人工智能吗?我们也能开发像阿法狗那样威力强劲的围棋吗?这些想法都是我们的梦想,有梦想就是好事情,马云说:万一实现了呢,王建林说:挣一个亿是个小目标,那么我们的小目标在那里呢?拿AI挣一个亿,呵呵,那是未来的小目标。现在我们的小目标是使用Tensorflow实现一个线性回归,说白了,就是给一大堆二维的数据,让TF告诉这个方程的系数。学习过方程的表示吧,它一般采用截距式:y = kx + b, 这里k是斜率,b是截距,如果我们给一大堆数据(x, y),然后确定k和b的值。

    线性回归,是利用数理统计中回归分析,来确定两种或两种以上变量间相互依赖的定量关系的一种统计分析方法,运用十分广泛。其表达形式为y = w'x+e,e为误差服从均值为0的正态分布。

    采用高数的解决方法如下:


    现在,有一个更先进的办法,让TF来给我们解决这样的问题,只要把数据给TF,立即就计算出来了,先来看下面这段代码:

    import tensorflow as tf
    import numpy as np
    
    # Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
    x_data = np.random.rand(100).astype(np.float32)
    y_data = x_data * 0.1 + 0.3
    
    # Try to find values for W and b that compute y_data = W * x_data + b
    # (We know that W should be 0.1 and b 0.3, but TensorFlow will
    # figure that out for us.)
    W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
    b = tf.Variable(tf.zeros([1]))
    y = W * x_data + b
    
    # Minimize the mean squared errors.
    loss = tf.reduce_mean(tf.square(y - y_data))
    optimizer = tf.train.GradientDescentOptimizer(0.5)
    train = optimizer.minimize(loss)
    
    # Before starting, initialize the variables.  We will 'run' this first.
    init = tf.global_variables_initializer()
    
    # Launch the graph.
    sess = tf.Session()
    sess.run(init)
    
    # Fit the line.
    for step in range(201):
        sess.run(train)
        if step % 20 == 0:
            print(step, sess.run(W), sess.run(b))
    
    # Learns best fit is W: [0.1], b: [0.3]
    
    # Close the Session when we're done.
    sess.close()
    

    先不管是否看得懂,它的输出的结果如下:

    ====================== RESTART: D:/AI/sample/TF_1.0.py ======================
    0 [-0.40531504] [ 0.8575213]
    20 [-0.06070571] [ 0.39130008]
    40 [ 0.05844307] [ 0.32360932]
    60 [ 0.08925379] [ 0.30610514]
    80 [ 0.09722114] [ 0.30157873]
    100 [ 0.09928144] [ 0.30040824]
    120 [ 0.0998142] [ 0.30010557]
    140 [ 0.09995197] [ 0.30002731]
    160 [ 0.09998759] [ 0.30000708]
    180 [ 0.0999968] [ 0.30000183]
    200 [ 0.09999917] [ 0.30000049]
    >>> 

    可以看到它计算出来的系数k = 0.09999917, b = 0.30000049.

    如果你对python代码,还是有点迷茫,还是看不懂,老司机在这里:

    跟老菜鸟学python

    http://edu.csdn.net/course/detail/2592

    如果你已经很熟悉了,可以略过这些,直接理解它。后面以这个例子为基础,一步一步地来解释它的行为和目标。



    1. C++标准模板库从入门到精通 

    http://edu.csdn.net/course/detail/3324

    2.跟老菜鸟学C++

    http://edu.csdn.net/course/detail/2901

    3. 跟老菜鸟学python

    http://edu.csdn.net/course/detail/2592

    4. 在VC2015里学会使用tinyxml库

    http://edu.csdn.net/course/detail/2590

    5. 在Windows下SVN的版本管理与实战 

     http://edu.csdn.net/course/detail/2579

    6.Visual Studio 2015开发C++程序的基本使用 

    http://edu.csdn.net/course/detail/2570

    7.在VC2015里使用protobuf协议

    http://edu.csdn.net/course/detail/2582

    8.在VC2015里学会使用MySQL数据库

    http://edu.csdn.net/course/detail/2672



沪ICP备19023445号-2号
友情链接