『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 ...
随机推荐
- ThreadLocal 的机制与内存泄漏
ThreadLocal笔记 如上图所示 每个Thread 都有一个map,里面存着Entry<Key,value>,而key是实现了WeakReference的ThreadLocal,如果 ...
- p3396 哈希冲突(暴力)
想了好久,没想到优秀的解法,结果是个暴力大吃一静.jpg 分类讨论,预处理\(p\le \sqrt{n}\) 的情况,其他直接暴力,复杂度\(O(n \sqrt{n} )\) #include < ...
- Vue.directive自定义指令
Vue除了内部指令,我们也可以定义一些属于自己的指令,比如我们要定义一个v-diy的指令,作用就是让文字变成红色. 写好了这个功能,我们现在就自己定义一个全局的指令.我们这里使用Vue.directi ...
- 如何某个js文件中的 console
因为自己引用了别人的一个 js 文件,但里面有很多事件相关的 console 输出.自己并不想去修改别人的文件.但想屏蔽掉里面的 console . 有多个 js 文件里有 console.log . ...
- centos7 修改密码
Centos7破解密码的方法 Centos7忘记密码 在工作或者自己练习的时候我们难免会大意忘掉自己的root密码,有些同学忘掉密码竟然第一选择是重装系统,工作中可万万使不得! 本篇博客将讲解 ...
- arcgis api for js 之发布要素服务
1. 引言 如果我们要在网页端实现要素的增删改查操作,需要使用到要素服务(FeatureService),本篇文章将介绍如何发布要素服务. 1.1 什么是要素服务 在发布之前,我们先了解下要素服务:要 ...
- 自定义标签TLD文件中,rtexprvalue子标签的意思
rtexprvalue的全称是 Run-time Expression Value, 它用于表示是否能够利用JSP表达式. 举例子: 1.定义一个TLD文件: <tag> <name ...
- SVN服务器搭建和使用教程
安装SVNserver 点击Next下一步,如下: 然后再点击Next项,下一步,如下: 点击[Next] 如下: Location是指VisualSVN Server的安装目录,Repository ...
- Javascript 垃圾回收机制
转载于https://www.cnblogs.com/zhwl/p/4664604.html 一.垃圾回收的必要性 由于字符串.对象和数组没有固定大小,所有当他们的大小已知时,才能对他们进行动态的存储 ...
- python 传递多个参数
def oper(a,*args): print(args) print(a) oper("q","s","d","z" ...