Skip to content

Commit 908e162

Browse files
committed
finish review of linear algebra
1 parent 51b4557 commit 908e162

File tree

10 files changed

+589
-0
lines changed

10 files changed

+589
-0
lines changed

optimization.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
# @Date : Jul-01-19 16:26
4+
# @Author : Your Name (you@example.org)
5+
# @Link : http://example.org
6+
7+
import os

probability.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
# @Date : Jul-01-19 12:50
4+
# @Author : Your Name (you@example.org)
5+
# @Link : http://example.org
6+
7+
import os
8+
from scipy.integrate import quad
9+
from numpy import sqrt, pi, exp
10+
11+
12+
def gaussian(x, mu, sigma):
13+
return 1/sqrt(2*pi*sigma**2)*exp(-((x-mu)/sigma)**2/2)
14+
15+
16+
if __name__ == "__main__":
17+
mu, sigma = 1, sqrt(2)
18+
a, b = 0.5, 2
19+
I = quad(gaussian, a, b, args=(mu, sigma))
20+
print(I)

project0/debug.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def get_sum_metrics(predictions, metrics=[]):
2+
for i in range(3):
3+
metrics.append(lambda x: x + i)
4+
5+
sum_metrics = 0
6+
for metric in metrics:
7+
sum_metrics += metric(predictions)
8+
9+
return sum_metrics
10+
11+
12+
def main():
13+
print(get_sum_metrics(0)) # Should be (0 + 0) + (0 + 1) + (0 + 2) = 3
14+
print(get_sum_metrics(1)) # Should be (1 + 0) + (1 + 1) + (1 + 2) = 6
15+
print(get_sum_metrics(2)) # Should be (2 + 0) + (2 + 1) + (2 + 2) = 9
16+
print(get_sum_metrics(3, [lambda x: x])) # Should be (3) + (3 + 0) + (3 + 1) + (3 + 2) = 15
17+
print(get_sum_metrics(0)) # Should be (0 + 0) + (0 + 1) + (0 + 2) = 3
18+
print(get_sum_metrics(1)) # Should be (1 + 0) + (1 + 1) + (1 + 2) = 6
19+
print(get_sum_metrics(2)) # Should be (2 + 0) + (2 + 1) + (2 + 2) = 9
20+
21+
if __name__ == "__main__":
22+
main()

project0/main.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import numpy as np
2+
3+
def randomization(n):
4+
"""
5+
Arg:
6+
n - an integer
7+
Returns:
8+
A - a randomly-generated nx1 Numpy array.
9+
"""
10+
#Your code here
11+
raise NotImplementedError
12+
13+
def operations(h, w):
14+
"""
15+
Takes two inputs, h and w, and makes two Numpy arrays A and B of size
16+
h x w, and returns A, B, and s, the sum of A and B.
17+
18+
Arg:
19+
h - an integer describing the height of A and B
20+
w - an integer describing the width of A and B
21+
Returns (in this order):
22+
A - a randomly-generated h x w Numpy array.
23+
B - a randomly-generated h x w Numpy array.
24+
s - the sum of A and B.
25+
"""
26+
#Your code here
27+
raise NotImplementedError
28+
29+
30+
def norm(A, B):
31+
"""
32+
Takes two Numpy column arrays, A and B, and returns the L2 norm of their
33+
sum.
34+
35+
Arg:
36+
A - a Numpy array
37+
B - a Numpy array
38+
Returns:
39+
s - the L2 norm of A+B.
40+
"""
41+
#Your code here
42+
raise NotImplementedError
43+
44+
45+
def neural_network(inputs, weights):
46+
"""
47+
Takes an input vector and runs it through a 1-layer neural network
48+
with a given weight matrix and returns the output.
49+
50+
Arg:
51+
inputs - 2 x 1 NumPy array
52+
weights - 2 x 1 NumPy array
53+
Returns (in this order):
54+
out - a 1 x 1 NumPy array, representing the output of the neural network
55+
"""
56+
#Your code here
57+
raise NotImplementedError

project0/test.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import traceback
2+
import torch.nn as nn
3+
4+
5+
def green(s):
6+
return '\033[1;32m%s\033[m' % s
7+
8+
9+
def yellow(s):
10+
return '\033[1;33m%s\033[m' % s
11+
12+
13+
def red(s):
14+
return '\033[1;31m%s\033[m' % s
15+
16+
17+
def log(*m):
18+
print(" ".join(map(str, m)))
19+
20+
21+
def log_exit(*m):
22+
log(red("ERROR:"), *m)
23+
exit(1)
24+
25+
26+
def check_numpy():
27+
try:
28+
import numpy
29+
log(green("PASS"), "NumPy installed")
30+
except ModuleNotFoundError:
31+
log(red("FAIL"), "NumPy not installed")
32+
33+
34+
def check_scipy():
35+
try:
36+
import scipy
37+
log(green("PASS"), "SciPy installed")
38+
except ModuleNotFoundError:
39+
log(red("FAIL"), "SciPy not installed")
40+
41+
42+
def check_matplotlib():
43+
try:
44+
import matplotlib
45+
log(green("PASS"), "matplotlib installed")
46+
except ModuleNotFoundError:
47+
log(red("FAIL"), "matplotlib not installed")
48+
49+
50+
def check_torch():
51+
try:
52+
import torch
53+
log(green("PASS"), "PyTorch installed")
54+
except ModuleNotFoundError:
55+
log(red("FAIL"), "PyTorch not installed")
56+
57+
58+
def check_tqdm():
59+
try:
60+
import tqdm
61+
log(green("PASS"), "tqdm installed")
62+
except ModuleNotFoundError:
63+
log(red("FAIL"), "tqdm not installed")
64+
65+
66+
def main():
67+
try:
68+
check_numpy()
69+
check_scipy()
70+
check_matplotlib()
71+
check_torch()
72+
check_tqdm()
73+
except Exception:
74+
log_exit(traceback.format_exc())
75+
76+
77+
if __name__ == "__main__":
78+
main()

requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
numpy
2+
matplotlib
3+
SciPy
4+
tqdm
5+
torch

resources_project0.tar.gz

1.2 KB
Binary file not shown.

sqrt_newton.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Date : 2019-06-01 20:13:43
4+
# @Author : Your Name (you@example.org)
5+
# @Link : http://example.org
6+
# @Version : $Id$
7+
8+
import os
9+
10+
def sqrt_newton(y):
11+
if y < 0:
12+
return None
13+
err = 1e-7
14+
xn = y
15+
while abs(y - xn*xn) > err:
16+
xn = (y/xn + xn) / 2.0;
17+
print(xn)
18+
return xn
19+
20+
def main():
21+
print(sqrt_newton(2))
22+
23+
if __name__ == '__main__':
24+
main()

0 commit comments

Comments
 (0)