文章目录
1 可查询站点
tensorflow官网
TensorFlow 2/2.0中文文档
tensorflow2_tutorials_chinese
2 安装
2.1 TensorFlow 1.x
Tensorflow在win10和linux下安装基本一样。1.x系列建议安装1.15.0,因为在mvnrepository提供了较为稳定的Java工程库。
# cuda10.2
pip3 install tensorflow-gpu==1.15.0
# cpu
pip3 install tensorflow-cpu==1.15.0
2.2 TensorFlow 2.x
1.x时代已经过去,开始使用2.x
pip3 install tensorflow-gpu==2.3.0
pip3 install tensorflow-cpu==2.3.0
【特别注意】
2.3 TensorFlow的一些版本约束
Linux下的https://www.tensorflow.org/install/source#tested_build_configurations
Win下的https://www.tensorflow.org/install/source_windows#tested_build_configurations
当然也可以自己去编译TensorFlow
Win GPU版本 | Python 版本 | 编译器 | 构建工具 | cuDNN | CUDA |
---|---|---|---|---|---|
tensorflow_gpu-2.3.0 | 3.5-3.8 | MSVC 2019 | Bazel 3.1.0 | 7.4 | 10.1 |
tensorflow_gpu-2.2.0 | 3.5-3.8 | MSVC 2019 | Bazel 2.0.0 | 7.4 | 10.1 |
tensorflow_gpu-2.1.0 | 3.5-3.7 | MSVC 2019 | Bazel 0.27.1-0.29.1 | 7.4 | 10.1 |
tensorflow_gpu-2.0.0 | 3.5-3.7 | MSVC 2017 | Bazel 0.26.1 | 7.4 | 10 |
tensorflow_gpu-1.15.0 | 3.5-3.7 | MSVC 2017 | Bazel 0.26.1 | 7.4 | 10 |
tensorflow_gpu-1.14.0 | 3.5-3.7 | MSVC 2017 | Bazel 0.24.1-0.25.2 | 7.4 | 10 |
tensorflow_gpu-1.13.0 | 3.5-3.7 | MSVC 2015 update 3 | Bazel 0.19.0-0.21.0 | 7.4 | 10 |
tensorflow_gpu-1.12.0 | 3.5-3.6 | MSVC 2015 update 3 | Bazel 0.15.0 | 7 | 9 |
tensorflow_gpu-1.11.0 | 3.5-3.6 | MSVC 2015 update 3 | Bazel 0.15.0 | 7 | 9 |
tensorflow_gpu-1.10.0 | 3.5-3.6 | MSVC 2015 update 3 | Cmake v3.6.3 | 7 | 9 |
tensorflow_gpu-1.9.0 | 3.5-3.6 | MSVC 2015 update 3 | Cmake v3.6.3 | 7 | 9 |
tensorflow_gpu-1.8.0 | 3.5-3.6 | MSVC 2015 update 3 | Cmake v3.6.3 | 7 | 9 |
tensorflow_gpu-1.7.0 | 3.5-3.6 | MSVC 2015 update 3 | Cmake v3.6.3 | 7 | 9 |
tensorflow_gpu-1.6.0 | 3.5-3.6 | MSVC 2015 update 3 | Cmake v3.6.3 | 7 | 9 |
tensorflow_gpu-1.5.0 | 3.5-3.6 | MSVC 2015 update 3 | Cmake v3.6.3 | 7 | 9 |
tensorflow_gpu-1.4.0 | 3.5-3.6 | MSVC 2015 update 3 | Cmake v3.6.3 | 6 | 8 |
tensorflow_gpu-1.3.0 | 3.5-3.6 | MSVC 2015 update 3 | Cmake v3.6.3 | 6 | 8 |
tensorflow_gpu-1.2.0 | 3.5-3.6 | MSVC 2015 update 3 | Cmake v3.6.3 | 5.1 | 8 |
tensorflow_gpu-1.1.0 | 3.5 | MSVC 2015 update 3 | Cmake v3.6.3 | 5.1 | 8 |
tensorflow_gpu-1.0.0 | 3.5 | MSVC 2015 update 3 | Cmake v3.6.3 | 5.1 | 8 |
Linux GPU版本 | Python 版本 | 编译器 | 构建工具 | cuDNN | CUDA |
---|---|---|---|---|---|
tensorflow-2.1.0 | 2.7、3.5-3.7 | GCC 7.3.1 | Bazel 0.27.1 | 7.6 | 10.1 |
tensorflow-2.0.0 | 2.7、3.3-3.7 | GCC 7.3.1 | Bazel 0.26.1 | 7.4 | 10.0 |
tensorflow_gpu-1.14.0 | 2.7、3.3-3.7 | GCC 4.8 | Bazel 0.24.1 | 7.4 | 10.0 |
tensorflow_gpu-1.13.1 | 2.7、3.3-3.7 | GCC 4.8 | Bazel 0.19.2 | 7.4 | 10.0 |
tensorflow_gpu-1.12.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.15.0 | 7 | 9 |
tensorflow_gpu-1.11.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.15.0 | 7 | 9 |
tensorflow_gpu-1.10.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.15.0 | 7 | 9 |
tensorflow_gpu-1.9.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.11.0 | 7 | 9 |
tensorflow_gpu-1.8.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.10.0 | 7 | 9 |
tensorflow_gpu-1.7.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.9.0 | 7 | 9 |
tensorflow_gpu-1.6.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.9.0 | 7 | 9 |
tensorflow_gpu-1.5.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.8.0 | 7 | 9 |
tensorflow_gpu-1.4.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.5.4 | 6 | 8 |
tensorflow_gpu-1.3.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.4.5 | 6 | 8 |
tensorflow_gpu-1.2.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.4.5 | 5.1 | 8 |
tensorflow_gpu-1.1.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.4.2 | 5.1 | 8 |
tensorflow_gpu-1.0.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.4.2 | 5.1 | 8 |
2.4 tensorboard
pip3 install tensorboard
3 备注
3.1 TensorFlow 2.x后Keras和tf.keras选择问题
- 【1】tf.keras和TensorFlow 2.x集成更为紧密,如果以TensorFlow为后端,那么建议直接使用tf.keras,同时tf.keras支持TF 2.x的所有特性如基于tf.data作为数据输入、TPU训练、eager mode、完美兼容等
- 【2】如果使用其他平台如pytorch在keras上,那么建议用keras
4 简单总结
4.1 TensorFlow1.x系列概念
import tensorflow tf
a= tf.placeholder(tf.types.float32)
b= tf.placeholder(tf.types.float32)
c= tf.mul(a, b)
init_op = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init_op)
result=sess.run([c], feed_dict={a:[7.], b:[2.]})
print(result)
4.2 TensorFlow2.x系列概念
这些查询站点见顶端。
- 【1】张量类型
【tf.Variable 】:变量 Tensor,需要指定初始值,常用于定义可变参数,例如神经网络的权重。
**【tf.constant 】**常量 Tensor,需要指定初始值,定义不变化的张量。
# 形状为 (2, 2) 的二维变量
v = tf.Variable([[1, 2], [3, 4]])
# 形状为 (2, 2) 的二维常量
c = tf.constant([[1, 2], [3, 4]])
# 直接输出numpy数组
c.numpy()
- 【2】Eager Execution
不再需要构建图,直接和Numpy一样命令行式运算即可。
a = tf.constant([1., 2., 3., 4., 5., 6.], shape=[2, 3])
b = tf.constant([7., 8., 9., 10., 11., 12.], shape=[3, 2])
c = tf.linalg.matmul(a, b) # 矩阵乘法
print(c)
- 【3】自动微分
import tensorflow as tf
import numpy as np
# f(x) = a*x**2 + b*x + c的导数
x = tf.Variable(0.0, name = "x", dtype = tf.float32)
a = tf.constant(1.0)
b = tf.constant(-2.0)
c = tf.constant(1.0)
with tf.GradientTape() as tape:
y = a*tf.pow(x,2) + b*x + c
# 求一阶导数
dy_dx = tape.gradient(y, x)
tf.print(dy_dx) # -2
# 求二阶导数
with tf.GradientTape() as tape2:
with tf.GradientTape() as tape1:
y = a*tf.pow(x,2) + b*x + c
dy_dx = tape1.gradient(y,x)
dy2_dx2 = tape2.gradient(dy_dx,x)
tf.print(dy2_dx2)
# 多项求导
with tf.GradientTape() as tape:
tape.watch([a,b,c])
y = a*tf.pow(x,2) + b*x + c
dy_dx,dy_da,dy_db,dy_dc = tape.gradient(y, [x,a,b,c])
tf.print(dy_da)
tf.print(dy_dc)
5 TensorBoard使用技巧
TensorBoard并不局限于tensorflow,也可以用在pytorch,keras。keras比较特殊,是集成式的不需要自己去实现保存loss。
5.1 keras中使用
from tensorflow.keras.callbacks import TensorBoard
model.fit([trainX, trainY], [trainY, trainY],
batch_size=128,
epochs=20,
validation_data=([testX, testY], [testY, testY]),
callbacks=[TensorBoard(log_dir='./log_dir')])
然后直接启动TensorBoard即可。
5.2 TensorFlow保存loss和acc
# 导入库
try:
image_summary = tf.image_summary
scalar_summary = tf.scalar_summary
histogram_summary = tf.histogram_summary
merge_summary = tf.merge_summary
SummaryWriter = tf.train.SummaryWriter
except:
image_summary = tf.summary.image
scalar_summary = tf.summary.scalar
histogram_summary = tf.summary.histogram
merge_summary = tf.summary.merge
SummaryWriter = tf.summary.FileWriter
# 加入监测key
with tf.name_scope('loss'):
loss = -tf.reduce_sum(y * tf.log(y_conv))
loss_summary = scalar_summary('loss', loss)
with tf.name_scope('accuracy'):
acc = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
acc_summary = scalar_summary('acc', acc)
#合并变量
#merged = merge_summary([loss_summary, acc_summary])
#定义writer保存路径
writer = SummaryWriter('save/logs', sess.graph)
#更新merged值并计数(每次一次更新+1)
summary, _ = sess.run([merged, train_step], feed_dict={x:x_batch, y:y_batch})
counter += 1
writer.add_summary(summary, counter)
# 终端运行即可
tensorboard --logdir=save/logs
5.3 PyTorch使用TensorBoard
详细见PyTorch一些trick
另外需要注意的是TensorFlow是使用TensorBoard,而非Tensorflow一般使用TensorBoardX。
6 VisualDL
VisualDL也是一款非常好用的深度学习可视化工具,很接近TensorBoard,非常中文式人性化。
如何在PyTorch中使用VisualDL
VisualDL 使用文档