def train(self, lr=cfg.lr): train_data = self.reader.mini_batch_iterator(self.reader.train, requires_speaker=True) optim = torch.optim.Adam(self.clf.parameters(), lr=lr) criterion = torch.nn.CrossEntropyLoss() for itr, total_iter, feat, label, files, speaker in train_data: optim.zero_grad() features = [ self.feature_extract_mfcc(a, b, filename, augment=True) for (a, b), filename in zip(feat, files) ] features = [_ for _ in zip(*features)] features[0], features[1], features[2] = self.pad_batch( features[0], features[1], features[2]) features[3], features[4], features[5] = np.array( features[3]), np.array(features[4]), np.array(features[5]) mfcc0, mfcc1, mfcc2 = cuda_(Variable(torch.from_numpy(features[0]).float())), \ cuda_(Variable(torch.from_numpy(features[1]).float())),\ cuda_(Variable(torch.from_numpy(features[2]).float())) mfcc0, mfcc1, mfcc2 = mfcc0.transpose(0, 1), mfcc1.transpose( 0, 1), mfcc2.transpose(0, 1) label = cuda_(Variable(torch.LongTensor(label))) speaker = cuda_(Variable(torch.LongTensor(speaker))) out1, out2 = self.clf(mfcc0, mfcc1, mfcc2, features[3]) loss1 = criterion(out1, label) loss2 = criterion(out2, speaker) loss = loss1 + loss2 loss.backward() optim.step() printer.info( '%d/%d loss:%f %f %f' % (itr, total_iter, loss1.data[0], loss2.data[0], loss.data[0]))
def test_iter(self, itr, total_iter, feat, label, files): features = [ self.feature_extract_mfcc(a, b, filename) for (a, b), filename in zip(feat, files) ] features = [_ for _ in zip(*features)] features[0], features[1], features[2] = self.pad_batch( features[0], features[1], features[2]) features[3], features[4], features[5] = np.array( features[3]), np.array(features[4]), np.array(features[5]) mfcc0, mfcc1, mfcc2 = cuda_(Variable(torch.from_numpy(features[0]).float())), \ cuda_(Variable(torch.from_numpy(features[1]).float())), \ cuda_(Variable(torch.from_numpy(features[2]).float())) mfcc0, mfcc1, mfcc2 = mfcc0.transpose(0, 1), mfcc1.transpose( 0, 1), mfcc2.transpose(0, 1) out, _ = self.clf(mfcc0, mfcc1, mfcc2, features[3]) out = F.softmax(out, dim=1) prob, pred_ = torch.max(out, 1) pred_ = pred_.data.cpu().numpy().tolist() prob = prob.data.cpu().numpy().tolist() return pred_, prob
def train(self, lr=cfg.lr): #train_data = self.reader.mini_batch_iterator(self.reader.train) train_data = self.reader.mini_batch_iterator(self.reader.train) optim = torch.optim.Adam(self.clf.parameters(), lr=lr) criterion = torch.nn.CrossEntropyLoss() for itr, total_iter, feat, label, files in train_data: optim.zero_grad() inp, len0 = self.get_batch_full(feat, files, augment=True) label = cuda_(Variable(torch.LongTensor(label))) out = self.clf(inp, len0) loss = criterion(out, label) loss.backward() optim.step() printer.info('%d/%d loss:%f' % (itr, total_iter, loss.data[0]))
def cvar(arr): v = cuda_(Variable(torch.from_numpy(arr).float())) return v
def __init__(self): super().__init__() self.clf = HMRNN() #self.clf = CNN_SP() self.clf = cuda_(self.clf)
def __init__(self): super().__init__() self.clf = AdvHRNN() self.clf = cuda_(self.clf)