Skip to content

Commit 00eac8e

Browse files
authored
Merge pull request #536 from Open-Deep-ML/tinygrad-quesitons-part1
Tinygrad-quesitons-part1
2 parents 34c5cc4 + 856b1f2 commit 00eac8e

File tree

9 files changed

+23
-39
lines changed

9 files changed

+23
-39
lines changed
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
from tinygrad.tensor import Tensor
22

3-
def matrix_dot_vector_tg(a, b) -> Tensor:
3+
def matrix_dot_vector_tg(a: Tensor, b: Tensor) -> Tensor:
44
"""
55
Compute the product of matrix `a` and vector `b` using tinygrad.
6-
Inputs can be Python lists, NumPy arrays, or tinygrad Tensors.
6+
Inputs will be tinygrad Tensors.
77
Returns a 1-D Tensor of length m, or Tensor(-1) if dimensions mismatch.
88
"""
99
if len(a[0]) != len(b):
1010
return Tensor(-1)
11-
a_t = Tensor(a)
12-
b_t = Tensor(b)
13-
return a_t.matmul(b_t)
11+
return a @ b
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
from tinygrad.tensor import Tensor
22

3-
def matrix_dot_vector_tg(a, b) -> Tensor:
3+
def matrix_dot_vector_tg(a:Tensor, b:Tensor) -> Tensor:
44
"""
55
Compute the product of matrix `a` and vector `b` using tinygrad.
6-
Inputs can be Python lists, NumPy arrays, or tinygrad Tensors.
6+
Will be tinygrad Tensors.
77
Returns a 1-D Tensor of length m, or Tensor(-1) if dimensions mismatch.
88
"""
9-
# Dimension mismatch check
10-
if len(a[0]) != len(b):
11-
return Tensor(-1)
12-
# Convert to Tensor
13-
a_t = Tensor(a)
14-
b_t = Tensor(b)
15-
# Your implementation here
169
pass
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
[
22
{
3-
"test": "from tinygrad.tensor import Tensor\nres = matrix_dot_vector_tg(\n [[1,2,3],[2,4,5],[6,8,9]],\n [1,2,3]\n)\nprint(res.numpy().tolist())",
3+
"test": "from tinygrad.tensor import Tensor\nres = matrix_dot_vector_tg(\n Tensor([[1,2,3],[2,4,5],[6,8,9]]),\n Tensor([1,2,3])\n)\nprint(res.numpy().tolist())",
44
"expected_output": "[14.0, 25.0, 49.0]"
55
},
66
{
7-
"test": "from tinygrad.tensor import Tensor\nres = matrix_dot_vector_tg(\n [[1,2,3],[2,4,5]],\n [1,2]\n)\nprint(res.numpy().tolist())",
7+
"test": "from tinygrad.tensor import Tensor\nres = matrix_dot_vector_tg(\n Tensor([[1,2,3],[2,4,5]]),\n Tensor([1,2])\n)\nprint(res.numpy().tolist())",
88
"expected_output": "-1"
9+
},
10+
{
11+
"test": "from tinygrad.tensor import Tensor\nres = matrix_dot_vector_tg(\n Tensor([[1, 2], [2, 4]]),\n Tensor([1, 2])\n)\nprint(res.numpy().tolist())",
12+
"expected_output": "[5, 10]"
913
}
1014
]

questions/2_transpose-of-a-matrix/tinygrad/solution.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
def transpose_matrix_tg(a) -> Tensor:
44
"""
55
Transpose a 2D matrix `a` using tinygrad.
6-
Inputs can be Python lists, NumPy arrays, or tinygrad Tensors.
6+
Inputs are tinygrad Tensors.
77
Returns a transposed Tensor.
88
"""
9-
a_t = Tensor(a)
10-
return a_t.transpose(0,1)
9+
return a.T
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
from tinygrad.tensor import Tensor
22

3-
def transpose_matrix_tg(a) -> Tensor:
3+
def transpose_matrix_tg(a:Tensor) -> Tensor:
44
"""
55
Transpose a 2D matrix `a` using tinygrad.
6-
Inputs can be Python lists, NumPy arrays, or tinygrad Tensors.
6+
Inputs are tinygrad Tensors.
77
Returns a transposed Tensor.
88
"""
9-
# Convert to Tensor
10-
a_t = Tensor(a)
11-
# Your implementation here
129
pass
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[
22
{
3-
"test": "from tinygrad.tensor import Tensor\nres = transpose_matrix_tg([[1,2,3],[4,5,6]])\nprint(res.numpy().tolist())",
3+
"test": "from tinygrad.tensor import Tensor\nres = transpose_matrix_tg(Tensor([[1,2,3],[4,5,6]]))\nprint(res.numpy().tolist())",
44
"expected_output": "[[1, 4], [2, 5], [3, 6]]"
55
},
66
{
7-
"test": "from tinygrad.tensor import Tensor\nres = transpose_matrix_tg([[1,2],[3,4]])\nprint(res.numpy().tolist())",
7+
"test": "from tinygrad.tensor import Tensor\nres = transpose_matrix_tg(Tensor([[1,2],[3,4]]))\nprint(res.numpy().tolist())",
88
"expected_output": "[[1, 3], [2, 4]]"
99
}
1010
]

questions/3_reshape-matrix/tinygrad/solution.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
def reshape_matrix_tg(a, new_shape) -> Tensor:
44
"""
55
Reshape a 2D matrix `a` to shape `new_shape` using tinygrad.
6-
Inputs can be Python lists, NumPy arrays, or tinygrad Tensors.
6+
Inputs are tinygrad Tensors.
77
Returns a Tensor of shape `new_shape`, or an empty Tensor on mismatch.
88
"""
99
# Dimension check
1010
if len(a) * len(a[0]) != new_shape[0] * new_shape[1]:
1111
return Tensor([])
12-
a_t = Tensor(a)
13-
return a_t.reshape(new_shape)
12+
return a.reshape(new_shape)
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
from tinygrad.tensor import Tensor
22

3-
def reshape_matrix_tg(a, new_shape) -> Tensor:
3+
def reshape_matrix_tg(a:Tensor, new_shape:tuple) -> Tensor:
44
"""
55
Reshape a 2D matrix `a` to shape `new_shape` using tinygrad.
6-
Inputs can be Python lists, NumPy arrays, or tinygrad Tensors.
6+
Inputs are tinygrad Tensors.
77
Returns a Tensor of shape `new_shape`, or an empty Tensor on mismatch.
88
"""
9-
# Dimension check
10-
if len(a) * len(a[0]) != new_shape[0] * new_shape[1]:
11-
return Tensor([])
12-
# Convert to Tensor and reshape
13-
a_t = Tensor(a)
14-
# Your implementation here
159
pass
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[
22
{
3-
"test": "from tinygrad.tensor import Tensor\nres = reshape_matrix_tg(\n [[1,2,3],[4,5,6]],\n (3, 2)\n)\nprint(res.numpy().tolist())",
3+
"test": "from tinygrad.tensor import Tensor\nres = reshape_matrix_tg(\n Tensor([[1,2,3],[4,5,6]]),\n (3, 2)\n)\nprint(res.numpy().tolist())",
44
"expected_output": "[[1, 2], [3, 4], [5, 6]]"
55
},
66
{
7-
"test": "from tinygrad.tensor import Tensor\nres = reshape_matrix_tg(\n [[1,2],[3,4]],\n (3, 2)\n)\nprint(res.numpy().tolist())",
7+
"test": "from tinygrad.tensor import Tensor\nres = reshape_matrix_tg(\n Tensor([[1,2],[3,4]]),\n (3, 2)\n)\nprint(res.numpy().tolist())",
88
"expected_output": "[]"
99
}
1010
]

0 commit comments

Comments
 (0)