#coding:utf-8
#0导入模块 ,生成模拟数据集
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)
#从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_
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))
plt.show()
#定义神经网络的输入、参数和输出,定义前向传播过程
def get_weight(shape, regularizer):
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))
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'))
#定义反向传播方法:不含正则化
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
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_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()]
#将网格坐标点喂入神经网络 ,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))
plt.contour(xx, yy, probs, levels=[.5])
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
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()
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
AI.zip (58个子文件)
AI-Practice-Tensorflow-Notes-f58bf9f81494d1d5b9fd874e39691c27d80d35be
pic.zip 2.12MB
pic
5.png 195KB
9.png 201KB
1.png 172KB
0.png 200KB
6.png 205KB
2.png 182KB
7.png 496KB
4.png 209KB
8.png 220KB
3.png 205KB
python.zip 2KB
fc1
mnist_forward.py 678B
mnist_backward.py 2KB
mnist_test.py 2KB
fc1.zip 2KB
python
tf3_1.py 98B
c.py 220B
b.py 178B
a.py 4B
save.dat 95B
animal.py 742B
fc3
pic
5.png 195KB
9.png 201KB
1.png 172KB
0.png 200KB
6.png 205KB
2.png 182KB
7.png 496KB
4.png 209KB
8.png 220KB
3.png 205KB
mnist_forward.py 678B
mnist_app.py 2KB
mnist_backward.py 2KB
mnist_test.py 2KB
fc2
mnist_forward.py 678B
mnist_backward.py 2KB
mnist_test.py 2KB
fc2.zip 3KB
tf
tf3_5.py 931B
tf3_2.py 162B
tf3_6.py 4KB
tf3_3.py 531B
tf3_4.py 641B
tf3_1.py 98B
opt
opt4_4.py 694B
opt4_8_backward.py 2KB
opt4_5.py 1KB
opt4_7.py 4KB
opt4_6.py 3KB
opt4_2.py 1KB
opt4_8_forward.py 665B
opt4_8_generateds.py 1KB
opt4_3.py 1KB
opt4_1.py 1KB
opt.zip 10KB
tf.zip 4KB
共 58 条
- 1
资源评论
APei
- 粉丝: 65
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于原生微信小程序实现的课堂考勤系统的设计与实现
- 商道融绿、润灵环球ESG评级数据(2015-2023年).xlsx
- 商道融绿、润灵环球ESG评级数据(2015-2023年).dta
- 基于 GDAL 与 PROJ4 的遥感图像处理软件,使用 Qt 构建课程设计
- 图形化界面采用Easyx编写,实现对哈夫曼树的显示操作
- 使用后端开发框架Spring Boot构建应用程序.pdf
- 基于Boson的计算机网络实验:RIP和IGRP的配置
- 在线教育系统 JAVA+Vue+SpringBoot+MySQL
- 健身房管理系统 JAVA+Vue+SpringBoot+MySQL
- 卓越导师双选系统 JAVA+Vue+SpringBoot+MySQL
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功