初始tensorflow
很早之前就听闻tensorflow大名,作为深度学习框架的明星产品tensorflow从诞生之初,就备受关注。作为入门小白,以后也会逐步学起,更新tensorflow认识和技能。
TensorFlow是Google开源的一款人工智能学习系统。为什么叫这个名字呢?Tensor的意思是张量,代表N维数组;Flow的意思是流,代表基于数据流图的计算。把N维数字从流图的一端流动到另一端的过程,就是人工智能神经网络进行分析和处理的过程。
使用tensorflow需要注意:
- 使用图 (graph) 来表示计算任务.
- 在被称之为
会话 (Session)
的上下文 (context) 中执行图. - 使用 tensor 表示数据.
- 通过
变量 (Variable)
维护状态. - 使用 feed 和 fetch 可以为任意的操作(arbitrary operation) 赋值或者从其中获取数据.
OK,讲了这么多概念,还是快速上手安装体验下吧。
机器环境是:ubuntu18.04 LTS+anaconda 5.2+python 3.6.5+tensorflow 1.11.0
1. cpu or gpu?
gpu可以加速tensorflow速度,如果显存在8G以上gpu远快于cpu。需要现查看下自己机器gpu情况如下,可以看出gpu不行,选择cpu版本进行安装。
cat /proc/cpuinfo #查看cpu
lspci | grep VGA #查看gpu
2. pip安装
由于python不同版本包依赖存在冲突,而anaconda是一个开源的包、环境管理器,可以用于在同一个机器上安装不同版本的软件包及其依赖,并能够在不同的环境之间切换,选用anaconda进行管理。
2.1 anaconda安装
在https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/选取合适的安装包,进行安装,如下:
sh Anaconda3-5.2.0-Linux-x86_64.sh
一路按yes即可。
2.2 tensorflow安装
建立一个conda计算环境,并激活该环境,最后安装tensorflow,如下。
conda create -n tensorflow #创建
source activate tensorflow #激活
pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.11.0-cp36-cp36m-linux_x86_64.whl
启动python shell,并导入tensorflow,如果没有报错,则成功。
import tensorflow as tf
当我们开始学习编程的时候,第一件事往往是学习打印"Hello World"。就好比编程入门有Hello World,机器学习入门有MNIST。
1. 数据集
MNIST是一个入门级的计算机视觉数据集,它包含各种手写数字图片(每一张图片包含28像素X28像素,长度是 28x28 = 784),也包含每一张图片对应的标签,MNIST数据集的官网是Yann LeCun"s website。下载下来的数据集被分成两部分:60000行的训练数据集(mnist.train
)和10000行的测试数据集(mnist.test
)。
对于由784个像素点组成图片,如何展开这个数组(数字间的顺序)不重要,只要保持各个图片采用相同的方式展开。因此,在MNIST训练数据集中,mnist.train.images
是一个形状为 [60000, 784]
的张量,第一个维度数字用来索引图片,第二个维度数字用来索引每张图片中的像素点。在此张量里的每一个元素,都表示某张图片里的某个像素的强度值,值介于0和1之间。
相对应的MNIST数据集的标签是介于0到9的数字,用来描述给定图片里表示的数字。使用one-hot处理标签,标签0将表示成([1,0,0,0,0,0,0,0,0,0,0])。因此, mnist.train.labels
是一个 [60000, 10]
的数字矩阵。
2. softmax回归
softmax模型可以用来给不同的对象分配概率。对于MNIST,每一张图片都表示一个数字,从0到9。我们希望得到给定图片代表每个数字的概率。比如说,我们的模型可能推测一张包含9的图片代表数字9的概率是80%但是判断它是8的概率是5%(因为8和9都有上半部分的小圆),然后给予它代表其他数字的概率更小的值。数学公式如下:
3. 实现softmax回归模型
tensorflow实现softmax代码只需要一行,如下
cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits,
labels=onehot_labels,
name="xentropy")
第一个参数logits:就是神经网络最后一层的输出,如果有batch的话,它的大小就是[batchsize,num_classes],单样本的话,大小就是
num_classes。
第二个参数labels:实际的标签,大小同上。
具体执行流程分为两部:
第一步是先对网络最后一层的输出做一个softmax,这一步通常是求取输出属于某一类的概率,对于单样本而言,输出就是一个
大小的向量(num_classes
元素分别代表了是属于该类的概率)。
第二步是softmax的输出向量
和样本的实际标签做一个交叉熵,公式如下:
其中,是实际的标签中第i个的值(用mnist数据举例,如果是3,那么标签是
,
是
softmax的输出向量
中,第i个元素的值。
具体代码见git。
4. tensorflow版本改动api bug说明
4.1 ValueError: Shapes (2, 100, 1) and () are incompatible
Traceback (most recent call last):
File "/home/lee/PycharmProjects/mnist/fully_connected_feed.py", line 222, in <module>
tf.app.run()
File "/opt/modules/anaconda3/envs/mnist/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 125, in run
_sys.exit(main(argv))
File "/home/lee/PycharmProjects/mnist/fully_connected_feed.py", line 218, in main
run_training()
File "/home/lee/PycharmProjects/mnist/fully_connected_feed.py", line 138, in run_training
loss = mnist.loss(logits, labels_placeholder)
File "/home/lee/PycharmProjects/mnist/mnist.py", line 97, in loss
concated = tf.concat(1, [indices, labels])
File "/opt/modules/anaconda3/envs/mnist/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1122, in concat
tensor_shape.scalar())
File "/opt/modules/anaconda3/envs/mnist/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py", line 848, in assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (2, 100, 1) and () are incompatible
解决方案:
concated = tf.concat(1, [indices, sparse_labels])改为:
concated= tf.concat([indices, sparse_labels], 1)
4.2 AttributeError: module "tensorflow" has no attribute "pack"
Traceback (most recent call last):
File "/home/lee/PycharmProjects/mnist/fully_connected_feed.py", line 222, in <module>
tf.app.run()
File "/opt/modules/anaconda3/envs/mnist/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 125, in run
_sys.exit(main(argv))
File "/home/lee/PycharmProjects/mnist/fully_connected_feed.py", line 218, in main
run_training()
File "/home/lee/PycharmProjects/mnist/fully_connected_feed.py", line 138, in run_training
loss = mnist.loss(logits, labels_placeholder)
File "/home/lee/PycharmProjects/mnist/mnist.py", line 99, in loss
concated, tf.pack([batch_size, NUM_CLASSES]), 1.0, 0.0)
AttributeError: module "tensorflow" has no attribute "pack"
TF后面的版本修改了这个函数的名称,把tf.pack改为 tf.stack,解决方案:
onehot_labels = tf.sparse_to_dense(
concated, tf.pack([batch_size, NUM_CLASSES]), 1.0, 0.0)改为:
onehot_labels = tf.sparse_to_dense(
concated, tf.stack([batch_size, NUM_CLASSES]), 1.0, 0.0)
4.3 ValueError: Only call `softmax_cross_entropy_with_logits` with named arguments (labels=..., logits=..., ...)
WARNING:tensorflow:From /home/lee/PycharmProjects/mnist/mnist.py:102: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Future major versions of TensorFlow will allow gradients to flow
into the labels input on backprop by default.
See `tf.nn.softmax_cross_entropy_with_logits_v2`.
Traceback (most recent call last):
File "/home/lee/PycharmProjects/mnist/fully_connected_feed.py", line 222, in <module>
tf.app.run()
File "/opt/modules/anaconda3/envs/mnist/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 125, in run
_sys.exit(main(argv))
File "/home/lee/PycharmProjects/mnist/fully_connected_feed.py", line 218, in main
run_training()
File "/home/lee/PycharmProjects/mnist/fully_connected_feed.py", line 138, in run_training
loss = mnist.loss(logits, labels_placeholder)
File "/home/lee/PycharmProjects/mnist/mnist.py", line 102, in loss
name="xentropy")
File "/opt/modules/anaconda3/envs/mnist/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 306, in new_func
return func(*args, **kwargs)
File "/opt/modules/anaconda3/envs/mnist/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 1971, in softmax_cross_entropy_with_logits
logits)
File "/opt/modules/anaconda3/envs/mnist/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 1783, in _ensure_xent_args
"named arguments (labels=..., logits=..., ...)" % name)
ValueError: Only call `softmax_cross_entropy_with_logits` with named arguments (labels=..., logits=..., ...)
解决方案为:
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits,
onehot_labels,
name="xentropy")改为:
cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits,
labels=onehot_labels,
name="xentropy")
4.4 AttributeError: module "tensorflow" has no attribute "scalar_summary"
Traceback (most recent call last):
File "/home/lee/PycharmProjects/mnist/fully_connected_feed.py", line 222, in <module>
tf.app.run()
File "/opt/modules/anaconda3/envs/mnist/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 125, in run
_sys.exit(main(argv))
File "/home/lee/PycharmProjects/mnist/fully_connected_feed.py", line 218, in main
run_training()
File "/home/lee/PycharmProjects/mnist/fully_connected_feed.py", line 141, in run_training
train_op = mnist.training(loss, FLAGS.learning_rate)
File "/home/lee/PycharmProjects/mnist/mnist.py", line 120, in training
tf.scalar_summary(loss.op.name, loss)
AttributeError: module "tensorflow" has no attribute "scalar_summary"
解决方案为:
tf.scalar_summary(loss.op.name, loss)改为:tf.summary.scalar(loss.op.name, loss)
4.5 AttributeError: module "tensorflow" has no attribute "merge_all_summaries"
Traceback (most recent call last):
File "/home/lee/PycharmProjects/mnist/fully_connected_feed.py", line 222, in <module>
tf.app.run()
File "/opt/modules/anaconda3/envs/mnist/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 125, in run
_sys.exit(main(argv))
File "/home/lee/PycharmProjects/mnist/fully_connected_feed.py", line 218, in main
run_training()
File "/home/lee/PycharmProjects/mnist/fully_connected_feed.py", line 147, in run_training
summary_op = tf.merge_all_summaries()
AttributeError: module "tensorflow" has no attribute "merge_all_summaries"
解决方案为:
summary_op = tf.merge_all_summaries()改为:summary_op = tf.summary.merge_all()
4.6 AttributeError: module "tensorflow.train" has no attribute "SummaryWriter"
WARNING:tensorflow:From /opt/modules/anaconda3/envs/mnist/lib/python3.6/site-packages/tensorflow/python/util/tf_should_use.py:189: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.
Traceback (most recent call last):
File "/home/lee/PycharmProjects/mnist/fully_connected_feed.py", line 222, in <module>
tf.app.run()
File "/opt/modules/anaconda3/envs/mnist/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 125, in run
_sys.exit(main(argv))
File "/home/lee/PycharmProjects/mnist/fully_connected_feed.py", line 218, in main
run_training()
File "/home/lee/PycharmProjects/mnist/fully_connected_feed.py", line 160, in run_training
summary_writer = tf.train.SummaryWriter(FLAGS.train_dir,
AttributeError: module "tensorflow.train" has no attribute "SummaryWriter"
解决方案为:
(1) init=initialize_all_variables改为:init = tf.global_variables_initializer
(2) summary_writer = tf.summary.FileWriter(FLAGS.train_dir,graph_def=sess.graph_def)改为:summary_writer = tf.summary.FileWriter(FLAGS.train_dir,graph_def=sess.graph_def)
参考文章
http://www.tensorfly.cn/tfdoc/tutorials/mnist_beginners.html
https://www.cnblogs.com/welhzh/p/6595032.html