tensorflow google实战 学习笔记——TensorFlow入门(3)
来源:互联网 发布:sql语句升序降序一起用 编辑:程序博客网 时间:2024/06/06 20:06
TensorFlow实现反向传播算法(backpropagation)
通过前向传播获得预测值,通过反向传播更新变量。
反向传播的算法实现了一个迭代的过程,每次迭代的开始都需要选取一小部分训练数据,这一小部分训练数据称为一个batch,这个batch的样例通过前项传播算法得到预测值,因为训练数据都是有正确答案标注的,所以可以计算出当前神经网络预测值与正确值之间的差距,基于这个差距,反向传播算法会相应更新神经网络参数的取值,使得在这个batch上的预测结果和真实答案更接近。
TensorFlow提供用于数据输入的机制:placeholder
placeholder相当于定义了一个位置,这个位置中的数据在程序运行时再指定。
import tensorflow as tf 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 = tf.placeholder(tf.float32,shape=(1,2),name='input')a = tf.matmul(x,w1)y = tf.matmul(a,w2)sess = tf.Session()init_op=tf.initialize_all_variables()sess.run(init_op)print(sess.run(y))sess.close()
这样没有指定输入数据,所以会报错。print(sess.run(y))改为
print(sess.run(y,feed_dict={x:[[0.7,0.9]]}))
如果要对3各样例进行前向传播的计算:
import tensorflow as tf 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 = tf.placeholder(tf.float32,shape=(3,2),name='input')a = tf.matmul(x,w1)y = tf.matmul(a,w2)sess = tf.Session()init_op=tf.initialize_all_variables()sess.run(init_op)print(sess.run(y,feed_dict={x:[[0.7,0.9],[0.1,0.4],[0.5,0.8]]}))sess.close()
输出为:
[[ 3.95757794]
[ 1.15376544]
[ 3.16749239]]
在得到一个batch的前向传播结果后,要定义损失函数来刻画预测值与真实值之间的差距。
一个完整神经网络样例程序:
import tensorflow as tfimport numpy as npfrom numpy.random import RandomStatebatch_size = 8w1 = tf.Variable(tf.random_normal([2,3],stddev=1,seed=1))w2 = tf.Variable(tf.random_normal([3,1],stddev=1,seed=1))x = tf.placeholder(tf.float32,shape=(None,2),name = 'x_input')y_= tf.placeholder(tf.float32,shape=(None,1),name = 'y_input')a = tf.matmul(x,w1)y = tf.matmul(a,w2)cross_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)rdm = RandomState(1)dataset_size=128X = rdm.rand(dataset_size,2)Y = [[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): 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]}) if i%1000 == 0: total_cross_entropy=sess.run(cross_entropy,feed_dict={x:X,y_:Y}) print("AFTER %d training steps,cross entropy an all data is %g"%(i,total_cross_entropy)) print(sess.run(w1)) print(sess.run(w2))
阅读全文
0 0
- tensorflow google实战 学习笔记——TensorFlow入门(3)
- tensorflow google实战 学习笔记——TensorFlow入门(1)
- tensorflow google实战 学习笔记——TensorFlow入门(2)
- 我是初学者——TensorFlow实战Google深度学习框架(学习笔记一)
- Tensorflow实战Google深度学习框架 笔记
- TensorFlow学习笔记(二):TensorFlow入门
- TensorFlow实战——入门
- Tensorflow实战Google深度学习框架-学习笔记
- TensorFlow学习笔记(四)——TensorFlow运作方式入门、可视化
- tensorflow入门学习笔记
- Tensorflow入门学习笔记
- Tensorflow入门学习笔记
- TensorFlow学习笔记:入门
- tensorflow02 《TensorFlow实战Google深度学习框架》笔记-03
- tensorflow03 《TensorFlow实战Google深度学习框架》笔记-04-01
- TensorFlow实战:Chapter-2(TensorFlow入门)
- tensorflow学习笔记(一)——tensorflow基本使用
- tensorflow26《TensorFlow实战Google深度学习框架》笔记-10-03 分布式TensorFlow code
- Centos7安装完毕后重启提示Initial setup of CentOS Linux 7 (core)的解决方法
- POI 导入excel
- PCA人脸识别个人理解及步骤
- 如何处理负载、高并发?
- mybatis调用存储过程
- tensorflow google实战 学习笔记——TensorFlow入门(3)
- 自动驾驶相关工具整理
- php常用方法
- opencv 形状变换 图像阈值 仿射变换
- 01 用Intellij idea+Gradle+jmonkey开发3d仿真应用
- Generative Adversarial Networks: An Overview笔记
- 'mysql' 不是内部或外部命令,也不是可运行的程序或批处理文件
- ios精品源码,仿微对话框弹窗父类swift椭圆Cell进度动画下载源码
- 极乐技术周报(第三十五期)-APPx小程序工具转载