From a839e0e0dc21614df185fd69f5a12fec25a9c9da Mon Sep 17 00:00:00 2001 From: Maziar Sargordi <62127902+msargordi@users.noreply.github.com> Date: Sat, 11 Nov 2023 14:54:23 -0500 Subject: [PATCH 1/2] Update dataGen.py adding labels when training_just_with_labels=True --- examples/shortest_path/dataGen.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/shortest_path/dataGen.py b/examples/shortest_path/dataGen.py index bfa1b7f..a64543e 100755 --- a/examples/shortest_path/dataGen.py +++ b/examples/shortest_path/dataGen.py @@ -74,12 +74,16 @@ 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 = [] @@ -87,4 +91,4 @@ def generateDataset(inPath, outPath): 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)}) \ No newline at end of file + dataListTest.append({'g': Variable(torch.from_numpy(data).float(), requires_grad=False)}) From c5cd09dfef0c29a6dd376102c6ef6e3dadc9f17c Mon Sep 17 00:00:00 2001 From: Maziar Sargordi <62127902+msargordi@users.noreply.github.com> Date: Sat, 11 Nov 2023 14:56:59 -0500 Subject: [PATCH 2/2] a commented example for training with just labels added. --- examples/shortest_path/train.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/shortest_path/train.py b/examples/shortest_path/train.py index 714555f..832dde9 100755 --- a/examples/shortest_path/train.py +++ b/examples/shortest_path/train.py @@ -115,6 +115,8 @@ 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)) @@ -122,4 +124,4 @@ 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) \ No newline at end of file +torch.save(m.state_dict(), saveModelPath)