2016-02-14 159 views
7

我使用TensorFlow創建了一個具有金字塔結構的隱藏層神經網絡。下面是代碼:使用TensorFlow進行驗證和測試

num_classes = 10 
image_size = 28 

#Read the data 
train_dataset, train_labels, valid_dataset, valid_labels, test_dataset, test_labels = OpenDataSets("...") 
#Create and convert what is needed. 
tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) 
tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) 
tf_valid_dataset = tf.constant(valid_dataset) 
tf_test_dataset = tf.constant(test_dataset) 

#Then I create the NN. 
Wh = tf.Variable(tf.truncated_normal([image_size * image_size, image_size * image_size/2])) 
bh = tf.Variable(tf.truncated_normal([image_size * image_size/2])) 
hidden = tf.nn.relu(tf.matmul(tf_train_dataset, Wh) + bh) 

Wout = tf.Variable(tf.truncated_normal([image_size * image_size/2, num_labels])) 
bout = tf.Variable(tf.truncated_normal([num_labels])) 
logits = tf.nn.relu(tf.matmul(hidden, Wout) + bout) 

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) 
optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) 
train_prediction = tf.nn.softmax(logits) 

現在我訓練我的NN:

with tf.Session(graph=graph) as session: 
    tf.initialize_all_variables().run() 
    for step in range(1000): 
     offset = (step * batch_size) % (train_labels.shape[0] - batch_size) 
     batch_data = train_dataset[offset:(offset + batch_size), :] 
     batch_labels = train_labels[offset:(offset + batch_size), :] 
     feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} 
     _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) 

現在我想驗證和培訓後測試我的NN。但我不知道如何創建新的feed_dict並使用session.run來驗證/測試。

感謝您的幫助!

回答

8

您將首先創建適當的驗證/測試張量函數。對於單層MPL,它涉及與權重和偏差相加的嵌套(並且因爲您在原始模型中有這些偏差,所以也是Relu的)。定義這些右下方您的火車預測

valid_prediction = tf.nn.softmax(
         tf.nn.relu(tf.matmul(
         tf.nn.relu(tf.matmul(tf_valid_dataset, Wh) + bh)), Wout) + bout))) 
test_prediction = tf.nn.softmax(
         tf.nn.relu(tf.matmul(
         tf.nn.relu(tf.matmul(tf_test_dataset, Wh) + bh)), Wout) + bout))) 

這些表達式其實在你的代碼中定義,logit變量相當一致僅用tf_valid_datasettf_test_dataset分別。您可以創建中間變量來簡化它們。您將不得不創建一些驗證/測試功能來測試準確性。最簡單的就是測試最可能預測的類別(粗略地說,錯分類錯誤)。在你的圖表/會話之外定義它。

def accuracy(predictions, labels): 
     pred_class = np.argmax(predictions, 1) 
     true_class = np.argmax(labels, 1) 
     return (100.0 * np.sum(pred_class == true_class)/predictions.shape[0]) 

之後,您可以簡單地在相同的session/feed_dict中傳遞此精度函數來計算驗證/測試分數。

print 'Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels) 
print 'Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels) 
+1

非常感謝您的回答。所以,如果我理解正確,我必須創建另外兩個NN,這些NN實際上是指向我的原始NN的指針,因爲它們使用完全相同的可訓練權重。我對嗎? – FiReTiTi

+1

我希望使用我的原始NN具有不同的輸入。 – FiReTiTi

+1

否否,您將使用完全相同的網絡進行驗證 - 只需在同一網絡中定義兩個函數並在同一會話中調用準確性() –