def iteration_completed(self, engine):
     iter = (engine.state.iteration - 1)
     if iter % self.log_interval == 0:
         current_state = Message.from_objects(
             deepcopy(engine.state.output['state']))
         current_state['iteration'] = [iter]
         self.model_state = self.model_state.append(current_state)
Beispiel #2
0
def test_Message_from_objects():

    v = vectors.copy()
    t = tensors.copy()
    v['c'] = np.array([1.,2.])
    v['r'] = 'howdy'
    t['a'] = torch.randn(5)
    t['q'] = torch.randn([4,3])
    combined = {**t, **v}

    m = Message.from_objects(t, v)
    assert (set(m.keys()) == set(['c','d','r','b','a','q']))
    for key in ['c','d','b','a','q']:
        assert (m[key][0] == combined[key]).all()
    assert m['r'][0] == combined['r']
    assert len(m) == 1
    test_normalizer = Normalizer()
    test_normalizer.set_state(normalizer.get_state())
    test_normalizer.enable_inference()
    test_normalizer.disable_updates()
    test_normalizer.input = BatchingPipe(test, batch_size=20)
    test_set = TensorPipe(
        FunctionPipe(ShufflerPipe(test_normalizer),
                     function=inject_oversample_weights),
        columns=['examples', 'label', 'prevalence', 'label_index'],
        device=device)

    # Save models
    state = conv_net.get_state()
    Message.from_objects(state).to('json',
                                   path=experiment.open("conv_net.json",
                                                        string_only=True))
    state = deep_net.get_state()
    Message.from_objects(state).to('json',
                                   path=experiment.open("deep_net.json",
                                                        string_only=True))
    state = classifier.get_state()
    Message.from_objects(state).to('json',
                                   path=experiment.open("classifier.json",
                                                        string_only=True))
    Message.from_objects(state).to('json',
                                   path=experiment.open("deep_only.json",
                                                        string_only=True))
    state = None

    # Compute accuracy
if __name__ == "__main__":

    model_state_metric = ModelSaverMetric()
    model_state_metric.attach(trainer, 'state')

    x = Message({'x': np.arange(-10, 10, .2)}).to_tensors()

    # Run initial evaluation
    y_initial = model(x)['y_pred'].detach().numpy()
    initial_loss = loss(model(test_set[0:250]))
    print("Initial loss on test set: {0}".format(initial_loss))

    # Save initial state of model
    file_path = experiment.open('initial_model', string_only=True)
    initial_state = model.get_state()
    Message.from_objects(initial_state).to('json', path=file_path)

    trainer.train(max_epochs=200)

    final_loss = loss(model(test_set[0:250]))
    print("Final loss on test set:: {0}".format(final_loss))

    # Visualize functions
    true_model = NonlinearModel(
        components={
            'a': [params['a']],
            'b': [params['b']],
            'c': [params['c']],
            'd': [0],
            'e': [0]
        })