TF随笔-8
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Mon Jul 10 09:35:04 2017 @author: myhaspl@myhaspl.com,http://blog.csdn.net/myhaspl """ #逻辑或 import tensorflow as tf batch_size=10 w1=tf.Variable(tf.random_normal([2,6],stddev=1,seed=1)) w2=tf.Variable(tf.random_normal([6,1],stddev=1,seed=1)) b=tf.Variable(tf.zeros([6]),tf.float32) x=tf.placeholder(tf.float32,shape=(None,2),name="x") y=tf.placeholder(tf.float32,shape=(None,1),name="y") h=tf.matmul(x,w1)+b yo=tf.matmul(h,w2) #损失函数计算差异平均值 cross_entropy=tf.reduce_mean(tf.abs(y-yo)) #反向传播 train_step=tf.train.AdamOptimizer(0.05).minimize(cross_entropy) #生成样本 x_=[[0.,0.],[0.,1.],[1.,0.],[1.,1.]] y_=[[0.],[1.],[1.],[1.]] b_=tf.zeros([6]) with tf.Session() as sess: #初始化变量 init_op=tf.global_variables_initializer() sess.run(init_op) print sess.run(w1) print sess.run(w2) #设定训练轮数 TRAINCOUNT=500 for i in range(TRAINCOUNT): #开始训练 sess.run(train_step,feed_dict={x:x_,y:y_}) if i%10==0: total_cross_entropy=sess.run(cross_entropy,feed_dict={x:x_,y:y_}) print("%d 次训练之后,损失:%g"%(i+1,total_cross_entropy)) print(sess.run(w1)) print(sess.run(w2)) #生成测试样本,仅进行前向传播验证: testyo=sess.run(yo,feed_dict={x:[[0.,1.],[1.,1.]]}) myout=[int(testout>0.5) for testout in testyo] print myout
(Initialize initial 1st moment vector)
v_0 <- 0 (Initialize initial 2nd moment vector)
t <- 0 (Initialize timestep)
The update rule for variable
with gradient g
uses an optimization described at the end of section2 of the paper:
t <- t + 1
lr_t <- learning_rate * sqrt(1 - beta2^t) / (1 - beta1^t)
m_t <- beta1 * m_{t-1} + (1 - beta1) * g
v_t <- beta2 * v_{t-1} + (1 - beta2) * g * g
variable <- variable - lr_t * m_t / (sqrt(v_t) + epsilon)
The default value of 1e-8 for epsilon might not be a good default in general. For example, when training an Inception network on ImageNet a current good choice is 1.0 or 0.1. Note that since AdamOptimizer uses the formulation just before Section 2.1 of the Kingma and Ba paper rather than the formulation in Algorithm 1, the "epsilon" referred to here is "epsilon hat" in the paper.
The sparse implementation of this algorithm (used when the gradient is an IndexedSlices object, typically because of tf.gather
or an embedding lookup in the forward pass) does apply momentum to variable slices even if they were not used in the forward pass (meaning they have a gradient equal to zero). Momentum decay (beta1) is also applied to the entire momentum accumulator. This means that the sparse behavior is equivalent to the dense behavior (in contrast to some momentum implementations which ignore momentum unless a variable slice was actually used).
TF随笔-8的更多相关文章
- TF随笔-13
import tensorflow as tf a=tf.constant(5) b=tf.constant(3) res1=tf.divide(a,b) res2=tf.div(a,b) with ...
- TF随笔-11
#!/usr/bin/env python2 # -*- coding: utf-8 -*- import tensorflow as tf my_var=tf.Variable(0.) step=t ...
- TF随笔-10
#!/usr/bin/env python# -*- coding: utf-8 -*-import tensorflow as tf x = tf.constant(2)y = tf.constan ...
- TF随笔-9
计算累加 #!/usr/bin/env python2 # -*- coding: utf-8 -*-"""Created on Mon Jul 24 08:25:41 ...
- TF随笔-7
求平均值的函数 reduce_mean axis为1表示求行 axis为0表示求列 >>> xxx=tf.constant([[1., 10.],[3.,30.]])>> ...
- tf随笔-6
import tensorflow as tfx=tf.constant([-0.2,0.5,43.98,-23.1,26.58])y=tf.clip_by_value(x,1e-10,1.0)ses ...
- tf随笔-5
# -*- coding: utf-8 -*-import tensorflow as tfw1=tf.Variable(tf.random_normal([2,6],stddev=1))w2=tf. ...
- TF随笔-4
>>> import tensorflow as tf>>> a=tf.constant([[1,2],[3,4]])>>> b=tf.const ...
- TF随笔-3
>>> import tensorflow as tf>>> node1 = tf.constant(3.0, dtype=tf.float32)>>& ...
随机推荐
- snapshot与release
总结自:https://www.jianshu.com/p/084fd2408d9a 这两个概念是用于描述jar包,jar包提供给其他系统作为依赖. 1. snapshot版本代表不稳定.尚处于开发中 ...
- 20144303 《Java程序设计》第二次实验实验报告
20144303 <Java程序设计>第二次实验实验报告 北京电子科技学院(besti)实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握U ...
- 20135302魏静静——linux课程第四周实验及总结
linux课程第四周实验及总结 一.实验 我选择的是第20号系统调用,getpid 代码如下: /* getpid.c */ #include <unistd.h> #include &l ...
- javaWeb中JNDI的使用,为什么要加java:comp/env前缀
转载自(http://blog.csdn.net/guodongsoft/article/details/52399527) 我们在使用JNDI调用某个对象时,会有下述两种方式 context.loo ...
- 爬虫之Requests: 让 HTTP 服务人类
Requests: 让 HTTP 服务人类 虽然Python的标准库中 urllib2 模块已经包含了平常我们使用的大多数功能,但是它的 API 使用起来让人感觉不太好,而 Requests 自称 “ ...
- 51 Nod 1091 线段的重叠
2017-09-24 19:51:41 writer:pprp 上一个题目就是关于线段重叠最大值,这个是找区间最长重合? 给你n个线段,然后让你在其中选择两条,使两条尽可能重合多一点 解决方法; 1. ...
- 物料类型AM11没有任务清单类型N定义
CA01 创建工艺路线时报错信息:“物料类型AM11没有为任务清单类型N定义” (如下图) 处理方法: 配置路径:生产->基本数据->工艺路线->通用数据->定义物料类型分配 ...
- uva 1619 - Feel Good || poj 2796 单调栈
1619 - Feel Good Time limit: 3.000 seconds Bill is developing a new mathematical theory for human ...
- 关于ckeditor 之 上传功能
度了很多文章,看了很多关于ckeditor配置上传功能的文章,没一个写得清楚的, 就是简单的根目录下.config.js 增加 config.filebrowserUploadUrl="/a ...
- 【三小时学会Kubernetes!(二) 】Kubernetes 简介及Pod实践
Kubernetes 简介 我向你保证我没有夸大其词,读完本文你会问“为什么我们不称它为 Supernetes?” Kubernetes 是什么? 从容器启动微服务后,我们有一个问题,让我们通过如下问 ...