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 )
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))))
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
def scalars(draw, min_value=-100000, max_value=100000): val = draw(floats(min_value=min_value, max_value=max_value)) return minitorch.Scalar(val)
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)
def test_scalar_name(): x = minitorch.Scalar(10, name="x") y = (x + 10.0) * 20 y.name = "y" hash(y) return y