Example #1
0
def test_toplayer_delta():
    X = np.random.randn(3, 5).astype(np.float32)
    A = 5*np.random.randn(30, 50).astype(np.float32)
    D = 5*np.random.randn(30, 50).astype(np.float32)
    D_expected = D.copy()
    D_expected = A - D_expected
    Dd = op.to_gpu(D)
    Yh = op.toplayer_delta(A, D, X)
    assert_allclose(D_expected, Yh, rtol=1e-5, err_msg="CPU")
    Ad = op.to_gpu(A)
    Xd = op.to_gpu(X)
    Yhd = op.toplayer_delta(Ad, Dd, Xd)
    assert_allclose(D_expected, op.to_cpu(Yhd), rtol=1e-5, err_msg="GPU")
Example #2
0
def test_toplayer_delta():
    X = np.random.randn(3, 5).astype(np.float32)
    A = 5 * np.random.randn(30, 50).astype(np.float32)
    D = 5 * np.random.randn(30, 50).astype(np.float32)
    D_expected = D.copy()
    D_expected = A - D_expected
    Dd = op.to_gpu(D)
    Yh = op.toplayer_delta(A, D, X)
    assert_allclose(D_expected, Yh, rtol=1e-5, err_msg="CPU")
    Ad = op.to_gpu(A)
    Xd = op.to_gpu(X)
    Yhd = op.toplayer_delta(Ad, Dd, Xd)
    assert_allclose(D_expected, op.to_cpu(Yhd), rtol=1e-5, err_msg="GPU")
Example #3
0
 def backward_pass(self, out, y, momentum=0.0):
     delta = op.toplayer_delta(out, y, self.layers[-1].Z,
                               stream=op.streams[0])
     if self.output_weights is not None:
         op.inplace_scale_columns(delta, self.output_weights)
     for l in reversed(self.layers):
         delta = l.bprop(delta, momentum)
     return delta # most of the time this is ignored anyhow
Example #4
0
def test_nan_in_toplayer_delta():
    size = (200, 10)
    X = np.random.normal(size=size).astype(np.float32, order="c")
    A = op.sigmoid(X)
    Y = np.random.binomial(1.0, p=0.5, size=size).astype(np.float32)
    M = np.random.binomial(1.0, p=0.9, size=size).astype(np.float32)
    Y[~M.astype(np.bool)] = np.nan
    Y_orig = Y.copy()
    D = M * (A - Y)
    D[~M.astype(np.bool)] = 0.0

    Y = op.toplayer_delta(A, Y, X)
    assert_allclose(Y, D)

    Yd = op.to_gpu(Y_orig)
    Ad = op.to_gpu(A)
    Xd = op.to_gpu(X)
    Yd = op.toplayer_delta(Ad, Yd, Xd)
    assert_allclose(Yd.get(), D)
Example #5
0
 def backward_pass(self, out, y, momentum=0.0):
     delta = op.toplayer_delta(out,
                               y,
                               self.layers[-1].Z,
                               stream=op.streams[0])
     if self.output_weights is not None:
         op.inplace_scale_columns(delta, self.output_weights)
     for l in reversed(self.layers):
         delta = l.bprop(delta, momentum)
     return delta  # most of the time this is ignored anyhow
Example #6
0
def test_nan_in_toplayer_delta():
    size = (200, 10)
    X = np.random.normal(size=size).astype(np.float32, order="c")
    A = op.sigmoid(X)
    Y = np.random.binomial(1.0, p=0.5, size=size).astype(np.float32)
    M = np.random.binomial(1.0, p=0.9, size=size).astype(np.float32)
    Y[~M.astype(np.bool)] = np.nan
    Y_orig = Y.copy()
    D = M * (A - Y)
    D[~M.astype(np.bool)] = 0.0

    Y = op.toplayer_delta(A, Y, X)
    assert_allclose(Y, D)

    Yd = op.to_gpu(Y_orig)
    Ad = op.to_gpu(A)
    Xd = op.to_gpu(X)
    Yd = op.toplayer_delta(Ad, Yd, Xd)
    assert_allclose(Yd.get(), D)