set_svg_draw_height(200)
set_svg_height(200)
opts = ArrowOpts(arc_height=0.5, shaft_style=astyle)
d = hcat([matrix(3, 2, "a"), right_arrow, matrix(3, 2, "b")], 1)
d.connect(("a", 0, 0), ("b", 0, 0), opts).connect(("a", 1, 0), ("b", 1, 0), opts)
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)
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), opts).connect(("a", 2, 1), ("a", 0, 1), opts)
vector1 + 10
opts = ArrowOpts(arc_height=0.5, shaft_style=astyle)
opts2 = ArrowOpts(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)
view| 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 |
3 4 1
1 5
-------
3 4 5
import torch
x = torch.tensor([1, 2, 3])
print(x.shape)
print(x.view(3, 1).shape)
print(x.view(1, 3).shape)
torch.Size([3]) torch.Size([3, 1]) torch.Size([1, 3])
x = minitorch.tensor([[1, 2], [3, 4], [5, 6]])
y = minitorch.tensor([[1], [3], [5]])
z = x + y
z.shape
(3, 2)
def col(c):
return (
matrix(3, 2).line_color(c).align_t().align_l()
+ matrix(3, 1).align_t().align_l()
).center_xy()
hcat(
[
matrix(3, 2),
col(drawing.white),
right_arrow,
matrix(3, 2),
col(drawing.papaya),
right_arrow,
matrix(3, 2),
],
0.4,
)
# + [markdown] slideshow={"slide_type": "slide"}
# Matrix-Matrix
# ================
x = minitorch.zeros((4, 5))
y = minitorch.zeros((3, 1, 5))
z = x + y
z.shape
(3, 4, 5)
def t(d, r, c, n=""):
return tensor(0.5, d, r, c, n).fill_color(drawing.white)
d, r, c = 3, 4, 5
base = t(d, r, c).line_color(drawing.papaya)
chalk.set_svg_height(300)
hcat(
[
t(1, r, c),
t(d, 1, c),
right_arrow,
(base + t(1, r, c)),
(base + t(d, 1, c)),
],
sep=2.5,
) / vstrut(1) / hcat([right_arrow,
t(d, r, c)], sep=2.5).align_l()
x = minitorch.zeros((3, 2))
y = minitorch.zeros((2, 1, 2))
z = (x * y).sum(2)
z.shape
(2, 3, 1)
d, r, c = 2, 3, 2
base = t(d, r, c).line_color(drawing.papaya)
d = hcat(
[
t(1, r, c),
t(d, 1, c),
right_arrow,
(base + t(1, r, c)),
(base + t(d, 1, c)),
],
sep=2.5,
) / vstrut(1) / hcat(
[right_arrow,
t(d, r, c, "s"),
right_arrow,
t(2, 3, 1),
right_arrow,
matrix(3, 2)], sep=2.5)
d.connect(("s", 0, 0, 1), ("s", 0, 0, 0)).connect(
("s", 0, 1, 1), ("s", 0, 1, 0)
).connect(("s", 0, 2, 1), ("s", 0, 2, 0))
shape_broadcast - create the broadcast dimsbroadcast_index - map from broadcasted to original valueclass TensorOps:
@staticmethod
def map(fn: Callable[[float], float]) -> Callable[[Tensor], Tensor]:
pass
@staticmethod
def zip(fn: Callable[[float, float], float]) -> Callable[[Tensor, Tensor], Tensor]:
pass
@staticmethod
def reduce(
fn: Callable[[float, float], float], start: float = 0.0
) -> Callable[[Tensor, int], Tensor]:
pass
tensor_op.pyself.neg_map = ops.map(operators.neg)
self.sigmoid_map = ops.map(operators.sigmoid)
self.relu_map = ops.map(operators.relu)
self.log_map = ops.map(operators.log)
self.exp_map = ops.map(operators.exp)
self.id_map = ops.map(operators.id)
t1 = minitorch.tensor([1, 2, 3])
t1.f.neg_map(t1)
[-1.00 -2.00 -3.00]
(Not available in minitorch)
import torch
x = torch.tensor([1, 2, 3])
print(x.shape)
print(x[None].shape)
print(x[:, None].shape)
torch.Size([3]) torch.Size([1, 3]) torch.Size([3, 1])
x = torch.arange(10)
print(x)
y = torch.arange(4)
print(y)
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) tensor([0, 1, 2, 3])
x = torch.where(
torch.tensor([True, False]),
torch.tensor([1, 1]),
torch.tensor([0, 0]),
)
print(x)
tensor([1, 0])