• TensorFlow-线程回归模型


    实验目的:

    方程:y = Wx + b

    通过大量的(x, y)坐标值,模型可以计算出接近W和b的值

    实验步骤:

    第一步:生成线程回归方程模型所需要的数据

    import numpy as np
    import tensorflow as tf
    import matplotlib.pyplot as plt
    
    # 随机生成1000个点,围绕在y=0.1x+0.3的直线周围
    num_points = 1000
    vectors_set = []
    for i in range(num_points):
        x1 = np.random.normal(0.0, 0.50)  # 正态分布 0.0:均值,0.50:标准差
        y1 = x1 * 0.1 + 0.3 + np.random.normal(0.0, 0.03)
        vectors_set.append([x1, y1])
        
    # 生成一些样本数据
    x_data = [v[0] for v in vectors_set]
    y_data = [v[1] for v in vectors_set]
    # print(x_data) 会有1000个数据
    plt.scatter(x_data, y_data, c
    ='r') plt.show()

    第二步:建立线性回归模型,将生成的数据(x_data,y_data)喂给模型,并产生结果。

    # 生成1维的W矩阵, 取值是[-1,1]之间的随机数
    W = tf.Variable(tf.random_uniform([1], -1.0, 1.0), name='W')
    # 生成1维的b矩阵,初始值是0
    b = tf.Variable(tf.zeros([1]), name='b')
    # 记过计算得出预估值y
    y = W * x_data + b
    
    # 以预估值y和实际值y_data之间的均方误差作为损失
    loss = tf.reduce_mean(tf.square(y - y_data, name='loss'))
    # 采用梯度下降法来优化参数
    optimizer = tf.train.GradientDescentOptimizer(0.5)
    # 训练的过程就是最小化这个误差值
    train = optimizer.minimize(loss, name='train')
    
    sess = tf.Session()
    # 初始化sess
    init = tf.global_variables_initializer()
    sess.run(init)
    
    # 初始化的W和b是多少
    print("W = ", sess.run(W), "b = ", sess.run(b), "loss = ", sess.run(loss))
    # 执行20次训练
    for step in range(20):
        sess.run(train)
        # 输出寻来你好的W和b
        print("W = ", sess.run(W), "b = ", sess.run(b), "loss = ", sess.run(loss))

    结果:

    W =  [0.6756458] b =  [0.] loss =  0.16456214
    W =  [0.53388023] b =  [0.2748111] loss =  0.050819542
    W =  [0.4182969] b =  [0.28113642] loss =  0.027618099
    W =  [0.33377516] b =  [0.28629357] loss =  0.015202045
    W =  [0.2719439] b =  [0.2900648] loss =  0.0085575525
    W =  [0.22671175] b =  [0.29282364] loss =  0.00500173
    W =  [0.19362253] b =  [0.29484183] loss =  0.0030988192
    W =  [0.16941638] b =  [0.2963182] loss =  0.0020804694
    W =  [0.15170857] b =  [0.29739827] loss =  0.0015354961
    W =  [0.13875458] b =  [0.29818836] loss =  0.0012438523
    W =  [0.12927818] b =  [0.29876634] loss =  0.0010877779
    W =  [0.12234581] b =  [0.29918915] loss =  0.0010042539
    W =  [0.11727448] b =  [0.29949847] loss =  0.0009595558
    W =  [0.11356459] b =  [0.29972476] loss =  0.0009356354
    W =  [0.11085065] b =  [0.29989028] loss =  0.00092283427
    W =  [0.10886529] b =  [0.30001137] loss =  0.0009159839
    W =  [0.10741292] b =  [0.30009997] loss =  0.0009123176
    W =  [0.10635044] b =  [0.30016476] loss =  0.00091035594
    W =  [0.1055732] b =  [0.30021217] loss =  0.0009093059
    W =  [0.10500462] b =  [0.30024683] loss =  0.00090874406
    W =  [0.10458867] b =  [0.30027223] loss =  0.00090844336

    我们可以看到W不断趋近于0.1,b不断趋近于0.3,loss不断变小。

    说明模型是可用的。

  • 相关阅读:
    【文本编辑】Raspberry B+上手教程--自己梳理,亲测有效
    【超级用户】Raspberry B+上手教程--自己梳理,亲测有效
    【连接网络】Raspberry B+上手教程--自己梳理,亲测有效
    【连接PC(路由)】Raspberry B+上手教程--自己梳理,亲测有效
    JavaScript的继承
    javascript的面向对象程序设计(二)
    javascript的面向对象程序设计(一)
    变量
    抽象工厂模式
    在jsp中应用MVC模型
  • 原文地址:https://www.cnblogs.com/chuijingjing/p/11232396.html
Copyright © 2020-2023  润新知