Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
96 lines (80 sloc) 3.46 KB
import os
import numpy as np
import tensorflow as tf
import time
import utils
# Define paramaters for the model
learning_rate = 0.01
batch_size = 128
n_epochs = 30
n_train = 60000
n_test = 10000
# Step 1: 读入数据
mnist_folder = 'data/mnist'
# 我已经下载过,就不用再次下载
#utils.download_mnist(mnist_folder)
# 这里把二维的图片展开成一维的向量
train, val, test = utils.read_mnist(mnist_folder, flatten=True)
# Step 2: 创建 dataset 和 iterator
train_data = tf.data.Dataset.from_tensor_slices(train)
train_data = train_data.shuffle(10000) # if you want to shuffle your data
train_data = train_data.batch(batch_size)
test_data = tf.data.Dataset.from_tensor_slices(test)
test_data = test_data.batch(batch_size)
iterator = tf.data.Iterator.from_structure(train_data.output_types,
train_data.output_shapes)
img, label = iterator.get_next()
train_init = iterator.make_initializer(train_data) # initializer for train_data
test_init = iterator.make_initializer(test_data) # initializer for test_data
# Step 3: 创建 weights 和 bias
# w is initialized to random variables with mean of 0, stddev of 0.01
# b is initialized to 0
# shape of w depends on the dimension of X and Y so that Y = tf.matmul(X, w)
# shape of b depends on Y
w = tf.compat.v1.get_variable(name='weights', shape=(784, 10), initializer=tf.random_normal_initializer(0, 0.01))
b = tf.compat.v1.get_variable(name='bias', shape=(1, 10), initializer=tf.zeros_initializer())
# Step 4: 构造模型
# the model that returns the logits.
# this logits will be later passed through softmax layer
# 这里的 logits 就是神经网络的一层输出,后面会连接 softmax 用于多分类
logits = tf.matmul(img, w) + b
# Step 5: 确定损失函数
# use cross entropy of softmax of logits as the loss function
entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=label, name='entropy')
loss = tf.reduce_mean(entropy, name='loss') # computes the mean over all the examples in the batch
# Step 6: 确定训练步骤
# using gradient descent with learning rate of 0.01 to minimize loss
optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate).minimize(loss)
# Step 7: 用测试集计算准确率
preds = tf.nn.softmax(logits)
correct_preds = tf.equal(tf.argmax(preds, 1), tf.argmax(label, 1))
accuracy = tf.reduce_sum(tf.cast(correct_preds, tf.float32))
writer = tf.compat.v1.summary.FileWriter('data/graphs/logreg', tf.compat.v1.get_default_graph())
with tf.compat.v1.Session() as sess:
start_time = time.time()
sess.run(tf.compat.v1.global_variables_initializer())
# train the model n_epochs times
for i in range(n_epochs):
sess.run(train_init) # drawing samples from train_data
total_loss = 0
n_batches = 0
try:
while True:
_, l = sess.run([optimizer, loss])
total_loss += l
n_batches += 1
except tf.errors.OutOfRangeError:
pass
print('Average loss epoch {0}: {1}'.format(i, total_loss / n_batches))
print('Total time: {0} seconds'.format(time.time() - start_time))
# test the model
sess.run(test_init) # drawing samples from test_data
total_correct_preds = 0
try:
while True:
accuracy_batch = sess.run(accuracy)
total_correct_preds += accuracy_batch
except tf.errors.OutOfRangeError:
pass
print('Accuracy {0}'.format(total_correct_preds / n_test))
writer.close()
You can’t perform that action at this time.