2016-02-14 160 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

否否,您将使用完全相同的网络进行验证 - 只需在同一网络中定义两个函数并在同一会话中调用准确性() –