-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
79 lines (66 loc) · 1.81 KB
/
main.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
import numpy as np
def randomization(n):
"""
Arg:
n - an integer
Returns:
A - a randomly-generated nx1 Numpy array.
"""
return np.random.rand(n)
def operations(h, w):
"""
Takes two inputs, h and w, and makes two Numpy arrays A and B of size
h x w, and returns A, B, and s, the sum of A and B.
Arg:
h - an integer describing the height of A and B
w - an integer describing the width of A and B
Returns (in this order):
A - a randomly-generated h x w Numpy array.
B - a randomly-generated h x w Numpy array.
s - the sum of A and B.
"""
A= np.random.rand(h,w)
B = np.random.rand(h,w)
s = np.add(A,B)
return A,B,s
def norm(A, B):
"""
Takes two Numpy column arrays, A and B, and returns the L2 norm of their
sum.
Arg:
A - a Numpy array
B - a Numpy array
Returns:
s - the L2 norm of A+B.
"""
return np.linalg.norm(np.add(A,B))
def neural_network(inputs, weights):
"""
Takes an input vector and runs it through a 1-layer neural network
with a given weight matrix and returns the output.
Arg:
inputs - 2 x 1 NumPy array
weights - 2 x 1 NumPy array
Returns (in this order):
out - a 1 x 1 NumPy array, representing the output of the neural network
"""
product=np.multiply(inputs,weights)
return np.tanh(np.sum(product))
def scalar_function(x, y):
"""
Returns the f(x,y) defined in the problem statement.
"""
if x<=y:
return x*y
else:
return x/y
def vector_function(x, y):
"""
Arg:
x - h x w array
y - h x w array
Returns:
out - h x w array after the function is operated on the input arrays
"""
vector_fn=np.vectorize(scalar_function)
return vector_fn(x,y)