莫烦主页:https://mofanpy.com/
5. 高阶内容
5.1 为什么 torch 是动态的
TensorFlow 是先搭建好一个计算系统(计算图),一旦搭建好了,就不能改动了,所有的计算都会在这个图中流动。
PyTorch 是动态搭建,动态计算,每次都会重新搭建一个新的计算图。
用 RNN 的例子展示动态计算时,数据的格式是 ( batch, time_step, input_size ),通常在 TensorFlow 中会将 batch 设置为 None ,可以让 batch_size 随时产生变化;但有时 time_step 也是随机变化的,也需要将 time_step 设置为 None,但是 TensorFlow 不支持两个随机变化的维度,Torch 当中可以。
5.2 GPU(CUDA) 显卡加速运算
利用 CNN 的例子来展示如何修改,使得在 GPU 上进行运算
- 参数移动
# 每次 loader train data 以后都需要将 x 移动到 cuda 中
b_x = x.cuda() # Tensor on GPU
# 移动 test data
test_x = torch.unsqueeze(test_data.test_data, dim=1).type(torch.FloatTensor)[:2000].cuda()/255. # Tensor on GPU
- 计算图纸移动
pred_y = torch.max(test_output, 1)[1].cuda().data # move the computation in GPU
# m

最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



