一、在解决一些实际问题时,pytorch中自带的激活函数可能不能满足需求,就需要自定义一些激活函数,比如我需要一个使得输出值在0到140之间的激活层,pytorch中不含有,所以自定义:
#define a self activation function
class Act_fun(nn.Module):
def __init__(self):
super(Act_fun, self).__init__()
def forward(self, x):
x = F.sigmoid(x)
x = x * 140
return x
在之后的使用中,直接调用Act_op()即可,如:
self.linear2 = nn.Sequential(nn.Linear(1024,1980),Act_fun())
二、多块GPU下的同时训练
目前用的服务器中有6块1080Ti,编号是从0-5,为了全部使用,需要用到pytorch中的nn.DataParallel()函数来进行数据的并行:
#Define Network
model = Net(args.input_channel,args.output_channel)
#using the multiple GPU to train the model
if torch.cuda.device_count()>1:
model = nn.DataParallel(model)
model.to(device)
DataParallel()中有个参数,divice_ids,当不设置值是使用的是全部的GPU,设置值时,如divice_ids=[0,1,2,3],使用的第一块到第四块GPU。不过这种方法存在不均衡的问题,即主GPU利用率高而次GPU利用率低。