def _rwd(self, x, u): th, al, thd, ald = x cost = al**2 + 5e-3 * ald**2 + 1e-1 * th**2 + 2e-2 * thd**2 + 3e-3 * u[ 0]**2 done = not self.state_space.contains(x) rwd = -cost * self.timing.dt_ctrl return np.float32(rwd), False
def QuadFitCurvatureMap(x): curv = [] for i in range(len(x)): # do a look-ahead quadratic fit, just like the car would do pts = x[(np.arange(6) + i) % len(x)] / 100 # convert to meters basis = (pts[1] - pts[0]) / np.abs(pts[1] - pts[0]) # project onto forward direction pts = (np.conj(basis) * (pts - pts[0])) p = np.polyfit(np.real(pts), np.imag(pts), 2) curv.append(p[0] / 2) return np.float32(curv)
def _sim_step(self, u): # Add a bit of noise to action for robustness u_noisy = u + 1e-6 * np.float32( self._np_random.randn(self.action_space.shape[0])) acc = self.dyn(self._sim_state, u_noisy) # Update internal simulation state self._sim_state[3] += self.timing.dt * acc[-1] self._sim_state[2] += self.timing.dt * acc[-2] self._sim_state[1] += self.timing.dt * self._sim_state[3] self._sim_state[0] += self.timing.dt * self._sim_state[2] # Pretend to only observe position and obtain velocity by filtering pos = self._sim_state[:2] # vel = self._sim_state[2:] vel = self._vel_filt(pos) return np.concatenate([pos, vel])
def hyperopt_train_test(params): clf = rxn_estimator(np.float32(params[0]), np.float32(params[1]), np.int(params[2]), other_param_dict) return cross_val_score(clf, X, y, cv=3).mean()
def _observation(self, state): obs = np.float32([state[0], state[1], state[2], state[3]]) return obs
def _observation(self, state): obs = np.float32( [state[0], np.cos(state[1]), np.sin(state[1]), state[2], state[3]]) return obs
def _rwd(self, x, u): th, al, thd, ald = x cost = al**2 + 5e-3 * ald**2 + 1e-1 * th**2 + 2e-2 * thd**2 + 3e-3 * u[ 0]**2 rwd = -cost * self.timing.dt_ctrl return np.float32(rwd), False
def fit(y, X, Wprior, H, solver='BFGS', use_autograd=True, bounds=None, maxiter=10000, disp=False): """ Bayesian Logistic Regression Solver. Assumes Laplace (Gaussian) Approximation to the posterior of the fitted parameter vector. Uses scipy.optimize.minimize Parameters ---------- y : array-like, shape (N, ) starting at 0 vector of binary ({0, 1, ... C} possible responses) X : array-like, shape (N, p) array of features Wprior : array-like, shape (C, p) vector of prior means on the parameters to be fit H : array-like, shape (C*p, C*p) or independent between classes (C, p, p) Array of prior Hessian (inverse covariance of prior distribution of parameters) solver : string scipy optimize solver used. this should be either 'Newton-CG', 'BFGS' or 'L-BFGS-B'. The default is BFGS. use_autograd: whether to use autograd's jacobian and hessian functions to solve bounds : iterable of length p a length p list (or tuple) of tuples each of length 2. This is only used if the solver is set to 'L-BFGS-B'. In that case, a tuple (lower_bound, upper_bound), both floats, is defined for each parameter. See the scipy.optimize.minimize docs for further information. maxiter : int maximum number of iterations for scipy.optimize.minimize solver. disp: bool whether to print convergence messages and additional information Returns ------- W_results : array-like, shape (C, p) posterior parameters (MAP estimate) H_results : array-like, shape like `H` posterior Hessian (Hessian of negative log posterior evaluated at MAP parameters) References ---------- Chapter 8 of Murphy, K. 'Machine Learning a Probabilistic Perspective', MIT Press (2012) Chapter 4 of Bishop, C. 'Pattern Recognition and Machine Learning', Springer (2006) """ # Check dimensionalities and data types # check X if len(X.shape) != 2: raise ValueError('X should be a matrix of shape (N, p)') (nX, pX) = X.shape if not np.issubdtype(X.dtype, np.float): X = np.float32(X) # check y if len(y.shape) > 1: raise ValueError('y should be a vector of shape (N, )') if len(y) != nX: raise ValueError('y and X should have the same number of examples') if not np.issubdtype(y.dtype, np.integer): y = np.int32(y) # check Wprior if len(Wprior.shape) != 2: raise ValueError('prior mean should be a vector of shape (C, p)') cW, pW = Wprior.shape if cW == 1: raise ValueError('please use binary logistic regression since the number of classes is 1') if pW != pX: raise ValueError('prior mean should have the same number of features as X') if not np.issubdtype(Wprior.dtype, np.float): Wprior = np.float32(Wprior) # check H if len(H.shape) == 3: cH, pH1, pH2 = H.shape if cH != cW: raise ValueError('prior Hessian does not have the same number of classes as prior mean') if pH1 != pX: raise ValueError('prior Hessian does not have the same number of features as prior mean') if pH1 != pH2: raise ValueError('prior Hessian should be a square matrix of shape (C, p, p)') elif len(H.shape) == 2: cpH1, cpH2 = H.shape if cpH1 != cpH2: raise ValueError('prior Hessian should be a square matrix of shape (C*p, C*p)') if cpH1 != pX * cW: raise ValueError('prior Hessian should be a square matrix of shape (C*p, C*p)') else: raise ValueError('prior Hessian should be of shape (C*p, C*p) or (C, p, p)') if not np.issubdtype(H.dtype, np.float): H = np.float32(H) if not has_autograd: use_autograd = False # choose between manually coded or autograd's jacobian and hessian functions # and use hessian product rather than hessian for newton-cg solver if use_autograd: jac_f = jacobian(_get_f_log_posterior) hess_f = hessian(_get_f_log_posterior) else: jac_f = _get_grad_log_post hess_f = _get_H_log_post # Do the regression if solver == 'Newton-CG': hessp_f = lambda W1D, q, Wprior, H, y, X: hess_f(W1D, Wprior, H, y, X) @ q results = minimize(_get_f_log_posterior, Wprior.reshape(-1), args=(Wprior, H, y, X), jac=jac_f, hessp=hessp_f, method='Newton-CG', options={'maxiter': maxiter, 'disp': disp}) W_results1D = results.x H_results = hess_f(W_results1D, Wprior, H, y, X) elif solver == 'BFGS': results = minimize(_get_f_log_posterior, Wprior.reshape(-1), args=(Wprior, H, y, X), jac=jac_f, method='BFGS', options={'maxiter': maxiter, 'disp': disp}) W_results1D = results.x H_results = hess_f(W_results1D, Wprior, H, y, X) elif solver == 'L-BFGS-B': results = minimize(_get_f_log_posterior, Wprior.reshape(-1), args=(Wprior, H, y, X), jac=jac_f, method='L-BFGS-B', bounds=bounds, options={'maxiter': maxiter, 'disp': disp}) W_results1D = results.x H_results = hess_f(W_results1D, Wprior, H, y, X) else: raise ValueError('Unknown solver specified: "{0}"'.format(solver)) W_results = W_results1D.reshape(Wprior.shape) return W_results, H_results