Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions examples/shortest_path/dataGen.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,21 @@ def generateDataset(inPath, outPath):
dataset = generateDataset('data/shortestPath.data', 'data/shorteatPath_')

# for training
training_just_with_labels = False
obsList = []
with open('data/shorteatPath_train.txt', 'r') as f:
obsList = f.read().strip().strip('#evidence').split('#evidence')
dataList = []
for data in dataset['train']:
dataList.append({'g': Variable(torch.from_numpy(data).float(), requires_grad=False)})
for data, label in zip(dataset['train'], dataset['train_label']):
if training_just_with_labels:
dataList.append({'g': (Variable(torch.from_numpy(data).float(), requires_grad=False), {'sp': Variable(torch.from_numpy(label).float().view(-1, 1), requires_grad=False)})})
else:
dataList.append({'g': Variable(torch.from_numpy(data).float(), requires_grad=False)})

# for testing
obsListTest = []
with open('data/shorteatPath_test.txt', 'r') as f:
obsListTest = f.read().strip().strip('#evidence').split('#evidence')
dataListTest = []
for data in dataset['test']:
dataListTest.append({'g': Variable(torch.from_numpy(data).float(), requires_grad=False)})
dataListTest.append({'g': Variable(torch.from_numpy(data).float(), requires_grad=False)})
4 changes: 3 additions & 1 deletion examples/shortest_path/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,13 @@
print('Continuously training for 10 epochs round {}...'.format(i+1))
time1 = time.time()
NeurASPobj.learn(dataList=dataList, obsList=obsList, epoch=10, opt=True, smPickle='data/stableModels.pickle', bar=True)
# If training MLP with just labels, use this line instead of above. Also change training_just_with_labels to True in dataGen.py
# NeurASPobj.learn(dataList=dataList, obsList=obsList, epoch=10, opt=True, smPickle='data/stableModels.pickle', bar=True, alpha=1, lossFunc=torch.nn.BCELoss())
time2 = time.time()
NeurASPobj.testConstraint(dataList=dataListTest, obsList=obsListTest, mvppList=combinations)
print("--- train time: %s seconds ---" % (time2 - time1))
print("--- test time: %s seconds ---" % (time.time() - time2))
print('--- total time from beginning: %s minutes ---' % int((time.time() - startTime)/60) )

print('Storing the trained model into {}'.format(saveModelPath))
torch.save(m.state_dict(), saveModelPath)
torch.save(m.state_dict(), saveModelPath)