抱歉,如果我没有清楚地表达我的问题,英语不是我的第一语言
问题
简短的介绍:
我想训练一个将输入x
(形状为[n_sample, timestamp, feature]
)映射到输出y
(形状完全相同)的模型。这就像映射 2 个空间
更长的版本:
我有 2 个 floatndarray
形状[n_sample, timestamp, feature]
,代表音频文件MFCC
的特征。n_sample
这 2 个ndarray
是 2 个说话者在同一个语料库中的语音,由 DTW 对齐。让我们将这 2 个数组命名x
为y
. 我想训练一个模型,它预测y[k]
给定x[k]
的 . 这就像从空间x
到空间的映射y
,输出必须与输入的形状完全相同
我试过的
这是时间序列问题,所以我决定使用RNN
方法。这是我在 PyTorch 中的代码(我在代码中添加了注释。为简单起见,我删除了平均损失的计算)。请注意,我尝试了许多学习率选项,行为仍然相同
类定义
class Net(nn.Module):
def __init__(self, in_size, hidden_size, out_size, nb_lstm_layers):
super().__init__()
self.in_size = in_size
self.hidden_size = hidden_size
self.out_size = out_size
self.nb_lstm_layers = nb_lstm_layers
# self.fc1 = nn.Linear()
self.lstm = nn.LSTM(input_size=self.in_size, hidden_size=self.hidden_size, num_layers=self.nb_lstm_layers, batch_first=True, bias=True)
# self.fc = nn.Linear(self.hidden_size, self.out_size)
self.fc1 = nn.Linear(self.hidden_size, 128)
self.fc2 = nn.Linear(128, 128)
self.fc3 = nn.Linear(128, self.out_size)
def forward(self, x, h_state):
out, h_state = self.lstm(x, h_state)
output_fc = []
for frame in out:
output_fc.append(self.fc3(torch.tanh(self.fc1(frame)))) # I added fully connected layer to each frame, to make an output with same shape as input
return torch.stack(output_fc), h_state
def hidden_init(self):
if use_cuda:
h_state = torch.stack([torch.zeros(nb_lstm_layers, batch_size, 20) for _ in range(2)]).cuda()
else:
h_state = torch.stack([torch.zeros(nb_lstm_layers, batch_size, 20) for _ in range(2)])
return h_state
训练步骤:
net = Net(20, 20, 20, nb_lstm_layers)
optimizer = optim.Adam(net.parameters(), lr=0.0001, weight_decay=0.0001)
criterion = nn.MSELoss()
for epoch in range(nb_epoch):
count = 0
loss_sum = 0
batch_x = None
for i in (range(len(data))):
# data is my entire data, which contain A and B i specify above.
temp_x = torch.tensor(data[i][0])
temp_y = torch.tensor(data[i][1])
for ii in range(0, data[i][0].shape[0] - nb_frame_in_batch*2 + 1): # Create batches
batch_x, batch_y = get_batches(temp_x, temp_y, ii, batch_size, nb_frame_in_batch)
# this will return 2 tensor of shape (batch_size, nb_frame_in_batch, 20),
# with `batch_size` is the number of sample each time I feed to the net,
# nb_frame_in_batch is the number of frame in each sample
optimizer.zero_grad()
h_state = net.hidden_init()
prediction, h_state = net(batch_x.float(), h_state)
loss = criterion(prediction.float(), batch_y.float())
h_state = (h_state[0].detach(), h_state[1].detach())
loss.backward()
optimizer.step()
问题是,损失似乎没有减少而是波动很大,没有明确的行为
请帮我。任何建议将不胜感激。如果有人可以检查我的代码并提供一些评论,那就太好了。
提前致谢!