matrix(3, 5)
tensor(0.75, 2, 3, 5)
x.dims)x.shape)x.size)matrix(3, 5) | chalk.hstrut(1) | matrix(5, 3)
x = minitorch.tensor([[1, 2, 3], [3, 2, 1]])
x.shape
(2, 3)
x.permute(1, 0).shape
(3, 2)
Storage : 1-D array of numbers of length size
Strides : tuple that provides the mapping from user indexing
to the position in the 1-D storage.
d = (
matrix(5, 2, "n", colormap=color(5, 2))
/ vstrut(1)
/ matrix(1, 10, "s", colormap=lambda i, j: color(5, 2)(j % 5, j // 5))
)
d.connect(("n", 3, 0), ("s", 0, 3)).connect(("n", 3, 1), ("s", 0, 8))
tensor(0.75, 2, 2, 2)
Index for position 0? Position 1? Position 2?
$[0, 0, 0], [0, 0, 1], [0, 1, 0]$
(
tensor(0.5, 2, 2, 2, "n", colormap=lambda i, j, k: color(4, 2)(i * 2 + j, k))
/ vstrut(1)
/ matrix(1, 8, "s", colormap=color(1, 8))
)
def add2(a, b):
out_tensor = minitorch.zeros(*a.shape)
for i in range(a.shape[0]):
for j in range(a.shape[1]):
out_tensor[i, j] = a[i, j] + b[i, j]
return out_tensor
a = minitorch.tensor([3, 2, 1])
b = minitorch.tensor([1, 2, 3])
out = a + b
print(out)
[4.00 4.00 4.00]
Function class (forward / backward)for loop for each# Unary
new_tensor = a.log()
# Binary (for now, only same shape)
new_tensor = a + b
# Reductions
new_tensor = a.sum()
set_svg_draw_height(200)
set_svg_height(200)
astyle = Style().line_width(.4).line_color(Color("purple"))
opts = ArrowOpts(shaft_style=astyle)
d = hcat([matrix(3, 2, "a"), vstrut(1) / right_arrow, matrix(3, 2, "b")], 1)
d.connect(("a", 0, 0), ("b", 0, 0), style=opts).connect(("a", 1, 0), ("b", 1, 0), style=opts)
Binary operations
new_tensor = a.log()
new_tensor = a.exp()
new_tensor = -b
opts = ArrowOpts(arc_height=0.5, shaft_style=astyle)
opts2 = ArrowOpts(arc_height=0.2, shaft_style=astyle)
d = hcat([matrix(3, 2, "a"), matrix(3, 2, "b"), right_arrow, matrix(3, 2, "c")], 1)
d.connect(("a", 0, 0), ("c", 0, 0), opts).connect(
("a", 1, 0), ("c", 1, 0), opts
).connect(("b", 0, 0), ("c", 0, 0), opts2).connect(("b", 1, 0), ("c", 1, 0), opts2)
Binary operations
new_tensor = a + b
new_tensor = a * b
new_tensor = a < b
opts = ArrowOpts(shaft_style=astyle)
d = hcat([matrix(3, 2, "a"), right_arrow, matrix(1, 2, "c")], 1)
d.connect(("a", 2, 0), ("a", 0, 0), style=opts).connect(("a", 2, 1), ("a", 0, 1), style=opts)
out = minitorch.rand((3, 4, 5)).mean(1)
print(out.shape)
# (3, 1, 5)
(3, 1, 5)
Binary operations
new_tensor = a.mean()
new_tensor = out.sum(1)
Code
vector1 + 10
a = minitorch.tensor([1])
b = minitorch.tensor([1, 2, 4])
tensor_to_diagram(b) | chalk.hstrut(1) | tensor_to_diagram(a)
vector1 = minitorch.tensor([1, 2, 3])
print(vector1 + minitorch.tensor([10, 10, 10]))
[11.00 12.00 13.00]
for looptemp_vector = minitorch.zeros((vector1.shape[0],))
for i in range(temp_vector.shape[0]):
temp_vector[i] = vector1[i] + 10
a = minitorch.tensor([1, 2, 4])
b = minitorch.tensor([3, 2])
out = minitorch.zeros((3, 2))
for i in range(3):
for j in range(2):
out[i, j] = a[i] + b[j]
opts = ArrowOpts(head_arrow=empty(), arc_height=0.5, shaft_style=astyle)
opts2 = ArrowOpts(head_arrow=empty(), arc_height=0.2, shaft_style=astyle)
d = hcat([matrix(3, 1, "a"), matrix(1, 2, "b"), right_arrow, matrix(3, 2, "c")], 1)
d.connect(("a", 0, 0), ("c", 0, 0), opts).connect(
("a", 1, 0), ("c", 1, 1), opts
).connect(("b", 0, 0), ("c", 0, 0), opts2).connect(("b", 0, 1), ("c", 1, 1), opts2)
viewa = minitorch.tensor([1])
b = minitorch.tensor([[1, 2], [3,4]])
tensor_to_diagram(a) | chalk.hstrut(1) | tensor_to_diagram(b)
Matrix + Vector
matrix1 = minitorch.zeros((4, 3))
a = matrix1.view(4, 3)
b = minitorch.tensor([1, 2, 3])
out = a + b
tensor_to_diagram(a) | chalk.hstrut(1) | tensor_to_diagram(b) | chalk.hstrut(1) | tensor_to_diagram(a + b)
# Doesn't Work!
# matrix1.view(4, 3) + minitorch.tensor([1, 2, 3, 5])
# Does Work!
# matrix1.view(4, 3) + tensor([1, 2, 3, 5]).view(4, 1)
| A | B | = |
|---|---|---|
| (3, 4, 5) | (3, 1, 5) | (3, 4, 5) |
| (3, 4, 1) | (3, 1, 5) | (3, 4, 5) |
| (3, 4, 1) | (1, 5) | (3, 4, 5) |
| (3, 4, 1) | (3, 5) | Fail |
| A | B | = |
|---|---|---|
| (1, 3, 4) | (1, 3, 1) | |
| (1, 4, 4) | (3, 1, 5) | |
| (3, 4, 1) | (1, ) |