2017-09-16 67 views
2

我需要某人的幫助才能向我解釋下面的代碼。我有點新TensorFlow,但我在下面TensorFlow代碼幫助 - 入門示例

import tensorflow as tf 

# Model parameters 
#Why are variables initialize with .3 and -.3 
W = tf.Variable([.3], dtype=tf.float32) 
b = tf.Variable([-.3], dtype=tf.float32) 

中的代碼中定義的特定問題,什麼是X,B,W和Y變量代表什麼?

# Model input and output 
x = tf.placeholder(tf.float32) # this is the input 
linear_model = W * x + b  # this is the linear_model operation 
y = tf.placeholder(tf.float32) # Is this the output we're trying to predict. 

爲什麼代碼將參數值0.01傳遞給GradientDescentOptimizer函數?

# loss - measure how far apart the current model is from the provided data. 
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares 
# optimizer 
optimizer = tf.train.GradientDescentOptimizer(0.01) # Why are we passing the value '0.01' 
train = optimizer.minimize(loss) 

y_train在這裏代表什麼?

# training data 
x_train = [1, 2, 3, 4] # the input variables we know 
y_train = [0, -1, -2, -3] # 

# training loop 
init = tf.global_variables_initializer() # init is a handle to the TensorFlow sub-graph that initializes all the global variables. Until we call sess.run, the variables are unitialized 
sess = tf.Session() # Sesson encapsulates the control and state of the TensorFlow runtime. ITs used to evaluate the nodes, we must run the computational graph within a session 
sess.run(init) # reset values to wrong 
for i in range(1000): 
    sess.run(train, {x: x_train, y: y_train}) 

這裏的變量curr_W,curr_b代表什麼?

# evaluate training accuracy 
# Why does the variables W and b represent? 
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train}) 
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss)) 

的代碼示例來自Tensorflow網站:https://www.tensorflow.org/get_started/get_started#complete_program

回答

0

x,b,W和y變量代表什麼?

這些是模型將要使用的符號變量 - 輸入,輸出和神經網絡參數。 xy是數據,它們不會更改,這就是爲什麼它們被定義爲tf.placeholderWy是可學習的參數(在TF術語可訓練的)。初始值不如這些參數的尺寸重要(實際上,not exactly,但這是一個高級主題)。在此示例中,Wb都是一維的,但通常W是矩陣,而b是矢量。

在一起,所有定義的變量形成所謂的計算圖

爲什麼代碼傳遞參數值爲0.01到 GradientDescentOptimizer函數?

這是的學習率。簡而言之,引擎在優化目標函數loss時所採用的步長。學習率通常接近0,但確切的價值取決於許多因素。實際上,這是研究人工嘗試的常用超參數之一。 0.01似乎是一個很好的起點,因爲它在很多情況下足夠好。

y_train在這裏代表什麼?

x_trainy_train和是訓練數據中,第一個是輸入,而第二個是預期的標籤。在這種情況下,您告訴輸入1應該導致結果0,輸入21等等。希望網絡能夠從這4個例子中找出它應該學習一個「減1」的操作(注意:神經網絡只是一個完美匹配的線性模型)。它被稱爲監督學習

這裏的變量curr_W,curr_b代表什麼?

首先,注意curr_Wcurr_b都是普通的Python變量,而Wb是象徵性的變量。符號變量定義了您的計算組織方式,並在訓練過程中採用不同的值。 curr_Wcurr_b只是這些值之後的一些迭代。基本上,您可以拍攝模型的快照並將其打印出來,以查看神經網絡學到了什麼。結果值-11(幾乎)表示神經網絡成功地進行線性變換。

+0

我的代碼示例中的偏見和重量的值是多少? –

+0

'W'和'b'的初始值分別爲0.3和-0.3,正如答案中所述,這並不重要。然後,在訓練期間,這些值不斷變化(一個很好的練習 - 在每次迭代中打印出來),直到最後,它們接近-1和1。 – Maxim

0

什麼是X,B,W和Y變量代表什麼?

x是輸入,b是這些輸入的偏差,W是這些輸入的權重矩陣,y是這些輸入的目標值。

要訓練一個像這樣的監督學習模型,您需要一些訓練數據。你的是這樣的:

# training data 
x_train = [1, 2, 3, 4] # the input variables we know 
y_train = [0, -1, -2, -3] # 

y_train是對應於輸入值[1,2,3,4]的目標值。

curr_W, curr_b 

分別是您的模型在當前的一輪訓練後的權重和偏差。

+0

感謝您的快速響應。那麼爲什麼y_train只是目標值呢? –

+0

你還想要什麼?這是一個慣例。 x =輸入值,y =相同輸入的相應目標值。 –

+0

我猜你的目標價值是什麼意思?我是這個ML的新手 –