Exemplo n.º 1
0
def test_std():
    """Tests std"""

    # Should std along the last dimension by default
    ones = tf.ones([5, 4, 3])
    val = ops.std(ones)
    assert isinstance(val, tf.Tensor)
    assert val.ndim == 2
    assert val.shape[0] == 5
    assert val.shape[1] == 4
    assert np.all(val.numpy() == 0.0)

    # But can change that w/ the axis kwarg
    ones = tf.ones([5, 4, 3])
    val = ops.std(ones, axis=1)
    assert isinstance(val, tf.Tensor)
    assert val.ndim == 2
    assert val.shape[0] == 5
    assert val.shape[1] == 3
    assert np.all(val.numpy() == 0.0)

    # Actually test values
    val = ops.std(tf.constant([0.9, 1.9, 2.1, 3.1]))
    assert is_close(val.numpy(), 0.781024968)
    val = ops.std(tf.constant([1.0, 2.0, 3.0]))
    assert is_close(val.numpy(), 0.816496581)
Exemplo n.º 2
0
def test_std():
    """Tests std"""

    pf.set_backend('pytorch')

    # Should std along the last dimension by default
    ones = torch.ones([5, 4, 3])
    val = ops.std(ones)
    assert isinstance(val, torch.Tensor)
    assert val.ndim == 2
    assert val.shape[0] == 5
    assert val.shape[1] == 4
    assert np.all(val.numpy() == 0.0)

    # But can change that w/ the axis kwarg
    ones = torch.ones([5, 4, 3])
    val = ops.std(ones, axis=1)
    assert isinstance(val, torch.Tensor)
    assert val.ndim == 2
    assert val.shape[0] == 5
    assert val.shape[1] == 3
    assert np.all(val.numpy() == 0.0)

    # Actually test values
    val = ops.std(torch.Tensor([0.9, 1.9, 2.1, 3.1]))
    assert is_close(val.numpy(), np.std([0.9, 1.9, 2.1, 3.1], ddof=1))
    val = ops.std(torch.Tensor([1.0, 2.0, 3.0]))
    assert is_close(val.numpy(), np.std([1.0, 2.0, 3.0], ddof=1))
Exemplo n.º 3
0
 def __call__(self, x):
     """Perform the forward pass"""
     mean = O.mean(x, axis=0)
     std = O.std(x, axis=0)
     return self.weight()*(x-mean)/std+self.bias()