Tensorflow--多层神经网络

#coding=utf-8
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import input_data
import math
#import mnist 
import time

#参数设置
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_float('learning_rate',0.01,'Initial learning rate')
flags.DEFINE_integer('max_steps',2000,'Number of steps to run trainer')
flags.DEFINE_integer('hidden1_unit',128,'Number of units in hidden layer 1')
flags.DEFINE_integer('hidden2_unit',32,'Number of units in hidden layer 2')
flags.DEFINE_integer('batch_size',100,'Batch size')
flags.DEFINE_string('train_dir','mnist-data/','Directory to put the training data')
flags.DEFINE_boolean('fake_data', False, 'If true, use fake data '
                     'for unit testing')
NUM_CLASSES = 10
IMAGE_SIZE = 28
IMAGE_PIXELS = IMAGE_SIZE*IMAGE_SIZE

def inference (image,hidden1_unit,hidden2_unit):

	with tf.name_scope("hidden1"):
		#tf.truncated_normal()-->从截断的正态分布中输出随机值,stddev为正态分布的标准差
		w = tf.Variable(tf.truncated_normal([IMAGE_PIXELS,hidden1_unit],stddev=1.0/math.sqrt(IMAGE_PIXELS)))
		b = tf.Variable(tf.zeros([hidden1_unit]))
		#tf.nn.relu(features)<-->max(features,0)
		hidden1 = tf.nn.relu(tf.matmul(image,w) + b)

	with tf.name_scope("hidden2"):
		w = tf.Variable(tf.truncated_normal([hidden1_unit,hidden2_unit],stddev = 1.0/math.sqrt(hidden1_unit)))
		b = tf.Variable(tf.zeros([hidden2_unit]))
		hidden2 = tf.nn.relu(tf.matmul(hidden1,w) + b)

	with tf.name_scope("softmax_linear"):
		w = tf.Variable(tf.truncated_normal([hidden2_unit,NUM_CLASSES],stddev = 1.0/math.sqrt(hidden2_unit)))
		b = tf.Variable(tf.zeros([NUM_CLASSES]))
		logits = tf.matmul(hidden2,w)+b

	return logits

def loss1(logits,lables):
	lables = tf.to_int64(lables)
	#第一步:对logits做一个softmax,求取属于某一类的概率,输出一个向量
	#第二步:对输出向量和样本的实际标签做交叉熵,并对向量里所有元素求和
	cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits = logits,labels = lables,name = 'xentropy')
	#对向量求平均
	loss = tf.reduce_mean(cross_entropy)
	return loss
#训练
def training(loss,learning_rate):
	tf.summary.scalar(loss.op.name,loss)
	optimizer = tf.train.GradientDescentOptimizer(learning_rate)
	global_step = tf.Variable(0,name = 'global_step',trainable = False)
	train_op = optimizer.minimize(loss,global_step = global_step)
	return train_op
#评估过程
def evaluation(logits,lables):
	correct = tf.nn.in_top_k(logits,lables,1)
	result = tf.reduce_sum(tf.cast(correct,tf.int32))
	return result

def do_eval(sess,eval_correct,image_placeholder,label_placeholder,data_set):
	true_count = 0
	steps_per_epoch = data_set.num_examples
	num_examples = steps_per_epoch*FLAGS.batch_size
	for step in range(steps_per_epoch):
		feed_dict = fill_placeholder(data_set,image_placeholder,label_placeholder)
		true_count +=sess.run(eval_correct,feed_dict = feed_dict)
	precision = 1.0*true_count/num_examples
	print ('Number of examples:%d Num correct:%d Precision:@ 1:%.4f'%(num_examples,true_count,precision))


#定义占位符
def placeholder_inputs(batch_size):
	image_placeholder = tf.placeholder(tf.float32,shape = [batch_size,IMAGE_PIXELS])
	label_placeholder = tf.placeholder(tf.int32,shape = batch_size)
	return image_placeholder,label_placeholder

def fill_placeholder(data_set,images_pl,labels_pl):
	image_feed,label_feed = data_set.next_batch(FLAGS.batch_size,FLAGS.fake_data)
	feed_dict = {images_pl:image_feed,labels_pl:label_feed}
	return feed_dict
#训练过程
def run_train():
	data_sets = input_data.read_data_sets(FLAGS.train_dir,FLAGS.fake_data)
	with tf.Graph().as_default():
		image_placeholder,label_placeholder = placeholder_inputs(FLAGS.batch_size)

		logits = inference(image_placeholder,FLAGS.hidden1_unit,FLAGS.hidden2_unit)
		loss = loss1(logits,label_placeholder)
		train_op = training(loss,FLAGS.learning_rate)
		eval_correct = evaluation(logits,label_placeholder)

		summary_op = tf.summary.merge_all()
		saver = tf.train.Saver()
		with tf.Session() as sess:
			sess.run(tf.initialize_all_variables())
			summary_writer = tf.summary.FileWriter(FLAGS.train_dir, sess.graph)
			for step in range(FLAGS.max_steps):
				start_time = time.time()
				feed_dict = fill_placeholder(data_sets.train,image_placeholder,label_placeholder)
				_,loss_value = sess.run([train_op,loss],feed_dict = feed_dict)
				duration = time.time() - start_time

				if step%100 == 0:
					print ('Step %d: loss = %.2f (%.3f sec)' %(step,loss_value,duration))
					summary_str = sess.run(summary_op, feed_dict = feed_dict)
					summary_writer.add_summary(summary_str, step)
					summary_writer.flush()
				if step%1000 == 0:
					saver.save(sess,FLAGS.train_dir,global_step = step)

					print('Training Data Eval:')
					do_eval(sess,eval_correct,image_placeholder,label_placeholder,data_sets.train)

					print('Validation Data Eval:')
					do_eval(sess,eval_correct,image_placeholder,label_placeholder,data_sets.validation)

					print('Test Data Eval:')
					do_eval(sess,eval_correct,image_placeholder,label_placeholder,data_sets.test)

def main(_):
	run_train()

if __name__ == '__main__':
	tf.app.run()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值