Tensorflow实战Google-第三章-简单神经网络
来源:互联网 发布:深入浅出node infoq 编辑:程序博客网 时间:2024/06/05 15:46
# 神经网络实现二分类问题tensorflowimport tensorflow as tffrom numpy.random import RandomState# 定义一个batch的大小batch_size = 8# 定义初始化权重参数,stddev标准偏差,seed随机种子# w1和w2分别表示第一列和第二列权重参数;这里的输入为2个参数x1+x2;隐藏层只有一层,有3个节点;# 输出层只有一个节点;所以2-w1-3-w2-1::输入层-隐藏层-输出层# random_normal表示随机生成矩阵,参数mean表示均值,不写默认为0,stddev表示标准差,# seed表示选定一个初始态,以后每次运行时产生的随机数是一样的w1 = tf.Variable(tf.random_normal([2,3],stddev=1,seed=1))w2 = tf.Variable(tf.random_normal([3,1],stddev=1,seed=1))# 输入输出(训练数据样本:x分别为输入特征,y_为对应的正确结果)# placeholder用于暂时存储数据,第一个参数表示类型,第二个shape表示矩阵形式,# (m,n)表示m*n,这里的None表示输入样本数量不确定,2表示特征值为2个,即x1+x2x = tf.placeholder(tf.float32,shape=(None,2),name='x-input')y_ = tf.placeholder(tf.float32,shape=(None,1),name='y-input')# 传播过程,matmul为矩阵相乘,传播方向:x->a->ya = tf.matmul(x,w1)y = tf.matmul(a,w2)# 定义损失函数和反向传播算法# 学习率为0.001cross_entropy = -tf.reduce_mean(y_*tf.log(tf.clip_by_value(y,1e-10,1.0)))train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy)# 随机生成测试数据集# RandomState(1),这里的1表示随机种子,表示固定,每次都不变,# rdm = RandomState(i)就会变化# 这里表示X为128*2;rdm = RandomState(1)dataset_size = 128X = rdm.rand(dataset_size,2)# 定义xi+x2<1时,y=1,为正样本;否则为负样本0# Y= 0/1Y = [[int(x1+x2 < 1)] for (x1 , x2) in X]# 创建一个会话with tf.Session() as sess: # 初始化相关变量 init_op = tf.initialize_all_variables() sess.run(init_op) print(sess.run(w1)) print(sess.run(w2)) # 定义训练的轮数 STEPS = 5000 for i in range(STEPS): # 每次选取一个batch_size个样本,即8;进行训练 start = (i*batch_size)%dataset_size end = min(start+batch_size,dataset_size) # 更新权重 sess.run(train_step,feed_dict={x:X[start:end], y_:Y[start:end]}) # 每训练1000次,就计算一次在所有数据上的交叉熵输出,便于观察 if i%1000 == 0: total_cross_entropy = sess.run(cross_entropy,feed_dict = {x:X,y_:Y}) print("After %d training step(s),cross entropy on all data is %g"% (i,total_cross_entropy)) print(sess.run(w1)) print(sess.run(w2))
阅读全文
0 0
- Tensorflow实战Google-第三章-简单神经网络
- Tensorflow实战Google-第四章深层神经网络知识点
- TensorFlow实战Google深度学习框架第三章总结
- tensorflow实战之 简单卷积神经网络
- [action]tensorflow深度学习实战 (4) 实现简单卷积神经网络
- TensorFlow实战4:实现简单的多层神经网络案例
- Deep Learning-TensorFlow (7) CNN卷积神经网络_《Tensorflow:实战Google深度学习框架》
- 《tensorflow实战google》第二章代码
- tensorflow构建简单神经网络
- Tensorflow简单的神经网络
- tensorflow13《TensorFlow实战Google深度学习框架》笔记-06-02mnist LeNet5卷积神经网络 code
- [TensorFlow实战] 简单CNN
- 小白学Tensorflow之简单神经网络
- tensorflow:1)简单的神经网络
- Tensorflow 搭建简单神经网络 | Python
- tensorflow 仿真已知简单神经网络
- 《tensorflow实战》学习3——实现简单的卷积神经网络
- Tensorflow实战Google-第五章mnist数字识别
- Qt定时器的应用,实现时钟的状态
- Gym 101142G Gangsters in Central City【思维+Lca】
- 高级C语言教程-C语言函数setjmp()函数
- bzoj1731 [Usaco2005 dec]Layout 排队布局(差分约束+spfa)
- PHP最常用的设计模式
- Tensorflow实战Google-第三章-简单神经网络
- 【51nod】 1279
- NOIP专题训练——网络流 题解
- MyApplication框架或者变量的初始化
- < 笔记 > Python
- Hibernate的配置文件与测试
- UVA 1594 Ducci Sequence
- 素数个数
- 项目开发必备:Iconfont-阿里巴巴矢量图标库