【Tensorflow 1.X】不同接口下实现MNIST手写数字识别

1. 确定CUDA和Driver版本,安装相应的Tensorflow版本,避免软件冲突。

例如:Ubuntu 16.04 (CUDA 9.0 for TensorFlow < 1.13.0)

2. 为了加快安装速度,更换了国内Conda源

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --setshow_channel_urls yes

3. keras接口代码实现

代码:

import tensorflow as tf
def imp_keras():
    mnist = tf.keras.datasets.mnist
    (x_train,y_train),(x_test,y_test)=mnist.load_data()
    x_train, x_test = x_train / 255.0, x_test / 255.0
    model = tf.keras.models.Sequential([
        tf.keras.layers.Flatten(input_shape=(28,28)),
        tf.keras.layers.Dense(128,activation='relu'),
        tf.keras.layers.Dropout(0.2),
        tf.keras.layers.Dense(10,activation='softmax')
    ])
    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])
    model.fit(x_train,y_train,epochs=5)
    model.evaluate(x_test,y_test,verbose=2)

if __name__ == '__main__':
    imp_keras()

结果:

Epoch 1/5
2020-02-16 21:15:57.492961: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use
: SSE4.1 SSE4.2 AVX AVX2 FMA2020-02-16 21:15:57.772672: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1392] Found device 0 with properties: 
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:03:00.0
totalMemory: 10.91GiB freeMemory: 10.72GiB
2020-02-16 21:15:57.965191: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1392] Found device 1 with properties: 
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:04:00.0
totalMemory: 10.92GiB freeMemory: 10.76GiB
2020-02-16 21:15:58.134077: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1392] Found device 2 with properties: 
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:81:00.0
totalMemory: 10.92GiB freeMemory: 10.76GiB
2020-02-16 21:15:58.135430: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1471] Adding visible gpu devices: 0, 1, 2
2020-02-16 21:15:58.977674: I tensorflow/core/common_runtime/gpu/gpu_device.cc:952] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-02-16 21:15:58.977745: I tensorflow/core/common_runtime/gpu/gpu_device.cc:958]      0 1 2 
2020-02-16 21:15:58.977754: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 0:   N Y N 
2020-02-16 21:15:58.977758: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 1:   Y N N 
2020-02-16 21:15:58.977763: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 2:   N N N 
2020-02-16 21:15:58.979327: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1084] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 1
0375 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:03:00.0, compute capability: 6.1)2020-02-16 21:15:59.157664: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1084] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:1 with 1
0411 MB memory) -> physical GPU (device: 1, name: GeForce GTX 1080 Ti, pci bus id: 0000:04:00.0, compute capability: 6.1)2020-02-16 21:15:59.335279: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1084] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:2 with 1
0411 MB memory) -> physical GPU (device: 2, name: GeForce GTX 1080 Ti, pci bus id: 0000:81:00.0, compute capability: 6.1)60000/60000 [==============================] - 11s 182us/step - loss: 0.3033 - acc: 0.9123
Epoch 2/5
60000/60000 [==============================] - 8s 133us/step - loss: 0.1456 - acc: 0.9578
Epoch 3/5
60000/60000 [==============================] - 8s 133us/step - loss: 0.1122 - acc: 0.9662
Epoch 4/5
60000/60000 [==============================] - 8s 132us/step - loss: 0.0917 - acc: 0.9717
Epoch 5/5
60000/60000 [==============================] - 8s 134us/step - loss: 0.0757 - acc: 0.9760

4. layers 接口实现

代码:

def cnn_model_fn(features,labels,mode):
    input_layer = tf.reshape(features["x"],[-1,28,28,1])
    conv1 = tf.layers.conv2d(inputs=input_layer,filters=32,kernel_size=[5,5],padding="same",activation=tf.nn.relu)
    pool1 = tf.layers.max_pooling2d(inputs=conv1,pool_size=[2,2],strides=2)
    conv2 = tf.layers.conv2d(inputs=pool1,filters=64,kernel_size=[5,5])
    pool2 = tf.layers.max_pooling2d(inputs=conv2,pool_size=[2,2],strides=2)
    print(pool2.shape)
    pool2_flat = tf.reshape(pool2,[-1,5*5*64])
    dense = tf.layers.dense(inputs=pool2_flat,units=1024,activation=tf.nn.relu)
    dropout = tf.layers.dropout(inputs=dense,rate=0.4,training=mode==tf.estimator.ModeKeys.TRAIN)
    logits = tf.layers.dense(inputs=dropout,units=10)
    predictions={
        "classes":tf.argmax(input=logits,axis=1),
        "prob":tf.nn.softmax(logits,name='softmax_tensor')
    }
    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode=mode,predictions=predictions)
    loss = tf.losses.sparse_softmax_cross_entropy(labels=labels,logits=logits)
    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)
    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 imp_layers():
    mnist = tf.keras.datasets.mnist
    (x_train, y_train), (x_test, y_test) = mnist.load_data()

    mnist_classifier = tf.estimator.Estimator(model_fn=cnn_model_fn,model_dir='./mnist_model')
    tensors_to_log = {"prob": "softmax_tensor"}
    logging_hook = tf.train.LoggingTensorHook(
        tensors=tensors_to_log, every_n_iter=50)
    # 模型训练
    train_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={"x": x_train.astype(np.float32)},
        y=y_train.astype(np.int32),
        batch_size=100,
        num_epochs=10,
        shuffle=True)
    mnist_classifier.train(
        input_fn=train_input_fn,
        steps=20000,
        hooks=[logging_hook])
    # 评估模型并输出结果
    eval_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={"x": x_test.astype(np.float32)},
        y=y_test.astype(np.int32),
        num_epochs=1,
        shuffle=False)
    eval_results = mnist_classifier.evaluate(input_fn=eval_input_fn)
    print(eval_results)

if __name__ == '__main__':
    imp_layers()

结果:

2020-02-17 17:56:48.168697: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use
: SSE4.1 SSE4.2 AVX AVX2 FMA2020-02-17 17:56:48.435044: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1392] Found device 0 with properties: 
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:03:00.0
totalMemory: 10.91GiB freeMemory: 10.72GiB
2020-02-17 17:56:48.596724: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1392] Found device 1 with properties: 
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:04:00.0
totalMemory: 10.92GiB freeMemory: 10.76GiB
2020-02-17 17:56:48.755223: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1392] Found device 2 with properties: 
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:81:00.0
totalMemory: 10.92GiB freeMemory: 10.76GiB
2020-02-17 17:56:48.756597: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1471] Adding visible gpu devices: 0, 1, 2
2020-02-17 17:56:49.590519: I tensorflow/core/common_runtime/gpu/gpu_device.cc:952] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-02-17 17:56:49.590566: I tensorflow/core/common_runtime/gpu/gpu_device.cc:958]      0 1 2 
2020-02-17 17:56:49.590574: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 0:   N Y N 
2020-02-17 17:56:49.590579: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 1:   Y N N 
2020-02-17 17:56:49.590583: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 2:   N N N 
2020-02-17 17:56:49.592092: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1084] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 1
0375 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:03:00.0, compute capability: 6.1)2020-02-17 17:56:49.772996: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1084] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:1 with 1
0411 MB memory) -> physical GPU (device: 1, name: GeForce GTX 1080 Ti, pci bus id: 0000:04:00.0, compute capability: 6.1)2020-02-17 17:56:49.950974: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1084] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:2 with 1
0411 MB memory) -> physical GPU (device: 2, name: GeForce GTX 1080 Ti, pci bus id: 0000:81:00.0, compute capability: 6.1)(?, 5, 5, 64)
2020-02-17 17:57:21.350067: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1471] Adding visible gpu devices: 0, 1, 2
2020-02-17 17:57:21.350221: I tensorflow/core/common_runtime/gpu/gpu_device.cc:952] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-02-17 17:57:21.350240: I tensorflow/core/common_runtime/gpu/gpu_device.cc:958]      0 1 2 
2020-02-17 17:57:21.350252: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 0:   N Y N 
2020-02-17 17:57:21.350262: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 1:   Y N N 
2020-02-17 17:57:21.350273: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 2:   N N N 
2020-02-17 17:57:21.351181: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1084] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 1
0375 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:03:00.0, compute capability: 6.1)2020-02-17 17:57:21.351312: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1084] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:1 with 1
0411 MB memory) -> physical GPU (device: 1, name: GeForce GTX 1080 Ti, pci bus id: 0000:04:00.0, compute capability: 6.1)2020-02-17 17:57:21.357179: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1084] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:2 with 1
0411 MB memory) -> physical GPU (device: 2, name: GeForce GTX 1080 Ti, pci bus id: 0000:81:00.0, compute capability: 6.1){'accuracy': 0.9835, 'loss': 0.048244454, 'global_step': 6600}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

穆友航

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值