tf2.0
练习代码 pex 基于tensorflow2.0
tensorflow_vs_pytorch
tf1.* 静态图
import tensorflow as tf
print(tf.__version__) # 1.9.0
x = tf.Variable(0.)
y = tf.Variable(1.)
print(x)
print(y)
# 构建计算图
# x = x + y
add_op = x.assign(x +y)
# y = y / 2
div_op = y.assign(y / 2)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(50):
sess.run(add_op)
sess.run(div_op)
print(x.eval())
tf2.* 动态图 -> tf1.x中调用tf.enable_eager_execution()方法打开eager mode
import tensorflow as tf
print(tf.__version__) # 2.0.0
x = tf.constant(0.)
y = tf.constant(1.)
for i in range(50):
x = x + y
y = y / 2
print(x.numpy())
PyTorch 动态图
import torch
print(torch.__version__) # 1.4.0
x = torch.Tensor([0.])
y = torch.Tensor([1.])
for i in range(50):
x = x + y
y = y / 2
print(x)
分类问题与回归问题
分类问题预测的是类别,模型的输出是概率分布
回归问题预测是值,模型的输出是一个实数值
目标函数 ↓
- 分类问题–衡量目标类别与当前预测的差距
e.g. 三分类问题,输出:[0.2, 0.7, 0.1] -> 第1类
真实类别第2类 -> one_hot -> [0, 0, 1]
one_hot编码,把正整数变为向量表达,生成一个不小于正整数的向量,只有正整数的位置为1,其余位置为0
- 分类常用损失函数:平方差损失、交叉熵损失
- 回归问题–预测值与真实值差距
- 回归常用损失函数:平方差损失、绝对值损失
回调函数
tf.keras.callbacks
- EarlyStopping
- ModelCheckpoint
- TensorBoard
在命令行中调用tensorboard:tensorboard –logdir=[你的文件夹名称]