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)>>& ...
随机推荐
- 一键安装lnmp-php(5)
php(){cd $pathtar zxvf yasm-1.3.0.tar.gz cd yasm-1.3.0.tar.gz ./configuremakemake installcd $pathtar ...
- 20145333 《Java程序设计》第5周学习总结
20145333 <Java程序设计>第5周学习总结 教材学习内容总结 语法与继承架构 使用try.catch Java中所有错误都会被包装成对象,可以尝试(try)执行程序并捕捉(cat ...
- 前端小炒的win7使用笔记(收藏篇)
收藏篇 此中技巧及使用笔记,大多为冲浪时无意间发现,进而总结,其中种种小超都已一一验证过. 传说中WIN7上帝模式可查看200多项系统设置项目 在桌面创建文件夹,命名为 GodModel.{ED7BA ...
- 总结的一些json格式和对象/String/Map/List等的互转工具类
总结的一些json格式和对象/String/Map/List等的互转工具类,有需要的可以看看,需要引入jackson-core-asl-1.7.1.jar.jackson-jaxrs-1.7.1.ja ...
- Juniper SRX防火墙简明配置手册(转)
在执行mit命令前可通过配置模式下show命令查看当前候选配置(Candidate Config),在执行mit后配置模式下可通过run show config命令查看当前有效配置(Active co ...
- CentOS 7 SSH远程证书登陆
SSH远程证书登陆是使用"公私钥"认证的方式来进行SSH登录. 1.创建公私钥 创建方式有很多种,比如说通用ssh连接工具创建,然后把公钥上传到Server主机对应的用户目录下: ...
- WIN7环境安装informatica 提示 不能创建Domain或者node
查看infa安装的bat文件install.bat,会发现,它调用的是.\Server\Windows\Disk1\InstData\VM\install.exe.所以,我们在安装执行,右键insta ...
- Animal_human_kp人脸与马脸迁移学习GitHub 论文实现
Interspecies Knowledge Transfer for Facial Keypoint Detection关键点检测 Github地址:Interspecies Knowledge ...
- 数据结构实习 problem L 由二叉树的中序层序重建二叉树
由二叉树的中序层序重建二叉树 writer:pprp 用层序中序来重建二叉树 代码点这里 其实本质上与前序中序建立二叉树没有什么太大区别 大概思路: 递归解法,对当前层进行处理,通过层序遍历可以得到当 ...
- 编译Python2.7.10
为了测试 mesos,搞了一个 centos7.1,使用最小化安装,然后自己安装了 net-tools,“开发工具”集.后来想装一下 DCOS Cli工具,结果发现 python 的 pip 不可用. ...