Beispiel #1
0
def test_multi_convolutional_feature_map_fprop():
    cplane1 = ConvolutionalPlane((5, 5), (20, 20), bias=False)
    cplane2 = ConvolutionalPlane((5, 5), (20, 20), bias=False)
    sigmoid = TanhSigmoid((16, 16), bias=True)
    mfmap = MultiConvolutionalFeatureMap((5, 5), (20, 20), 2)
    mfmap.initialize()
    cplane1.params[:] = mfmap.planes[0].params
    cplane2.params[:] = mfmap.planes[1].params
    sigmoid.params[:] = mfmap.params[0:1]
    inputs1 = random.normal(size=(20, 20))
    inputs2 = random.normal(size=(20, 20))
    control = sigmoid.fprop(cplane1.fprop(inputs1) + cplane2.fprop(inputs2))
    mfmap_out = mfmap.fprop([inputs1, inputs2])
    assert_array_almost_equal(control, mfmap_out)
Beispiel #2
0
def test_multi_convolutional_feature_map_fprop():
    cplane1 = ConvolutionalPlane((5, 5), (20, 20), bias=False)
    cplane2 = ConvolutionalPlane((5, 5), (20, 20), bias=False)
    sigmoid = TanhSigmoid((16, 16), bias=True)
    mfmap = MultiConvolutionalFeatureMap((5, 5), (20, 20), 2)
    mfmap.initialize()
    cplane1.params[:] = mfmap.planes[0].params
    cplane2.params[:] = mfmap.planes[1].params
    sigmoid.params[:] = mfmap.params[0:1]
    inputs1 = random.normal(size=(20, 20))
    inputs2 = random.normal(size=(20, 20))
    control = sigmoid.fprop(cplane1.fprop(inputs1) + cplane2.fprop(inputs2))
    mfmap_out = mfmap.fprop([inputs1, inputs2])
    assert_array_almost_equal(control, mfmap_out)
Beispiel #3
0
def test_multi_convolutional_feature_map_singleplane_bprop():
    size = (20, 20)
    elems = np.prod(size)
    fsize = (5, 5)
    osize = (16, 16)
    mfmap = MultiConvolutionalFeatureMap(fsize, size, 1)
    mfmap.initialize()
    in1 = random.normal(size=size)
    dout = np.ones(osize)
    bprop = lambda inp: mfmap.bprop(dout, inp)
    grad1 = lambda var: bprop((var.reshape(size),))[0].reshape(elems)
    func1 = lambda var: mfmap.fprop((var.reshape(size),)).sum()
    varied_input = random.normal(size=size)
    fd_grad1 = fd_grad(func1, varied_input.reshape(elems), 1e-4)
    real_grad1 = grad1(varied_input)
    assert_array_almost_equal(fd_grad1, real_grad1)
Beispiel #4
0
def test_multi_convolutional_feature_map_singleplane_bprop():
    size = (20, 20)
    elems = np.prod(size)
    fsize = (5, 5)
    osize = (16, 16)
    mfmap = MultiConvolutionalFeatureMap(fsize, size, 1)
    mfmap.initialize()
    in1 = random.normal(size=size)
    dout = np.ones(osize)
    bprop = lambda inp: mfmap.bprop(dout, inp)
    grad1 = lambda var: bprop((var.reshape(size), ))[0].reshape(elems)
    func1 = lambda var: mfmap.fprop((var.reshape(size), )).sum()
    varied_input = random.normal(size=size)
    fd_grad1 = fd_grad(func1, varied_input.reshape(elems), 1e-4)
    real_grad1 = grad1(varied_input)
    assert_array_almost_equal(fd_grad1, real_grad1)
Beispiel #5
0
def test_multi_convolutional_feature_map_twoplane_params():
    size = (20, 20)
    elems = np.prod(size)
    fsize = (5, 5)
    osize = (16, 16)
    mfmap = MultiConvolutionalFeatureMap(fsize, size, 2)
    mfmap.initialize()

    inp1 = random.normal(size=size)
    inp2 = random.normal(size=size)

    dout = np.ones(osize)

    def fprop_params1(params):
        mfmap.planes[0].params[:] = params
        return mfmap.fprop((inp1, inp2)).sum()

    def fprop_params2(params):
        mfmap.planes[1].params[:] = params
        return mfmap.fprop((inp1, inp2)).sum()

    def fprop_bias_adjust(params):
        mfmap.params[0:1] = params
        return mfmap.fprop((inp1, inp2)).sum()

    # Reset the parameters after we calculate each gradient approximation.
    resetparams = mfmap.params.copy()
    params1 = mfmap.planes[0].params
    params2 = mfmap.planes[1].params
    paramsb = mfmap.params[0:1]

    real = mfmap.grad(dout, (inp1, inp2))

    approxb = fd_grad(fprop_bias_adjust, paramsb.copy(), 1e-4)
    mfmap.params[:] = resetparams
    approx1 = fd_grad(fprop_params1, params1.copy(), 1e-4)
    mfmap.params[:] = resetparams
    approx2 = fd_grad(fprop_params2, params2.copy(), 1e-4)

    all_approx = np.concatenate((approxb, approx1, approx2))

    assert_array_almost_equal(real, all_approx)
Beispiel #6
0
def test_multi_convolutional_feature_map_twoplane_params():
    size = (20, 20)
    elems = np.prod(size)
    fsize = (5, 5)
    osize = (16, 16)
    mfmap = MultiConvolutionalFeatureMap(fsize, size, 2)
    mfmap.initialize()

    inp1 = random.normal(size=size)
    inp2 = random.normal(size=size)

    dout = np.ones(osize)

    def fprop_params1(params):
        mfmap.planes[0].params[:] = params
        return mfmap.fprop((inp1, inp2)).sum()

    def fprop_params2(params):
        mfmap.planes[1].params[:] = params
        return mfmap.fprop((inp1, inp2)).sum()

    def fprop_bias_adjust(params):
        mfmap.params[0:1] = params
        return mfmap.fprop((inp1, inp2)).sum()

    # Reset the parameters after we calculate each gradient approximation.
    resetparams = mfmap.params.copy()
    params1 = mfmap.planes[0].params
    params2 = mfmap.planes[1].params
    paramsb = mfmap.params[0:1]

    real = mfmap.grad(dout, (inp1, inp2))

    approxb = fd_grad(fprop_bias_adjust, paramsb.copy(), 1e-4)
    mfmap.params[:] = resetparams
    approx1 = fd_grad(fprop_params1, params1.copy(), 1e-4)
    mfmap.params[:] = resetparams
    approx2 = fd_grad(fprop_params2, params2.copy(), 1e-4)

    all_approx = np.concatenate((approxb, approx1, approx2))

    assert_array_almost_equal(real, all_approx)
Beispiel #7
0
def test_multi_convolutional_feature_map_twoplane_bprop():
    size = (20, 20)
    elems = np.prod(size)
    fsize = (5, 5)
    osize = (16, 16)
    mfmap = MultiConvolutionalFeatureMap(fsize, size, 2)
    mfmap.initialize()

    inp = random.normal(size=size)
    cnst = random.normal(size=size)

    dout = np.ones(osize)

    fprop = lambda y: mfmap.fprop((y.reshape(size), cnst)).sum()
    approximate = fd_grad(fprop, inp.reshape(elems))
    actual = mfmap.bprop(np.ones(osize), (inp, cnst))[0].reshape(elems)
    assert_array_almost_equal(approximate, actual)

    # Swap the order - should make no difference
    fprop = lambda y: mfmap.fprop((cnst, y.reshape(size))).sum()
    approximate = fd_grad(fprop, inp.reshape(elems))
    actual = mfmap.bprop(np.ones(osize), (cnst, inp))[1].reshape(elems)
    assert_array_almost_equal(approximate, actual)
Beispiel #8
0
def test_multi_convolutional_feature_map_twoplane_bprop():
    size = (20, 20)
    elems = np.prod(size)
    fsize = (5, 5)
    osize = (16, 16)
    mfmap = MultiConvolutionalFeatureMap(fsize, size, 2)
    mfmap.initialize()

    inp = random.normal(size=size)
    cnst = random.normal(size=size)

    dout = np.ones(osize)

    fprop = lambda y: mfmap.fprop((y.reshape(size), cnst)).sum()
    approximate = fd_grad(fprop, inp.reshape(elems))
    actual = mfmap.bprop(np.ones(osize), (inp, cnst))[0].reshape(elems)
    assert_array_almost_equal(approximate, actual)

    # Swap the order - should make no difference
    fprop = lambda y: mfmap.fprop((cnst, y.reshape(size))).sum()
    approximate = fd_grad(fprop, inp.reshape(elems))
    actual = mfmap.bprop(np.ones(osize), (cnst, inp))[1].reshape(elems)
    assert_array_almost_equal(approximate, actual)