Beispiel #1
0
def test_binary_layer_config_qconv(bits,
                                   weight_quantization,
                                   input_shape=(1, 3, 7, 7),
                                   channel=2):
    d = np.random.uniform(-1, 1, input_shape)
    in_data = mx.nd.array(d)
    in_data.attach_grad()

    qconv = nn.QConv2D(channel,
                       3,
                       bits=bits,
                       quantization=weight_quantization,
                       in_channels=input_shape[1])
    qconv.initialize(mx.init.Xavier(magnitude=2))

    with nn.set_binary_layer_config(bits=bits,
                                    weight_quantization=weight_quantization):
        qconv_config = nn.QConv2D(channel,
                                  3,
                                  params=qconv.collect_params(),
                                  in_channels=input_shape[1])

    grad, y = forward(in_data, qconv)
    grad_, y_ = forward(in_data, qconv_config)

    np.testing.assert_almost_equal(y, y_)
    np.testing.assert_almost_equal(grad, grad_)
Beispiel #2
0
def test_binary_layer_config_qact(grad_cancel,
                                  bits_a,
                                  activation,
                                  input_shape=(1, 2, 4, 4)):
    d = np.random.uniform(-1, 1, input_shape)
    in_data = mx.nd.array(d)
    in_data.attach_grad()

    qact = nn.QActivation(bits=bits_a,
                          gradient_cancel_threshold=grad_cancel,
                          method=activation)
    with nn.set_binary_layer_config(grad_cancel=grad_cancel,
                                    bits_a=bits_a,
                                    activation=activation):
        qact_config = nn.QActivation()

    grad, y = forward(in_data, qact)
    grad_, y_ = forward(in_data, qact_config)

    np.testing.assert_almost_equal(y, y_)
    np.testing.assert_almost_equal(grad, grad_)
Beispiel #3
0
def test_binary_layer_config_scaling():
    assert isinstance(nn.activated_conv(3), nn.BinaryConvolution)
    with nn.set_binary_layer_config(approximation="xnor"):
        assert isinstance(nn.activated_conv(3), nn.ScaledBinaryConv)
    assert isinstance(nn.activated_conv(3), nn.BinaryConvolution)