tensorFlow入门实践(一)
首先应用TensorFlow完成一个线性回归,了解TensorFlow的数据类型和运行机制。
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt rng = np.random # 参数设定
learning_rate = 0.01
training_epochs = 10000
display_step = 50 #50代display一次 # 训练数据
train_X = np.asarray([3.3, 4.4, 5.5, 6.71, 6.93, 4.168, 9.779, 6.182, 7.59, 2.167, 7.042,
10.791, 5.313, 7.997, 5.654, 9.27, 3.1])
train_Y = np.array([1.7, 2.76, 2.09, 3.19, 1.694, 1.573, 3.366, 2.596, 2.53, 1.221,
2.827, 3.465, 1.65, 2.904, 2.42, 2.94, 1.3])
n_samples = train_X.shape[0] #维度 # 设置placeholder
X = tf.placeholder("float")
Y = tf.placeholder("float") # 设置模型的权重和偏置
W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")
# <tf.Variable 'weight:0' shape=() dtype=float32_ref>
# <tf.Variable 'bias:0' shape=() dtype=float32_ref> # 设置线性回归方程
pred = tf.add(tf.multiply(X, W), b) # 设置cost为均方差
cost = tf.reduce_sum(tf.pow(pred-Y, 2)) / (2 * n_samples)
# 梯度下降
# 注意,minimize() 可以自动修正W和b,因为默认设置Variables的trainable=True
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) # 初始化所有的variables
init = tf.global_variables_initializer() with tf.Session() as sess:
sess.run(init)
# 开始训练
# 灌入所有训练数据
for epoch in range(training_epochs):
for (x, y) in zip(train_X, train_Y): # zip 搞成字典
sess.run(optimizer, feed_dict={X: x, Y: y}) # 打印出每次迭代的log日志
if (epoch+1) % display_step == 0:
c = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
print ("Epoch:%04d cost=" %(epoch+1,), '{:.9f}'.format(c), "W=", sess.run(W), "b=", sess.run(b)) print("Optimization Finished!")
training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
print("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b)) # 作图
# figure 1
plt.figure()
plt.plot(train_X, train_Y, 'ro', label='Original data')
plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
plt.legend() # 测试样本
test_X = np.asarray([6.83, 4.668, 8.9, 7.91, 5.7, 8.7, 3.1, 2.1])
test_Y = np.asarray([1.84, 2.273, 3.2, 2.831, 2.92, 3.24, 1.35, 1.03]) print("Testing...(Mean square loss Comparison)")
testing_cost = sess.run(
tf.reduce_sum(tf.pow(pred - Y, 2)) / (2 * test_X.shape[0]),
feed_dict={X: test_X, Y: test_Y}) # same function as cost above
print("Testing cost=", testing_cost)
print("Absolute mean square loss difference:", abs(training_cost - testing_cost)) # figure 2
plt.figure()
plt.plot(test_X, test_Y, 'bo', label='Testing data')
plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
plt.legend()
plt.show()
在训练完成后,用test数据集进行测试,训练和测试得到的数据结果如下:
Optimization Finished!
Training cost= 0.07699074 W= 0.24960834 b= 0.80136055
Testing...(Mean square loss Comparison)
Testing cost= 0.07910849
Absolute mean square loss difference: 0.002117753
训练和测试得到的结果图像如下:

接下来,做一个简单的逻辑回归进行手写数字识别例子,来进一步感受一下TensorFlow应用中,计算图的建立和工作机制。希望能在示例的实现中体会其思路,慢慢融会贯通。代码如下:
import tensorflow as tf # 加载mnist数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/temp/data", one_hot=True)
print(mnist) # 设置参数
learning_rate = 0.01
training_epochs = 50
batch_size = 100 # 每次放入一定批量的数据放入模型中去训练
display_step = 5 # tf Graph的输入
x = tf.placeholder(tf.float32, [None, 784]) # mnist data image of shape 28*28
y = tf.placeholder(tf.float32, [None, 10]) # 0-9 digits recognition => 10 # 设置权重和偏置
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10])) # 设定运行模型
pred = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax # 设定cost function为 cross entropy 交叉熵
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=1))
# 梯度下降
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) # 初始化权重
init = tf.global_variables_initializer() # 开始训练
with tf.Session() as sess:
sess.run(init) for epoch in range(training_epochs):
avg_cost = 0.0
total_batch = int(mnist.train.num_examples/batch_size)
# 遍历每个batch
for i in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
# 把每个batch的数据放进去训练
_, c = sess.run([optimizer, cost], feed_dict={x: batch_xs, y: batch_ys}) # 计算平均损失
avg_cost += c / total_batch
# 展示每次迭代的日志
if (epoch+1) % display_step == 0:
print("Epoch:", (epoch+1), "cost=", "{:.9f}".format(avg_cost)) print("Optimization Finished!") # 测试模型
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
# 计算3000个样本的准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("Accuracy:", accuracy.eval({x:mnist.test.images[:3000], y: mnist.test.labels[:3000]}))
运行后得到结果如下:
Epoch: 5 cost= 0.465533400
Epoch: 10 cost= 0.392410529
Epoch: 15 cost= 0.362706895
Epoch: 20 cost= 0.345443823
Epoch: 25 cost= 0.333700618
Epoch: 30 cost= 0.325048538
Epoch: 35 cost= 0.318335179
Epoch: 40 cost= 0.312850520
Epoch: 45 cost= 0.308320769
Epoch: 50 cost= 0.304484692
Optimization Finished!
Accuracy: 0.896
总结:
一、关于梯度下降
1.根据公式直接计算
2.自动求导
由于手动求导方法有两个缺陷:
(1)深度神经网络中公式较长
(2)计算效率较低
可以采用以下命令进行自动求导:
gradient = tf.gradients(mse, [theta])[0]
(1)op,损失函数,这里是mse,即均方误差
(2)variable lists,变量列表即权重,这里是theta的值
3. 更简便的方法:使用Optimizer
optimizer = tf.train.GradientDecentOptimizer(learning_rate = learning_rate)
train_op = optimizer.minimize(mse)
还有很多其他优化器,例如收敛更快的MomentumOptimizer优化器等。
梯度下降中传输数据的方式
(1)mini-batch小批量灌入数据,如手写数字识别中所用的方式
(用batch_size进行分割,可以通过如下函数完成取批量数据)
(2)方法:使用占位符placeholder
def fetch_batch(epoch, batch_index, batch_size):
np.random.seed(epoch * n_batches + batch_index)
indices = np.random.randint(m, size=batch_size)
x_batch = data[indices]
y_batch = housing.target.reshape(-1, 1)[indices]
return X_batch, y_batch
2. 模型保存和恢复
在训练完成后,我们经常需要保存模型,方便随时进行预测。
有时在训练过程中,我们也希望将训练的中间结果保存下来。
在TensorFlow中实现模型的保存和恢复还是非常简单便捷的:
(1)模型保存:
在创建图阶段创建一个Saver的结点,在执行阶段需要保存模型的地方调用Save()函数即可。
saver = tf.train.Saver()
完成后再启动Session,在中间某一步需要保存,我们就调用saver函数,保存在相对应的路径下。
save_path = saver.save(sess, "/my_model.ckpt")
(2)模型恢复:
在构建图的结尾创建一个Saver结点,在执行阶段的开始用restore函数进行模型恢复
saver.restore(sess, "/tem/my_model_final.ckpt")
更多关于模型保存和恢复的情况和方法请参考下面一篇博客:
http://blog.csdn.net/liangyihuai/article/details/78515913
tensorFlow入门实践(一)的更多相关文章
- tensorFlow入门实践(三)实现lenet5(代码结构优化)
这两周我学习了北京大学曹建老师的TensorFlow笔记课程,认为老师讲的很不错的,很适合于想要在短期内上手完成一个相关项目的同学,课程在b站和MOOC平台都可以找到. 在卷积神经网络一节,课程以le ...
- tensorFlow入门实践(三)初识AlexNet实现结构
参考黄文坚<TensorFlow实战>一书,完成AlexNet的整体实现并展望其训练和预测过程. import tensorflow as tf batch_size = 32 num_b ...
- tensorFlow入门实践(二)模块化
实现过一个例子之后,对TensorFlow运行机制有了初步的了解,但脑海中还没有一个如何实现神经网络的一个架构模型.下面我们来探讨如何模块化搭建神经网络,完成数据训练和预测. 首先我们将整体架构分为两 ...
- TensorFlow入门之MNIST最佳实践
在上一篇<TensorFlow入门之MNIST样例代码分析>中,我们讲解了如果来用一个三层全连接网络实现手写数字识别.但是在实际运用中我们需要更有效率,更加灵活的代码.在TensorFlo ...
- TensorFlow入门之MNIST最佳实践-深度学习
在上一篇<TensorFlow入门之MNIST样例代码分析>中,我们讲解了如果来用一个三层全连接网络实现手写数字识别.但是在实际运用中我们需要更有效率,更加灵活的代码.在TensorFlo ...
- (转)TensorFlow 入门
TensorFlow 入门 本文转自:http://www.jianshu.com/p/6766fbcd43b9 字数3303 阅读904 评论3 喜欢5 CS224d-Day 2: 在 Da ...
- 分享《机器学习实战基于Scikit-Learn和TensorFlow》中英文PDF源代码+《深度学习之TensorFlow入门原理与进阶实战》PDF+源代码
下载:https://pan.baidu.com/s/1qKaDd9PSUUGbBQNB3tkDzw <机器学习实战:基于Scikit-Learn和TensorFlow>高清中文版PDF+ ...
- 利用 TensorFlow 入门 Word2Vec
利用 TensorFlow 入门 Word2Vec 原创 2017-10-14 chen_h coderpai 博客地址:http://www.jianshu.com/p/4e16ae0aad25 或 ...
- 分布式学习系列【dubbo入门实践】
分布式学习系列[dubbo入门实践] dubbo架构 组成部分:provider,consumer,registry,monitor: provider,consumer注册,订阅类似于消息队列的注册 ...
随机推荐
- 环境搭建之allure的安装配置,及简单使用
环境准备 首先是要安装好jdk的电脑上,运行java.javac这些命令都没有问题,要不安装allure时会报错 下载allure 如果直接用Jenkins上的插件,并不需要下载安装 allure官网 ...
- java日期和时间Date、Calendar、SimpleDateFormat
1 时间和日期 1.1 日期类Date和格式化SimpleDateFormat 日期使用过程中需要将日期Date对象转化为字符串,或者将字符串形式的日期转化为日期Date对象.可 ...
- 一张图了解Spring Cloud微服务架构
Spring Cloud作为当下主流的微服务框架,可以让我们更简单快捷地实现微服务架构.Spring Cloud并没有重复制造轮子,它只是将目前各家公司开发的比较成熟.经得起实际考验的服务框架组合起来 ...
- 在docker中快速创建包含ip相关tool的ubuntu镜像
在docker学习中需要创建轻量级的,包含ip相关工具的容器,支持ping,ip,ethtool,brctrl等相关指令. 下面就是快速创建一个满足需求的ubunut镜像的过程: 1) 在docker ...
- 第一次java程序设计作业
通过JAVA语言的学习,使我对计算机语言有了更加深入的认识和理解.知道了许多JAVA语言与其他语言的区别和特性,及其在我们生活中所发挥的重要作用.最后用一句话表明在学习JAVA语言过程中的感受,那就是 ...
- helm一键 安装mariadb-ha(详细)
一. 二.单机安装一主一从 先创建对应pv https://github.com/helm/charts/blob/master/stable/mariadb/templates/master-sta ...
- ASP.NET Razor - 标记
目录 什么是 Razor? Razor 帮助器 ASP.NET Razor - C# 和 VB 代码语法 主要的 Razor C# 语法规则 它是如何工作的? 使用对象 If 和 Else条件 读取用 ...
- Android proguard混淆签名打包出现"android proguard failed to export application"解决方案
刚刚接触安卓,不是很熟悉.发现之前可以正常打包的项目出现添加混淆再进行打包签名的APK之后提示"android proguard failed to export application&q ...
- max of 直线划平面
在一个无限延伸平面上有一个圆和n条直线,这些直线中每一条都在一个圆内,并且同其他所有的直线相交,假设没有3条直线相交于一点,试问这些直线最多将圆分成多少区域. Input 第一行包含一个整数T,(0& ...
- 【C/C++】C++11 Lambda
Lambda C++11 中 lambda 是一个匿名函数对象 最简形式 []{ cout << "lambda" << endl; }(); // pri ...