Udacity Deep Learning实战(二)

第二次作业比较简单,实现一个隐含层1024个节点的三层神经网络,选用ReLU作为激活函数,实现作业一中的字母图片分类。实现代码如下,主要是实现computation(dataset, weights, biases)函数,返回logits取代原来的线性回归模型。

hidden_nodes = 1024
batch_size = 128

def computation(dataset, weights, biases):
    weight_sum = tf.add(tf.matmul(dataset, weights[0]), biases[0])
    hidden_layer = tf.nn.relu(weight_sum)
    logits = tf.add(tf.matmul(hidden_layer, weights[1]), biases[1])
    return logits

graph = tf.Graph()
with graph.as_default():

    # Input data. For the training data, we use a placeholder that will be fed
    # at run time with a training minibatch.
    tf_train_dataset = tf.placeholder(tf.float32,
                                    shape=(batch_size, image_size * image_size))
    tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))
    tf_valid_dataset = tf.constant(valid_dataset)
    tf_test_dataset = tf.constant(test_dataset)

    # Variables.
    weights = [tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])), 
             tf.Variable(tf.truncated_normal([hidden_nodes, num_labels]))
            ]
    biases = [tf.Variable(tf.zeros([hidden_nodes])),
            tf.Variable(tf.zeros([num_labels]))]

    # Training computation.
    logits = computation(tf_train_dataset, weights, biases)

    loss = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits))

    # Optimizer.
    optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)

    # Predictions for the training, validation, and test data.
    train_prediction = tf.nn.softmax(logits)
    valid_prediction = tf.nn.softmax(computation(tf_valid_dataset, weights, biases))
    test_prediction = tf.nn.softmax(computation(tf_test_dataset, weights, biases))
def accuracy(predictions, labels):
  return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))
          / predictions.shape[0])

num_steps = 3001

with tf.Session(graph=graph) as session:
    tf.global_variables_initializer().run()
    print("Initialized")
    for step in range(num_steps):

    # Pick an offset within the training data, which has been randomized.
    # Note: we could use better randomization across epochs. 
        offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
        # Generate a minibatch.
        batch_data = train_dataset[offset:(offset + batch_size), :]
        batch_labels = train_labels[offset:(offset + batch_size), :]
        # Prepare a dictionary telling the session where to feed the minibatch.
        # The key of the dictionary is the placeholder node of the graph to be fed,
        # and the value is the numpy array to feed to it.
        feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}
        _, l, predictions = session.run(
          [optimizer, loss, train_prediction], feed_dict=feed_dict)
        if (step % 500 == 0):
            print("Minibatch loss at step %d: %f" % (step, l))
            print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels))
            print("Validation accuracy: %.1f%%" % accuracy(
            valid_prediction.eval(), valid_labels))
    print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels))

结果如下:

Initialized
Minibatch loss at step 0: 333.015137
Minibatch accuracy: 11.7%
Validation accuracy: 42.1%
Minibatch loss at step 500: 17.960817
Minibatch accuracy: 85.2%
Validation accuracy: 77.0%
Minibatch loss at step 1000: 8.218993
Minibatch accuracy: 82.0%
Validation accuracy: 81.0%
Minibatch loss at step 1500: 18.049088
Minibatch accuracy: 71.9%
Validation accuracy: 79.2%
Minibatch loss at step 2000: 3.682401
Minibatch accuracy: 84.4%
Validation accuracy: 81.5%
Minibatch loss at step 2500: 6.891292
Minibatch accuracy: 82.8%
Validation accuracy: 81.2%
Minibatch loss at step 3000: 4.673051
Minibatch accuracy: 84.4%
Validation accuracy: 81.8%
Test accuracy: 89.7%

此外,官方代码教程ipynb里有些代码值得学习一下(基于numpy和TensorFlow的操作):

对numpy的label变量进行OneHot编码
# num_labels是label数目,labels是label具体列表
labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32) # one-hot encoding

计算准确率

def accuracy(predictions, labels):
  return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))
          / predictions.shape[0])

TensorFlow使用placeholder保存训练数据,每次epoch中加载更新;用constant保存验证数据;用随机分布初始化权值

tf_train_dataset = tf.placeholder(tf.float32,
                                    shape=(batch_size, image_size * image_size))
tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))
tf_valid_dataset = tf.constant(valid_dataset)
tf_test_dataset = tf.constant(test_dataset)

# Variables.
weights = tf.Variable(
    tf.truncated_normal([image_size * image_size, num_labels]))
biases = tf.Variable(tf.zeros([num_labels]))

TensorFlow定义计算图,并调用session.run()执行计算图

graph = tf.Graph()
with graph.as_default():
    ...

with tf.Session(graph=graph) as session:
    tf.global_variables_initializer().run()
    ...

TensorFlow实现在每次epoch中基于offset实现minibatch训练

“`

for each epoch

offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
# Generate a minibatch.
batch_data = train_dataset[offset:(offset + batch_size), :]
batch_labels = train_labels[offset:(offset + batch_size), :]
# 用一个字典记录batch_data,key值是graph的placeholder节点(即训练数据),value是label数组
feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}
_, l, predictions = session.run(
  [optimizer, loss, train_prediction], feed_dict=feed_dict)

转自http://blog.csdn.net/draco_mystack/article/details/77388888

猜你喜欢

转载自blog.csdn.net/zsz_shsf/article/details/78935508