我正在我的 M1 Mac 上使用 PyTorch 1.13.0 训练模型(我也在每晚构建 torch-1.14.0.dev20221207 上尝试过此操作,但无济于事),并希望使用 MPS 硬件加速。我的项目中有以下相关代码,用于将模型和输入张量发送到 MPS:
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu") # This always results in MPS
model.to(device)
Run Code Online (Sandbox Code Playgroud)
...在我的数据集子类中:
class MyDataset(Dataset):
def __init__(self, df, window_size):
self.df = df
self.window_size = window_size
self.data = []
self.labels = []
for i in range(len(df) - window_size):
x = torch.tensor(df.iloc[i:i+window_size].values, dtype=torch.float, device=device)
y = torch.tensor(df.iloc[i+window_size].values, dtype=torch.float, device=device)
self.data.append(x)
self.labels.append(y)
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
return self.data[idx], self.labels[idx]
Run Code Online (Sandbox Code Playgroud)
这会在我的第一个训练步骤中产生以下回溯:
Traceback (most recent call last):
File …Run Code Online (Sandbox Code Playgroud)