Beispiel #1
0
def test_variable():
    input_axes = ng.make_axes([ng.make_axis(10), ng.make_axis(3)])
    var = ng.variable(axes=input_axes)
    assign_val = np.random.rand(10, 3)
    var_assign = ng.AssignOp(tensor=var, val=assign_val)
    var_seq = ng.sequential([var_assign, var])
    var_comp = ng.computation(var_seq, "all")
    results = dict()
    weight_saver = Saver()
    with closing(ngt.make_transformer()) as transformer:
        var_func = transformer.add_computation(var_comp)
        weight_saver.setup_save(transformer=transformer, computation=var_comp)
        results['saved'] = var_func().copy()
        weight_saver.save(filename="test_variable")

    reassign_val = np.random.rand(10, 3)
    var_reassign = ng.AssignOp(tensor=var, val=reassign_val)

    var_recomp = ng.computation(var_reassign, "all")
    var_read = ng.computation(var, "all")
    with closing(ngt.make_transformer()) as restore_transformer:
        var_recompfunc = restore_transformer.add_computation(var_recomp)
        weight_saver.setup_restore(transformer=restore_transformer,
                                   computation=var_recomp,
                                   filename="test_variable")
        var_readfunc = restore_transformer.add_computation(var_read)
        var_recompfunc()
        results['reassigned'] = var_readfunc().copy()
        weight_saver.restore()
        results['restored'] = var_readfunc().copy()
    os.remove("test_variable.npz")
    assert np.allclose(results['saved'], assign_val, atol=0)
    assert np.allclose(results['reassigned'], reassign_val, atol=0)
    assert np.allclose(results['saved'], results['restored'], atol=0)
def test_dilated_conv(dilation):
    """Test that the dilated convolution layer output matches expected. This test compares
    the maximum output value to an expected max output value. The expected value is computed
    based on the dilation parameter. The test also checks that the output size matches the
    expected size based on the dilaton parameter value."""
    image_size = 3
    batch_size = 1
    init_val = 0.1
    conv_size = 3
    pad = 3
    N_filters = 1
    image_channels = 3
    model = Sequential([
        Convolution((conv_size, conv_size, N_filters),
                    filter_init=ConstantInit(val=init_val),
                    padding=pad,
                    dilation=dilation)
    ])
    X = np.ones(shape=(batch_size, 3, image_size,
                       image_size))  # Create dummy image
    data = {'image': X, 'iteration': 1}
    data_size = OrderedDict([('N', batch_size), ('C', 3), ('H', image_size),
                             ('W', image_size)])
    ax = [
        ng.make_axis(length=data_size[k], name=k)
        for k in list(data_size.keys())
    ]
    p_axes = ng.make_axes(ax)
    named_inputs = {'image': ng.placeholder(p_axes)}
    outputs = model(named_inputs['image'])
    named_outputs = {outputs.name: outputs}
    with closing(ngt.make_transformer()) as transformer:
        m = make_bound_computation(transformer, named_outputs, named_inputs)
    output = m(data)[list(m(data).keys())[0]]
    filter_size = dilation * (conv_size -
                              1) + 1  # Compute expected filter size
    # Compute the expected output size based on convolution parameters
    out_size = (image_size + 2 * pad - filter_size) + 1
    filt_tmp = np.zeros(filter_size)
    filt_tmp[0::dilation] = 1
    # max overlap between dilated filter and image (in 1-d)
    max_overlap = int(np.min([filter_size, image_size]))
    exp_max_output = init_val * image_channels * (np.sum(
        filt_tmp[0:max_overlap]))**2
    # Expected max output changes for different dilation parameter values#
    assert int(10 * np.max(output)) == int(10 * exp_max_output), \
        ("Dilated conv max outputs do not match expected: "
         "{} != {}").format(np.max(output),
                            init_val * conv_size * ((image_size - (dilation - 1))**2))
    assert np.shape(output) == (batch_size, N_filters, out_size, out_size), \
        ("Dilated conv output is not expected size: "
         "{} != {}").format(np.shape(output), (batch_size, N_filters, out_size, out_size))
Beispiel #3
0
def test_persistent_tensor():
    input_axes = ng.make_axes([ng.make_axis(10), ng.make_axis(3)])
    bgr = ng.persistent_tensor(axes=input_axes,
                               initial_value=np.array([113.9, 123.0, 125.3]))
    bgr_comp = ng.computation(bgr, "all")

    results = dict()
    weight_saver = Saver()
    with closing(ngt.make_transformer()) as transformer:
        bgr_func = transformer.add_computation(bgr_comp)
        weight_saver.setup_save(transformer=transformer, computation=bgr_comp)
        results['saved'] = bgr_func().copy()
        weight_saver.save(filename="test_persistent_tensor")
    with closing(ngt.make_transformer()) as restore_transformer:
        bgr_refunc = restore_transformer.add_computation(bgr_comp)
        weight_saver.setup_restore(transformer=restore_transformer,
                                   computation=bgr_comp,
                                   filename="test_persistent_tensor")
        weight_saver.restore()
        results['restored'] = bgr_refunc().copy()
    os.remove("test_persistent_tensor.npz")
    assert np.allclose(results['saved'], results['restored'], atol=0)
def test_deconv():
    """
    basic test of deconv fprop.
    ngraph/tests/test_conv.py tests ng.deconvolution bprop
    """

    # filter params
    R, S = 5, 5
    fshape = (R, S, 1)
    strides = 2
    filter_val_nz = np.arange(1, R * S + 1).reshape(R, S)
    filter_val = np.zeros(fshape)
    filter_val[:, :, 0] = filter_val_nz

    deconv = Deconvolution(fshape,
                           filter_init=ConstantInit(filter_val),
                           strides=strides,
                           padding=0,
                           dilation=1)

    N = ng.make_axis(name='N', length=1)  # batch
    image_shape = (1, 8, 8)  # CHW
    image_axes = ng.make_axes(
        [ng.make_axis(name=nm, length=l) for nm, l in zip('CHW', image_shape)])
    image_axes |= N
    image = ng.placeholder(axes=image_axes)

    output = deconv(image)

    with closing(ngt.make_transformer()) as transformer:
        comp = transformer.add_computation(ng.computation(output, image))
        input_val = np.zeros(image_shape + (N.length, ), dtype=float)
        input_val[0, 0, 0] = 1
        input_val[0, 5, 5] = 1
        input_val[0, 7, 7] = 1
        result = comp(input_val)
        feature_map = np.squeeze(result)

        assert (feature_map[:5, :5] == filter_val_nz).all()

        result2 = filter_val_nz.copy()
        result2[-1, -1] = 26
        assert (feature_map[10:15, 10:15] == result2).all()

        result3 = filter_val_nz.copy()
        result3[0, 0] = 26
        assert (feature_map[-5:, -5:] == result3).all()
def test_conv1d(transformer_factory, filter_width, num_filters, strides,
                padding, time_steps, feature_dimension, batch_size):

    dilation = 1  # reference conv does not support dilation

    F = ng.make_axis(name='F', length=feature_dimension)
    REC = ng.make_axis(name='REC', length=time_steps)
    N = ng.make_axis(name='N', length=batch_size)
    in_axes = ng.make_axes([F, REC, N])

    inputs = ng.placeholder(axes=in_axes)
    input_vals = np.random.randn(*in_axes.lengths)

    filter_init = GaussianInit()

    conv1d = Convolution((filter_width, num_filters),
                         filter_init,
                         strides=strides,
                         padding=padding,
                         dilation=dilation,
                         bias_init=None,
                         activation=Rectlin(),
                         batch_norm=None)

    result_op = conv1d(inputs, channel_axes='F', spatial_axes={'W': 'REC'})

    with closing(ngt.make_transformer()) as transformer:
        result_comp = transformer.add_computation(
            ng.computation(result_op, inputs))
        filter_vals = transformer.add_computation(ng.computation(
            conv1d.conv.W))()

        result_ng = result_comp(input_vals)
        result_np = np.squeeze(
            reference_conv1d(input_vals, filter_vals,
                             lambda x: np.maximum(0, x)))
        ng.testing.assert_allclose(result_ng, result_np)
Beispiel #6
0
    'name': 'schedule',
    'base_lr': 0.01,
    'gamma': (1 / 250.)**(1 / 3.),
    'schedule': [22, 44, 65]
}

optimizer = GradientDescentMomentum(lr_schedule,
                                    0.0,
                                    wdecay=0.0005,
                                    iteration=inputs['iteration'])
train_prob = seq1(inputs['image'])
train_loss = ng.cross_entropy_multi(train_prob,
                                    ng.one_hot(inputs['label'], axis=ax.Y))
batch_cost = ng.sequential(
    [optimizer(train_loss),
     ng.mean(train_loss, out_axes=())])
train_outputs = dict(batch_cost=batch_cost)

with closing(ngt.make_transformer()) as transformer:
    train_computation = make_bound_computation(transformer, train_outputs,
                                               inputs)

    cbs = make_default_callbacks(transformer=transformer,
                                 output_file=args.output_file,
                                 frequency=args.iter_interval,
                                 train_computation=train_computation,
                                 total_iterations=args.num_iterations,
                                 use_progress_bar=args.progress_bar)

    loop_train(train_set, cbs)
Beispiel #7
0
 def __enter__(self):
     self.transformer = ngt.make_transformer()
     if is_flex_transformer(self.transformer):
         self.cpu_transformer = ngt.Transformer.transformers['cpu']()
     return self