變量維護圖執(zhí)行過程中的狀態(tài)信息. 下面的例子演示了如何使用變量實現(xiàn)一個簡單的計數(shù)器.
# 創(chuàng)建一個變量, 初始化為標量 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)
# 啟動圖后, 變量必須先經過`初始化` (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() 調用 執(zhí)行圖時, 傳入一些 tensor, 這些 tensor 會幫助你取回結果. 在之前的例子里, 我們只取回了單個節(jié)點 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 數(shù)據(jù)作為 run() 調用的參數(shù). feed 只在調用它的方法內有效, 方法結束, feed 就會消失. 最常見的用例是將某些特殊的操作指定為 "feed" 操作, 標記的方法是使用 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]))
輸出內容為:
[14.0]
[15.0]
這說明feed機制提供了一種“打樁”的機制蓬坡,我們可以任意控制節(jié)點的輸出張量。
使用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年后的購買力不好估計聪铺,總比沒有存款強。