torch.nn.LeakyReLU(), torch.nn.Linear(10, 10), torch.nn.LeakyReLU(), torch.nn.Linear(10, 10), torch.nn.LeakyReLU(), torch.nn.Linear(10, 10), torch.nn.LeakyReLU(), torch.nn.Linear(10, 2), torch.nn.Softplus(), ) if CUDA: model = model.cuda() emb = emb.cuda() optim = torch.optim.Adam(model.parameters(), lr=10e-4) emb_optim = torch.optim.Adam(emb.parameters(), lr=10e-4) class DyadLSTMDataset(Dataset): def __init__(self): self.labels = [] self.words = [] with open('dyad_dataset_for_lstm.txt') as f: for line in f.readlines(): user1, user2, acts, pred1, num_days1, pred2, num_days2 = line.strip( ).split('\t') acts = json.loads(acts) pred1 = float(pred1) pred2 = float(pred2) labels = [[pred1, pred2] if a[0] == 1 else [pred2, pred1] for a in acts if len(a[3]) > 0]
print('Shuffling indices') random.shuffle(indices) valid_indices = indices[:10000] train_indices = indices[10001:] print('Creating samplers') train_dataset = TensorDataset(features[train_indices], labels[train_indices]) valid_dataset = TensorDataset(features[valid_indices], labels[valid_indices]) train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True) valid_loader = DataLoader(valid_dataset, batch_size=BATCH_SIZE, shuffle=True) num_entities = len(node_ids) emb = EmbeddingBag(num_entities, DIM, mode='sum') optim = torch.optim.Adam(emb.parameters(), lr=10e-4) if CUDA: emb = emb.cuda() rev_node_ids = dict((x[1], x[0]) for x in node_ids.items()) def compute_loss(batch): ids, labels = batch labels = torch.FloatTensor(labels.numpy().tolist()) ids = Variable(ids) labels = Variable(labels) if CUDA: ids = ids.cuda() labels = labels.cuda()