Hike News
Hike News

深度學習-tensorflow基礎-變量作用域

Introduction

  • 目的是為了使代碼更清晰區分功能,使其作用分明
  • 讓tensorboard更清晰顯示代碼運作的模式

創建作用域

  • 使用tf.variable_scope()函數創建作用域
    • scope_name參數:創建指定的名字
    • 其相當於是一種上下文環境
      • with tf.variable_scope("作用域名稱"):
      • 將相對應的操作放置到相對應的作用域中使graph更整齊

未使用作用域

以上一篇文章所使用的線性回歸代碼作為例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import tensorflow as tf

def LinearRegression():
X = tf.random_normal([100,1],mean=1.75,stddev=0.5, name="x_data")
y_true = tf.matmul(X,[[0.7]]) + 0.8

weight = tf.Variable(tf.random_normal([1,1],mean=0.0,stddev=1.0),name="w") #必須用變量定義才能優化(改變)
bias = tf.Variable(0.0,name="bias")

y_predict = tf.matmul(X,weight) + bias

loss = tf.reduce_mean(tf.square(y_true - y_predict))

train_op = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

init_var_op = tf.global_variables_initializer()



with tf.Session() as sess:
sess.run(init_var_op)

print("起始初始化權重:%f, 初始化偏置:%f"%(weight.eval(),bias.eval()))

# 建立事件文件(指定存放文件夾及graph)
FileWriter = tf.summary.FileWriter("./summary/",graph=sess.graph)

for i in range(1000):
sess.run(train_op)
if i%50 == 0:
print("優化%d次後 權重:%f, 優化偏置:%f" % (i,weight.eval(), bias.eval()))

if __name__ == '__main__':
LinearRegression()

result

Imgur

  • 可以看到很多op裸露在外面較雜亂

使用作用域整理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import tensorflow as tf

def LinearRegression():
with tf.variable_scope("data_preparation"):
X = tf.random_normal([100,1],mean=1.75,stddev=0.5, name="x_data")
y_true = tf.matmul(X,[[0.7]]) + 0.8

with tf.variable_scope("LinearRegression_model_build"):
weight = tf.Variable(tf.random_normal([1,1],mean=0.0,stddev=1.0),name="w") #必須用變量定義才能優化(改變)
bias = tf.Variable(0.0,name="bias")
y_predict = tf.matmul(X,weight) + bias

with tf.variable_scope("loss_calculate"):
loss = tf.reduce_mean(tf.square(y_true - y_predict))

with tf.variable_scope("optimize"):
train_op = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

init_var_op = tf.global_variables_initializer()

with tf.Session() as sess:
sess.run(init_var_op)

print("起始初始化權重:%f, 初始化偏置:%f"%(weight.eval(),bias.eval()))

# 建立事件文件(指定存放文件夾及graph)
FileWriter = tf.summary.FileWriter("./summary/",graph=sess.graph)

for i in range(1000):
sess.run(train_op)
if i%50 == 0:
print("優化%d次後 權重:%f, 優化偏置:%f" % (i,weight.eval(), bias.eval()))

if __name__ == '__main__':
LinearRegression()

result

Imgur

  • 產生的graph比未整理過的清爽
  • graph理解性變高,結構調理清楚