-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsc1.py
More file actions
93 lines (85 loc) · 2.11 KB
/
sc1.py
File metadata and controls
93 lines (85 loc) · 2.11 KB
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
import torch
import torch.nn as nn
# ==== Put your solutions here ====
# Each function receives an nn.Linear.
# Hint #1: Make sure you print out the shape of the weights
# Hint #2: You can fill multiple weights at once by assigning
# the weights to a tensor. e.g.
# lin.weight.data[:] = torch.tensor([
# [1, 0],
# [0, 1],
# ], dtype=lin.weight.dtype, device=lin.weight.device)
# ==== Testing code: Tests your solutions ====
def test_1(fnc):
inp = torch.tensor([1., 5, 11, 20, 21]).reshape([-1, 1])
tar = torch.tensor([2., 6, 12, 21, 22]).reshape([-1, 1])
layer = nn.Linear(1, 1)
fnc(layer)
out = layer(inp)
torch.allclose(out, tar)
def test_2(fnc):
inp = torch.tensor([1., 5, 11, 20, 21]).reshape([-1, 1])
tar = torch.tensor([5., 17, 35, 62, 65]).reshape([-1, 1])
layer = nn.Linear(1, 1)
fnc(layer)
out = layer(inp)
torch.allclose(out, tar)
def test_3(fnc):
inp = torch.tensor([
[1., 1, 1, 1],
[5, 10, 15, 20],
[11, 20, 21, 25]
])
tar = inp.mean(dim=1, keepdim=True)
layer = nn.Linear(4, 1)
fnc(layer)
out = layer(inp)
torch.allclose(out, tar)
def test_4(fnc):
inp = torch.tensor([
[1., 1, 1, 1],
[5, 10, 15, 20],
[11, 20, 21, 25]
])
tar = torch.stack([
inp.mean(dim=1),
inp.sum(dim=1)
], dim=1)
layer = nn.Linear(4, 2)
fnc(layer)
out = layer(inp)
torch.allclose(out, tar)
def test_5(fnc):
inp = torch.tensor([
[1., 1, 1],
[5, 10, 15],
[11, 20, 21],
[4, 12, 2],
[6, 5, 4],
])
tar = torch.tensor([
[1., 1, 1],
[15, 10, 5],
[21, 20, 11],
[2, 12, 4],
[4, 5, 6],
])
layer = nn.Linear(3, 3)
fnc(layer)
out = layer(inp)
torch.allclose(out, tar)
def test_6(fnc):
inp = torch.tensor([
[1., 2, 3, 4, 5],
[1e10, 2e20, 3e30, 4e40, 5e50],
[-150, 150, 15, -15, 0.1]
])
tar = torch.tensor([
[4., 2],
[4, 2],
[4, 2],
])
layer = nn.Linear(5, 2)
fnc(layer)
out = layer(inp)
torch.allclose(out, tar)