1、乘法
torch中矩阵元素点乘【哈达马积(hadamard product)】为t.mul(batch数据也是)
torch中矩阵乘法为t.mm 涉及到batch的矩阵乘法为t.bmm, t.matmul也可以
2、指定GPU
PyTorch默认使用从0开始的GPU,如果GPU0正在运行程序,需要指定其他GPU。
有如下两种方法来指定需要使用的GPU。
- 类似tensorflow指定GPU的方式,使用CUDA_VISIBLE_DEVICES。
1.1 直接终端中设定:
CUDA_VISIBLE_DEVICES=1 python main.py
1.2 python代码中设定:
import os
os.environ[“CUDA_VISIBLE_DEVICES”] = “2”
见网址:http://www.cnblogs.com/darkknightzh/p/6591923.html
- 使用函数 set_device
import torch
torch.cuda.set_device(id)
该函数见 pytorch-master\torch\cuda_init_.py。
不过官方建议使用CUDA_VISIBLE_DEVICES,不建议使用 set_device 函数。