我正在研究带有数字的经典示例。我想创建我的第一个神经网络来预测数字图像 {0,1,2,3,4,5,6,7,8,9} 的标签。所以第一列train.txt有标签,所有其他列是每个标签的特征。我定义了一个类来导入我的数据:
class DigitDataset(Dataset):
"""Digit dataset."""
def __init__(self, file_path, transform=None):
"""
Args:
csv_file (string): Path to the csv file with annotations.
root_dir (string): Directory with all the images.
transform (callable, optional): Optional transform to be applied
on a sample.
"""
self.data = pd.read_csv(file_path, header = None, sep =" ")
self.transform = transform
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
labels = self.data.iloc[idx,0]
images = self.data.iloc[idx,1:-1].values.astype(np.uint8).reshape((1,16,16))
if self.transform is not None:
sample = …Run Code Online (Sandbox Code Playgroud) python pattern-recognition machine-learning python-3.x pytorch