如何使用Estimator类输出tensorflow中的最后一层?

时间:2017-09-13 03:44:52

标签: python-3.x tensorflow neural-network

下面的代码使用Tensorflow在python中实现CNN。 如何输出最后一层的功能? 我在网上找到了调用tf.Session()的情况的代码,但下面的代码(改编自TensorFlow教程)不使用tf.Session()。我在return pool2_flat中尝试使用cnn_model_fn,但我收到了错误消息: ValueError: model_fn should return an EstimatorSpec.

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()

1 个答案:

答案 0 :(得分:0)

EstimatorSpec有一个字段predictions,您可以在其中将字符串名称中的字典放到所需模型的任何输出中。