主要内容:手写字分类
数据集:mnist
涉及方法:逻辑回归、简单神经网络、CNN、LSTM
逻辑回归示意图:
代码:
def LogicG(mnist, dim, nclasses):
x = tf.placeholder("float32", [None, dim])
y = tf.placeholder('float32', [None, nclasses])
w = tf.Variable(tf.random_normal([dim, nclasses]))
b = tf.Variable(tf.zeros([10]))
learning_rate = 0.01
iter = 140
display_step = 5 # 每迭代5次打印一次
batchsize = 100
out = tf.nn.softmax(tf.matmul(x, w) + b)
# 对数
loss = tf.reduce_mean(-tf.reduce_sum(y*tf.log(out), reduction_indices=1))
pred = tf.equal(tf.argmax(out, 1), tf.argmax(y, 1)) # 输出true or false
accr = tf.reduce_mean(tf.cast(pred, 'float32')) # 输出 1 or 0
train = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
for step in range(iter):
avg_cost = 0
num_batch = int(mnist.train.num_examples/batchsize)
for i in range(num_batch):
batch_xs, batch_ys = mnist.train.next_batch(batchsize)
feeds = {x: batch_xs, y: batch_ys}
sess.run(train, feed_dict=feeds)
avg_cost += sess.run(loss, feed_dict=feeds)/num_batch
if step % display_step == 0:
feeds_train = {x: batch_xs, y: batch_ys}
feeds_test = {x: mnist.test.images, y: mnist.test.labels}
train_accr = sess.run(accr, feed_dict=feeds_train)
test_accr = sess.run(accr, feed_dict=feeds_test)
print("step:", step, ", loss:", avg_cost, ", train_accr:",
train_accr, ", test_accr:", test_accr)
简单神经网络示意图:
代码:
def sampleNetwork(mnist, dim, nclasses):
diminput = dim
dimoutput = nclasses
n_hidden_1 = 256
n_hidden_2 = 128
learning_rate = 0.001
stddev = 0.1 # ??
iter = 100
batchsize = 100
display_step = 20
w = {
'hidden1': tf.Variable(tf.random_normal([diminput, n_hidden_1],
stddev=stddev)),
'hidden2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2],
stddev=stddev)),
'out': tf.Variable(tf.random_normal([n_hidden_2, dimoutput],
stddev=stddev))
}
b = {
'hidden1': tf.Variable(tf.random_normal([n_hidden_1])),
'hidden2': tf.Variable(tf.random_normal([n_hidden_2])),
'out': tf.Variable(tf.random_normal([dimoutput]))
}
x = tf.placeholder("float32", [None, diminput])
y = tf.placeholder('float32', [None, dimoutput])
layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, w['hidden1']), b['hidden1']))
layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, w['hidden2']), b['hidden2']))
out = tf.nn.softmax(tf.matmul(layer_2, w['out']) + b['out'])
loss = tf.reduce_mean(-tf.reduce_sum(y*tf.log(out), reduction_indices=1))
train = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
accr = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(out, 1),
tf.argmax(y, 1)), tf.float32))
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
for step in range(iter):
avg_cost = 0
num_batch = int(mnist.train.num_examples / batchsize)
for i in range(num_batch):
batch_xs, batch_ys = mnist.train.next_batch(batchsize)
feeds = {x: batch_xs, y: batch_ys}
sess.run(train, feed_dict=feeds)
avg_cost += sess.run(loss, feed_dict=feeds)/num_batch
if step % display_step == 0:
feeds_train = {x: batch_xs, y: batch_ys}
feeds_test = {x: mnist.test.images, y: mnist.test.labels}
train_accr = sess.run(accr, feed_dict=feeds_train)
test_accr = sess.run(accr, feed_dict=feeds_test)
print("step:", step, ", loss:", avg_cost, ", train_accr:",
train_accr, ", test_accr:", test_accr)
CNN示意图:
代码:
def MnistCnn(mnist, dim, nclasses):
f1_h = 64
f2_h = 128
fsize = 3
fc = 1024
w_input = 28
deep = 1
stddev = 0.1
keepratio = 0.7
learning_rate = 0.001
iter = 15
batchsize = 10
display_step = 1
do_train = 1
save_step = 1
weights = {
'wc1': tf.Variable(tf.random_normal([fsize, fsize, deep, f1_h], stddev=stddev)),
'wc2': tf.Variable(tf.random_normal([fsize, fsize, f1_h, f2_h], stddev=stddev)),
'wd1': tf.Variable(tf.random_normal([7 * 7 * f2_h, fc], stddev=stddev)),
'wd2': tf.Variable(tf.random_normal([fc, nclasses], stddev=stddev))
}
biases = {
'bc1': tf.Variable(tf.random_normal([f1_h], stddev=stddev)),
'bc2': tf.Variable(tf.random_normal([f2_h], stddev=stddev)),
'bd1': tf.Variable(tf.random_normal([fc], stddev=stddev)),
'bd2': tf.Variable(tf.random_normal([nclasses], stddev=stddev))
}
x = tf.placeholder(tf.float32, [None, dim])
y = tf.placeholder(tf.float32, [None, nclasses])
# [batchsize, width, height, deep]
input = tf.reshape(x, shape=[-1, w_input, w_input, deep])
# strides=[1, 1, 1, 1]:移动的步子
conv1 = tf.nn.conv2d(input, weights['wc1'], strides=[1, 1, 1, 1], padding='SAME')
conv1 = tf.nn.relu(tf.nn.bias_add(conv1, biases['bc1']))
pool1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
pool1_drop = tf.nn.dropout(pool1, keepratio)
conv2 = tf.nn.conv2d(pool1_drop, weights['wc2'], strides=[1, 1, 1, 1], padding='SAME')
conv2 = tf.nn.relu(tf.nn.bias_add(conv2, biases['bc2']))
pool2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
pool2_drop = tf.nn.dropout(pool2, keepratio)
densel = tf.reshape(pool2_drop, [-1, weights['wd1'].get_shape().as_list()[0]])
fc1 = tf.nn.relu(tf.add(tf.matmul(densel, weights['wd1']), biases['bd1']))
fc1_drop = tf.nn.dropout(fc1, keepratio)
out = tf.add(tf.matmul(fc1_drop, weights['wd2']), biases['bd2'])
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=out))
train = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
accr = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(out, 1), tf.argmax(y, 1)), tf.float32))
saver = tf.train.Saver(max_to_keep=2)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
if do_train == 1:
for step in range(iter):
avg_cost = 0
# num_batch = int(mnist.train.num_examples / batchsize)
num_batch = 10
for i in range(num_batch):
batch_xs, batch_ys = mnist.train.next_batch(batchsize)
feeds = {x: batch_xs, y: batch_ys}
sess.run(train, feed_dict=feeds)
avg_cost += sess.run(loss, feed_dict=feeds) / num_batch
if step % display_step == 0:
feeds_train = {x: batch_xs, y: batch_ys}
# feeds_test = {x: mnist.test.images, y: mnist.test.labels}
train_accr = sess.run(accr, feed_dict=feeds_train)
# test_accr = sess.run(accr, feed_dict=feeds_test)
print("step:", step, ", loss:", avg_cost, ", train_accr:", train_accr)
if step % save_step == 0:
saver.save(sess, "save/nets/cnn_mnist_basic.ckpt-" + str(step))
if do_train == 1:
step = iter-1
saver.restore(sess, "save/nets/cnn_mnist_basic.ckpt-" + str(step))
feeds_test = {x: mnist.test.images, y: mnist.test.labels}
test_accr = sess.run(accr, feed_dict=feeds_test)
LSTM示意图:
代码:
def LstmRnn(mnist, dim, nclasses, name):
dimhidden = dim
dimoutput = nclasses
diminput = 28
nsteps = 28
learning_rate = 0.001
iter = 5
batchsize = 16
display_step = 1
w = {
'hidden': tf.Variable(tf.random_normal([diminput, dimhidden])),
'out': tf.Variable(tf.random_normal([dimhidden, dimoutput]))
}
b = {
'hidden': tf.Variable(tf.random_normal([dimhidden])),
'out': tf.Variable(tf.random_normal([dimoutput]))
}
x = tf.placeholder('float32', [None, nsteps, diminput])
y = tf.placeholder('float32', [None, dimoutput])
# ---------------------------------------------------------------------------------------
# 输入层x的处理:
# x = [bachsize, nsteps, diminput] -----> x = [nsteps*bachsize, diminput]
x = tf.transpose(x, [1, 0, 2])
x = tf.reshape(x, [-1, diminput])
# 隐层输出
h = tf.matmul(x, w['hidden']) + b['hidden']
# 切分为nsteps个块,每个块使用一个LSTM神经网络
hsplit = tf.split(h, nsteps, 1)
with tf.variable_scope(name) as scope:
scope.reuse_variables()
# lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(dimhidden, forget_bias=1.0)
lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(dimhidden)
lstm_o, lstm_s = tf.nn.dynamic_rnn(lstm_cell, hsplit, dtype=tf.float32)
out = tf.matmul(lstm_o[-1], w['out'], b['out'])
# ----------------------------------------------------------------------------------------
# softmax_cross_entropy_with_logits:交叉熵计算损失
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(out, y))
train = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
accr = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(out, 1), tf.argmax(y, 1)), tf.float32))
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
for step in range(iter):
avg_cost = 0
num_batch = int(mnist.train.num_examples / batchsize)
for i in range(num_batch):
batch_xs, batch_ys = mnist.train.next_batch(batchsize)
batch_xs = batch_xs.reshape((batchsize, nsteps, diminput))
feeds = {x: batch_xs, y: batch_ys}
sess.run(train, feed_dict=feeds)
avg_cost += sess.run(loss, feed_dict=feeds)/num_batch
if step % display_step == 0:
feeds_train = {x: batch_xs, y: batch_ys}
testimgs = mnist.test.images.reshape(
mnist.test.num_examples,
nsteps, diminput)
feeds_test = {x: testimgs, y: mnist.test.labels}
train_accr = sess.run(accr, feed_dict=feeds_train)
test_accr = sess.run(accr, feed_dict=feeds_test)
print("step:", step, ", loss:", avg_cost, ", train_accr:",
train_accr, ", test_accr:", test_accr)