我使用 pytorch 為模型構建訓練資料。
def shufflerow(tensor1, tensor2, axis):
row_perm = torch.rand(tensor1.shape[:axis 1]).argsort(axis) # get permutation indices
for _ in range(tensor1.ndim-axis-1): row_perm.unsqueeze_(-1)
row_perm = row_perm.repeat(*[1 for _ in range(axis 1)], *(tensor1.shape[axis 1:])) # reformat this for the gather operation
return tensor1.gather(axis, row_perm),tensor2.gather(axis, row_perm)
class Dataset:
def __init__(self, observation, next_observation):
self.data =(observation, next_observation)
indices = torch.randperm(observation.shape[0])
self.train_samples = (observation[indices ,:], next_observation[indices ,:])
self.test_samples = shufflerow(observation, next_observation, 0)
我也有這個功能,它檢查資料是否轉換為 torch.tensor 并設定設備
def to_tensor(x, device):
if torch.is_tensor(x):
return x
elif isinstance(x, np.ndarray):
return torch.from_numpy(x).to(device=device, dtype=torch.float32)
elif isinstance(x, list):
if all(isinstance(item, np.ndarray) for item in x):
return [torch.from_numpy(item).to(device=device, dtype=torch.float32) for item in x]
elif isinstance(x, tuple):
return (torch.from_numpy(item).to(device=device, dtype=torch.float32) for item in x)
else:
print(f"X:{x} and X's type{type(x)}")
return torch.tensor(x).to(device=device, dtype=torch.float32)
但是通過 Dataset 類 data=Dataset(s1,s2) print(data.train_samples) 傳遞基本上看起來像這樣的輸入資料
(tensor([[-0.3121, -0.9500, 1.4518],
[-0.9903, -0.1391, -4.4141],
[-0.9645, -0.2642, 5.0233],
[-0.6413, 0.7673, -4.5495],
[-0.3073, 0.9516, -1.0128],
[-0.5495, 0.8355, 3.4044],
[-0.5710, -0.8209, -3.2716],
[-0.9388, 0.3445, 3.9225],
[-0.8402, -0.5423, -4.0820]]), tensor([[-0.2723, -0.9622, 0.8342],
[-0.9958, 0.0912, -4.6186],
[-0.8747, -0.4847, 4.7741],
[-0.5495, 0.8355, 3.4044],
[-0.7146, 0.6996, 4.2841],
[-0.7128, -0.7014, -3.7148],
[-0.9915, 0.1303, 4.4200],
[-0.9358, -0.3526, -4.2585]]))
我收到此錯誤訊息
-> 1725 self._target_samples = to_tensor(true_samples)
1726 self._steps = []
/content/data_gen.py in to_tensor(x)
1368 else:
1369 print(f"X:{x} and X's type{type(x)}")
-> 1370 return torch.tensor(x).to(device=device, dtype=torch.float32)
X:<generator object to_tensor.<locals>.<genexpr> at 0x7f380235d6d0> and X's type<class 'generator'>
RuntimeError: Could not infer dtype of generator
任何建議,為什么我會收到此錯誤?
uj5u.com熱心網友回復:
該運算式(torch.from_numpy(item).to(device=device, dtype=torch.float32) for item in x)不是創建元組,而是生成器運算式。由于在您測驗元組的情況下,我懷疑您想要一個元組而不是生成器。嘗試:
elif isinstance(x, tuple):
return tuple(torch.from_numpy(item).to(device=device, dtype=torch.float32) for item in x)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/507298.html
上一篇:在OOP中有控制器類是否常見
