我目前正在計劃我的第一個Conv。 NN在Tensorflow中的實現,並且已經閱讀了Tensorflow的website上提供的許多教程以獲取見解。TensorFlow:tf.layers vs低級API
看來,有兩種基本的方法來創建自定義CNN:
1)使用Tensorflow層模塊tf.layers
,這是「高級API」。使用此方法,您可以定義一個由tf.layers
對象組成的模型定義函數,並在主函數中實例化一個tf.learn.Estimator
,將模型定義函數傳遞給它。從這裏,可以分別在Estimator
對象上調用fit()
和evaluate()
方法,該對象分別進行訓練和驗證。鏈接:https://www.tensorflow.org/tutorials/layers。下面主要功能:
def main(unused_argv):
# Load training and eval data
mnist = learn.datasets.load_dataset("mnist")
train_data = mnist.train.images # Returns np.array
train_labels = np.asarray(mnist.train.labels, dtype=np.int32)
eval_data = mnist.test.images # Returns np.array
eval_labels = np.asarray(mnist.test.labels, dtype=np.int32)
# Create the Estimator
mnist_classifier = learn.Estimator(
model_fn=cnn_model_fn, model_dir="/tmp/mnist_convnet_model")
# Set up logging for predictions
# Log the values in the "Softmax" tensor with label "probabilities"
tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(
tensors=tensors_to_log, every_n_iter=50)
# Train the model
mnist_classifier.fit(
x=train_data,
y=train_labels,
batch_size=100,
steps=20000,
monitors=[logging_hook])
# Configure the accuracy metric for evaluation
metrics = {
"accuracy":
learn.MetricSpec(
metric_fn=tf.metrics.accuracy, prediction_key="classes"),
}
# Evaluate the model and print results
eval_results = mnist_classifier.evaluate(
x=eval_data, y=eval_labels, metrics=metrics)
print(eval_results)
完整代碼here
2)使用Tensorflow的 「低級API」,其中層以定義功能定義。在這裏,圖層是手動定義的,用戶必須手動執行許多計算。在主要功能中,用戶啓動tf.Session()
,並使用循環手動配置培訓/驗證。鏈接:https://www.tensorflow.org/get_started/mnist/pros。以下主要功能:
def main(_):
# Import data
mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
# Create the model
x = tf.placeholder(tf.float32, [None, 784])
# Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, 10])
# Build the graph for the deep net
y_conv, keep_prob = deepnn(x)
with tf.name_scope('loss'):
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y_,
logits=y_conv)
cross_entropy = tf.reduce_mean(cross_entropy)
with tf.name_scope('adam_optimizer'):
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
with tf.name_scope('accuracy'):
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
correct_prediction = tf.cast(correct_prediction, tf.float32)
accuracy = tf.reduce_mean(correct_prediction)
graph_location = tempfile.mkdtemp()
print('Saving graph to: %s' % graph_location)
train_writer = tf.summary.FileWriter(graph_location)
train_writer.add_graph(tf.get_default_graph())
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(20000):
batch = mnist.train.next_batch(50)
if i % 100 == 0:
train_accuracy = accuracy.eval(feed_dict={
x: batch[0], y_: batch[1], keep_prob: 1.0})
print('step %d, training accuracy %g' % (i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
print('test accuracy %g' % accuracy.eval(feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
的完整代碼here
我的困境是,我喜歡定義使用tf.layers
(選項1)神經網絡的簡單,但我想培訓的定製是「低級API」(選項2)提供。具體來說,當使用tf.layers
實現時,是否有一種方法可以每n次迭代訓練報告驗證準確度?或者更一般地說,我可以使用tf.Session()
進行培訓/驗證,還是僅限於使用tf.learn.Estimator
的fit()
和evaluate()
方法?
在所有培訓完成後,人們會希望獲得最終評估分數,這似乎很奇怪,因爲我認爲整個驗證的重點是跟蹤培訓期間的網絡進展。否則,驗證和測試之間會有什麼區別?
任何幫助,將不勝感激。