Ejemplo n.º 1
0
    def test_save_sup_load_rl(self):
        pass

        model_to_save = MockModel(spinn.spinn_core_model.BaseModel,
                                  default_args())

        # Parse command line flags.
        get_flags()
        FLAGS(sys.argv)

        log_temp = tempfile.NamedTemporaryFile()
        ckpt_temp = tempfile.NamedTemporaryFile()

        logger = afs_safe_logger.ProtoLogger(log_temp.name)
        FLAGS.ckpt_path = '.'

        trainer_to_save = ModelTrainer(model_to_save, logger, FLAGS)

        model_to_load = MockModel(spinn.rl_spinn.BaseModel, default_args())
        trainer_to_load = ModelTrainer(model_to_load, logger, FLAGS)

        # Save to and load from temporary file.
        trainer_to_save.save(ckpt_temp.name)
        trainer_to_load.load(ckpt_temp.name, cpu=True)

        compare_models(model_to_save, model_to_load)

        # Cleanup temporary file.
        ckpt_temp.close()
        log_temp.close()
Ejemplo n.º 2
0
    def test_init_models(self):
        MockModel(spinn.spinn_core_model.BaseModel, default_args())
        MockModel(spinn.rl_spinn.BaseModel, default_args())

        MockModel(spinn.spinn_core_model.BaseModel,
                  default_args(use_sentence_pair=True))
        MockModel(spinn.rl_spinn.BaseModel,
                  default_args(use_sentence_pair=True))
Ejemplo n.º 3
0
    def test_soft_eval(self):
        args = default_args()
        args['test_temperature_multiplier'] = 0.0000001
        model = MockModel(Pyramid, args)

        X, transitions = get_batch()

        class Projection(nn.Module):
            def forward(self, x):
                return x[:, :default_args()['model_dim']]

        class mlp(nn.Module):
            def forward(self, x):
                return x

        def selection_fn(selection_input):
            # Compose in random order
            return Variable(torch.rand(selection_input.data.size()[0], 1))

        class Reduce(nn.Module):
            def forward(self, lefts, rights):
                return lefts + rights

        model.encode = Projection()
        model.composition_fn = Reduce()
        model.selection_fn = selection_fn
        model.mlp = mlp()

        model.eval()
        outputs = model(X, transitions)

        assert outputs[0][0].data[0] == (3 + 1 + 2 + 1)
        assert outputs[1][0].data[0] == (3 + 2 + 4 + 5)
Ejemplo n.º 4
0
 def test_pair_rnn(self):
     model = MockModel(RNNModel, default_args(use_sentence_pair=True))
     X, transitions = get_batch_pair()
     assert X.shape == (2, 4, 2)
     assert transitions.shape == (2, 7, 2)
     outputs = model(X, transitions)
     assert outputs.size() == (2, 3)
Ejemplo n.º 5
0
    def test_basic_stack(self):
        model = MockModel(SentenceModel, default_args())

        train = False

        X, transitions = get_batch()

        class Projection(nn.Module):
            def forward(self, x):
                return x[:, :default_args()['model_dim']]

        class Reduce(nn.Module):
            def forward(self, lefts, rights, tracking):
                batch_size = len(lefts)
                return torch.chunk(
                    torch.cat(lefts, 0) - torch.cat(rights, 0), batch_size, 0)

        model.embed.projection = Projection()
        model.spinn.reduce = Reduce()

        model(X, transitions)
        outputs = model.spinn_outp[0]

        assert outputs[0][0].data[0] == (3 - (1 - (2 - 1)))
        assert outputs[1][0].data[0] == ((3 - 2) - (4 - 5))
Ejemplo n.º 6
0
    def test_save_load_model(self):
        model_to_save = MockModel(BaseModel, default_args())
        model_to_load = MockModel(BaseModel, default_args())

        # Save to and load from temporary file.
        temp = tempfile.NamedTemporaryFile()
        torch.save(model_to_save.state_dict(), temp.name)
        model_to_load.load_state_dict(torch.load(temp.name))

        compare_models(model_to_save, model_to_load)

        # Cleanup temporary file.
        temp.close()
Ejemplo n.º 7
0
    def test_validate_transitions_cantskip(self):
        model = MockModel(BaseModel, default_args())

        train = False

        # To Test:
        # 1. Cant SKIP
        # 2. Cant SHIFT
        # 3. Cant REDUCE
        # 4. No change SHIFT
        # 5. No change REDUCE

        bufs = [
            [None],
            [],
            [None],
            [None],
            [None],
            [],
        ]

        stacks = [
            [None],
            [None],
            [None],
            [],
            [],
            [None, None],
        ]

        transitions = [
            2, 1, 0, 0, 0, 1
            ]
        preds = np.array([
            0, 0, 1, 1, 0, 1
            ]).astype(np.int32)


        ret, _ = model.spinn.validate(transitions, preds, stacks, bufs, zero_padded=False)
        expected = np.array([
            2, 1, 0, 0, 0, 1
        ], dtype=np.int32)

        assert all(p == e for p, e in zip(ret.ravel().tolist(), expected.ravel().tolist())), \
            "gold: {}\npreds: {}".format(expected, ret)
Ejemplo n.º 8
0
    def test_save_sup_load_rl(self):
        pass

        model_to_save = MockModel(spinn.pyramid.Pyramid, default_args())
        opt_to_save = optim.SGD(model_to_save.parameters(), lr=0.1)
        trainer_to_save = ModelTrainer(model_to_save, opt_to_save)

        model_to_load = MockModel(spinn.pyramid.Pyramid, default_args())
        opt_to_load = optim.SGD(model_to_load.parameters(), lr=0.1)
        trainer_to_load = ModelTrainer(model_to_load, opt_to_load)

        # Save to and load from temporary file.
        temp = tempfile.NamedTemporaryFile()
        trainer_to_save.save(temp.name, 0, 0)
        trainer_to_load.load(temp.name)

        compare_models(model_to_save, model_to_load)

        # Cleanup temporary file.
        temp.close()
Ejemplo n.º 9
0
    def test_save_sup_load_rl(self):
        scalar = 11
        other_scalar = 0

        model_to_save = MockModel(spinn.fat_stack.BaseModel, default_args())
        opt_to_save = optim.SGD(model_to_save.parameters(), lr=0.1)
        trainer_to_save = ModelTrainer(model_to_save, opt_to_save)

        model_to_load = MockModel(spinn.rl_spinn.BaseModel, default_args())
        opt_to_load = optim.SGD(model_to_load.parameters(), lr=0.1)
        trainer_to_load = ModelTrainer(model_to_load, opt_to_load)

        # Save to and load from temporary file.
        temp = tempfile.NamedTemporaryFile()
        trainer_to_save.save(temp.name, 0, 0)
        trainer_to_load.load(temp.name)

        compare_models(model_to_save, model_to_load)

        # Cleanup temporary file.
        temp.close()
Ejemplo n.º 10
0
 def test_single_cbow(self):
     model = MockModel(SentenceModel, default_args())
     X, transitions = get_batch()
     outputs = model(X, transitions)
     assert outputs.size() == (2, 3)
Ejemplo n.º 11
0
    def test_init_models(self):
        MockModel(spinn.pyramid.Pyramid, default_args())

        MockModel(spinn.pyramid.Pyramid, default_args(use_sentence_pair=True))