#!/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的更多相关文章

  1. 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 ...

  2. TF随笔-11

    #!/usr/bin/env python2 # -*- coding: utf-8 -*- import tensorflow as tf my_var=tf.Variable(0.) step=t ...

  3. TF随笔-10

    #!/usr/bin/env python# -*- coding: utf-8 -*-import tensorflow as tf x = tf.constant(2)y = tf.constan ...

  4. TF随笔-9

    计算累加 #!/usr/bin/env python2 # -*- coding: utf-8 -*-"""Created on Mon Jul 24 08:25:41 ...

  5. TF随笔-7

    求平均值的函数 reduce_mean axis为1表示求行 axis为0表示求列 >>> xxx=tf.constant([[1., 10.],[3.,30.]])>> ...

  6. 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 ...

  7. tf随笔-5

    # -*- coding: utf-8 -*-import tensorflow as tfw1=tf.Variable(tf.random_normal([2,6],stddev=1))w2=tf. ...

  8. TF随笔-4

    >>> import tensorflow as tf>>> a=tf.constant([[1,2],[3,4]])>>> b=tf.const ...

  9. TF随笔-3

    >>> import tensorflow as tf>>> node1 = tf.constant(3.0, dtype=tf.float32)>>& ...

随机推荐

  1. Android studio通过连接蓝叠模拟器大幅提高速度!

    因为我的电脑是X200,集成显卡,不支持Opengl 2.0,很多安卓模拟器都不能用(夜神.海马玩,mumu等)最后发现蓝叠. 首先WIN+R,CMD,输入ADB,如果没有命令信息,说明我们需要在PA ...

  2. Linux读书笔记1/2章

    linux的内核设计: 第一章 1.1Linux历史: 历经时间的考验,今天Unix已经发展成一个支持抢占式多任务.多线程.虚拟内存.换页.动态链接.TCP/Ip网络的现代化操作系统. 1.2追寻Li ...

  3. http://www.jb51.net/article/28619.htm

    http://www.jb51.net/article/28619.htm js autocomplete 自动完成

  4. Nginx解决post请求405问题

    和工商银行的一个合作项目,对方客户端需要请求我们的一个静态页面,但是客户端发送过来的请求方法用的post,日志显示405错误(请求方法错误),正常一个静态页面直接用get请求就可以了,工行那边说写死了 ...

  5. JAVA基础补漏--可变参数

    使用场景:当参数列表数据类型已确定,但参数的个数不确定,就可以用可变参数 格式: 修饰符 返回值类型 方法名(数据类型...变量名){} 原理:可变参数底层为一数组,可根据变量个数的不同,创建不同长度 ...

  6. 管理账号密码的工具-KeePass使用方法

    附件链接:https://files.cnblogs.com/files/stxs/KeePass.zip 打开压缩包“KeePass.zip",将文件"KeePass.exe&q ...

  7. git gc内存错误的解决方案

    Auto packing the repository for optimum performance. You may alsorun "git gc" manually. Se ...

  8. MongoDB 性能优化

    Read Preferences/读写分离 有时候为了考虑应用程序的性能或响应性,为了提高读取操作的吞吐率,一个常见的措施就是进行读写分离,MongoDB副本集对读写分离的支持是通过Read Pref ...

  9. vue-cli Uncaught SyntaxError: Use of const in strict mode解决办法

    vue-cli初始化项目,开发环境运行项目使用了webpack-dev-server,而最新版本webpack-dev-server@2.9.1运行项目时,并不能成功的把es6语法转化为es5,所以在 ...

  10. 最新Dubbo-admin+Zookeeper搭建

    Zookeeper搭建: 下载zookeeper压缩包并解压,下载地址:http://www.apache.org/dyn/closer.cgi/zookeeper/进入conf目录下将 zoo_sa ...