Step1:

目标:

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

代码:

 import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt def show_data(x,y,w,b):
'''
绘图函数
:param x: 横坐标散点
:param y: 纵坐标散点
:param w: 权重
:param b: 偏移量
:return: 无
'''
plt.figure()
plt.scatter(x,y,marker='.')
plt.scatter(x,(w*x+b),marker='.')
plt.show() ### 生成数据 ###
x_data = np.random.rand(100).astype(np.float32)
y_data = 0.1*x_data + 0.3 ### 创建结构 ### Weights = tf.Variable(tf.random_uniform([1],-1.0,1.0))
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() # 初始化全变量节点 ### 训练部分 with tf.Session() as sess:
sess.run(init)
for step in range(200):
sess.run(train)
if step % 20 == 0:
print(step,sess.run(Weights),sess.run(biases))
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:

目标:

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

代码:

 import numpy as np
import tensorflow as tf def add_layer(input,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)
# [in]*[[out]*in]+[out]
Wx_plus_b = tf.matmul(input,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)[:,np.newaxis] # 插入新维度(300)->(300,1)
noise = np.random.normal(0,0.05,x_data.shape)
y_data = np.square(x_data) - 0.5 + noise # 平方 # 这样也可以在feed时修改标准数据的类型
xs = tf.placeholder(tf.float32,[None,1])
ys = tf.placeholder(tf.float32,[None,1]) 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]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) init = tf.global_variables_initializer() 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})
if i % 50 == 0:
print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))
# 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维度扩充:

 import numpy as np
a = np.array([1,2,3,4,5])
a[:,np.newaxis]
# Out[7]:
# array([[1],
# [2],
# [3],
# [4],
# [5]])
a[np.newaxis,:]
# Out[8]:
# array([[1, 2, 3, 4, 5]])
a.shape
# Out[12]:
# (5,)
a[:,np.newaxis].shape
# Out[10]:
# (5, 1)
a[np.newaxis,:].shape
# Out[11]:
# (1, 5)

loss函数计算分析:

将输出改成下面:

 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})
if i % 50 == 0:
#print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))
print(sess.run(tf.square(ys - prediction),feed_dict={xs:x_data,ys:y_data})) # 300行
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]。

修改如下的话:

 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})
if i % 50 == 0:
#print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))
print(sess.run(tf.square(ys - prediction),feed_dict={xs:x_data,ys:y_data}).shape) # 300行
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:

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

『TensorFlow』第二弹_线性拟合&神经网络拟合_恰是故人归的更多相关文章

  1. 『PyTorch』第二弹重置_Tensor对象

    『PyTorch』第二弹_张量 Tensor基础操作 简单的初始化 import torch as t Tensor基础操作 # 构建张量空间,不初始化 x = t.Tensor(5,3) x -2. ...

  2. 关于『HTML5』:第二弹

    关于『HTML5』:第二弹 建议缩放90%食用 咕咕咕咕咕咕咕!!1 (蒟蒻大鸽子终于更新啦) 自开学以来,经过了「一脸蒙圈的 半期考试」.「二脸蒙圈的 体测」的双重洗礼,我终于有空肝 HTML5 辣 ...

  3. 关于『Markdown』:第二弹

    关于『Markdown』:第二弹 建议缩放90%食用 道家有云:一生二,二生三,三生万物 为什么我的帖子不是这样 各位打工人们! 自从我学了Markdown以来 发现 Markdown 语法真的要比 ...

  4. 关于『HTML』:第二弹

    关于『HTML』:第二弹 建议缩放90%食用 第二弹! 它来了! 它来了! 我竟然没有拖更,对了,你们昨天用草稿纸了么 开始正文之前提一个问题:大家知道"%%%"是什么意思吗?就这 ...

  5. 『TensorFlow』第九弹_图像预处理_不爱红妆爱武装

    部分代码单独测试: 这里实践了图像大小调整的代码,值得注意的是格式问题: 输入输出图像时一定要使用uint8编码, 但是数据处理过程中TF会自动把编码方式调整为float32,所以输入时没问题,输出时 ...

  6. 『PyTorch』第二弹_张量

    参考:http://www.jianshu.com/p/5ae644748f21# 几个数学概念: 标量(Scalar)是只有大小,没有方向的量,如1,2,3等 向量(Vector)是有大小和方向的量 ...

  7. 『MXNet』第二弹_Gluon构建模型

    上节用了Sequential类来构造模型.这里我们另外一种基于Block类的模型构造方法,它让构造模型更加灵活,也将让你能更好的理解Sequential的运行机制. 回顾: 序列模型生成 层填充 初始 ...

  8. 『TensorFlow』DCGAN生成动漫人物头像_下

    『TensorFlow』以GAN为例的神经网络类范式 『cs231n』通过代码理解gan网络&tensorflow共享变量机制_上 『TensorFlow』通过代码理解gan网络_中 一.计算 ...

  9. 关于『Markdown』:第一弹

    关于『Markdown』:第一弹 建议缩放90%食用 声明: 在我之前已有数位大佬发布 "Markdown" 的语法知识点, 在此, 仅整理归类以及补缺, 方便阅读. 感谢 C20 ...

随机推荐

  1. 101. Symmetric对称 Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  2. 【Dalston】【第二章】客户端负载均衡(Ribbon)

    对于大型应用系统负载均衡(LB:Load Balancing)是首要被解决一个问题.在微服务之前LB方案主要是集中式负载均衡方案,在服务消费者和服务提供者之间又一个独立的LB,LB通常是专门的硬件,如 ...

  3. Unity3D学习笔记(二十九):AssetBundle

    AssetBundle 什么是AssetBundle? AssetBundle是把一些资源文件或场景文件,以某种方式保存在一个文件中.一个AssetBundle可以包含模型.材质.图片或场景等.但是A ...

  4. 3、My Scripts

    .用for循环批量修改文件扩展名(P240) .使用专业改名命令rename来实现 .通过脚本实现sshd.rsyslog.crond.network.sysstat服务在开机时自动启动(P244) ...

  5. python学习打卡 day12 生成器

    本节主要内容 : 生成器 生成器函数 各种推导式 生成器表达式 一.生成器 什么是生成器.生成器的本质就是迭代器. 在python中有三种方式来获取生成器: 1.通过生成器函数 2.通过各种推导式来实 ...

  6. 【Selenium2】【selenium之 定位以及切换frame(iframe)】

    参考:http://blog.csdn.net/huilan_same/article/details/52200586 总有人看不明白,以防万一,先在开头大写加粗说明一下: frameset不用切, ...

  7. MVC杂记

    @{ Layout = “…”} To define layout page Equivalent to asp.NET master-page 要定义相当于ASP.Net母版页的页面布局 @mode ...

  8. Linux 服务器 安装 goflyway

    github官方开发主页:https://github.com/coyove/goflyway goflyway的用途就不说了,你能搜到此文章就说明已经知道了. centos和Ubuntu均可使用,其 ...

  9. _event_phase_team

    EventId 事件ID Phase 阶段ID,从1开始 TeamId 事件玩家分组,攻守(防守为1,进攻为2),自定义阵营(_faction表自定义阵营ID),公会(公会guid) Graveyar ...

  10. python redis 操作

    1.String 操作 redis中的String在在内存中按照一个name对应一个value来存储 set() #在Redis中设置值,默认不存在则创建,存在则修改 r.set('name', 'z ...