1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| from torch import nn model2 = nn.DataParallel(model1) model2.cuda()
````
```python from torch import nn import torch as t class Model(nn.Module): def __init__(self, input_size, output_size): super(Model, self).__init__() self.fc = nn.Linear(input_size, output_size) def forward(self, input): output = self.fc(input) print(" In Model: input size", input.size(), "output size", output.size()) return output ---- model1 = Model(3,4) print(model1) for var in model1.parameters(): print(var)
Model( (fc): Linear(in_features=3, out_features=4, bias=True) )
Parameter containing: tensor([[ 0.1964, 0.4389, -0.2216], [-0.1046, -0.2055, -0.5383], [ 0.0673, 0.0949, 0.5205], [ 0.5473, -0.3700, -0.4179]]) Parameter containing: tensor([ 0.2416, 0.4188, -0.0096, 0.1569]) ---- model2 = nn.DataParallel(model1) print(model2) for var in model2.parameters(): print(var)
DataParallel( (module): Model( (fc): Linear(in_features=3, out_features=4, bias=True) ) ) Parameter containing: tensor([[ 0.1964, 0.4389, -0.2216], [-0.1046, -0.2055, -0.5383], [ 0.0673, 0.0949, 0.5205], [ 0.5473, -0.3700, -0.4179]], device='cuda:0') Parameter containing: tensor([ 0.2416, 0.4188, -0.0096, 0.1569], device='cuda:0') ---- model2.cuda() print(model2) for var in model2.parameters(): print(var)
DataParallel( (module): Model( (fc): Linear(in_features=3, out_features=4, bias=True) ) ) Parameter containing: tensor([[ 0.1964, 0.4389, -0.2216], [-0.1046, -0.2055, -0.5383], [ 0.0673, 0.0949, 0.5205], [ 0.5473, -0.3700, -0.4179]], device='cuda:0') Parameter containing: tensor([ 0.2416, 0.4188, -0.0096, 0.1569], device='cuda:0')
|