TensorFlow開發Demo

一、模型基本知識

  • 卷積神經網絡

  • 卷積

  • 採樣

二、模型定義

本文以CNN卷積神經網絡原型爲示例:

  • 初始化輸入層,轉換成統一尺寸大小:對於本例子中全部resize成28*28大小

代碼塊

Python

  input_layer = tf.reshape(features["x"], [-1, 28, 28, 1])
  • 定義第一層,卷積層,卷積核大小和數量:卷積核數量32個,每個卷積核大小5✖️5

代碼塊

Python

conv1 = tf.layers.conv2d(
      inputs=input_layer,
      filters=32,
      kernel_size=[5, 5],
      padding="same",
      activation=tf.nn.relu)

  • 定義第二層,池化層,所採用的池化方法爲最大池化方法,池化的batch大小爲:2✖️2,步長爲2

代碼塊

Python

  pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)
  • 定義第三層,卷積層,卷積核大小和數量:卷積核數量64個,每個卷積核大小5✖️5

代碼塊

Python

conv2 = tf.layers.conv2d(
      inputs=pool1,
      filters=64,
      kernel_size=[5, 5],
      padding="same",
      activation=tf.nn.relu)
  • 定義第四層,池化層,所採用的池化方法爲最大池化方法,池化的batch大小爲:2✖️2,步長爲2

代碼塊

Python

  pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)

  • 定義第五層,全連接層,將第四層降採樣之後的數據resize層7*7*64以爲向量大小

代碼塊

Python

 # Dense Layer
  pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 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 Layer
  logits = tf.layers.dense(inputs=dropout, units=10) 
  • 定義第五層,利用softmax算法計算預測每個類別的概率:

---訓練樣本,更新損失函數,更新梯度

---測試樣本,返回預測結果值

三、訓練數據代碼

過程:讀取數據-》構建結果評估模型-》訓練樣本-》預測樣本

# -*- coding: UTF-8 -*-
import tensorflow as tf
import numpy as np
import cnn_mnist
import input_data
# Load training and eval data
#mnist = tf.contrib.learn.datasets.load_dataset("/Users/dingjie/PycharmProjects/baby-robot/minist/MNIST-data/")
#mnist = tf.contrib.learn.datasets.load_dataset("MNIST-data")
mnist = input_data.read_data_sets("MNIST_data/")
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 = tf.estimator.Estimator(
    model_fn=cnn_mnist.cnn_model_fn, model_dir="model")
# Set up logging for predictions
tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(
    tensors=tensors_to_log, every_n_iter=50)
# Train the model  ---初始化訓練數據
train_input_fn = tf.estimator.inputs.numpy_input_fn(
    x={"x": train_data},
    y=train_labels,
    batch_size=100,
    num_epochs=None,
    shuffle=True)

#訓練數據,得到訓練模型
mnist_classifier.train(
    input_fn=train_input_fn,
    steps=20000,
    hooks=[logging_hook])

# Evaluate the model and print results   ----結果評估
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
    x={"x": eval_data},
    y=eval_labels,
    num_epochs=1,
    shuffle=False)
eval_results = mnist_classifier.evaluate(input_fn=eval_input_fn)
print(eval_results)

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章