MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / ANN

Class ANN

rl3/es_mnist.py:56–88  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

54
55
56class ANN:
57 def __init__(self, D, M, K):
58 self.D = D
59 self.M = M
60 self.K = K
61
62 def init(self):
63 D, M, K = self.D, self.M, self.K
64 self.W1 = np.random.randn(D, M) / np.sqrt(D)
65 self.b1 = np.zeros(M)
66 self.W2 = np.random.randn(M, K) / np.sqrt(M)
67 self.b2 = np.zeros(K)
68
69 def forward(self, X):
70 Z = np.tanh(X.dot(self.W1) + self.b1)
71 return softmax(Z.dot(self.W2) + self.b2)
72
73 def score(self, X, Y):
74 P = np.argmax(self.forward(X), axis=1)
75 return np.mean(Y == P)
76
77 def get_params(self):
78 # return a flat array of parameters
79 return np.concatenate([self.W1.flatten(), self.b1, self.W2.flatten(), self.b2])
80
81 def set_params(self, params):
82 # params is a flat list
83 # unflatten into individual weights
84 D, M, K = self.D, self.M, self.K
85 self.W1 = params[:D * M].reshape(D, M)
86 self.b1 = params[D * M:D * M + M]
87 self.W2 = params[D * M + M:D * M + M + M * K].reshape(M, K)
88 self.b2 = params[-K:]
89
90
91def evolution_strategy(

Callers 2

reward_functionFunction · 0.70
es_mnist.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected