双层卷积
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import os
def get_weight(shape):
w = tf.random_normal(shape=shape, mean=1.0, stddev=1.0)
w = tf.Variable(w)
return w
def get_bais(shape):
b = tf.random_normal(shape=shape, mean=1.0, stddev=1.0)
b = tf.Variable(b)
return b
def model():
# 载入数据
# [None,784]
mnist = input_data.read_data_sets("./data/mnist/input_data/", one_hot=True)
# 占位
with tf.variable_scope("data"):
x = tf.placeholder(tf.float32, shape=[None, 784])
y_true = tf.placeholder(tf.int32, shape=[None, 10])
# 构建第一层卷积
with tf.variable_scope("first_jiji"):
# 构建变量
# 权重 [5,5,1,32]
weight_1 = get_weight([5, 5, 1, 32])
bais_1 = get_bais([32])
x_reshape = tf.reshape(x, shape=[-1, 28, 28, 1])
# 卷积
x_conv2d = tf.nn.conv2d(x_reshape, filter=weight_1, strides=[1, 1, 1, 1], padding="SAME") + bais_1
# 激活
x_relu_1 = tf.nn.relu(x_conv2d)
# 池化
x_pool_1 = tf.nn.max_pool(x_relu_1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
# 构建第二层卷积
with tf.variable_scope("second_jiji"):
# 构建变量
# 权重 [5,5,1,32]
weight_2 = get_weight([5, 5, 32, 64])
bais_2 = get_bais([64])
# 卷积
x_conv2d_2 = tf.nn.conv2d(x_pool_1, filter=weight_2, strides=[1, 1, 1, 1], padding="SAME") + bais_2
# 激活
x_relu_2 = tf.nn.relu(x_conv2d_2)
# 池化
x_pool_2 = tf.nn.max_pool(x_relu_2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
# 全连接
with tf.variable_scope("full_connection"):
w_fc = get_weight([7 * 7 * 64, 10])
b_fc = get_bais([10])
x_fc = tf.reshape(x_pool_2, [-1, 7 * 7 * 64])
y_predict = tf.matmul(x_fc, w_fc) + b_fc
return x, y_true, y_predict
def start_run():
mnist = input_data.read_data_sets("./data/mnist/input_data/", one_hot=True)
x, y_true, y_predict = model()
# 求损失
with tf.variable_scope("loss"):
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_predict))
# 梯度下降
with tf.variable_scope("optimizer"):
train_op = tf.train.GradientDescentOptimizer(0.00001).minimize(loss)
# 求准确率
with tf.variable_scope("acc"):
equal_list = tf.equal(tf.argmax(y_true, 1), tf.argmax(y_predict, 1))
# equal_list None个样本 [1, 0, 1, 0, 1, 1,..........]
accuracy = tf.reduce_mean(tf.cast(equal_list, tf.float32))
init_op = tf.global_variables_initializer()
with tf.variable_scope("merge"):
tf.summary.scalar("losser", loss)
meg = tf.summary.merge_all()
# 开启会话
with tf.Session() as sess:
sess.run(init_op)
file_writer = tf.summary.FileWriter("./tmp/juanji/brand/writer/data", sess.graph)
for i in range(1000):
# 得到数据
mnist_x, mnist_y = mnist.train.next_batch(50)
# 计算
sess.run(train_op, feed_dict={x: mnist_x, y_true: mnist_y})
print("训练第%d步,准确率为:%f" % (i, sess.run(accuracy, feed_dict={x: mnist_x, y_true: mnist_y})))
summary = sess.run(meg, feed_dict={x: mnist_x, y_true: mnist_y})
file_writer.add_summary(summary, i)
if __name__ == '__main__':
start_run()
简单神经网络
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import os
def simple():
train = 0
model_path = "./tmp/brand/model/data"
# 加载数据
mnist = input_data.read_data_sets("./data/mnist/input_data/", one_hot=True)
# 建立占位符
with tf.variable_scope("data"):
x = tf.placeholder(tf.float32, shape=[None, 784])
y_true = tf.placeholder(tf.int32, shape=[None, 10])
# 创建模型
with tf.variable_scope("model"):
weight = tf.Variable(tf.random_normal(shape=[784, 10], mean=0.1, stddev=1.0))
bias = tf.Variable(tf.random_normal(shape=[10], mean=0.1, stddev=1.0))
y_predict = tf.matmul(x, weight) + bias
# 求损失
with tf.variable_scope("loss"):
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_predict))
# 梯度下降
with tf.variable_scope("optimizer"):
train_op = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
# 求准确率
with tf.variable_scope("acc"):
equal_list = tf.equal(tf.argmax(y_true, 1), tf.argmax(y_predict, 1))
# equal_list None个样本 [1, 0, 1, 0, 1, 1,..........]
accuracy = tf.reduce_mean(tf.cast(equal_list, tf.float32))
init_op = tf.global_variables_initializer()
with tf.variable_scope("merge"):
tf.summary.scalar("losser", loss)
tf.summary.histogram("my_weight", weight)
meg = tf.summary.merge_all()
saver = tf.train.Saver()
# 开启会话
with tf.Session() as sess:
sess.run(init_op)
file_writer = tf.summary.FileWriter("./tmp/brand/writer/data", sess.graph)
if os.path.exists("./tmp/brand/model/checkpoint"):
saver.restore(sess, model_path)
if train == 1:
for i in range(1000):
# 得到数据
mnist_x, mnist_y = mnist.train.next_batch(50)
# 计算
sess.run(train_op, feed_dict={x: mnist_x, y_true: mnist_y})
print("训练第%d步,准确率为:%f" % (i, sess.run(accuracy, feed_dict={x: mnist_x, y_true: mnist_y})))
summary = sess.run(meg, feed_dict={x: mnist_x, y_true: mnist_y})
file_writer.add_summary(summary, i)
saver.save(sess, model_path)
else:
for i in range(40):
x_test, y_test = mnist.train.next_batch(1)
y_by_file = tf.arg_max(y_test, 1).eval()
y_by_computer = tf.arg_max(sess.run(y_predict, feed_dict={x: x_test, y_true: y_test}), 1).eval()
print("第%d次,真实数据%d,预测数据%d" % (i, y_by_file, y_by_computer))
if __name__ == '__main__':
simple()