Мой код:
Код: Выделить всё
import torch.nn as nn
class A(nn.Module):
def __init__(self):
super().__init__()
self.fc = nn.Linear(10, 5)
self.relu = nn.ReLU()
def forward(self, x):
return self.relu(self.fc(x))
a = A().to('cuda')
weight = {}
for key, value in a.state_dict().items():
weight[key] = value
a.to('cpu')
print("a.state_dict() device:", [t.device for t in a.state_dict().values()]) # in CPU
print("weight device:", [t.device for t in weight.values()]) # still in GPU
Код: Выделить всё
a.state_dict() device: [device(type='cpu'), device(type='cpu'), device(type='cpu'), device(type='cpu')]
weight device: [device(type='cuda', index=0), device(type='cuda', index=0), device(type='cuda', index=0), device(type='cuda', index=0)]
Подробнее здесь: https://stackoverflow.com/questions/798 ... in-gpu-why