2017-07-06 123 views
0

我是新來tensorflow和我有一個快速的問題,這是我的模式,爲MNISTMNIST圖像預測模型

def neural_network_model(data): 

    hidden_1_layer = {'weights': tf.Variable(tf.random_normal([784, n_nodes_hl1])), 
         'biases': tf.Variable(tf.random_normal([n_nodes_hl1]))} 

    hidden_2_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])), 
         'biases': tf.Variable(tf.random_normal([n_nodes_hl2]))} 

    hidden_3_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])), 
         'biases': tf.Variable(tf.random_normal([n_nodes_hl3]))} 

    output_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])), 
        'biases': tf.Variable(tf.random_normal([n_classes])), } 
    l1 = tf.add(
     tf.matmul(
      data, 
      hidden_1_layer['weights']), 
     hidden_1_layer['biases']) 
    l1 = tf.nn.relu(l1) 

    l2 = tf.add(
     tf.matmul(
      l1, 
      hidden_2_layer['weights']), 
     hidden_2_layer['biases']) 
    l2 = tf.nn.relu(l2) 

    l3 = tf.add(
     tf.matmul(
      l2, 
      hidden_3_layer['weights']), 
     hidden_3_layer['biases']) 
    l3 = tf.nn.relu(l3) 

    output = tf.matmul(l3, output_layer['weights']) + output_layer['biases'] 

    return output 

我的問題是,這種功能代表了輸入輸出值「數據的代碼'?或者這個函數代表了一個完整的模型,將用於測試/預測訓練後的圖像?

這裏是我用於特定圖像的預測的代碼:

prediction=neural_network_model(mnist_training_data_set) 
p=tf.argmax(prediction,1) 
print(p.eval(feed_dict={x: i}, session=sess)) 

所以在這裏我很困惑,即函數是否是一個模型或僅返回預測輸出。任何人都可以解釋,謝謝

+0

此代碼是否運行?你能以一種讓我運行它的格式提供完整的代碼嗎? (例如Github) – finbarr

+0

是的代碼工作,但我的問題是關於模型製作 –

+0

這個功能據說是訓練模型以及輸入圖像的輸出嗎? –

回答

2

該函數創建模型並將其添加到計算圖。預計輸出將由p.eval(feed_dict={x: i}, session=sess)行返回。

因此,該函數返回模型的輸出層,這將用於做出預測。可以說,你可以稱之爲「模型」,但我認爲將會話變量稱爲「模型」會更好。

+0

這是什麼意思? prediction = neural_network_model(mnist_training_data_set) –

+0

預測變量得到什麼?函數調用後 –

+0

這意味着你所預測的值等於'output',它是你在函數內定義的神經網絡的輸出層。 – finbarr