zoukankan      html  css  js  c++  java
  • 『TensorFlow』第二弹_线性拟合&神经网络拟合_恰是故人归

    Step1:

    目标:

    使用线性模拟器模拟指定的直线:y = 0.1*x + 0.3

    代码:

     1 import tensorflow as tf
     2 import numpy as np
     3 import matplotlib.pyplot as plt
     4 
     5 def show_data(x,y,w,b):
     6     '''
     7     绘图函数
     8     :param x: 横坐标散点 
     9     :param y: 纵坐标散点
    10     :param w: 权重
    11     :param b: 偏移量
    12     :return:  无
    13     '''
    14     plt.figure()
    15     plt.scatter(x,y,marker='.')
    16     plt.scatter(x,(w*x+b),marker='.')
    17     plt.show()
    18 
    19 ### 生成数据 ###
    20 x_data = np.random.rand(100).astype(np.float32)
    21 y_data = 0.1*x_data + 0.3
    22 
    23 
    24 ### 创建结构 ###
    25 
    26 Weights = tf.Variable(tf.random_uniform([1],-1.0,1.0))
    27 biases = tf.Variable(tf.zeros([1]))
    28 
    29 y = Weights*x_data + biases
    30 
    31 loss = tf.reduce_mean(tf.square(y-y_data))              # 损失函数
    32 optimizer = tf.train.GradientDescentOptimizer(0.5)      # 优化器&学习率选择
    33 train = optimizer.minimize(loss)                        # 优化器优化目标选择
    34 
    35 init = tf.global_variables_initializer()                # 初始化全变量节点
    36 
    37 
    38 ### 训练部分
    39 
    40 with tf.Session() as sess:
    41     sess.run(init)
    42     for step in range(200):
    43         sess.run(train)
    44         if step % 20 == 0:
    45             print(step,sess.run(Weights),sess.run(biases))
    46             show_data(x_data, y_data, sess.run(Weights), sess.run(biases))

     返回:

    /home/hellcat/anaconda2/envs/python3_6/bin/python /home/hellcat/PycharmProjects/data_analysis/TensorFlow/line_polyfit.py
    2017-05-16 14:30:21.054380: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
    2017-05-16 14:30:21.054405: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
    2017-05-16 14:30:21.054412: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
    0 [-0.04776853] [ 0.48895872]
    20 [ 0.05342153] [ 0.32313728]
    40 [ 0.08880574] [ 0.30556062]
    60 [ 0.09730968] [ 0.30133641]
    80 [ 0.09935342] [ 0.30032119]
    100 [ 0.0998446] [ 0.3000772]
    120 [ 0.09996266] [ 0.30001855]
    140 [ 0.09999102] [ 0.30000448]
    160 [ 0.09999786] [ 0.30000108]
    180 [ 0.09999949] [ 0.30000028]
    
    Process finished with exit code 0

    给出第一次(左)和最后一次(右)的图,直观的感受一下拟合效果(蓝色为标准,黄色为拟合):

     step2:

    目标:

    使用神经网络拟合二次函数(加噪声)

    代码:

     1 import numpy as np
     2 import tensorflow as tf
     3 
     4 def add_layer(input,in_size,out_size,activation_function=None):
     5     Weights = tf.Variable(tf.random_normal([in_size,out_size]))
     6     biases = tf.Variable(tf.zeros([1,out_size]) + 0.1)
     7     # [in]*[[out]*in]+[out]
     8     Wx_plus_b = tf.matmul(input,Weights) + biases
     9     if activation_function is None:
    10         outputs = Wx_plus_b
    11     else:
    12         outputs = activation_function(Wx_plus_b)
    13     return outputs
    14 
    15 x_data = np.linspace(-1,1,300)[:,np.newaxis]  # 插入新维度(300)->(300,1)
    16 noise = np.random.normal(0,0.05,x_data.shape)
    17 y_data = np.square(x_data) - 0.5 + noise      # 平方
    18 
    19 # 这样也可以在feed时修改标准数据的类型
    20 xs = tf.placeholder(tf.float32,[None,1])
    21 ys = tf.placeholder(tf.float32,[None,1])
    22 
    23 l1 = add_layer(xs,1,10,activation_function=tf.nn.relu)
    24 prediction =add_layer(l1,10,1,activation_function=None)
    25 
    26 loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1]))
    27 train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
    28 
    29 init = tf.global_variables_initializer()
    30 
    31 with tf.Session() as sess:
    32     sess.run(init)
    33     for i in range(1000):
    34         sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
    35         if i % 50 == 0:
    36             print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))
    37             # print(sess.run(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1]), feed_dict={xs: x_data, ys: y_data}))

    返回:

    10.3325
    0.0496612
    0.0166555
    0.0104833
    0.00823758
    0.00722597
    0.00659782
    0.00605279
    0.0056059
    0.00526385
    0.00496126
    0.00471807
    0.00449589
    0.00431383
    0.00418103
    0.00405819
    0.00395102
    0.00385027
    0.0037683
    0.00369631
    

    np.newaxis维度扩充:

     1 import numpy as np
     2 a = np.array([1,2,3,4,5])
     3 a[:,np.newaxis]
     4 # Out[7]: 
     5 # array([[1],
     6 #        [2],
     7 #        [3],
     8 #        [4],
     9 #        [5]])
    10 a[np.newaxis,:]
    11 # Out[8]: 
    12 # array([[1, 2, 3, 4, 5]])
    13 a.shape
    14 # Out[12]: 
    15 # (5,)
    16 a[:,np.newaxis].shape
    17 # Out[10]: 
    18 # (5, 1)
    19 a[np.newaxis,:].shape
    20 # Out[11]: 
    21 # (1, 5)

    loss函数计算分析:

    将输出改成下面:

    1 with tf.Session() as sess:
    2     sess.run(init)
    3     for i in range(1000):
    4         sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
    5         if i % 50 == 0:
    6             #print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))
    7             print(sess.run(tf.square(ys - prediction),feed_dict={xs:x_data,ys:y_data}))                                         # 300行
    8             print(sess.run(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1]), feed_dict={xs: x_data, ys: y_data}))  # 300列

    会发现tf.square(ys - prediction)输出:

    [[  1.27196927e-02]
     [  3.22369207e-03]
     [  1.74964982e-04]
     ....
     [  1.06449667e-02]
     [  4.93255538e-05]
     [  3.47382086e-03]]

     共计300个元素[300,1]。

    而tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1])输出:

    [  1.27196927e-02   3.22369207e-03   1.74964982e-04   3.40620875e-02
       1.67742800e-02   7.89122283e-03   8.79658014e-03   3.09832394e-04
       2.58327164e-02   8.12971615e-04   3.06550064e-03   2.16252869e-04
       .....
       7.49099301e-04   9.10352624e-04   1.99158350e-03   2.43023387e-04
       5.97979059e-04   8.30261386e-04   1.25318235e-02   1.10179959e-02
       5.22381114e-03   1.06449667e-02   4.93255538e-05   3.47382086e-03]

    共计300个元素[300]。

    修改如下的话:

    1 with tf.Session() as sess:
    2     sess.run(init)
    3     for i in range(1000):
    4         sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
    5         if i % 50 == 0:
    6             #print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))
    7             print(sess.run(tf.square(ys - prediction),feed_dict={xs:x_data,ys:y_data}).shape)                                         # 300行
    8             print(sess.run(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1]), feed_dict={xs: x_data, ys: y_data}).shape)  # 300列
    (300, 1)
    (300,)

    有意思的发现是sess.run的输出是numpy.ndarray类型

    查询reduction_indices=[1]可知是指定操作坐标轴的函数:

    即把[300,1]按行求和后拼接为新的数组,也就是[300]的尺寸。

    step3:

    之前学习的记忆渐渐复苏,感觉,还真的恰是故人归。

  • 相关阅读:
    Akka(2):Actor生命周期管理
    Akka(1):Actor
    Akka(0):聊聊对Akka的初步了解和想法
    FunDA:一个开源的函数式数据处理工具库,也是Slick的补充
    FunDA(17)- 示范:异常处理与事后处理
    FunDA(16)- 示范:整合并行运算
    FunDA(15)- 示范:任务并行运算
    FunDA(14)- 示范:并行运算,并行数据库读取
    FunDA(13)- 示范:用户自定义操作函数
    thinkjs中自定义sql语句
  • 原文地址:https://www.cnblogs.com/hellcat/p/6861340.html
Copyright © 2011-2022 走看看