Esempio n. 1
0
def run(training_data, test_data, problog_files):
    queries = load(training_data)
    test_queries = load(test_data)

    problog_string = ''
    for problog_file in problog_files:
        with open(problog_file) as f:
            problog_string += f.read()
            problog_string += '\n\n'

    network = MNIST_Net()
    net = Network(network, 'mnist_net', neural_predicate)
    net.optimizer = torch.optim.Adam(network.parameters(), lr=0.001)
    model = Model(problog_string, [net], caching=False)
    optimizer = Optimizer(model, 2)

    train_model(model,
                queries,
                1,
                optimizer,
                test_iter=1000,
                test=test_MNIST,
                snapshot_iter=10000)
Esempio n. 2
0
def run(training_data,
        test_data,
        problog_files,
        problog_train_files=(),
        problog_test_files=()):
    queries = load(training_data)
    test_queries = load(test_data)

    problog_string = add_files_to(problog_files, '')

    problog_train_string = add_files_to(problog_train_files, problog_string)
    problog_test_string = add_files_to(problog_test_files, problog_string)

    network = MNIST_Net()
    net = Network(network, 'mnist_net', neural_predicate)
    net.optimizer = torch.optim.Adam(network.parameters(), lr=0.001)
    model_to_train = Model(problog_train_string, [net], caching=False)
    optimizer = Optimizer(model_to_train, 2)

    model_to_test = Model(problog_test_string, [net], caching=False)

    train_model(model_to_train,
                queries,
                1,
                optimizer,
                test_iter=len(queries),
                test=lambda _: my_test(
                    model_to_test,
                    test_queries,
                    test_functions={
                        'mnist_net':
                        lambda *args, **kwargs: neural_predicate(
                            *args, **kwargs, dataset='test')
                    },
                ),
                log_iter=1000,
                snapshot_iter=len(queries))
Esempio n. 3
0
from network import Network
from examples.NIPS.MNIST.mnist import MNIST_Net, neural_predicate


class FC(nn.Module):
    def __init__(self, in_size, out_size):
        super(FC, self).__init__()
        self.fc1 = nn.Linear(in_size, out_size,bias=False)
        self.softmax = nn.Softmax(1)

    def forward(self, x):
        x = self.fc1(x)
        x = self.softmax(x)
        return x


def colour_predicate(net, *colour):
    d = torch.FloatTensor(colour)
    d = Variable(d.unsqueeze(0))
    outputs = net.net(d)
    return outputs.squeeze(0)


coin_network = MNIST_Net(2)
coin_net = Network(coin_network, 'coin_net', neural_predicate)
coin_net.optimizer = optim.Adam(coin_network.parameters(), lr=1e-3)

colour_network = FC(3, 3)
colour_net = Network(colour_network, 'colour_net', colour_predicate)
colour_net.optimizer = optim.Adam(colour_network.parameters(), lr=1.0)
Esempio n. 4
0
import json

from train import train_model
from data_loader import load
from examples.NIPS.MNIST.mnist import test_MNIST, MNIST_Net, MNIST_Net2, neural_predicate
from model import Model
from optimizer import Optimizer
from network import Network
import torch

queries = load('train_data.txt')

with open('abs.pl') as f:
    problog_string = f.read()

network = MNIST_Net()
network.load_state_dict(torch.load('sd_rec_cnn.pt'))
network.eval()
net = Network(network, 'mnist_net', neural_predicate)
net.optimizer = torch.optim.Adam(network.parameters(), lr=0.001)
model = Model(problog_string, [net], caching=False)
optimizer = Optimizer(model, 2)

log = {}

logs = test_MNIST(model)
for e in logs:
    log[e[0]] = e[1]

with open('notl_rec2abs_dpl.json', 'w') as outfile:
    json.dump(log, outfile)
Esempio n. 5
0
from train import train_model
from data_loader import load
from examples.NIPS.MNIST.mnist import test_MNIST, MNIST_Net, neural_predicate
from model import Model
from optimizer import Optimizer
from network import Network
import torch

queries = load('train_data.txt')

with open('addition.pl') as f:
    problog_string = f.read()

network = MNIST_Net()
net = Network(network, 'mnist_net', neural_predicate)
net.optimizer = torch.optim.Adam(network.parameters(), lr=0.001)
model = Model(problog_string, [net], caching=False)
optimizer = Optimizer(model, 2)

train_model(model,
            queries,
            1,
            optimizer,
            test_iter=1000,
            test=test_MNIST,
            snapshot_iter=10000)