1-tensorflow學習筆記-會話
會話
會話擁有並管理TensorFlow程式執行時的所有資源
當所有計算完成之後要關閉會話,幫助系統回收資源
import tensorflow as tf
# 定義計算圖
tens1 = tf.constant([1,2,3])
# 建立一個會話
sess = tf.Session()
# 使用會話來得到運算結果
result= sess.run(tens1)
print(result)
# 關閉會話並釋放資源
sess.close()
[1 2 3]
會話模式一
# 定義計算圖
tens2= tf.constant([1,2,3])
# 建立一個會話
sess = tf.Session()
# 使用會話來得到運算結果
try:
print(sess.run(tens2))
except:
print("Exception!")
finally:
# 關閉會話並釋放資源
sess.close()
[1 2 3]
會話模式二
node1 = tf.constant(3.0, tf.float32, name="node1")
node2 = tf.constant(4.0, tf.float32, name='nmde2')
result=tf.add(node1,node2)
# 建立一個會話,並用Python的上下文管理器來管理這個會話
with tf.Session() as sess:
print(sess.run(result))
# 改方法不需要再使用sess.close()方法。
7.0
指定預設會話,需要手動指定
TensorFlow不會自動生成預設會話,需要手動指定當預設的會話被指定後可以通過tf.Tensor.eval函式來計算一個張量的取值
node1 = tf.constant(3.0, tf.float32, name='node1')
node2 = tf.constant(4.0, tf.float32, name='node2')
result = tf.add( node1, node2)
sess = tf.Session()
with sess.as_default():
print(result.eval())
# 可以發現print(sess.run(result)) 與print(result.eval(session=sess)) 相同
7.0
互動式環境下設定預設會話
在互動式環境下,Python指令碼或者Jupyter編輯器下,通過設定預設會話來獲取張量的取值
更加方便
tf.InteractiveSession 使用這個函式會自動將生成的會話註冊為預設會話
常量與變數
常量constant
在執行過程中值不會改變的單元,在TensorFlow中無須進行初始化操作
建立語句:
constant_ name = tf.constant(value)
a = tf.constant(1.0, name='a')
b = tf.constant(2.5, name='b')
c = tf.add(a, b, name='c')
sess = tf.Session()
c_value = sess.run(c)
print(c_value)
sess.close()
3.5
變數 Variable
在執行過程中值會改變的單元,在TensorFlow中須進行初始化操作
建立語句:
name_variable = tf.Variable(value, name)
個別變數初始化:
init_op = name_variable.initializer()
所有變數初始化:
init_op = tf.global_variables_initializer()
a = tf.Variable(1.0, name='a')
b = tf.Variable(2.5, name='b')
c = tf.add(a, b, name='add')
sess = tf.Session()
init = tf.global_variables_initializer() # 增加了一個init初始化的變數
sess.run(init)
c_value = sess.run(result)
print(c_value)
sess.close()
7.0
變數賦值
- 與傳統程式語言不同,TensorFlow中的變數定義後,一般無需人工賦值,
系統會根據演算法模型,訓練優化過程中自動調整變數對應的數值 - 後面在將機器學習模型訓練時會更能體會,比如權重Weight變數w,經
過多次迭代,會自動調
epoch = tf.Variable(0,name='epoch',trainable=False)
- 特殊情況需要人工更新的,可用變數賦值語句
變數更新語句:
update_op = tf.assign(variable_to_be_updated, new_value)
# 變數賦值
value = tf.Variable(0, name= 'value')
one = tf.constant(1)
new_value = tf.add(value, one)
update_value = tf.assign(value, new_value) # 更新變數
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for _ in range(10):
sess.run(update_value)
print(sess.run(value))
1
2
3
4
5
6
7
8
9
10
思考題
如何通過TensorFlow的變數賦值計算:1+2+3+…+10 ?
value =tf.Variable(1, name="value")
sums = tf.Variable(0)
one = tf.constant(1)
new_sums = tf.add(sums,value)
update_sums = tf.assign(sums, new_sums)
new_value = tf.add(value, one)
update_value = tf.assign(value,new_value)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for _ in range(10):
sess.run(update_sums)
sess.run(update_value)
print(sess.run(sums))
55
佔位符
佔位符 placeholder
-
TensorFlow中的Variable變數型別,在定義時需要初始化,但有些變數
定義時並不知道其數值,只有當真正開始執行程式時,才由外部輸入,
比如訓練資料,這時候需要用到佔位符 -
tf.placeholder佔位符,是TensorFlow中特有的一種資料結構,類似動
態變數,函式的引數、或者C語言或者Python語言中格式化輸出時的“%”
佔位符 -
TensorFlow佔位符Placeholder,先定義一種資料,其引數為資料的
Type和Shape
佔位符Placeholder的函式介面如下:
tf.placeholder(dtype, shape=None, name=None)
x = tf.placeholder(tf.float32, [2,3], name= 'tx')
# 生成一個2*3的二維2陣列
Feed提交資料和Fetch提取資料
Feed提交資料
如果構建了一個包含placeholder操作的計算圖,當在session中呼叫run方
法時,placeholder佔用的變數必須通過feed_dict引數傳遞進去,否則報錯
a = tf.placeholder(tf.float32,name='a')
b = tf.placeholder(tf.float32,name='b')
c = tf.multiply(a, b, name='c')
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
result = sess.run(c, feed_dict={a: 8.0, b: 3.5})
print(result)
28.0
多個操作可以通過一次Feed完成執行
a = tf.placeholder(tf.float32, name='a')
b = tf.placeholder(tf.float32, name='b')
c = tf.multiply(a,b,name='c')
d = tf.subtract(a,b,name='d')
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
result = sess.run([c,d], feed_dict={a:[8.1,2.0,3.5], b:[1.5,2.0,4.0]})
print(result)
print(result[0])
[array([ 12.15000057, 4. , 14. ], dtype=float32), array([ 6.60000038, 0. , -0.5 ], dtype=float32)]
[ 12.15000057 4. 14. ]
一次返回多個值分別賦給多個變數
a = tf.placeholder(tf.float32, name='a')
b = tf.placeholder(tf.float32, name='b')
c = tf.multiply(a,b,name='c')
d = tf.subtract(a,b,name='d')
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
rc, rd = sess.run([c,d], feed_dict={a:[8.1,2.0,3.5], b:[1.5,2.0,4.0]})
print(rc)
print(rd)
[ 12.15000057 4. 14. ]
[ 6.60000038 0. -0.5 ]