Tensorflow高层封装Estimator-自定义模型

Tensorflow高层封装Estimator除了可以使用DNNClassifier进行简单的模型定义外(https://mp.csdn.net/postedit/83828873),还可以自定义模型。

1.自定义模型并训练

import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#日志信息输入到屏幕
tf.logging.set_verbosity(tf.logging.INFO)
#通过tf.layers来定义模型结构,x为输入张量,is_training指明是否为训练
def lenet(x, is_training):
    x = tf.reshape(x, shape=[-1, 28, 28, 1])
                                  #-1:batch数目不定,28,28,1:图片为但通道大小为28x28

    conv1 = tf.layers.conv2d(x, 32, 5, activation=tf.nn.relu)
    conv1 = tf.layers.max_pooling2d(conv1, 2, 2)

    conv2 = tf.layers.conv2d(conv1, 64, 3, activation=tf.nn.relu)
    conv2 = tf.layers.max_pooling2d(conv2, 2, 2)

    fc1 = tf.contrib.layers.flatten(conv2)
    fc1 = tf.layers.dense(fc1, 1024)
    fc1 = tf.layers.dropout(fc1, rate=0.4, training=is_training)
    return tf.layers.dense(fc1, 10)
#features为输入张量,是一个字典,labels对应正确答案,mode有3种模式分别对应
#train,evaluate,predict
def model_fn(features, labels, mode, params):
#前向传播结果
    predict = lenet(
        features["image"], mode == tf.estimator.ModeKeys.TRAIN)

    if mode == tf.estimator.ModeKeys.PREDICT:#predict模式直接返回结果
        return tf.estimator.EstimatorSpec(
            mode=mode,
            predictions={"result": tf.argmax(predict, 1)})

    loss = tf.reduce_mean(#损失函数
        tf.nn.sparse_softmax_cross_entropy_with_logits(
           logits=predict, labels=labels))

    optimizer = tf.train.GradientDescentOptimizer(#优化函数
        learning_rate=params["learning_rate"])

    train_op = optimizer.minimize(#训练过程
        loss=loss, global_step=tf.train.get_global_step()

    eval_metric_ops = {#评测标准
        "accuracy": tf.metrics.accuracy(
            tf.argmax(predict, 1), labels)
    }

    return tf.estimator.EstimatorSpec(#返回损失函数、训练过程、评测标准
        mode=mode,
        loss=loss,
        train_op=train_op,
        eval_metric_ops=eval_metric_ops)

mnist = input_data.read_data_sets("../../datasets/MNIST_data", one_hot=False)

model_params = {"learning_rate": 0.01}#通过params指定超参数
estimator = tf.estimator.Estimator(model_fn=model_fn, params=model_params)
  
train_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={"image": mnist.train.images},
      y=mnist.train.labels.astype(np.int32),
      num_epochs=None,
      batch_size=128,
      shuffle=True)

estimator.train(input_fn=train_input_fn, steps=30000)#训练模型

 2.在测试数据上测试模型

test_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={"image": mnist.test.images},
      y=mnist.test.labels.astype(np.int32),
      num_epochs=1,
      batch_size=128,
      shuffle=False)

test_results = estimator.evaluate(input_fn=test_input_fn)
accuracy_score = test_results["accuracy"]
print("\nTest accuracy: %g %%" % (accuracy_score*100))

3.预测过程

predict_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={"image": mnist.test.images[:10]},
      num_epochs=1,
      shuffle=False)

predictions = estimator.predict(input_fn=predict_input_fn)
for i, p in enumerate(predictions):
    print("Prediction %s: %s" % (i + 1, p["result"]))

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值