| 41 | # return action_max * np.tanh(x) |
| 42 | |
| 43 | class ANN: |
| 44 | def __init__(self, D, M, K, f=relu): |
| 45 | self.D = D |
| 46 | self.M = M |
| 47 | self.K = K |
| 48 | self.f = f |
| 49 | |
| 50 | def init(self): |
| 51 | D, M, K = self.D, self.M, self.K |
| 52 | self.W1 = np.random.randn(D, M) / np.sqrt(D) |
| 53 | # self.W1 = np.zeros((D, M)) |
| 54 | self.b1 = np.zeros(M) |
| 55 | self.W2 = np.random.randn(M, K) / np.sqrt(M) |
| 56 | # self.W2 = np.zeros((M, K)) |
| 57 | self.b2 = np.zeros(K) |
| 58 | |
| 59 | def forward(self, X): |
| 60 | Z = self.f(X.dot(self.W1) + self.b1) |
| 61 | return np.tanh(Z.dot(self.W2) + self.b2) * action_max |
| 62 | |
| 63 | def sample_action(self, x): |
| 64 | # assume input is a single state of size (D,) |
| 65 | # first make it (N, D) to fit ML conventions |
| 66 | X = np.atleast_2d(x) |
| 67 | Y = self.forward(X) |
| 68 | return Y[0] # the first row |
| 69 | |
| 70 | def get_params(self): |
| 71 | # return a flat array of parameters |
| 72 | return np.concatenate([self.W1.flatten(), self.b1, self.W2.flatten(), self.b2]) |
| 73 | |
| 74 | def get_params_dict(self): |
| 75 | return { |
| 76 | 'W1': self.W1, |
| 77 | 'b1': self.b1, |
| 78 | 'W2': self.W2, |
| 79 | 'b2': self.b2, |
| 80 | } |
| 81 | |
| 82 | def set_params(self, params): |
| 83 | # params is a flat list |
| 84 | # unflatten into individual weights |
| 85 | D, M, K = self.D, self.M, self.K |
| 86 | self.W1 = params[:D * M].reshape(D, M) |
| 87 | self.b1 = params[D * M:D * M + M] |
| 88 | self.W2 = params[D * M + M:D * M + M + M * K].reshape(M, K) |
| 89 | self.b2 = params[-K:] |
| 90 | |
| 91 | |
| 92 | def evolution_strategy( |
no outgoing calls
no test coverage detected