-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprerequisite_exercises.py
533 lines (399 loc) · 16.3 KB
/
prerequisite_exercises.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
import math
from einops import rearrange, repeat, reduce
import torch as t
def assert_all_equal(actual: t.Tensor, expected: t.Tensor) -> None:
assert (
actual.shape == expected.shape
), f"Shape mismatch, got: {actual.shape} expected: {expected.shape}"
assert (
actual == expected
).all(), f"Value mismatch, got: {actual} expected: {expected}"
print("Passed!")
def assert_all_close(
actual: t.Tensor, expected: t.Tensor, rtol=1e-05, atol=0.0001
) -> None:
assert (
actual.shape == expected.shape
), f"Shape mismatch, got: {actual.shape} expected: {expected.shape}"
assert t.allclose(actual, expected, rtol=rtol, atol=atol)
print("Passed!")
def rearrange_1() -> t.Tensor:
"""Return the following tensor using only torch.arange and einops.rearrange:
[[3, 4],
[5, 6],
[7, 8]]
"""
Z = rearrange(t.arange(3, 9), "(i1 i2) -> i1 i2", i1=3)
# print(Z)
return Z
expected = t.tensor([[3, 4], [5, 6], [7, 8]])
assert_all_equal(rearrange_1(), expected)
def rearrange_2() -> t.Tensor:
"""Return the following tensor using only torch.arange and einops.rearrange:
[[1, 2, 3],
[4, 5, 6]]
"""
Z = rearrange(t.arange(1, 7), "(i1 i2) -> i1 i2", i1=2)
# print(Z)
return Z
assert_all_equal(rearrange_2(), t.tensor([[1, 2, 3], [4, 5, 6]]))
def rearrange_3() -> t.Tensor:
"""Return the following tensor using only torch.arange and einops.rearrange:
[[[1], [2], [3], [4], [5], [6]]]
"""
Z = rearrange(t.arange(1, 7), "(i1 i2 i3) -> i1 i2 i3", i1=1, i2=6, i3=1)
# print(Z)
return Z
assert_all_equal(rearrange_3(), t.tensor([[[1], [2], [3], [4], [5], [6]]]))
def temperatures_average(temps: t.Tensor) -> t.Tensor:
"""Return the average temperature for each week.
temps: a 1D temperature containing temperatures for each day.
Length will be a multiple of 7 and the first 7 days are for the first week, second 7 days for the second week, etc.
You can do this with a single call to reduce.
"""
assert len(temps) % 7 == 0
Z = reduce(temps, "(i1 i2) -> i1", "mean", i2=7)
# print(Z)
return Z
temps = t.Tensor(
[71, 72, 70, 75, 71, 72, 70, 68, 65, 60, 68, 60, 55, 59, 75, 80, 85, 80, 78, 72, 83]
)
expected = t.tensor([71.5714, 62.1429, 79.0])
assert_all_close(temperatures_average(temps), expected)
def temperatures_differences(temps: t.Tensor) -> t.Tensor:
"""For each day, subtract the average for the week the day belongs to.
temps: as above
"""
assert len(temps) % 7 == 0
# Z = reduce(temps, "(i1 i2) -> i1", "mean", i2=7)
# Z = rearrange(repeat(Z, "i -> i days", days=7), "i1 i2 -> (i1 i2)")
# Z = temps - Z
# Mine was a little ham-fisted. Should reuse the temperatures_average function,
# and repeat and rearrange steps can be combined into one call
Z = repeat(temperatures_average(temps), "i -> (i days)", days=7)
Z = temps - Z
# print(Z)
return Z
expected = t.tensor(
[
-0.5714,
0.4286,
-1.5714,
3.4286,
-0.5714,
0.4286,
-1.5714,
5.8571,
2.8571,
-2.1429,
5.8571,
-2.1429,
-7.1429,
-3.1429,
-4.0,
1.0,
6.0,
1.0,
-1.0,
-7.0,
4.0,
]
)
actual = temperatures_differences(temps)
assert_all_close(actual, expected)
def temperatures_normalized(temps: t.Tensor) -> t.Tensor:
"""For each day, subtract the weekly average and divide by the weekly standard deviation.
temps: as above
Pass torch.std to reduce.
"""
Z = t.std(rearrange(temps, "(i days) -> i days", days=7), dim=1)
Z = repeat(Z, "i -> (i days)", days=7)
Z = (temps - repeat(temperatures_average(temps), "i -> (i days)", days=7)) / Z
# print(Z)
return Z
expected = t.tensor(
[
-0.3326,
0.2494,
-0.9146,
1.9954,
-0.3326,
0.2494,
-0.9146,
1.1839,
0.5775,
-0.4331,
1.1839,
-0.4331,
-1.4438,
-0.6353,
-0.8944,
0.2236,
1.3416,
0.2236,
-0.2236,
-1.5652,
0.8944,
]
)
actual = temperatures_normalized(temps)
assert_all_close(actual, expected)
def batched_dot_product_nd(a: t.Tensor, b: t.Tensor) -> t.Tensor:
"""Return the batched dot product of a and b, where the first dimension is the batch dimension.
That is, out[i] = dot(a[i], b[i]) for i in 0..len(a).
a and b can have any number of dimensions greater than 1.
a: shape (b, i_1, i_2, ..., i_n)
b: shape (b, i_1, i_2, ..., i_n)
Returns: shape (b, )
Use torch.einsum. You can use the ellipsis "..." in the einsum formula to represent an arbitrary number of dimensions.
"""
assert a.shape == b.shape
pass
# Z = t.einsum("a..., a... -> a...", a, b)
# print(Z)
# return Z
# actual = batched_dot_product_nd(
# t.tensor([[1, 1, 0], [0, 0, 1]]), t.tensor([[1, 1, 0], [1, 1, 0]])
# )
# expected = t.tensor([2, 0])
# assert_all_equal(actual, expected)
# actual2 = batched_dot_product_nd(
# t.arange(12).reshape((3, 2, 2)), t.arange(12).reshape((3, 2, 2))
# )
# expected2 = t.tensor([14, 126, 366])
# assert_all_equal(actual2, expected2)
def identity_matrix(n: int) -> t.Tensor:
"""Return the identity matrix of size nxn.
Don't use torch.eye or similar.
Hint: you can do it with arange, rearrange, and ==.
Bonus: find a different way to do it.
"""
assert n >= 0
# oh jeez, n or 1 is cheating but also who calls eye(0)???
Z = rearrange(t.arange(0, n**2), "(i j) -> i j", i=(n or 1)) == rearrange(
t.arange(0, n**2), "(i j) -> j i", i=(n or 1)
)
Z = 1 * Z
# print(Z)
return Z
assert_all_equal(identity_matrix(3), t.Tensor([[1, 0, 0], [0, 1, 0], [0, 0, 1]]))
assert_all_equal(identity_matrix(0), t.zeros((0, 0)))
def sample_distribution(probs: t.Tensor, n: int) -> t.Tensor:
"""Return n random samples from probs, where probs is a normalized probability distribution.
probs: shape (k,) where probs[i] is the probability of event i occurring.
n: number of random samples
Return: shape (n,) where out[i] is an integer indicating which event was sampled.
Use torch.rand and torch.cumsum to do this without any explicit loops.
Note: if you think your solution is correct but the test is failing, try increasing the value of n.
"""
assert abs(probs.sum() - 1.0) < 0.001
assert (probs >= 0).all()
Z = t.rand(n, 1)
# print(Z)
return Z
# n = 10000000
# probs = t.tensor([0.05, 0.1, 0.1, 0.2, 0.15, 0.4])
# freqs = t.bincount(sample_distribution(probs, n)) / n
# assert_all_close(freqs, probs, rtol=0.001, atol=0.001)
def classifier_accuracy(scores: t.Tensor, true_classes: t.Tensor) -> t.Tensor:
"""Return the fraction of inputs for which the maximum score corresponds to the true class for that input.
scores: shape (batch, n_classes). A higher score[b, i] means that the classifier thinks class i is more likely.
true_classes: shape (batch, ). true_classes[b] is an integer from [0...n_classes).
Use torch.argmax.
"""
assert true_classes.max() < scores.shape[1]
Z = t.argmax(scores, dim=1)
Z = t.sum(Z == true_classes)
return Z / true_classes.shape[0]
scores = t.tensor([[0.75, 0.5, 0.25], [0.1, 0.5, 0.4], [0.1, 0.7, 0.2]])
true_classes = t.tensor([0, 1, 0])
expected = 2.0 / 3.0
assert classifier_accuracy(scores, true_classes) == expected
def total_price_indexing(prices: t.Tensor, items: t.Tensor) -> float:
"""Given prices for each kind of item and a tensor of items purchased, return the total price.
prices: shape (k, ). prices[i] is the price of the ith item.
items: shape (n, ). A 1D tensor where each value is an item index from [0..k).
Use integer array indexing. The below document describes this for NumPy but it's the same in PyTorch:
https://numpy.org/doc/stable/user/basics.indexing.html#integer-array-indexing
"""
assert items.max() < prices.shape[0]
Z = prices[items]
# print(Z)
return t.sum(Z).item()
prices = t.tensor([0.5, 1, 1.5, 2, 2.5])
items = t.tensor([0, 0, 1, 1, 4, 3, 2])
assert total_price_indexing(prices, items) == 9.0
def gather_2d(matrix: t.Tensor, indexes: t.Tensor) -> t.Tensor:
"""Perform a gather operation along the second dimension.
matrix: shape (m, n)
indexes: shape (m, k)
Return: shape (m, k). out[i][j] = matrix[i][indexes[i][j]]
For this problem, the test already passes and it's your job to write at least three asserts relating the arguments and the output. This is a tricky function and worth spending some time to wrap your head around its behavior.
See: https://pytorch.org/docs/stable/generated/torch.gather.html?highlight=gather#torch.gather
"""
# After checking the answer, found out this is stored in .ndim
# assert len(indexes.shape) == len(matrix.shape)
assert matrix.ndim == indexes.ndim
# After checking the answer, it makes sense that indexes have smaller dim 0
# assert indexes.shape[0] == matrix.shape[0]
assert indexes.shape[0] <= matrix.shape[0]
out = matrix.gather(1, indexes)
assert out.shape == indexes.shape
return out
matrix = t.arange(15).view(3, 5)
indexes = t.tensor([[4], [3], [2]])
expected = t.tensor([[4], [8], [12]])
assert_all_equal(gather_2d(matrix, indexes), expected)
indexes2 = t.tensor([[2, 4], [1, 3], [0, 2]])
expected2 = t.tensor([[2, 4], [6, 8], [10, 12]])
assert_all_equal(gather_2d(matrix, indexes), expected)
def total_price_gather(prices: t.Tensor, items: t.Tensor) -> float:
"""Compute the same as total_price_indexing, but use torch.gather."""
assert items.max() < prices.shape[0]
Z = prices.gather(0, items)
# print(Z)
return t.sum(Z).item()
prices = t.tensor([0.5, 1, 1.5, 2, 2.5])
items = t.tensor([0, 0, 1, 1, 4, 3, 2])
assert total_price_gather(prices, items) == 9.0
def integer_array_indexing(matrix: t.Tensor, coords: t.Tensor) -> t.Tensor:
"""Return the values at each coordinate using integer array indexing.
For details on integer array indexing, see:
https://numpy.org/doc/stable/user/basics.indexing.html#integer-array-indexing
matrix: shape (d_0, d_1, ..., d_n)
coords: shape (batch, n)
Return: (batch, )
"""
# Z = rearrange(coords, "b ... -> ... b")
# Z = matrix[*Z]
# return Z
# After reviewing answers, realised rearrange is just performing transpose
# rearrange(coords, "b ... -> ... b") === coords.T
# duh...
return matrix[*coords.T]
mat_2d = t.arange(15).view(3, 5)
coords_2d = t.tensor([[0, 1], [0, 4], [1, 4]])
actual = integer_array_indexing(mat_2d, coords_2d)
assert_all_equal(actual, t.tensor([1, 4, 9]))
mat_3d = t.arange(2 * 3 * 4).view((2, 3, 4))
coords_3d = t.tensor([[0, 0, 0], [0, 1, 1], [0, 2, 2], [1, 0, 3], [1, 2, 0]])
actual = integer_array_indexing(mat_3d, coords_3d)
assert_all_equal(actual, t.tensor([0, 5, 10, 15, 20]))
def batched_logsumexp(matrix: t.Tensor) -> t.Tensor:
"""For each row of the matrix, compute log(sum(exp(row))) in a numerically stable way.
matrix: shape (batch, n)
Return: (batch, ). For each i, out[i] = log(sum(exp(matrix[i]))).
Do this without using PyTorch's logsumexp function.
A couple useful blogs about this function:
- https://leimao.github.io/blog/LogSumExp/
- https://gregorygundersen.com/blog/2020/02/09/log-sum-exp/
"""
a = matrix.max(dim=1).values
x_minus_a = matrix - a[:, None]
exp = t.exp(x_minus_a)
# I guess answers just use dims = -1 as a matter of preference? 1 and -1 are the same when ndims == 2 right?
sums = t.sum(exp, dim=1)
# print(sums)
result = a + t.log(sums)
return result
matrix = t.tensor([[-1000, -1000, -1000, -1000], [1000, 1000, 1000, 1000]])
expected = t.tensor([-1000 + math.log(4), 1000 + math.log(4)])
actual = batched_logsumexp(matrix)
assert_all_close(actual, expected)
matrix2 = t.randn((10, 20))
expected2 = t.logsumexp(matrix2, dim=-1)
actual2 = batched_logsumexp(matrix2)
assert_all_close(actual2, expected2)
def batched_softmax(matrix: t.Tensor) -> t.Tensor:
"""For each row of the matrix, compute softmax(row).
Do this without using PyTorch's softmax function.
Instead, use the definition of softmax: https://en.wikipedia.org/wiki/Softmax_function
matrix: shape (batch, n)
Return: (batch, n). For each i, out[i] should sum to 1.
"""
exp = t.exp(matrix)
expsum = t.sum(exp, dim=1, keepdim=True)
# print(expsum)
return t.divide(exp, expsum)
matrix = t.arange(1, 6).view((1, 5)).float().log()
expected = t.arange(1, 6).view((1, 5)) / 15.0
actual = batched_softmax(matrix)
assert_all_close(actual, expected)
for i in [0.12, 3.4, -5, 6.7]:
assert_all_close(actual, batched_softmax(matrix + i))
matrix2 = t.rand((10, 20))
actual2 = batched_softmax(matrix2)
assert actual2.min() >= 0.0
assert actual2.max() <= 1.0
assert_all_equal(actual2.argsort(), matrix2.argsort())
assert_all_close(actual2.sum(dim=-1), t.ones(matrix2.shape[:-1]))
def batched_logsoftmax(matrix: t.Tensor) -> t.Tensor:
"""Compute log(softmax(row)) for each row of the matrix.
matrix: shape (batch, n)
Return: (batch, n). For each i, out[i] should sum to 1.
Do this without using PyTorch's logsoftmax function.
For each row, subtract the maximum first to avoid overflow if the row contains large values.
"""
maxes = matrix.max(dim=1, keepdim=True).values
# print(maxes)
softmaxes = batched_softmax(matrix - maxes)
# print(softmaxes)
return softmaxes.log()
matrix = t.arange(1, 6).view((1, 5)).float()
start = 1000
matrix2 = t.arange(start + 1, start + 6).view((1, 5)).float()
actual = batched_logsoftmax(matrix2)
expected = t.tensor([[-4.4519, -3.4519, -2.4519, -1.4519, -0.4519]])
assert_all_close(actual, expected)
def batched_cross_entropy_loss(logits: t.Tensor, true_labels: t.Tensor) -> t.Tensor:
"""Compute the cross entropy loss for each example in the batch.
logits: shape (batch, classes). logits[i][j] is the unnormalized prediction for example i and class j.
true_labels: shape (batch, ). true_labels[i] is an integer index representing the true class for example i.
Return: shape (batch, ). out[i] is the loss for example i.
Hint: convert the logits to log-probabilities using your batched_logsoftmax from above.
Then the loss for an example is just the negative of the log-probability that the model assigned to the true class. Use torch.gather to perform the indexing.
"""
# print(logits)
neg_softmax = -batched_logsoftmax(logits)
# print(neg_softmax)
# print(true_labels)
gathered = neg_softmax.gather(1, true_labels[:, None])
gathered = rearrange(gathered, "n 1 -> n")
# print(gathered)
return gathered
logits = t.tensor(
[[float("-inf"), float("-inf"), 0], [1 / 3, 1 / 3, 1 / 3], [float("-inf"), 0, 0]]
)
true_labels = t.tensor([2, 0, 0])
expected = t.tensor([0.0, math.log(3), float("inf")])
actual = batched_cross_entropy_loss(logits, true_labels)
assert_all_close(actual, expected)
def collect_rows(matrix: t.Tensor, row_indexes: t.Tensor) -> t.Tensor:
"""Return a 2D matrix whose rows are taken from the input matrix in order according to row_indexes.
matrix: shape (m, n)
row_indexes: shape (k,). Each value is an integer in [0..m).
Return: shape (k, n). out[i] is matrix[row_indexes[i]].
"""
assert row_indexes.max() < matrix.shape[0]
# This was ominously easy...
return matrix[row_indexes]
matrix = t.arange(15).view((5, 3))
row_indexes = t.tensor([0, 2, 1, 0])
actual = collect_rows(matrix, row_indexes)
expected = t.tensor([[0, 1, 2], [6, 7, 8], [3, 4, 5], [0, 1, 2]])
assert_all_equal(actual, expected)
def collect_columns(matrix: t.Tensor, column_indexes: t.Tensor) -> t.Tensor:
"""Return a 2D matrix whose columns are taken from the input matrix in order according to column_indexes.
matrix: shape (m, n)
column_indexes: shape (k,). Each value is an integer in [0..n).
Return: shape (m, k). out[:, i] is matrix[:, column_indexes[i]].
"""
assert column_indexes.max() < matrix.shape[1]
return matrix.T[row_indexes].T
matrix = t.arange(15).view((5, 3))
column_indexes = t.tensor([0, 2, 1, 0])
actual = collect_columns(matrix, column_indexes)
expected = t.tensor(
[[0, 2, 1, 0], [3, 5, 4, 3], [6, 8, 7, 6], [9, 11, 10, 9], [12, 14, 13, 12]]
)
assert_all_equal(actual, expected)