Ejemplo n.º 1
0
 def __init__(self, in_size, out_size):
     super().__init__()
     self.weights = minitorch.Parameter(
         2 * (minitorch.rand((in_size, out_size)) - 0.5)
     )
     self.bias = minitorch.Parameter(2 * (minitorch.rand((out_size,)) - 0.5))
     self.out_size = out_size
Ejemplo n.º 2
0
def test_mm2():
    a = minitorch.rand((2, 3), backend=FastTensorBackend)
    b = minitorch.rand((3, 4), backend=FastTensorBackend)
    c = a @ b

    c2 = (a.view(2, 3, 1) * b.view(1, 3, 4)).sum(1).view(2, 4)

    for ind in c._tensor.indices():
        assert_close(c[ind], c2[ind])
Ejemplo n.º 3
0
def test_mm():
    a = minitorch.rand((2, 3))
    b = minitorch.rand((3, 4))
    c = minitorch.matmul(a, b)

    c2 = (a.view(2, 3, 1) * b.view(1, 3, 4)).sum(1).view(2, 4)

    print(c)
    print(c2)
    for ind in c._tensor.indices():
        assert_close(c[ind], c2[ind])
Ejemplo n.º 4
0
def test_max(t):
    out = minitorch.nn.max(t, 2)
    assert out[0, 0, 0] == max(t[0, 0, i] for i in range(4))
    out = minitorch.nn.max(t, 1)
    assert out[0, 0, 0] == max(t[0, i, 0] for i in range(3))
    out = minitorch.nn.max(t, 0)
    assert out[0, 0, 0] == max(t[i, 0, 0] for i in range(2))
    rand_tensor = minitorch.rand(t.shape) * 1e-5
    t = t + rand_tensor
    minitorch.grad_check(lambda t: minitorch.nn.max(t, 2), t)
Ejemplo n.º 5
0
def test_max(t):
    t_np = t.to_numpy()

    compare_0 = t_np.max(axis=0)
    out_0 = minitorch.Max.apply(t, 0)
    assert out_0.shape == (1, 3, 4)
    assert np.array_equal(compare_0.reshape(-1), out_0.to_numpy().reshape(-1))
    minitorch.grad_check(lambda t: minitorch.Max.apply(t, 0),
                         t + (minitorch.rand(t.shape) * 1e-5))

    compare_1 = t_np.max(axis=1)
    out_1 = minitorch.Max.apply(t, 1)
    assert out_1.shape == (2, 1, 4)
    assert np.array_equal(compare_1.reshape(-1), out_1.to_numpy().reshape(-1))
    minitorch.grad_check(lambda t: minitorch.Max.apply(t, 1),
                         t + (minitorch.rand(t.shape) * 1e-5))

    compare_2 = t_np.max(axis=2)
    out_2 = minitorch.Max.apply(t, 2)
    assert out_2.shape == (2, 3, 1)
    assert np.array_equal(compare_2.reshape(-1), out_2.to_numpy().reshape(-1))
    minitorch.grad_check(lambda t: minitorch.Max.apply(t, 0),
                         t + (minitorch.rand(t.shape) * 1e-5))
Ejemplo n.º 6
0
def RParam(*shape):
    r = 2 * (minitorch.rand(shape) - 0.5)
    return minitorch.Parameter(r)
Ejemplo n.º 7
0
import minitorch
import minitorch.nn as nn

input = minitorch.rand(2, 3)
linear = nn.Linear(3, 5, bias=True)
output = linear(input)
print(f"output: {output}")


class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear_1 = nn.Linear(3, 5, bias=True)
        self.linear_2 = nn.Linear(5, 6)

    def forward(self, input):
        output = self.linear_1(input)
        output = self.linear_2(output)
        return output


input = minitorch.rand(2, 3)
model = Model()
output = model(input)
print(f"output: {output}")

for name, parameter in model.named_parameters():
    print(f"{name}: {parameter}")

for name, module in model.named_modules(prefix='model'):
    print(f"{name}: {module}")
Ejemplo n.º 8
0
def RParam(*shape):
    r = 0.1 * (minitorch.rand(shape, backend=BACKEND) - 0.5)
    return minitorch.Parameter(r)
Ejemplo n.º 9
0
def RParam(*shape):
    p = 1.0
    for s in shape:
        p += s
    r = 2 * (minitorch.rand(shape, backend=BACKEND) - 0.5)
    return minitorch.Parameter(r)
Ejemplo n.º 10
0
def RParam(*shape, backend):
    r = minitorch.rand(shape, backend=backend) - 0.5
    return minitorch.Parameter(r)