Example #1
0
def test_two_args(fn, ts):
    t1, t2 = ts
    t3 = fn[1](t1, t2)
    for ind in t3._tensor.indices():
        assert (
            t3[ind] == fn[1](minitorch.Scalar(t1[ind]), minitorch.Scalar(t2[ind])).data
        )
Example #2
0
 def __init__(self, in_size, out_size):
     super().__init__()
     self.weights = []
     self.bias = []
     for i in range(in_size):
         self.weights.append([])
         for j in range(out_size):
             self.weights[i].append(
                 self.add_parameter(
                     f"weight_{i}_{j}",
                     minitorch.Scalar(2 * (random.random() - 0.5))))
     for j in range(out_size):
         self.bias.append(
             self.add_parameter(
                 f"bias_{j}",
                 minitorch.Scalar(2 * (random.random() - 0.5))))
Example #3
0
def test_nn_size():
    model = Network2()
    assert len(model.parameters()) == (len(model.layer1.parameters()) +
                                       len(model.layer2.parameters()))

    assert model.layer2.bias[0].value.data != 0
    assert model.layer1.bias[0].value.data != 0
    assert model.layer1.weights[0][0].value.data != 0

    for p in model.parameters():
        p.update(minitorch.Scalar(0))

    assert model.layer2.bias[0].value.data == 0
    assert model.layer1.bias[0].value.data == 0
    assert model.layer1.weights[0][0].value.data == 0
Example #4
0
def scalars(draw, min_value=-100000, max_value=100000):
    val = draw(floats(min_value=min_value, max_value=max_value))
    return minitorch.Scalar(val)
Example #5
0
def test_one_args(fn, t1):
    t2 = fn[1](t1)
    for ind in t2._tensor.indices():
        assert_close(t2[ind], fn[1](minitorch.Scalar(t1[ind])).data)
Example #6
0
def test_scalar_name():
    x = minitorch.Scalar(10, name="x")
    y = (x + 10.0) * 20
    y.name = "y"
    hash(y)
    return y