import tensorflow as tf
import input_data
import math
import os
import csv
from tqdm import tqdm
layer_sizes = [784, 1000, 500, 250, 250, 250, 10]
L = len(layer_sizes) - 1 # number of layers
num_examples = 60000
num_epochs = 150
num_labeled = 100
starter_learning_rate = 0.02
decay_after = 15 # epoch after which to begin learning rate decay 15个epoch之后LR降低
batch_size = 100
num_iter = (num_examples/batch_size) * num_epochs # number of loop iterations
inputs = tf.placeholder(tf.float32, shape=(None, layer_sizes[0]))
outputs = tf.placeholder(tf.float32)
def bi(inits, size, name):
return tf.Variable(inits * tf.ones([size]), name=name)
def wi(shape, name):
return tf.Variable(tf.random_normal(shape, name=name)) / math.sqrt(shape[0])
# 把lay_sizes打包成元组列表[(784,1000),(1000,500).........]
# shapes = zip(layer_sizes[:-1], layer_sizes[1:]) # shapes of linear layers
shapes = list(zip(list(layer_sizes)[:-1], list(layer_sizes[1:])))
weights = {'W': [wi(s, "W") for s in shapes], # Encoder weights
'V': [wi(s[::-1], "V") for s in shapes], # Decoder weights
# batch normalization parameter to shift the normalized value
'beta': [bi(0.0, layer_sizes[l+1], "beta") for l in range(L)],
# batch normalization parameter to scale the normalized value
'gamma': [bi(1.0, layer_sizes[l+1], "beta") for l in range(L)]}
noise_std = 0.3 # scaling factor for noise used in corrupted encoder
# hyperparameters that denote the importance of each layer 每层的权重
denoising_cost = [1000.0, 10.0, 0.10, 0.10, 0.10, 0.10, 0.10]
join = lambda l, u: tf.concat([l, u], 0) # 组合tensor
# 制造数据,有监督大小是bacth_size,无监督大小是所有样本减去bacth_size
labeled = lambda x: tf.slice(x, [0, 0], [batch_size, -1]) if x is not None else x
unlabeled = lambda x: tf.slice(x, [batch_size, 0], [-1, -1]) if x is not None else x
split_lu = lambda x: (labeled(x), unlabeled(x))
training = tf.placeholder(tf.bool)
# to calculate the moving averages of mean and variance 计算滑动平均
# 滑动平均更新参数依据公式:shadow_variable(cur) = decay * shadow_variable(pre) + (1 - decay) * variable
# 其中,decay控制更新速率,一般取值0.9以上,值越大,更新越慢,值越稳定;
# variable是当前变量的值;shadow_variable(pre)为上一次更新参数值;shadow_variable(cur)当前更新参数值。
ewma = tf.train.ExponentialMovingAverage(decay=0.99)
bn_assigns = [] # this list stores the updates to be made to average mean and variance
# 批归一化处理
# 1.加快训练速度
# 2.可以省去dropout,L1, L2等正则化处理方法
# 3.提高模型训练精度
def batch_normalization(batch, mean=None, var=None):
if mean is None or var is None:
mean, var = tf.nn.moments(batch, axes=[0]) # 计算平均值和方差
return (batch - mean) / tf.sqrt(var + tf.constant(1e-10))
# average mean and variance of all layers 占位list[tf.Variable()]
running_mean = [tf.Variable(tf.constant(0.0, shape=[l]), trainable=False) for l in layer_sizes[1:]]
running_var = [tf.Variable(tf.constant(1.0, shape=[l]), trainable=False) for l in layer_sizes[1:]]
def update_batch_normalization(batch, l):
"batch normalize + update average mean and variance of layer l"
mean, var = tf.nn.moments(batch, axes=[0])
assign_mean = running_mean[l-1].assign(mean) # 赋值
assign_var = running_var[l-1].assign(var)
bn_assigns.append(ewma.apply([running_mean[l-1], running_var[l-1]]))
with tf.control_dependencies([assign_mean, assign_var]):
return (batch - mean) / tf.sqrt(var + 1e-10)
def encoder(inputs, noise_std):
h = inputs + tf.random_normal(tf.shape(inputs)) * noise_std # add noise to input
d = {} # to store the pre-activation, activation, mean and variance for each layer
# The data for labeled and unlabeled examples are stored separately
d['labeled'] = {'z': {}, 'm': {}, 'v': {}, 'h': {}}
d['unlabeled'] = {'z': {}, 'm': {}, 'v': {}, 'h': {}}
d['labeled']['z'][0], d['unlabeled']['z'][0] = split_lu(h)
for l in range(1, L+1):
print("Layer ", l, ": ", layer_sizes[l-1], " -> ", layer_sizes[l])
d['labeled']['h'][l-1], d['unlabeled']['h'][l-1] = split_lu(h)
z_pre = tf.matmul(h, weights['W'][l-1]) # pre-activation
z_pre_l, z_pre_u = split_lu(z_pre) # split labeled and unlabeled examples
m, v = tf.nn.moments(z_pre_u, axes=[0])
# if training:
def training_batch_norm():
# Training batch normalization
# batch normalization for labeled and unlabeled examples is performed separately
if noise_std > 0:
# Corrupted encoder
# batch normalization + noise
z = join(batch_normalization(z_pre_l), batch_normalization(z_pre_u, m, v))
z += tf.random_normal(tf.shape(z_pre)) * noise_std
else:
# Clean encoder
# batch normalization + update the average mean and variance using batch mean and variance of labeled examples
z = join(update_batch_normalization(z_pre_l, l), batch_normalization(z_pre_u, m, v))
return z
# else:
def eval_batch_norm():
# Evaluation batch normalization
# obtain average mean and variance and use it to normalize the batch
mean = ewma.average(running_mean[l-1]) # 返回平均值是running_mean[l-1]的变量
var = ewma.average(running_var[l-1]) # 返回平均值是running_var[l-1]的变量
z = batch_normalization(z_pre, mean, var)
# Instead of the above statement, the use of the following 2 statements containing a typo
# consistently produces a 0.2% higher accuracy for unclear reasons.
# m_l, v_l = tf.nn.moments(z_pre_l, axes=[0])
# z = join(batch_normalization(z_pre_l, m_l, mean, var), batch_normalization(z_pre_u, mean, var))
return z
# perform batch normalization according to value of boolean "training" placeholder:
# training == True --> training_batch_norm else --> eval_batch_norm
z = tf.cond(training, training_batch_norm, eval_batch_norm)
if l == L:
# use softmax activation in output layer
h = tf.nn.softmax(weights['gamma'][l-1] * (z + weights["beta"][l-1]))
else:
# use ReLU activation in hidden layers
h = tf.nn.relu(z + weights["beta"][l-1])
d['labeled']['z'][l], d['unlabeled']['z'][l] = split_lu(z) # batch_normalization之后的输入
d['unlabeled']['m'][l], d['unlabeled']['v'][l] = m, v # save mean and variance of unlabeled examples for decoding
d['labeled']['h'][l], d['unlabeled']['h'][l] = split_lu(h)
return h, d
print("=== Corrupted Encoder ===")
y_c, corr = encoder(inputs, noise_std)
print("=== Clean Encoder ===")
y, clean = encoder(inputs, 0.0) # 0.0 -> do not add noise
print("=== Decoder ===")
def g_gauss(z_c, u, size):
"gaussian denoising function proposed in the original paper"
wi = lambda inits, name: tf.Variable(inits * tf.ones([size]), name=name)
a1 = wi(0., 'a1')
a2 = wi(1., 'a2')
a3 = wi(0., 'a3')
a4 = wi(0., 'a4')
a5 = wi(0., 'a5')
a6 = wi(0., 'a6')
a7 = wi(1., 'a7')
a8 = wi(0., 'a8')
a9 = wi(0., 'a9')
a10 = wi(0., 'a10')
mu = a1 * tf.sigmoid(a2 * u + a3) + a4 * u + a5
v = a6 * tf.sigmoid(a7 * u + a8) + a9 * u + a10
z_est = (z_c - mu) * v + mu
return z_est
# Decoder
z_est = {}
d_cost = [] # to store the denoising cost of all layers
for l in range(L, -1, -1): # 倒序到0
print("Layer ", l, ": ", layer_sizes[l+1] if l+1 < len(layer_sizes) else None, " -> ", layer_sizes[l], ", denoising cost: ", denoising_cost[l])
z, z_c = clean['unlabeled']['z'][l], corr['unlabeled']['z'][l]
m, v = clean['unlabeled']['m'].get(l, 0), clean['unlabeled']['v'].get(l, 1-1e-10)
if l == L:
u = unlabeled(y_c)
else:
u = tf.matmul(z_est[l+1], weights['V'][l])
u = batch_normalization(u)
z_est[l] = g_gauss(z_c, u, layer_sizes[l])
z_est_bn = (z_est[l] - m) / v
# append the cost of this layer to d_cost
d_cost.append((tf.reduce_mean(tf.reduce_sum(tf.square(z_est_bn - z), 1)) / layer_sizes[l]) * denoising_cost[l])
# calculate total unsupervised cost by adding the denoising cost of all layers
u_cost = tf.add_n(d_cost)
y_N = labeled(y_c)
cost = -tf.reduce_mean(tf.reduce_sum(outputs*tf.log(y_N), 1)) # supervised cost
loss = cost + u_cost # total cost
pred_cost = -tf.reduce_mean(tf.reduce_sum(outputs*tf.log(y), 1)) # cost used for prediction
# axis=1的时候,将每一行最大元素所在的索引记录下来,最后返回每一行最大元素所在的索引数组。
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(outputs, 1)) # no of correct predictions
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) * tf.constant(100.0) # tf.cast 数据类型转换
learning_rate = tf.Variable(starter_learning_rate, trainable=False)
train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss)
# add the updates of batch normalization statistics to train_step
bn_updates = tf.group(*bn_assigns) # *bn_assigns 代表[]中所有元素
# tf.control_dependencies 指定某些操作执行的依赖关系
# control_dependencies(control_inputs)返回一个控制依赖的上下文管理器,
# 使用with关键字可以让在这个上下文环境中的操作都在control_inputs 执行
# with g.control_dependencies([a, b, c]):
# d = ...
# e = ...
# `d` and `e` will only run after `a`, `b`, and `c` have executed.
with tf.control_dependencies([train_step]):
train_step = tf.group(bn_updates) # tf.group()组合训练
print("=== Loading Data ===")
mnist = input_data.read_data_sets(r"D:\QEL\Deep Learning\mnist", n_labeled=num_labeled, one_hot=True)
saver = tf.train.Saver()
print("=== Starting Session ===")
sess = tf.Session()
i_iter = 0
ckpt = tf.train.get_checkpoint_state('checkpoints/') # get latest checkpoint (if any)
if ckpt and ckpt.model_checkpoint_path:
# if checkpoint exists, restore the parameters and set epoch_n and i_iter
saver.restore(sess, ckpt.model_checkpoint_path)
epoch_n = int(ckpt.model_checkpoint_path.split('-')[1])
i_iter = (epoch_n+1) * (num_examples/batch_size)
print("Restored Epoch ", epoch_n)
else:
# no checkpoint exists. create checkpoints directory if it does not exist.
if not os.path.exists('checkpoints'):
os.makedirs('checkpoints')
init = tf.global_variables_initializer()
sess.run(init)
print("=== Training ===")
print("Initial Accuracy: ", sess.run(accuracy, feed_dict={inputs: mnist.test.images, outputs: mnist.test.labels, training: False}), "%")
# tqdm进度条显示
for i in tqdm(range(int(i_iter), int(num_iter))):
#这里images有200个,labels有100个
images, labels = mnist.train.next_batch(batch_size)
sess.run(train_step, feed_dict={inputs: images, outputs: labels, training: True})
if (i > 1) and ((i+1) % (num_iter/num_epochs) == 0):
epoch_n = i/(num_examples/batch_size)
if (epoch_n+1) >= decay_after:
# decay learning rate
# learning_rate = starter_learning_rate * ((num_epochs - epoch_n) / (num_epochs - decay_after))
ratio = 1.0 * (num_epochs - (epoch_n+1)) # epoch_n + 1 because learning rate is set for next epoch
ratio = max(0, ratio / (num_epochs - decay_after))
sess.run(learning_rate.assign(starter_learning_rate * ratio))
saver.save(sess, 'checkpoints/model.ckpt', int(epoch_n))
# print "Epoch ", epoch_n, ", Accuracy: ", sess.run(accuracy, feed_dict={inputs: mnist.test.images, outputs:mnist.test.labels, training: False}), "%"
# with open('train_log', 'ab') as train_log:
# # write test accuracy to file "train_log"
# train_log_w = csv.writer(train_log)
# log_i = [epoch_n] + sess.run([accuracy], feed_dict={inputs: mnist.test.images, outputs: mnist.test.labels, training: False})
# train_log_w.writerow(log_i)
print("Final Accuracy: ", sess.run(accuracy, feed_dict={inputs: mnist.test.images, outputs: mnist.test.labels, training: False}), "%")
sess.close()
input_data.py
"""Functions for downloading and reading MNIST data."""
from __future__ import print_function
import gzip
import os
import urllib.request
import numpy
SOURCE_URL = 'http://yann.lecun.com/exdb/mnist/'
def maybe_download(filename, work_directory):
"""Download the data from Yann's website, unless it's already here."""
if not os.path.exists(work_directory):
os.mkdir(work_directory)
filepath = os.path.join(work_directory, filename)
if not os.path.exists(filepath):
# filepath, _ = urllib.urlretrieve(SOURCE_URL + filename, filepath)
filepath, _ = urllib.request.urlretrieve(SOURCE_URL + filename, filepath)
statinfo = os.stat(filepath)
print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.')
return filepath
def _read32(bytestream):
dt = numpy.dtype(numpy.uint32).newbyteorder('>')
return numpy.frombuffer(bytestream.read(4), dtype=dt)[0]
def extract_images(filename):
"""Extract the images into a 4D uint8 numpy array [index, y, x, depth]."""
print('Extracting', filename)
with gzip.open(filename) as bytestream:
magic = _read32(bytestream)
if magic != 2051:
raise ValueError(
'Invalid magic number %d in MNIST image file: %s' %
(magic, filename))
num_images = _read32(bytestream)
rows = _read32(bytestream)
cols = _read32(bytestream)
buf = bytestream.read(rows * cols * num_images)
data = numpy.frombuffer(buf, dtype=numpy.uint8)
data = data.reshape(num_images, rows, cols, 1)
return data
def dense_to_one_hot(labels_dense, num_classes=10):
"""Convert class labels from scalars to one-hot vectors."""
num_labels = labels_dense.shape[0]
index_offset = numpy.arange(num_labels) * num_classes
labels_one_hot = numpy.zeros((num_labels, num_classes))
labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
return labels_one_hot
def extract_labels(filename, one_hot=False):
"""Extract the labels into a 1D uint8 numpy array [index]."""
print('Extracting', filename)
with gzip.open(filename) as bytestream:
magic = _read32(bytestream)
if magic != 2049:
raise ValueError(
'Invalid magic number %d in MNIST label file: %s' %
(magic, filename))
num_items = _read32(bytestream)
buf = bytestream.read(num_items)
labels = numpy.frombuffer(buf, dtype=numpy.uint8)
if one_hot:
return dense_to_one_hot(labels)
return labels
class DataSet(object):
def __init__(self, images, labels, fake_data=False):
if fake_data:
self._num_examples = 10000
else:
assert images.shape[0] == labels.shape[0], (
"images.shape: %s labels.shape: %s" % (images.shape,
labels.shape))
self._num_examples = images.shape[0]
# Convert shape from [num examples, rows, columns, depth]
# to [num examples, rows*columns] (assuming depth == 1)
assert images.shape[3] == 1
images = images.reshape(images.shape[0],
images.shape[1] * images.shape[2])
# Convert from [0, 255] -> [0.0, 1.0].
images = images.astype(numpy.float32)
images = numpy.multiply(images, 1.0 / 255.0)
self._images = images
self._labels = labels
self._epochs_completed = 0
self._index_in_epoch = 0
@property
def images(self):
return self._images
@property
def labels(self):
return self._labels
@property
def num_examples(self):
return self._num_examples
@property
def epochs_completed(self):
return self._epochs_completed
def next_batch(self, batch_size, fake_data=False):
"""Return the next `batch_size` examples from this data set."""
if fake_data:
fake_image = [1.0 for _ in xrange(784)]
fake_label = 0
return [fake_image for _ in xrange(batch_size)], [
fake_label for _ in xrange(batch_size)]
start = self._index_in_epoch
self._index_in_epoch += batch_size
if self._index_in_epoch > self._num_examples:
# Finished epoch
self._epochs_completed += 1
# Shuffle the data
perm = numpy.arange(self._num_examples)
numpy.random.shuffle(perm)
self._images = self._images[perm]
self._labels = self._labels[perm]
# Start next epoch
start = 0
self._index_in_epoch = batch_size
assert batch_size <= self._num_examples
end = self._index_in_epoch
return self._images[start:end], self._labels[start:end]
class SemiDataSet(object):
def __init__(self, images, labels, n_labeled):
self.n_labeled = n_labeled
# Unlabled DataSet
self.unlabeled_ds = DataSet(images, labels)
# Labeled DataSet
self.num_examples = self.unlabeled_ds.num_examples
indices = numpy.arange(self.num_examples)
shuffled_indices = numpy.random.permutation(indices)
images = images[shuffled_indices]
labels = labels[shuffled_indices]
y = numpy.array([numpy.arange(10)[l==1][0] for l in labels])
idx = indices[y==0][:5]
n_classes = y.max() + 1
n_from_each_class = n_labeled / n_classes
i_labeled = []
for c in range(n_classes):
i = indices[y==c][:int(n_from_each_class)]
i_labeled += list(i)
l_images = images[i_labeled]
l_labels = labels[i_labeled]
self.labeled_ds = DataSet(l_images, l_labels)
def next_batch(self, batch_size):
unlabeled_images, _ = self.unlabeled_ds.next_batch(batch_size)
if batch_size > self.n_labeled:
labeled_images, labels = self.labeled_ds.next_batch(self.n_labeled)
else:
labeled_images, labels = self.labeled_ds.next_batch(batch_size)
images = numpy.vstack([labeled_images, unlabeled_images])
return images, labels
def read_data_sets(train_dir, n_labeled = 100, fake_data=False, one_hot=False):
class DataSets(object):
pass
data_sets = DataSets()
if fake_data:
data_sets.train = DataSet([], [], fake_data=True)
data_sets.validation = DataSet([], [], fake_data=True)
data_sets.test = DataSet([], [], fake_data=True)
return data_sets
TRAIN_IMAGES = 'train-images-idx3-ubyte.gz'
TRAIN_LABELS = 'train-labels-idx1-ubyte.gz'
TEST_IMAGES = 't10k-images-idx3-ubyte.gz'
TEST_LABELS = 't10k-labels-idx1-ubyte.gz'
VALIDATION_SIZE = 0
local_file = maybe_download(TRAIN_IMAGES, train_dir)
train_images = extract_images(local_file)
local_file = maybe_download(TRAIN_LABELS, train_dir)
train_labels = extract_labels(local_file, one_hot=one_hot)
local_file = maybe_download(TEST_IMAGES, train_dir)
test_images = extract_images(local_file)
local_file = maybe_download(TEST_LABELS, train_dir)
test_labels = extract_labels(local_file, one_hot=one_hot)
validation_images = train_images[:VALIDATION_SIZE]
validation_labels = train_labels[:VALIDATION_SIZE]
train_images = train_images[VALIDATION_SIZE:]
train_labels = train_labels[VALIDATION_SIZE:]
data_sets.train = SemiDataSet(train_images, train_labels, n_labeled)
data_sets.validation = DataSet(validation_images, validation_labels)
data_sets.test = DataSet(test_images, test_labels)
return data_sets