『TensorFlow』第二弹_线性拟合&神经网络拟合_恰是故人归
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』第二弹_线性拟合&神经网络拟合_恰是故人归的更多相关文章
- 『PyTorch』第二弹重置_Tensor对象
『PyTorch』第二弹_张量 Tensor基础操作 简单的初始化 import torch as t Tensor基础操作 # 构建张量空间,不初始化 x = t.Tensor(5,3) x -2. ...
- 关于『HTML5』:第二弹
关于『HTML5』:第二弹 建议缩放90%食用 咕咕咕咕咕咕咕!!1 (蒟蒻大鸽子终于更新啦) 自开学以来,经过了「一脸蒙圈的 半期考试」.「二脸蒙圈的 体测」的双重洗礼,我终于有空肝 HTML5 辣 ...
- 关于『Markdown』:第二弹
关于『Markdown』:第二弹 建议缩放90%食用 道家有云:一生二,二生三,三生万物 为什么我的帖子不是这样 各位打工人们! 自从我学了Markdown以来 发现 Markdown 语法真的要比 ...
- 关于『HTML』:第二弹
关于『HTML』:第二弹 建议缩放90%食用 第二弹! 它来了! 它来了! 我竟然没有拖更,对了,你们昨天用草稿纸了么 开始正文之前提一个问题:大家知道"%%%"是什么意思吗?就这 ...
- 『TensorFlow』第九弹_图像预处理_不爱红妆爱武装
部分代码单独测试: 这里实践了图像大小调整的代码,值得注意的是格式问题: 输入输出图像时一定要使用uint8编码, 但是数据处理过程中TF会自动把编码方式调整为float32,所以输入时没问题,输出时 ...
- 『PyTorch』第二弹_张量
参考:http://www.jianshu.com/p/5ae644748f21# 几个数学概念: 标量(Scalar)是只有大小,没有方向的量,如1,2,3等 向量(Vector)是有大小和方向的量 ...
- 『MXNet』第二弹_Gluon构建模型
上节用了Sequential类来构造模型.这里我们另外一种基于Block类的模型构造方法,它让构造模型更加灵活,也将让你能更好的理解Sequential的运行机制. 回顾: 序列模型生成 层填充 初始 ...
- 『TensorFlow』DCGAN生成动漫人物头像_下
『TensorFlow』以GAN为例的神经网络类范式 『cs231n』通过代码理解gan网络&tensorflow共享变量机制_上 『TensorFlow』通过代码理解gan网络_中 一.计算 ...
- 关于『Markdown』:第一弹
关于『Markdown』:第一弹 建议缩放90%食用 声明: 在我之前已有数位大佬发布 "Markdown" 的语法知识点, 在此, 仅整理归类以及补缺, 方便阅读. 感谢 C20 ...
随机推荐
- Pig项目&Spring Boot&Spring Cloud学习
1.Spring条件加载原理(@Conditional,@ConditionalOnXXX注解) https://fangjian0423.github.io/2017/05/16/springboo ...
- IDEA旗舰版新建web项目
即在一个Project下(MyEclipse中为工作空间)新建一个Module. 点击,在弹出框上打一个勾,如下图: 点Next,输入项目名,如下图: 点Finish, 右键WEB-INF,新建2个D ...
- [bzoj 4196][NOI 2015]软件包管理器
大概算是一道模板题吧? 就是细节有点多 罗列一下: 如果习惯从1开始搞树的编号的话,处理输入进来的那个依赖关系在加边的时候两个都要+1,体现在代码就是i要从2枚举到n,然后输入进来的那个数要+1 这道 ...
- 如何创建并运行java线程 , 多线程使用
http://www.importnew.com/20672.html https://www.cnblogs.com/wxd0108/p/5479442.html https://www.cnblo ...
- vue的全局方法和局部方法
var infiniteScroll = require('vue-infinite-scroll') 等价写法 import infiniteScroll from 'vue-infinite-sc ...
- HDU 5069 Harry And Biological Teacher(AC自动机+线段树)
题意 给定 \(n\) 个字符串,\(m\) 个询问,每次询问 \(a\) 字符串的后缀和 \(b\) 字符串的前缀最多能匹配多长. \(1\leq n,m \leq 10^5\) 思路 多串匹配,考 ...
- Linux 下上手 STC89C52RC
第一次接触单片机,自然选择了简单的51单片机.然而我的操作系统是 Linux .在 Windows 下上手51似乎很容易.但是 Linux 上搭建 51 开发环境不是很顺. 那么谈谈 Linux 我如 ...
- Jdk在window环境下的安装与配置详解
本文为博主原创,转载请注明出处: 1.2 Java程序开发环境的配置 java开发工具包:java开发工具:记事本 IDE,这个只能写小程序,写大程序需要集成开发工具:反编译工具(我们可以在网上找一 ...
- strlen函数,strcat函数,strcpy函数,strncpy函数,strcmp函数
strcpy函数: char *strcpy(char *Dest , const char *Src) { assert((Dest != NULL) && (Src != NULL ...
- BZOJ 1497: [NOI2006]最大获利(最大权闭合图)
http://www.lydsy.com/JudgeOnline/problem.php?id=1497 题意: 思路: 论文题,只要看过论文的话就是小菜一碟啦~ 每个用户群i作为一个结点分别向相应的 ...