tensorflow 2. 变量和存钱

作者: YuanLulu | 来源:发表于2018-03-13 13:45 被阅读24次

    变量维护图执行过程中的状态信息. 下面的例子演示了如何使用变量实现一个简单的计数器.

    # 创建一个变量, 初始化为标量 0.
    state = tf.Variable(0, name="counter")
    
    # 创建一个 op, 其作用是使 state 增加 1
    
    one = tf.constant(1)
    new_value = tf.add(state, one)
    update = tf.assign(state, new_value)
    
    # 启动图后, 变量必须先经过`初始化` (init) op 初始化,
    # 首先必须增加一个`初始化` op 到图中.
    init_op = tf.initialize_all_variables()
    
    # 启动图, 运行 op
    with tf.Session() as sess:
      # 运行 'init' op
      sess.run(init_op)
      # 打印 'state' 的初始值
      print sess.run(state)
      # 运行 op, 更新 'state', 并打印 'state'
      for _ in range(3):
        sess.run(update)
        print sess.run(state)
    
    输出:
    # 0
    # 1
    # 2
    # 3
    
    Fetch

    为了取回操作的输出内容, 可以在使用 Session 对象的 run() 调用 执行图时, 传入一些 tensor, 这些 tensor 会帮助你取回结果. 在之前的例子里, 我们只取回了单个节点 state, 但是你也可以取回多个 tensor:

    input2 = tf.constant(2.0)
    input3 = tf.constant(5.0)
    intermed = tf.add(input2, input3)
    multiplication = tf.multiply(input1, intermed)
    
    sess = tf.Session()
    
    result = sess.run([multiplication, intermed])
    print(result)
    #输出
    #[21.0, 7.0]
    

    使用tf.mul会提示错误“AttributeError: module ‘tensorflow‘ has no attribute ‘mul‘”,因为乘法名字改了,需使用tf.multiply

    对于这类问题:module 'tensorflow' has no attribute 'xxx'
    网上的一个整理如下

    • tf.sub()更改为tf.subtract()
    • tf.mul()更改为tf.multiply()
    • tf.types.float32更改为tf.float32
    • tf.pact()更改为tf.stact()
    Feed

    上述示例在计算图中引入了 tensor, 以常量或变量的形式存储. TensorFlow 还提供了 feed 机制, 该机制 可以临时替代图中的任意操作中的 tensor 可以对图中任何操作提交补丁, 直接插入一个 tensor.
    feed 使用一个 tensor 值临时替换一个操作的输出结果. 你可以提供 feed 数据作为 run() 调用的参数. feed 只在调用它的方法内有效, 方法结束, feed 就会消失. 最常见的用例是将某些特殊的操作指定为 "feed" 操作, 标记的方法是使用 tf.placeholder() 为这些操作创建占位符.

    input1 = tf.placeholder(tf.float32)
    input2 = tf.placeholder(tf.float32)
    output = tf.multiply(input1, input2)
    
    sess = tf.Session()
    print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))
    
    sess.close()
    #输出:[array([ 14.], dtype=float32)]
    

    为了证明feed的功能,我使用feed修改变量的值

    input1 = tf.constant(3.0)
    input2 = tf.constant(5.0)
    output2 = tf.multiply(input1, input2)
    
    sess = tf.Session()
    print(sess.run([output2], feed_dict={input1:7., input2:2.}))
    print(sess.run([output2]))
    
    输出内容为:
    [14.0]
    [15.0]
    

    这说明feed机制提供了一种“打桩”的机制,我们可以任意控制节点的输出张量。

    使用tensorflow计算投资收益

    假设某个程序员30岁,为了预防以后社保亏空,决定每月存500块钱,按照年化5%来每月计算复利,共计30年。
    看看这个程序员到自己60岁时有多少存款。

    import tensorflow as tf 
    
    put_into = tf.constant(500., name="put_into")
    month_rate = tf.constant(0.05/12, name="month_rate")
    
    pool = tf.Variable(0.0, name="pool")
    
    sum1 = tf.add(put_into, pool, name="sum1")
    
    interest = tf.multiply(sum1, month_rate, name="interest")
    
    sum2 = tf.add(interest, sum1, name="sum2")
    
    # interest_sum = tf.assign()
    
    update_pool = tf.assign(pool, sum2)
    
    init_op = tf.global_variables_initializer()
    
    with tf.Session() as sess:
        sess.run(init_op)
    
        for i in range(12*30):
            sum = sess.run(sum2)
            sess.run(update_pool)
            if i%10 == 9:
                print("{}th moth, I have money {} Yuans".format(i, sum))
    
    2018-03-06 23:01:18.676497: I C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
    9th moth, I have money 5116.02783203125 Yuans
    19th moth, I have money 10449.265625 Yuans
    29th moth, I have money 16008.9345703125 Yuans
    39th moth, I have money 21804.646484375 Yuans
    ......
    309th moth, I have money 316806.09375 Yuans
    319th moth, I have money 335372.59375 Yuans
    329th moth, I have money 354727.4375 Yuans
    339th moth, I have money 374904.03125 Yuans
    349th moth, I have money 395937.28125 Yuans
    359th moth, I have money 417863.46875 Yuans
    
    本来18万的本金,借助复利,变成了41.7万。

    看来存钱还是必要的,虽然30年后的购买力不好估计,总比没有存款强。

    相关文章

      网友评论

        本文标题:tensorflow 2. 变量和存钱

        本文链接:https://www.haomeiwen.com/subject/rplzfftx.html