我正在使用 PyTorch 进行神经网络训练。我试图训练我的网络来替代匈牙利算法。我对成本矩阵中的每一行都有神经网络,大小为 500x500 - 包含 500 个列表的列表,每个列表包含 500 个元素。对于训练,我将每个矩阵中的一个特定行(包含 500 个元素的列表)作为输入传递,分配索引作为输出传递。批量大小为十。因此,每批包含 10 个列表的列表,每个列表包含 500 个元素,并且列表具有 10 个整数作为已分配给每一行的索引。
[tensor([[225., 297., 69., ..., 169., 223., 3.],
[107., 225., 2., ..., 95., 272., 85.],
[126., 37., 108., ..., 116., 225., 278.],
...,
[ 33., 158., 55., ..., 182., 266., 23.],
[258., 11., 103., ..., 31., 218., 289.],
[ 7., 113., 170., ..., 31., 275., 289.]]), tensor([329, 271, 40, 460, 466, 375, 206, 426, 263, 380])]
这对的第一个元素作为 net 的输入传递,第二个元素用于损失计算
m_size = 500
class Net(nn.Module):
def __init__(self):
super().__init__()
self.fullyConnectedLayer1 = nn.Linear(500, 100)
self.fullyConnectedLayer2 = nn.Linear(100, 100)
self.fullyConnectedLayer3 = nn.Linear(100, 100)
self.fullyConnectedLayer4 = nn.Linear(100, 500)
def forward(self, x):
x = torch.relu(self.fullyConnectedLayer1(x))
x = torch.relu(self.fullyConnectedLayer2(x))
x = torch.relu(self.fullyConnectedLayer3(x))
x = self.fullyConnectedLayer4(x)
return f.log_softmax(x, dim=1)
if __name__ == '__main__':
netBunch = []
optBunch = []
preTensorFt = []
preTensorLbl = []
for nnet in range(m_size):
net = Net()
optimizer = opt.Adam(net.parameters(), lr=0.001)
netBunch.append(net)
optBunch.append(optimizer)
loss = None
for net in range(len(netBunch)):
neural = netBunch[net]
optim = optBunch[net]
for epoch in range(3):
preTensorFt = []
preTensorLbl = []
batch = []
for sample in range(80000):
i_file = str(sys.path[0]) + '\\dataset\\in_sample_{0}.txt'.format(sample)
o_file = str(sys.path[0]) + '\\dataset\\out_sample_{0}.txt'.format(sample)
matrix = parse.parse_from_file(i_file, m_size)
res_mx = parse.parse_from_file(o_file, m_size)
preTensorFt.append(matrix[net])
preTensorLbl.append(res_mx[net].index(1))
if (sample % 10 == 9) and (sample != 0):
tensorFt = torch.tensor(preTensorFt, dtype=torch.float32)
tensorLbl = torch.LongTensor(preTensorLbl)
batch.append([tensorFt, tensorLbl])
preTensorFt = []
preTensorLbl = []
for pair in batch:
X, y = pair
neural.zero_grad()
output = neural(X)
loss = f.nll_loss(output, y)
loss.backward()
optim.step()
batch = []
print('Loss after batch calc = {0}'.format(loss))
print(loss)
正如我想象的那样,我必须将一批行作为输入传递,并将为此行索引分配的一批作为输出传递
但最终损失在 6.2 附近停止,并且不再减少。可能是我失去了一些基本概念。提前感谢您的帮助