2017-09-13 151 views
0

下面的代碼使用Tensorflow在python中實現CNN。 如何輸出最後一層的特徵? 我在線查找代碼tf.Session()被調用的情況,但下面的代碼(改編自TensorFlow教程)不使用tf.Session()。我return pool2_flat試圖在cnn_model_fn,但我得到的錯誤: ValueError: model_fn should return an EstimatorSpec.如何使用Estimator類輸出張量流中的最後一層?

import ... 

def cnn_model_fn(features, labels, mode): 
    # Input Layer 
    input_layer = tf.reshape(features["x"], [-1, 6, 40, 1]) 

    # Convolutional Layer #1 
    conv1 = tf.layers.conv2d(inputs=input_layer, filters=32, kernel_size=[3, 3], 
          padding="same", activation=tf.nn.relu) 

    # Pooling Layer #1 
    pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2) 

    # Flatten tensor 
    pool2_flat = tf.reshape(pool1, [-1, 20 * 3 * 64]) 

    # Dense Layer with 1024 neurons 
    dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu) 

    # Add dropout operation; 0.6 probability that element will be kept 
    dropout = tf.layers.dropout(
     inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN) 

    # Logits layer 
    logits = tf.layers.dense(inputs=dropout, units=10) 

    predictions = {"classes": tf.argmax(input=logits, axis=1), 
     "probabilities": tf.nn.softmax(logits, name="softmax_tensor") 
    } 
    if mode == tf.estimator.ModeKeys.PREDICT: 
     return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions) 

    # Calculate Loss (for both TRAIN and EVAL modes) 
    onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=10) 
    loss = tf.losses.softmax_cross_entropy(
     onehot_labels=onehot_labels, logits=logits) 

    # Configure the Training Op (for TRAIN mode) 
    if mode == tf.estimator.ModeKeys.TRAIN: 
     optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001) 
     train_op = optimizer.minimize(
      loss=loss, 
      global_step=tf.train.get_global_step()) 
     return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op) 

    # Add evaluation metrics (for EVAL mode) 
    eval_metric_ops = { 
     "accuracy": tf.metrics.accuracy(
      labels=labels, predictions=predictions["classes"])} 

    return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metric_ops) 


def main(unused_argv): 
    # Load training and eval data 
    ... 

    # Create the Estimator 
    convnetVoc_classifier = tf.estimator.Estimator(
     model_fn=cnn_model_fn, model_dir=TMP_FOLDER) 

    # Train the model 
    train_input_fn = tf.estimator.inputs.numpy_input_fn(
     x={"x": train_data}, 
     y=train_labels, 
     num_epochs=None, 
     shuffle=True) 
    convnetVoc_classifier.train(
     input_fn=train_input_fn, 
     steps=1) 

    # Evaluate model's accuracy & print results. 
    test_input_fn = tf.estimator.inputs.numpy_input_fn(
     x={"x": eval_data}, 
     y=eval_labels, 
     num_epochs=1, 
     shuffle=False) 
    eval_results = convnetVoc_classifier.evaluate(input_fn=test_input_fn) 
    print(eval_results) 

if __name__ == "__main__": 
    tf.app.run() 

回答

0

EstimatorSpec有一個字段predictions,在那裏你可以把從字符串名稱的字典到你想要的模型的任何輸出。

相關問題