tensorflow 2. 變量和存錢

變量維護(hù)圖執(zhí)行過程中的狀態(tài)信息. 下面的例子演示了如何使用變量實(shí)現(xiàn)一個簡單的計數(shù)器.

# 創(chuàng)建一個變量, 初始化為標(biāo)量 0.
state = tf.Variable(0, name="counter")

# 創(chuàng)建一個 op, 其作用是使 state 增加 1

one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)

# 啟動圖后, 變量必須先經(jīng)過`初始化` (init) op 初始化,
# 首先必須增加一個`初始化` op 到圖中.
init_op = tf.initialize_all_variables()

# 啟動圖, 運(yùn)行 op
with tf.Session() as sess:
  # 運(yùn)行 'init' op
  sess.run(init_op)
  # 打印 'state' 的初始值
  print sess.run(state)
  # 運(yùn)行 op, 更新 'state', 并打印 'state'
  for _ in range(3):
    sess.run(update)
    print sess.run(state)
輸出:
# 0
# 1
# 2
# 3
Fetch

為了取回操作的輸出內(nèi)容, 可以在使用 Session 對象的 run() 調(diào)用 執(zhí)行圖時, 傳入一些 tensor, 這些 tensor 會幫助你取回結(jié)果. 在之前的例子里, 我們只取回了單個節(jié)點(diǎn) 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‘”,因?yàn)槌朔指牧?,需使用tf.multiply

對于這類問題:module 'tensorflow' has no attribute 'xxx'
網(wǎng)上的一個整理如下

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

上述示例在計算圖中引入了 tensor, 以常量或變量的形式存儲. TensorFlow 還提供了 feed 機(jī)制, 該機(jī)制 可以臨時替代圖中的任意操作中的 tensor 可以對圖中任何操作提交補(bǔ)丁, 直接插入一個 tensor.
feed 使用一個 tensor 值臨時替換一個操作的輸出結(jié)果. 你可以提供 feed 數(shù)據(jù)作為 run() 調(diào)用的參數(shù). feed 只在調(diào)用它的方法內(nèi)有效, 方法結(jié)束, feed 就會消失. 最常見的用例是將某些特殊的操作指定為 "feed" 操作, 標(biāo)記的方法是使用 tf.placeholder() 為這些操作創(chuàng)建占位符.

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]))
輸出內(nèi)容為:
[14.0]
[15.0]

這說明feed機(jī)制提供了一種“打樁”的機(jī)制,我們可以任意控制節(jié)點(diǎn)的輸出張量。

使用tensorflow計算投資收益

假設(shè)某個程序員30歲,為了預(yù)防以后社保虧空,決定每月存500塊錢,按照年化5%來每月計算復(fù)利,共計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萬的本金,借助復(fù)利,變成了41.7萬。

看來存錢還是必要的,雖然30年后的購買力不好估計,總比沒有存款強(qiáng)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容