Beispiel #1
0
def test_model_get_outputs(backend_default, data):
    (X_train, y_train), (X_test, y_test), nclass = load_mnist(path=data)
    train_set = ArrayIterator(X_train[:backend_default.bsz * 3])

    init_norm = Gaussian(loc=0.0, scale=0.1)

    layers = [Affine(nout=20, init=init_norm, bias=init_norm, activation=Rectlin()),
              Affine(nout=10, init=init_norm, activation=Logistic(shortcut=True))]
    mlp = Model(layers=layers)
    out_list = []
    mlp.initialize(train_set)
    for x, t in train_set:
        x = mlp.fprop(x)
        out_list.append(x.get().T.copy())
    ref_output = np.vstack(out_list)

    train_set.reset()
    output = mlp.get_outputs(train_set)
    assert np.allclose(output, ref_output)
Beispiel #2
0
def test_model_get_outputs(backend_default, data):
    (X_train, y_train), (X_test, y_test), nclass = load_mnist(path=data)
    train_set = ArrayIterator(X_train[:backend_default.bsz * 3])

    init_norm = Gaussian(loc=0.0, scale=0.1)

    layers = [
        Affine(nout=20, init=init_norm, bias=init_norm, activation=Rectlin()),
        Affine(nout=10, init=init_norm, activation=Logistic(shortcut=True))
    ]
    mlp = Model(layers=layers)
    out_list = []
    mlp.initialize(train_set)
    for x, t in train_set:
        x = mlp.fprop(x)
        out_list.append(x.get().T.copy())
    ref_output = np.vstack(out_list)

    train_set.reset()
    output = mlp.get_outputs(train_set)
    assert np.allclose(output, ref_output)
Beispiel #3
0
def main():
    # setup the model and run for num_epochs saving the last state only
    # this is at the top so that the be is generated
    model = gen_model(args.backend)

    # setup data iterators
    (X_train, y_train), (X_test, y_test), nclass = load_mnist(path=args.data_dir)
    NN = batch_size*5  # avoid partial mini batches
    if args.backend == 'nervanacpu' or args.backend == 'cpu':
        # limit data since cpu backend runs slower
        train = ArrayIterator(X_train[:NN], y_train[:NN],
                              nclass=nclass, lshape=(1, 28, 28))
        valid = ArrayIterator(X_test[:NN], y_test[:NN],
                              nclass=nclass, lshape=(1, 28, 28))
    else:
        train = ArrayIterator(X_train, y_train, nclass=nclass, lshape=(1, 28, 28))
        valid = ArrayIterator(X_test, y_test, nclass=nclass, lshape=(1, 28, 28))

    # serialization related
    cost = GeneralizedCost(costfunc=CrossEntropyBinary())
    opt_gdm = GradientDescentMomentum(learning_rate=0.1, momentum_coef=0.9)

    checkpoint_model_path = os.path.join('./', 'test_oneshot.pkl')
    checkpoint_schedule = 1  # save at every step

    callbacks = Callbacks(model)
    callbacks.add_callback(SerializeModelCallback(checkpoint_model_path,
                                                  checkpoint_schedule,
                                                  history=2))

    # run the fit all the way through saving a checkpoint e
    model.fit(train,
              optimizer=opt_gdm,
              num_epochs=num_epochs,
              cost=cost,
              callbacks=callbacks)

    # setup model with same random seed run epoch by epoch
    # serializing and deserializing at each step
    model = gen_model(args.backend)
    cost = GeneralizedCost(costfunc=CrossEntropyBinary())
    opt_gdm = GradientDescentMomentum(learning_rate=0.1, momentum_coef=0.9)

    # reset data iterators
    train.reset()
    valid.reset()

    checkpoint_model_path = os.path.join('./', 'test_manyshot.pkl')
    checkpoint_schedule = 1  # save at evey step
    for epoch in range(num_epochs):
        # _0 points to state at end of epoch 0
        callbacks = Callbacks(model)
        callbacks.add_callback(SerializeModelCallback(checkpoint_model_path,
                                                      checkpoint_schedule,
                                                      history=num_epochs))
        model.fit(train,
                  optimizer=opt_gdm,
                  num_epochs=epoch+1,
                  cost=cost,
                  callbacks=callbacks)

        # load saved file
        prts = os.path.splitext(checkpoint_model_path)
        fn = prts[0] + '_%d' % epoch + prts[1]
        model.load_params(fn)  # load the saved weights

    # compare test_oneshot_<num_epochs>.pkl to test_manyshot_<num_epochs>.pkl
    if not compare_model_pickles('test_oneshot_%d.pkl' % (num_epochs-1),
                                 'test_manyshot_%d.pkl' % (num_epochs-1)):
        print 'No Match'
        sys.exit(1)
    else:
        print 'Match'