我正试图用pytorch编写一个卷积神经网络。我对机器学习和PyTorch非常陌生,所以我对软件包不是很熟悉
我已经编写了一个自定义数据集,它正确地从csv文件加载了我的数据。然而,当我将其加载到数据加载器中时,我的系统监视器显示python使用了大量内存。我目前只使用了我的数据集的一小部分,一个实例使用了大约5 Gig作为数据加载器
我的数据集是一维张量。每一个都非常长——大约3300万个值。我曾经
sys.getsizeof(train_set.sample_list[0][0].storage())
检查基本张量的大小,它只有271兆字节
此外,如果我继续并创建CNN的实例,初始化器会消耗内存,直到内核崩溃。原因我不清楚
以下是我的自定义数据集的代码:
def __init__(self, csv_file, train):
self.train = train
self.df_tmp = pd.read_csv(csv_file, header=None, sep='\t')
self.df_tmp.drop(self.df_tmp.shape[1]-1, axis=1, inplace=True)
self.df = self.df_tmp.transpose()
self.sample_list = []
for i in range(self.df.shape[0]): #num rows, 33 million ish
sample = torch.tensor([self.df.iloc[i][1:].values])
label = torch.tensor(self.df.iloc[i][0])
self.sample_list.append((sample, label))
def __len__(self):
return len(self.sample_list)
def __getitem__(self, idx):
return self.sample_list[idx]
以及NN的代码:
#input batch shape is (9 x 33889258 x 1)
def __init__(self):
super(CNN, self).__init__()
#input channels 1, output 3
self.conv1 = torch.nn.Conv1d(1, out_channels=3, kernel_size=(100), stride=10, padding=1)
#size in is 3,1,33889258
self.pool = torch.nn.MaxPool1d(kernel_size=2, stride=2, padding=0)
self.fc1 = torch.nn.Linear(45750366, 1000) #3 * 1 * 3388917
self.fc2 = torch.nn.Linear(1000, 2)
def forward(self, x):
#size: (1x1x33889258) to (3x1x33889258)
tmp = self.conv1(x.float())
x = F.relu(tmp) #
x = self.pool(x)
#whatever shape comes out of here needs to go into x.view
x = x.view(45750366) #-1, 1*1*3388927
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
return(x)
我的一些输入大小可能已关闭,我仍在努力解决,但内存问题阻碍了我的进步
您将所有数据点存储在列表中(即内存中),因此它有点像定制数据集/数据加载器的用途。您应该只在dataset类中保留dataframe的引用,并为每个索引返回正确的数据,如
一个小提示:您正在从dataset的getitem方法返回张量,返回纯numpy数组更容易,因为dataloader会将其转换为 Pytork张量
相关问题 更多 >
编程相关推荐