tensorflow学习笔记(北京大学) tf4_7.py 完全解析 正则化

#coding:utf-8
#0导入模块 ,生成模拟数据集
#tensorflow学习笔记(北京大学) tf4_7.py 完全解析  正则化
#QQ群:476842922(欢迎加群讨论学习)
#如有错误还望留言指正,谢谢
#正则化为去除模型的过拟合现象
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
BATCH_SIZE = 30 
seed = 2 
#基于seed产生随机数
rdm = np.random.RandomState(seed)
#随机数返回300行2列的矩阵,表示300组坐标点(x0,x1)作为输入数据集
X = rdm.randn(300,2)#300个点
#从X这个300行2列的矩阵中取出一行,判断如果两个坐标的平方和小于2,给Y赋值1,其余赋值0
#作为输入数据集的标签(正确答案)
Y_ = [int(x0*x0 + x1*x1 <2) for (x0,x1) in X]
#遍历Y中的每个元素,1赋值'red'其余赋值'blue',这样可视化显示时人可以直观区分
Y_c = [['red' if y else 'blue'] for y in Y_]
#对数据集X和标签Y进行shape整理,第一个元素为-1表示,随第二个参数计算得到,第二个元素表示多少列,把X整理为n行2列,把Y整理为n行1列
X = np.vstack(X).reshape(-1,2)
Y_ = np.vstack(Y_).reshape(-1,1)
print X#打印点
print Y_#打印0  1 
print Y_c#打印个点颜色
#用plt.scatter画出数据集X各行中第0列元素和第1列元素的点即各行的(x0,x1),用各行Y_c对应的值表示颜色(c是color的缩写) 
plt.scatter(X[:,0], X[:,1], c=np.squeeze(Y_c)) #绘图,X[:,0]第一列元素,X[:,1]第二列元素,颜色
plt.show()#显示


#定义神经网络的输入、参数和输出,定义前向传播过程 
def get_weight(shape, regularizer):#(shape,正则化权重)
	w = tf.Variable(tf.random_normal(shape), dtype=tf.float32)
	#把内容加到集合对应位置做加法
	tf.add_to_collection('losses', tf.contrib.layers.l2_regularizer(regularizer)(w))
	return w

def get_bias(shape):  
    b = tf.Variable(tf.constant(0.01, shape=shape))#偏执b 
    return b
	
x = tf.placeholder(tf.float32, shape=(None, 2))#站位
y_ = tf.placeholder(tf.float32, shape=(None, 1))#站位

w1 = get_weight([2,11], 0.01)	
b1 = get_bias([11])
y1 = tf.nn.relu(tf.matmul(x, w1)+b1)

w2 = get_weight([11,1], 0.01)
b2 = get_bias([1])
y = tf.matmul(y1, w2)+b2 


#定义损失函数
loss_mse = tf.reduce_mean(tf.square(y-y_))#均方误差
loss_total = loss_mse + tf.add_n(tf.get_collection('losses'))#加正则化W的损失


#定义反向传播方法:不含正则化
train_step = tf.train.AdamOptimizer(0.0001).minimize(loss_mse)

with tf.Session() as sess:
	init_op = tf.global_variables_initializer()#初始化
	sess.run(init_op)
	STEPS = 40000#训练40000次
	for i in range(STEPS):
		start = (i*BATCH_SIZE) % 300
		end = start + BATCH_SIZE
		sess.run(train_step, feed_dict={x:X[start:end], y_:Y_[start:end]})
		if i % 2000 == 0:#每2000轮打印loss值
			loss_mse_v = sess.run(loss_mse, feed_dict={x:X, y_:Y_})
			print("After %d steps, loss is: %f" %(i, loss_mse_v))
    #xx在-3到3之间以步长为0.01,yy在-3到3之间以步长0.01,生成二维网格坐标点
	xx, yy = np.mgrid[-3:3:.01, -3:3:.01]#打点
	#将xx , yy拉直,并合并成一个2列的矩阵,得到一个网格坐标点的集合
	grid = np.c_[xx.ravel(), yy.ravel()]#xx.ravel()把xx拉直  np.c_:对应位置配对,组成矩阵
	#将网格坐标点喂入神经网络 ,probs为输出
	probs = sess.run(y, feed_dict={x:grid})
	#probs的shape调整成xx的样子
	probs = probs.reshape(xx.shape)#改变形状
	print "w1:\n",sess.run(w1)
	print "b1:\n",sess.run(b1)
	print "w2:\n",sess.run(w2)	
	print "b2:\n",sess.run(b2)

plt.scatter(X[:,0], X[:,1], c=np.squeeze(Y_c))#绘画点(x坐标,y坐标,颜色)
plt.contour(xx, yy, probs, levels=[.5])#绘制等高线(x轴坐标,y轴坐标,改点高度,等高线高度)
plt.show()#显示



#定义反向传播方法:包含正则化
train_step = tf.train.AdamOptimizer(0.0001).minimize(loss_total)

with tf.Session() as sess:
	init_op = tf.global_variables_initializer()#初始化
	sess.run(init_op)
	STEPS = 40000#4000轮
	for i in range(STEPS):
		start = (i*BATCH_SIZE) % 300
		end = start + BATCH_SIZE
		sess.run(train_step, feed_dict={x: X[start:end], y_:Y_[start:end]})
		if i % 2000 == 0:
			loss_v = sess.run(loss_total, feed_dict={x:X,y_:Y_})
			print("After %d steps, loss is: %f" %(i, loss_v))

	xx, yy = np.mgrid[-3:3:.01, -3:3:.01]
	grid = np.c_[xx.ravel(), yy.ravel()]
	probs = sess.run(y, feed_dict={x:grid})
	probs = probs.reshape(xx.shape)
	print "w1:\n",sess.run(w1)
	print "b1:\n",sess.run(b1)
	print "w2:\n",sess.run(w2)
	print "b2:\n",sess.run(b2)

plt.scatter(X[:,0], X[:,1], c=np.squeeze(Y_c)) 
plt.contour(xx, yy, probs, levels=[.5])
plt.show()


猜你喜欢

转载自blog.csdn.net/weixin_33595571/article/details/83689050
今日推荐