Ejemplo n.º 1
0
    def test_array1(self):
        """
        Tests functions with single numpy 1d array output
        """

        def f1(x):
            # single scalar input
            return np.array([i*x**i for i in range(5)])

        df = jacobian(f1)(3.0)
        for i, d in enumerate(df):
            self.assertAlmostEqual(i**2 * 3.0 ** (i - 1), d)

        def f2(params):
            # one list, one numpy array input
            x,y = params[0]
            A = params[1]
            return np.linalg.dot(np.sin(A), np.array([x,y**2]))

        A = np.array([[1.0, 2.0],[3.0, 4.0]])
        x,y = 2.0, np.pi
        params = [[x, y], A]
        df = jacobian(f2)(params)
        # df0_dx
        self.assertAlmostEqual(df[0][0][0],  np.sin(A)[0][0])
        # df1_dx
        self.assertAlmostEqual(df[1][0][0],  np.sin(A)[1][0])
        # df0_dy
        self.assertAlmostEqual(df[0][0][1],  2*np.sin(A)[0][1]*y)
        # df1_dy
        self.assertAlmostEqual(df[1][0][1],  2*np.sin(A)[1][1]*y)
        # df_dA
        assert np.linalg.norm(df[0][1][0] - (np.cos(A)*np.array([x,y**2]))[0]) < 1e-10
        assert np.linalg.norm(df[1][1][1] - (np.cos(A)*np.array([x,y**2]))[1]) < 1e-10
Ejemplo n.º 2
0
    def _setup_input(self, X, y=None):
        if not isinstance(X, np.ndarray):
            X = np.array(X)

        if X.size == 0:
            raise ValueError("Number of features must be > 0")

        if X.ndim == 1:
            self.n_samples, self.n_features = 1, X.shape
        else:
            self.n_samples, self.n_features = X.shape[0], np.prod(X.shape[1:])

        self.X = X

        if self.y_required:
            if y is None:
                raise ValueError("Missed required argument y")

            if not isinstance(y, np.ndarray):
                y = np.array(y)

            if y.size == 0:
                raise ValueError("Number of targets must be > 0")

        self.y = y
Ejemplo n.º 3
0
def opt_traj(func, fdict, T, opt_method = 'SGD', init = None, \
    learning_rate = 0.1, seed = 100, momentum = False, noise_level = 0.0):
    # do optimization and return the trajectory
    params = {'x': 0.0, 'y': 0.0}
    domain = fdict['domain']
    optimum = fdict['optimum']
    loss_and_grad = value_and_grad(func)
    #quick_grad_check(func, params)   
    params = init_params(params, domain, init, seed)
    check_grads(func, params)
    opt_server = Parameter_Server(opt_method, momentum)
    opt_server.init_gradient_storage(params)
    
    x_traj = []
    y_traj = []
    f_traj = []
    
    print 'optimising function using %s...' % opt_method
    for t in xrange(T):
        (func_value, func_grad) = loss_and_grad(params)
        x_traj.append(params['x'])
        y_traj.append(params['y'])
        f_traj.append(func_value)
        func_grad = inject_noise(func_grad, noise_level)
        if opt_method == 'SGD':
            norm = np.sqrt(func_grad['x'] ** 2 + func_grad['y'] ** 2)
            if norm >= 2.0:
                func_grad['x'] /= norm / 2; func_grad['y'] /= norm / 2
        params = opt_server.update(params, func_grad, learning_rate)

    return np.array(x_traj), np.array(y_traj), np.array(f_traj)
Ejemplo n.º 4
0
def GetTimeSeries():
    TS = []
    ERR = []
    GYRO = []
    ACCEL = []
    lastim = None
    t0 = None
    for msg in parse.ParseLog(open('../rustlerlog-BMPauR')):
        if msg[0] == 'img':
            _, ts, im = msg
            if t0 is None:
                t0 = ts
            im = GammaCorrect(im)
            if lastim is not None:
                TS.append(ts - t0)
                ERR.append(np.sum(np.abs(lastim-im)) / (320*240))
                GYRO.append(GYRO[-1])
                ACCEL.append(ACCEL[-1])
            lastim = im
        elif msg[0] == 'imu':
            _, ts, gyro, mag, accel = msg
            if t0 is None:
                t0 = ts
            TS.append(ts - t0)
            GYRO.append(gyro)
            ACCEL.append(accel)
            if len(ERR):
                ERR.append(ERR[-1])
            else:
                ERR.append(0)
    return np.array(TS), np.array(ERR), np.array(GYRO), np.array(ACCEL)
Ejemplo n.º 5
0
def initial():
    q = np.array([0, 0, 0, 1], np.float32)  # unit quaternion orientation
    b_g = np.array([0, 0, 0], np.float32)  # gyroscope bias
    v_g = np.array([0, 0, 0], np.float32)  # velocity of IMU in global frame
    b_a = np.array([0, 0, 0], np.float32)  # accelerometer bias
    p_g = np.array([0, 0, 0], np.float32)  # position in global frame
    return (q, b_g, v_g, b_a, p_g)
Ejemplo n.º 6
0
    def _forward(self, g, beta, initval, ifx):
        """
        Applies the forward iteration of the Picard series
        """
        g = g.reshape((self.dim.R, self.dim.N)).T

        struct_mats = np.array([sum(brd * Ld
                                    for brd, Ld in zip(br, self.basis_mats))
                                for br in beta])

        # construct the sets of A(t_n) shape: (N, K, K)
        A = np.array([sum(gnr * Ar
                          for gnr, Ar in zip(gn, struct_mats[1:])) +
                      struct_mats[0]
                      for gn in g])

        # initial layer shape (N, K, N_samples)
        layer = np.dstack([np.row_stack([m]*self.dim.N)
                           for m in initval])

        # weight matrix
        weights = self._get_weight_matrix(self.ttc, ifx)

        for m in range(self.order):
            layer = get_next_layer(layer,
                                   initval,
                                   A,
                                   weights)

        return layer
Ejemplo n.º 7
0
    def test_linear_system(self):
        """
        Tests taking the derivative across a linear system solve
        """

        def linsolve(params):
            A, B = params
            return np.linalg.solve(A, B)


        B = np.array([1.0, 3.0])
        A = np.array([[5.0, 2.0],[1.0, 3.0]])
        df = jacobian(linsolve)

        diff = df([A, B])

        Ainv = np.linalg.inv(A)
        x = np.linalg.solve(A, B)

        #df_dB
        assert np.linalg.norm(diff[0][1] - Ainv[0]) < 1e-10
        assert np.linalg.norm(diff[1][1] - Ainv[1]) < 1e-10

        #df_fA
        dr_da = np.zeros((2,4))
        dr_da[0, 0:2] = x
        dr_da[1, 2:] = x

        df_fa = np.linalg.solve(A, -dr_da)
        assert np.linalg.norm(df_fa[0] - diff[0][0].flatten()) < 1e-10
        assert np.linalg.norm(df_fa[1] - diff[1][0].flatten()) < 1e-10
Ejemplo n.º 8
0
	def MLE_EP(self, random_init):
		w_init = RS.normal(0,1, (self.dimx, self.dimz))
		if random_init is False:
			print "FALSE"
			w_init = self.W
		mus = np.array([])
		#w = self.marginal_likelihood(w_init)
		print "initialisation of W:"
		print w_init
		print ""
		w = self.marginal_likelihood(w_init)
		print "True W"
		print self.W
		print "MLE W"
		print w
		
		mus = np.array([])
		
		for i in xrange(self.n):
			mu = self.get_mu(self.observed[i], w)
			mus = np.hstack((mus, mu))
		mus = mus.reshape((self.n,2))
		sig = np.dot(self.W.transpose(), self.W)
		sig = sig/self.sigx
		sig = np.linalg.inv(sig)

		return mus, sig
Ejemplo n.º 9
0
    def callback(params):
        print("Log likelihood {}, Squared Error {}".format(-objective(params),squared_error(params,X,y,n_samples)))
        
        # Show posterior marginals.
        if dimensions[0] == 1:
            plot_xs = np.reshape(np.linspace(-5, 5, 300), (300,1))
            plot_deep_gp(ax_end_to_end, params, plot_xs)
            deep_map = create_deep_map(params)
            if dimensions == [1,1]:
                ax_end_to_end.plot(np.ndarray.flatten(deep_map[0][0]['x0']),deep_map[0][0]['y0'], 'ro')
            elif dimensions == [1,1,1]:
                plot_single_gp(ax_x_to_h,params,0,0,plot_xs)
                ax_x_to_h.set_title("Inputs to hiddens, pesudo data in red")

                plot_single_gp(ax_h_to_y,params,1,0,plot_xs)
                ax_h_to_y.set_title("Hiddens to outputs, pesudo data in red")
            elif dimensions == [1,1,1,1]:
                plot_single_gp(ax_x_to_h, params,0,0, plot_xs)
                ax_x_to_h.set_title("Inputs to Hidden 1, pesudo data in red")

                plot_single_gp(ax_h_to_h2, params,1,0,plot_xs)
                ax_h_to_h2.set_title("Hidden 1 to Hidden 2, pesudo data in red")

                plot_single_gp(ax_h2_to_y, params,2,0, plot_xs)
                ax_h2_to_y.set_title("Hidden 2 to Outputs, pesudo data in red")
        elif dimensions[0] == 2:
            plot_xs = np.array([np.array([a,b]) for a in np.linspace(-1,1,40) for b in np.linspace(-1,1,40)])
            plot_deep_gp_2d(ax, params, plot_xs)
        plt.draw()
        plt.pause(1.0/60.0)
Ejemplo n.º 10
0
def initialize(deep_map, X,num_pseudo_params):
    smart_map = {}
    for layer,layer_map in deep_map.iteritems():
        smart_map[layer] = {}
        for unit,gp_map in layer_map.iteritems():
            smart_map[layer][unit] = {}
            cov_params = gp_map['cov_params']
            lengthscales = cov_params[1:]
            if layer == 0:
                pairs = itertools.combinations(X, 2)
                dists = np.array([np.abs(p1-p2) for p1,p2 in pairs])
                smart_lengthscales = np.array([np.log(np.median(dists[:,i])) for i in xrange(len(lengthscales))])
                kmeans = KMeans(n_clusters = num_pseudo_params, init = 'k-means++')
                fit = kmeans.fit(X)
                smart_x0 = fit.cluster_centers_
                #inds = npr.choice(len(X), num_pseudo_params, replace = False)
                #smart_x0 = np.array(X)[inds,:]
                smart_y0 = np.ndarray.flatten(smart_x0) 
                #smart_y0 = np.array(y)[inds]
                smart_noise_scale = np.log(np.var(smart_y0))
            else:
                smart_x0 = gp_map['x0']
                smart_y0 = np.ndarray.flatten(smart_x0[:,0])
                smart_lengthscales = np.array([np.log(1) for i in xrange(len(lengthscales))])
                smart_noise_scale = np.log(np.var(smart_y0))
            gp_map['cov_params'] = np.append(cov_params[0],smart_lengthscales)
            gp_map['x0'] = smart_x0
            gp_map['y0'] = smart_y0
            #gp_map['noise_scale'] = smart_noise_scale
            smart_map[layer][unit] = gp_map
    smart_params = pack_deep_params(smart_map)
    return smart_params
Ejemplo n.º 11
0
def gradient_check():
    params = np.array([2,2])
    h = np.array([1e-5,0])
    print (log_variational(params+h,0.5)-log_variational(params,0.5))/h[0]
    h = np.array([0,1e-5])
    print (log_variational(params+h,0.5)-log_variational(params,0.5))/h[1]
    print gradient_log_variational(params,0.5,0)
    print gradient_log_variational(params,0.5,1)
Ejemplo n.º 12
0
def initial():
    r_g = np.array([0, 0, 0], np.float32)   # global to imu frame rotation
    b_g = np.array([0, 0, 0], np.float32)   # gyroscope bias
    v_g = np.array([0, 0, 0], np.float32)   # velocity of IMU in global frame
    b_a = np.array([0, 0, 0], np.float32)   # accelerometer bias
    p_g = np.array([0, 0, 0], np.float32)   # position in global frame
    a_s = 1.0  # accelerometer scale
    return (r_g, b_g, v_g, b_a, p_g, a_s)
Ejemplo n.º 13
0
    def gen_point_source_psf_image(pixel_grid, image, loc,
                                   psf_weights, psf_means, psf_covars):
        # use image PSF
        icovs = np.array([npla.inv(c) for c in psf_covars])
        dets  = np.array([npla.det(c) for c in psf_covars])
        chols = np.array([npla.cholesky(c) for c in psf_covars])

        return mog_like(pixel_grid, psf_means, icovs, dets, psf_weights)
Ejemplo n.º 14
0
def time_vspace_flatten():
    val = {'k':  npr.random((4, 4)),
           'k2': npr.random((3, 3)),
           'k3': 3.0,
           'k4': [1.0, 4.0, 7.0, 9.0],
           'k5': np.array([4., 5., 6.]),
           'k6': np.array([[7., 8.], [9., 10.]])}

    vspace_flatten(val)
Ejemplo n.º 15
0
 def compute_modfeat(self, w):
     mod_feat = self.conv_data_fea.copy()
     mod_dem = np.ones(mod_feat.shape)  ## need to change to accommodate non-binary features
     ws = np.array([w[k*self.F:(k+1)*self.F] for k in range(self.K)])
     w_tiled = np.array([ws for i in range(mod_feat.shape[0])])
     mod_feat = mod_feat * w_tiled
     mod_dem = mod_dem * w_tiled
     mod_feat = np.sum(np.exp(mod_feat), axis=2)
     mod_dem = np.sum(np.exp(mod_dem), axis=2)
     return mod_feat / mod_dem
def predict1(params, x):
    c, lam = params
    x_predict = None
    for i in x:
        if i == 0:
            x_predict = np.array([c])
        else:
            curr_predict = np.array([c*(i**lam)])
            x_predict = np.concatenate([x_predict, curr_predict], axis=0)
    return x_predict
def predict2(params, x):
    w, b = params
    x_predict = None
    for i in x:
        if i == 0:
            x_predict = np.array([b])
        else:
            curr_predict = np.array([w*i + b])
            x_predict = np.concatenate([x_predict, curr_predict], axis=0)
    return x_predict
Ejemplo n.º 18
0
def stamps2array(gstamps):
    stamps = []
    for gstamp in gstamps:
        template = gstamp.replace('-g-', '-%s-')
        bands = ['u', 'g', 'r', 'i', 'z']
        imgs = [FitsImage(b, fits_file_template=template) for b in bands]
        img_array = np.array([img.nelec for img in imgs])
        stamps.append( np.rollaxis(img_array, 0, 3) )
    stamps = np.array(stamps)
    return stamps
Ejemplo n.º 19
0
def flatten(value):
    """value can be any nesting of tuples, arrays, dicts.
       returns 1D numpy array and an unflatten function."""
    if isinstance(getval(value), np.ndarray):
        def unflatten(vector):
            return np.reshape(vector, value.shape)
        return np.ravel(value), unflatten

    elif isinstance(getval(value), float):
        return np.array([value]), lambda x : x[0]

    elif isinstance(getval(value), tuple):
        if not value:
            return np.array([]), lambda x : ()
        flattened_first, unflatten_first = flatten(value[0])
        flattened_rest, unflatten_rest = flatten(value[1:])
        def unflatten(vector):
            N = len(flattened_first)
            return (unflatten_first(vector[:N]),) + unflatten_rest(vector[N:])

        return np.concatenate((flattened_first, flattened_rest)), unflatten

    elif isinstance(getval(value), list):
        if not value:
            return np.array([]), lambda x : []
        flattened_first, unflatten_first = flatten(value[0])
        flattened_rest, unflatten_rest = flatten(value[1:])
        def unflatten(vector):
            N = len(flattened_first)
            return [unflatten_first(vector[:N])] + unflatten_rest(vector[N:])

        return np.concatenate((flattened_first, flattened_rest)), unflatten

    elif isinstance(getval(value), dict):
        flattened = []
        unflatteners = []
        lengths = []
        keys = []
        for k, v in sorted(iteritems(value), key=itemgetter(0)):
            cur_flattened, cur_unflatten = flatten(v)
            flattened.append(cur_flattened)
            unflatteners.append(cur_unflatten)
            lengths.append(len(cur_flattened))
            keys.append(k)

        def unflatten(vector):
            split_ixs = np.cumsum(lengths)
            pieces = np.split(vector, split_ixs)
            return {key: unflattener(piece)
                    for piece, unflattener, key in zip(pieces, unflatteners, keys)}

        return np.concatenate(flattened), unflatten

    else:
        raise Exception("Don't know how to flatten type {}".format(type(value)))
Ejemplo n.º 20
0
def time_flatten():
    val = {'k':  npr.random((4, 4)),
           'k2': npr.random((3, 3)),
           'k3': 3.0,
           'k4': [1.0, 4.0, 7.0, 9.0],
           'k5': np.array([4., 5., 6.]),
           'k6': np.array([[7., 8.], [9., 10.]])}

    vect, unflatten = flatten(val)
    val_recovered = unflatten(vect)
    vect_2, _ = flatten(val_recovered)
Ejemplo n.º 21
0
    def testRemapPointsToSegments01(self):
        points  = np.array([[1, 2], [3, 4], [5, 6]])
        indices = np.array([[0, 1], [2, 0]])

        res     = contourloss.remapPointsToSegements(points, indices)
        ans     = np.array([
            [[1, 2], [3, 4]],
            [[5, 6], [1, 2]]
        ])
        self.assertEqual(res.shape, ans.shape)
        self.assertTrue(np.allclose(res, ans))
Ejemplo n.º 22
0
def gen_test_data() :
    ################################################################
    # using sklearn                                                #
    ################################################################
    N = 500
    #features,labels = ds.make_classification(n_samples = N,n_features = 2,n_informative = 2,n_redundant = 0,n_clusters_per_class = 1,class_sep = 2,shift = 2.2)
    features,labels = ds.make_circles(n_samples = N)
    #features,labels = ds.make_moons(n_samples = N)
    labels[labels == 0] = -1
    features = auto_np.array(features) * 4.0
    labels = auto_np.array(labels).reshape(features.shape[0],1)
    return features,labels
Ejemplo n.º 23
0
def read_as_float_array(content, truncated=None, delimiter=None):
    """
    Read input as a float array.
    :param content: string input
    :param truncated: head number of elements extracted
    :param delimiter: delimiter string
    :return: a numpy float array
    """
    if truncated is None:
        return np.array(map(float, content.split(delimiter)), dtype=np.float64)
    else:
        return np.array(map(float, content.split(delimiter)[:truncated]), dtype=np.float64)
Ejemplo n.º 24
0
def PyLQR_TrajCtrl_GeneralTest():
    #build RBF basis
    rbf_basis = np.array([
        [-1.0, -1.0],
        [-1.0, 1.0],
        [1.0, -1.0],
        [1.0, 1.0]
        ])
    gamma = 1
    T = 100
    R = 1e-5
    # rbf_funcs = [lambda x, u, t, aux: np.exp(-gamma*np.linalg.norm(x[0:2]-basis)**2) + .01*np.linalg.norm(u)**2 for basis in rbf_basis]
    rbf_funcs = [
    lambda x, u, t, aux: -np.exp(-gamma*np.linalg.norm(x[0:2]-rbf_basis[0])**2) + R*np.linalg.norm(u)**2,
    lambda x, u, t, aux: -np.exp(-gamma*np.linalg.norm(x[0:2]-rbf_basis[1])**2) + R*np.linalg.norm(u)**2,
    lambda x, u, t, aux: -np.exp(-gamma*np.linalg.norm(x[0:2]-rbf_basis[2])**2) + R*np.linalg.norm(u)**2,
    lambda x, u, t, aux: -np.exp(-gamma*np.linalg.norm(x[0:2]-rbf_basis[3])**2) + R*np.linalg.norm(u)**2
    ]

    weights = np.array([.75, .5, .25, 1.])
    weights = weights / (np.sum(weights) + 1e-6)

    cost_func = lambda x, u, t, aux: np.sum(weights * np.array([basis_func(x, u, t, aux) for basis_func in rbf_funcs]))

    lqr_traj_ctrl = PyLQR_TrajCtrl(use_autograd=True)
    lqr_traj_ctrl.build_ilqr_general_solver(cost_func, n_dims=rbf_basis.shape[1], T=T)

    n_eval_pnts = 50
    coords = np.linspace(-2.5, 2.5, n_eval_pnts)
    xv, yv = np.meshgrid(coords, coords)

    z = [[cost_func(np.array([xv[i, j], yv[i, j]]), np.zeros(2), None, None) for j in range(yv.shape[1])] for i in range(len(xv))]

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.hold(True)
    ax.contour(xv, yv, z)
    
    n_queries = 5
    u_array = np.random.rand(2, T-1).T * 2 - 1
    
    for i in range(n_queries):
        #start from a perturbed point
        x0 = np.random.rand(2) * 4 - 2
        syn_traj = lqr_traj_ctrl.synthesize_trajectory(x0, u_array)
        #plot it
        ax.plot([x0[0]], [x0[1]], 'k*', markersize=12.0)
        ax.plot(syn_traj[:, 0], syn_traj[:, 1], linewidth=3.5)

    plt.show()

    return
Ejemplo n.º 25
0
def dw(px, py, d, r, T):
    R = so3.exp(r)
    x0 = np.array([px, py, 1])
    X = np.dot(R, d * x0) + T
    # derivative of projection w = xy/z, as a matrix
    # dw/dk = dw/dX * dX/dk
    dwdX = np.array([
        [X[2], 0, -X[0]],
        [0, X[2], -X[1]]]) / (X[2]*X[2])
    dXdR = so3.diff(r, R, d * x0)
    dXdT = np.eye(3)
    dXdd = np.dot(R, x0).reshape((3, 1))
    return np.dot(dwdX, np.hstack((dXdR, dXdT, dXdd)))
Ejemplo n.º 26
0
	def init_weight(self, x, y):
		self.classes_ = np.unique(y)
		if self.prob_func_ == "sigmoid" and len(self.classes_) > 2:
			raise ValueError()
		if self.prob_func_ is None:
			if len(self.classes_) == 2:
				self.prob_func_ = "sigmoid"
			else:
				self.prob_func_ = "softmax"
		if self.prob_func_ == "sigmoid":
			return np.array([self.eps_] * (x.shape[1] + 1))
		else: # self.prob_func_ == "softmax"
			return np.array([[self.eps_] * len(self.classes_) for i in xrange(x.shape[1] + 1)])
Ejemplo n.º 27
0
    def apply_control(self, x_array, u_array, k_array, K_array, alpha):
        """
        apply the derived control to the error system to derive new x and u arrays
        """
        x_new_array = [None] * len(x_array)
        u_new_array = [None] * len(u_array)

        x_new_array[0] = x_array[0]
        for t in range(self.T):
            u_new_array[t] = u_array[t] + alpha * (k_array[t] + K_array[t].dot(x_new_array[t] - x_array[t]))
            x_new_array[t+1] = self.plant_dyn(x_new_array[t], u_new_array[t], t, self.aux)

        return np.array(x_new_array), np.array(u_new_array)
Ejemplo n.º 28
0
def create_batches(images, labels):
    l = zip(images, labels)
    random.shuffle(l)
    r = [l[i:i+BATCH_SIZE] for i in xrange(0, len(l), BATCH_SIZE)]
    res = []
    for i in r:
        images = []
        labels = []
        for j in i:
            images.append(j[0])
            labels.append(j[1])
        res.append((np.array(images), np.array(labels)))
    return res
Ejemplo n.º 29
0
def shape_match_1d(y, x):
    """
    match the shape of y to that of x and return the new y object.
    """
    #assert(len(y.shape) == 1)
    sh = (np.array(x.shape) == np.array(y.shape))
    if sh.sum() != 1:
        print("There have been " + str(sh.sum()) + " matches in shape instead of exactly 1. Using first match only.", file=sys.stderr)
        sh[np.argmax(sh)+1:] = False
    
    new_sh = np.ones(sh.shape)
    new_sh[sh] = y.shape
    return np.reshape(y, new_sh)
Ejemplo n.º 30
0
    def __init__(self, n_unique=2, n_lags=1, n_tied=0, n_features=1,
                 startprob_init=None, transmat_init=None, startprob_prior=1.0,
                 transmat_prior=None, algorithm="viterbi", random_state=None,
                 n_iter=25, n_iter_min=2, tol=1e-4,
                 params=string.ascii_letters,
                 init_params=string.ascii_letters, alpha_init=None,
                 mu_init=None, precision_init=None,
                 precision_prior=None, precision_weight=0.0, mu_prior=None,
                 mu_weight=0.0, shared_alpha=True,
                 n_iter_update=1, verbose=False,
                 mu_bounds=np.array([-1.0e5, 1.0e5]),
                 precision_bounds=np.array([-1.0e5, 1.0e5]),
                 alpha_bounds=np.array([-1.0e5, 1.0e5])):
        super(ARTHMM, self).__init__(n_unique=n_unique, n_tied=n_tied,
                                     n_features=n_features,
                                     algorithm=algorithm,
                                     params=params, init_params=init_params,
                                     startprob_init=startprob_init,
                                     startprob_prior=startprob_prior,
                                     transmat_init=transmat_init,
                                     transmat_prior=transmat_prior,
                                     mu_init=mu_init, mu_weight=mu_weight,
                                     mu_prior=mu_prior,
                                     precision_init=precision_init,
                                     precision_weight=precision_weight,
                                     precision_prior=precision_prior, tol=tol,
                                     n_iter=n_iter, n_iter_min=n_iter_min,
                                     n_iter_update=n_iter_update,
                                     random_state=random_state,
                                     verbose=verbose,
                                     mu_bounds=mu_bounds,
                                     precision_bounds=precision_bounds)

        self.n_lags = n_lags
        if self.n_lags < 1:
            raise ValueError("n_lags needs to be greater than 0")

        self.shared_alpha = shared_alpha
        self.alpha_ = alpha_init
        self.alpha_bounds = alpha_bounds
        self.wrt.extend(['a'])


        if not self.shared_alpha:
            self.wrt_dims.update({'a': (self.n_unique, self.n_lags)})
        else:
            self.wrt_dims.update({'a': (1, self.n_lags)})


        self.wrt_bounds.update({'a': (self.alpha_bounds[0],
                                      self.alpha_bounds[1])})
Ejemplo n.º 31
0
def predict(weights, inputs):
    return activation(np.dot(inputs, weights))


def loss(weights):
    preds = predict(weights, train_X)
    label_probabilities = preds * train_y + (1 - preds) * (1 - train_y)
    return -np.sum(np.log(label_probabilities))


# Compute the gradient of the loss function
gradient_loss = grad(loss)

# Set the initial weights
weights = np.array([1.0, 1.0])

# Steepest Descent
loss_values = []
learning_rate = 0.001
for i in range(100):
    loss_values.append(loss(weights))
    step = gradient_loss(weights)
    weights -= step * learning_rate

# Plot the decision boundary
x_min, x_max = train_X[:, 0].min() - 0.5, train_X[:, 0].max() + 0.5
y_min, y_max = train_X[:, 1].min() - 0.5, train_X[:, 1].max() + 0.5
x_mesh, y_mesh = np.meshgrid(np.arange(x_min, x_max, 0.01),
                             np.arange(y_min, y_max, 0.01))
Z = predict(weights, np.c_[x_mesh.ravel(), y_mesh.ravel()])
Ejemplo n.º 32
0
    print("")
    print("x ",    xOpt)
    print("f(x) ", fOpt)
    print("fevals: ", fevals)
    print("DeltaF: ", deltaF)

    print("\nCheck end point")
    print("xOpt", xOpt)
    print("f_1(x) = {} <= 0".format(L4_Q5_ineq1(xOpt)))
    print("f_2(x) = {} <= 0".format(L4_Q5_ineq2(xOpt)))


    optNameList.append(optName)
    xList.append(xOpt)
    fxList.append(fOpt)
    fevalsList.append(fevals)
    deltaFList.append(deltaF)

xList = np.array(xList)
resultsDf = pd.DataFrame(data={"Optimizer": optNameList,
                                "\\(x^*_1\\)"    : xList[:, 0],
                                "\\(x^*_2\\)"    : xList[:, 1],
                                "\\(x^*_3\\)"    : xList[:, 2],
                                "\\(x^*_4\\)"    : xList[:, 3],
                                "\\(f(x^*)\\)"   : fxList,
                                "FEvals"  : fevalsList,
                                "\\(\\Delta F\\)" : deltaFList})

print(resultsDf)
resultsDf.to_excel(savePath)
Ejemplo n.º 33
0
 def log_p_F_given_s(self,s,floor=1e-8):
     # bin-wise likelihood of s spikes in each bin given df/f data F
     # n x T x N
     prev = np.array([self.g[:,k:k+1]*(self.F-self.b)[:,self.p-(k+1):-(k+1)] for k in range(self.p)])
     log_p_raw = -0.5*((self.F-self.b)[:,self.p:]-self.a*s-prev.sum(0))**2/self.noise
     return log_p_raw
Ejemplo n.º 34
0
 def set_energy(self, key, geom, d=None):
     e = lj_potential(np.array(geom))
     if d is None:
         self.energy[key] = e
     else:
         d[key] = e
Ejemplo n.º 35
0
parser = argparse.ArgumentParser(description='Run Sparse gamma DEF example.')
parser.add_argument('--eta', type=float, help='Stepsize parameter')
parser.add_argument('-B', type=int, help='Alpha Boost')
args = parser.parse_args()

eta = 0.75
B = 4
if (B > 1):
    correction = True
else:
    correction = False

n_iter = 1000

# Setup sizes
K = np.array([100, 40, 15])
D = 64 * 64
N = 320
n_latent = N * np.sum(K) + K[0] * D + np.sum(K[1:] * K[:-1])
sigma = 0.1

# Load data
x = np.loadtxt('data/faces_training.csv', delimiter=',')
alphaz = 0.1

# Define truncation functions and stepsize updates
trunc_shape = np.log(np.exp(1e-3) - 1.)
trunc_mean = np.log(np.exp(1e-4) - 1.)


def stepSize(iteration, sPrev, gradient, eta=1.0):
Ejemplo n.º 36
0
def make_nascar_model():
    As = [
        random_rotation(D_latent, np.pi / 24.),
        random_rotation(D_latent, np.pi / 48.)
    ]

    # Set the center points for each system
    centers = [np.array([+2.0, 0.]), np.array([-2.0, 0.])]
    bs = [
        -(A - np.eye(D_latent)).dot(center) for A, center in zip(As, centers)
    ]

    # Add a "right" state
    As.append(np.eye(D_latent))
    bs.append(np.array([+0.1, 0.]))

    # Add a "right" state
    As.append(np.eye(D_latent))
    bs.append(np.array([-0.25, 0.]))

    # Construct multinomial regression to divvy up the space
    w1, b1 = np.array([+1.0, 0.0]), np.array([-2.0])  # x + b > 0 -> x > -b
    w2, b2 = np.array([-1.0, 0.0]), np.array([-2.0])  # -x + b > 0 -> x < b
    w3, b3 = np.array([0.0, +1.0]), np.array([0.0])  # y > 0
    w4, b4 = np.array([0.0, -1.0]), np.array([0.0])  # y < 0
    Rs = np.row_stack((100 * w1, 100 * w2, 10 * w3, 10 * w4))
    r = np.concatenate((100 * b1, 100 * b2, 10 * b3, 10 * b4))

    true_rslds = SLDS(D_obs,
                      K,
                      D_latent,
                      transitions="recurrent_only",
                      dynamics="gaussian",
                      emissions="gaussian",
                      single_subspace=True)
    true_rslds.dynamics.mu_init = np.array([0, 1])
    true_rslds.dynamics.inv_sigma_init = np.log(1e-4) * np.ones(2)
    true_rslds.dynamics.As = np.array(As)
    true_rslds.dynamics.bs = np.array(bs)
    true_rslds.dynamics.inv_sigmas = np.log(1e-4) * np.ones((K, D_latent))

    true_rslds.transitions.Rs = Rs
    true_rslds.transitions.r = r

    true_rslds.emissions.inv_etas = np.log(1e-2) * np.ones((1, D_obs))
    return true_rslds
Ejemplo n.º 37
0
def grad_f1(x):
    B = np.array([[3, -1], [-1, 3]])
    a = np.array([[1], [0]])
    b = np.array([[0], [-1]])
    grad = np.dot(2, x).transpose() + np.dot(2, np.dot(B, x)).transpose() - a.transpose() + b.transpose()
    return grad
Ejemplo n.º 38
0
         - np.exp(-np.dot(np.dot((x - b).transpose(), B), (x-b))) \
         + 1/10 * np.log(np.linalg.det(np.dot(1/100, np.identity(2)) + np.dot(x, x.transpose())))
    return c1


def grad_f3(x):
    B = np.array([[3, -1], [-1, 3]])
    a = np.array([[1], [0]])
    b = np.array([[0], [-1]])
    grad = np.dot((np.exp(-np.dot((x-a).transpose(), (x-a)))), (x-a).transpose()) \
           - np.dot(np.exp(-np.dot((x-b).transpose(), np.dot(B, (x-b)))), np.dot(2, np.dot((x-b).transpose(), B))) \
           + np.dot((1/10/(1/100 + np.dot(x, x.transpose()))), np.dot(2, x.transpose()))
    return grad


theta = np.array([[1], [-1]])
gradf1 = grad(f1)
gradf2 = grad(f2)
gradf3 = grad(f3)
print( "AutoGradient of f1 is", gradf1(theta))
print( "ManGradient of f1 is", grad_f1(theta))
print( "AutoGradient of f2 is", gradf2(theta))
print( "ManGradient of f2 is", grad_f2(theta))
print( "AutoGradient of f3 is", gradf3(theta))
#print( "ManGradient of f3 is", grad_f3(theta))





Ejemplo n.º 39
0
 def logp(x):
     return -logsumexp(
         np.log(probs) - np.array([logp(x) for logp in neg_log_probs]))
Ejemplo n.º 40
0
 def governing_equation(self, t, Y):
     ''' ODEs of Bicycle Track Problem '''
     x, y = Y
     k1 = np.array([self.dfx(t), self.dfy(t)])
     k2 = np.array([x - self.front_track_x(t), y - self.front_track_y(t)])
     return np.sum(k1 * k2) * k2 / self.L**2
Ejemplo n.º 41
0
            back_track.set_data(bx, by)

        return lines


if __name__ == '__main__':

    import matplotlib.pyplot as plt
    import matplotlib.animation as animation

    def fx(x):
        return 5 + 5 * np.cos(x)

    def fy(x):
        return 5 * np.sin(x)

    span = [-np.pi, np.pi]
    P0 = np.array([-3, 0])

    BT = BicycleTrack(fx, fy)

    fig = plt.figure(tight_layout=True)
    for i in range(4):
        BT.solve(span, np.array([-4 - i, 0]), err=1e-6)
        plt.subplot(221 + i)
        BT.plot(plt)

    BT.animate(plt, animation)

    plt.show()
Ejemplo n.º 42
0
def Styblinski_Tang_low(x, bounds):
    delta = bounds[:, 1] - bounds[:, 0]
    s = np.array([0.28, 0.59, 0.47, 0.16, 0.32])
    x = ((x.T * delta - s) / delta).T
    return Styblinski_Tang_high(x, bounds)
Ejemplo n.º 43
0
def string_to_one_hot(string, maxchar):
    """Converts an ASCII string to a one-of-k encoding."""
    ascii = np.array([ord(c) for c in string]).T
    return np.array(ascii[:,None] == np.arange(maxchar)[None, :], dtype=int)
Ejemplo n.º 44
0
def grad_f2(x):
    B = np.array([[3, -1], [-1, 3]])
    a = np.array([[1], [0]])
    b = np.array([[0], [-1]])
    grad = np.dot((np.cos(np.dot((x-a).transpose(), (x-a)))), (x-a).transpose()) + np.dot(2, np.dot((x-b).transpose(), B))
    return grad
from __future__ import absolute_import
from __future__ import print_function
import autograd.numpy as np
from autograd import value_and_grad
from scipy.optimize import minimize

def rosenbrock(x):
    return 100*(x[1] - x[0]**2)**2 + (1 - x[0])**2

# Build a function that also returns gradients using autograd.
rosenbrock_with_grad = value_and_grad(rosenbrock)

# Optimize using conjugate gradients.
result = minimize(rosenbrock_with_grad, x0=np.array([0.0, 0.0]), jac=True, method='CG')
print("Found minimum at {0}".format(result.x))
Ejemplo n.º 46
0
    thetaHat = model.params.get_free().copy()
    X = train.X
    Y = train.Y
    D1 = model.D1
    D2 = model.D2
    iprods = np.linalg.solve(model.hessian, X.T).T

    # Comptue exact and approximate Q_n
    Q = np.einsum('nd,nd->n', iprods, X)
    Qappx, estQErr = solvers.compute_Q(model, K=Xrank + 5, estErr=True)
    print(X.shape)

    exactCV, exactParams, sets = retrainingPlans.leave_k_out_cv(
        model, k=1, method="exact", B=100, hold_outs='stochastic')

    sets = np.array(sets).squeeze().astype(np.int32)
    exact = np.einsum('nd,nd->n', exactParams, X[sets])

    NS = np.zeros(sets.shape[0])
    IJ = np.zeros(sets.shape[0])
    NSAppx = np.zeros(sets.shape[0])
    IJAppx = np.zeros(sets.shape[0])
    IJAppxBnd = np.zeros(sets.shape[0])
    for idx, n in enumerate(sets):
        IJParams = thetaHat + D1[n] * iprods[n]
        NS[idx] = np.inner(thetaHat, X[n]) + D1[n] * Q[n] / (1 - D2[n] * Q[n])
        IJ[idx] = np.inner(thetaHat, X[n]) + D1[n] * Q[n]
        IJAppx[idx] = np.inner(thetaHat, X[n]) + D1[n] * Qappx[n]
        IJAppxBnd[idx] = np.abs(D1[n]) * estQErr[n]

    model.compute_bounds()
Ejemplo n.º 47
0
# Each row is a case
# Columns 0-4 are features
# Columns 5 & 6 are targets

features_and_targets = np.array(
                                   [[0, 0, 0, 0, 0, 0, 1],
                                    [0, 0, 0, 0, 1, 0, 1],
                                    [0, 0, 0, 1, 1, 0, 1],
                                    [0, 0, 1, 1, 1, 0, 1],
                                    [0, 1, 1, 1, 1, 0, 1],
                                    [1, 1, 1, 1, 0, 0, 1],
                                    [1, 1, 1, 0, 0, 0, 1],
                                    [1, 1, 0, 0, 0, 0, 1],
                                    [1, 0, 0, 0, 0, 0, 1],
                                    [1, 0, 0, 1, 0, 0, 1],
                                    [1, 0, 1, 1, 0, 0, 1],
                                    [1, 1, 0, 1, 0, 0, 1],
                                    [0, 1, 0, 1, 1, 0, 1],
                                    [0, 0, 1, 0, 1, 0, 1],
                                    [1, 0, 1, 1, 1, 1, 0],
                                    [1, 1, 0, 1, 1, 1, 0],
                                    [1, 0, 1, 0, 1, 1, 0],
                                    [1, 0, 0, 0, 1, 1, 0],
                                    [1, 1, 0, 0, 1, 1, 0],
                                    [1, 1, 1, 0, 1, 1, 0],
                                    [1, 1, 1, 1, 1, 1, 0],
                                    [1, 0, 0, 1, 1, 1, 0]], dtype=float)

# shuffle our cases
np.random.shuffle(features_and_targets)
Ejemplo n.º 48
0
 def plot_con(self, con, p, index=None):
     if not index:
         x = np.array(3 * [p] * len(self.itinerary) +
                      3 * [0] * len(self.itinerary))
         return con['fun'](x, con['args'][0], con['args'][1],
                           con['args'][2], con['args'][3], con['args'][4])
Ejemplo n.º 49
0
def cast_to_same_dtype(value, example):
    if hasattr(example, 'dtype') and example.dtype.type is not np.float64:
        return np.array(value, dtype=example.dtype)
    else:
        return value
Ejemplo n.º 50
0
def calculate_A_matrix_autograd(x1,
                                y1,
                                x2,
                                y2,
                                x3,
                                y3,
                                x4,
                                y4,
                                x5,
                                y5,
                                x6,
                                y6,
                                P,
                                normalize=False):
    """ Calculate the A matrix for the DLT algorithm:  A.H = 0
  all coordinates are in object plane
  """
    X1 = np.array([[x1], [y1], [0.], [1.]]).reshape(4, 1)
    X2 = np.array([[x2], [y2], [0.], [1.]]).reshape(4, 1)
    X3 = np.array([[x3], [y3], [0.], [1.]]).reshape(4, 1)
    X4 = np.array([[x4], [y4], [0.], [1.]]).reshape(4, 1)
    X5 = np.array([[x5], [y5], [0.], [1.]]).reshape(4, 1)
    X6 = np.array([[x6], [y6], [0.], [1.]]).reshape(4, 1)

    U1 = np.array(np.dot(P, X1)).reshape(3, 1)
    U2 = np.array(np.dot(P, X2)).reshape(3, 1)
    U3 = np.array(np.dot(P, X3)).reshape(3, 1)
    U4 = np.array(np.dot(P, X4)).reshape(3, 1)
    U5 = np.array(np.dot(P, X5)).reshape(3, 1)
    U6 = np.array(np.dot(P, X6)).reshape(3, 1)

    object_pts = np.hstack([X1, X2, X3, X4, X5, X6])
    image_pts = np.hstack([U1, U2, U3, U4, U5, U6])

    if normalize:
        object_pts_norm, T1 = normalise_points(object_pts)
        image_pts_norm, T2 = normalise_points(image_pts)
    else:
        object_pts_norm = object_pts[[0, 1, 3], :]
        image_pts_norm = image_pts

    x1 = object_pts_norm[0, 0] / object_pts_norm[2, 0]
    y1 = object_pts_norm[1, 0] / object_pts_norm[2, 0]

    x2 = object_pts_norm[0, 1] / object_pts_norm[2, 1]
    y2 = object_pts_norm[1, 1] / object_pts_norm[2, 1]

    x3 = object_pts_norm[0, 2] / object_pts_norm[2, 2]
    y3 = object_pts_norm[1, 2] / object_pts_norm[2, 2]

    x4 = object_pts_norm[0, 3] / object_pts_norm[2, 3]
    y4 = object_pts_norm[1, 3] / object_pts_norm[2, 3]

    x5 = object_pts_norm[0, 4] / object_pts_norm[2, 4]
    y5 = object_pts_norm[1, 4] / object_pts_norm[2, 4]

    x6 = object_pts_norm[0, 5] / object_pts_norm[2, 5]
    y6 = object_pts_norm[1, 5] / object_pts_norm[2, 5]

    u1 = image_pts_norm[0, 0] / image_pts_norm[2, 0]
    v1 = image_pts_norm[1, 0] / image_pts_norm[2, 0]

    u2 = image_pts_norm[0, 1] / image_pts_norm[2, 1]
    v2 = image_pts_norm[1, 1] / image_pts_norm[2, 1]

    u3 = image_pts_norm[0, 2] / image_pts_norm[2, 2]
    v3 = image_pts_norm[1, 2] / image_pts_norm[2, 2]

    u4 = image_pts_norm[0, 3] / image_pts_norm[2, 3]
    v4 = image_pts_norm[1, 3] / image_pts_norm[2, 3]

    u5 = image_pts_norm[0, 4] / image_pts_norm[2, 4]
    v5 = image_pts_norm[1, 4] / image_pts_norm[2, 4]

    u6 = image_pts_norm[0, 5] / image_pts_norm[2, 5]
    v6 = image_pts_norm[1, 5] / image_pts_norm[2, 5]

    A = np.array([
        [0, 0, 0, -x1, -y1, -1, v1 * x1, v1 * y1, v1],
        [x1, y1, 1, 0, 0, 0, -u1 * x1, -u1 * y1, -u1],
        [0, 0, 0, -x2, -y2, -1, v2 * x2, v2 * y2, v2],
        [x2, y2, 1, 0, 0, 0, -u2 * x2, -u2 * y2, -u2],
        [0, 0, 0, -x3, -y3, -1, v3 * x3, v3 * y3, v3],
        [x3, y3, 1, 0, 0, 0, -u3 * x3, -u3 * y3, -u3],
        [0, 0, 0, -x4, -y4, -1, v4 * x4, v4 * y4, v4],
        [x4, y4, 1, 0, 0, 0, -u4 * x4, -u4 * y4, -u4],
        [0, 0, 0, -x5, -y5, -1, v5 * x5, v5 * y5, v5],
        [x5, y5, 1, 0, 0, 0, -u5 * x5, -u5 * y5, -u5],
        [0, 0, 0, -x6, -y6, -1, v6 * x6, v6 * y6, v6],
        [x6, y6, 1, 0, 0, 0, -u6 * x6, -u6 * y6, -u6],
    ])
    return A
Ejemplo n.º 51
0
 def _create_initial_point(self, Ts, E, *args):
     base_point = self._base_fitter._create_initial_point(Ts, E, *args)
     return anp.array([0.5] + list(base_point))
Ejemplo n.º 52
0
def _perfect_match_values(set_1, set_2, close_fn=np.allclose):
  """Checks that there's a perfect matching between set_1 and set_2."""
  if len(set_1) == len(set_2):
    matches = np.array([[close_fn(a, b) for a in set_1] for b in set_2], int)
    return np.all(matches.sum(0) == 1) and np.all(matches.sum(1) == 1)
  return False
#cam.set_R_axisAngle(1.0,  0.0,  0.0, np.deg2rad(140.0))
#cam.set_t(0.0,-1,1.0, frame='world')
#
r = 0.5
angle = 10
x = r*np.cos(np.deg2rad(angle))
z = r*np.sin(np.deg2rad(angle))
cam.set_t(0, x,z)
cam.set_R_mat(R_matrix_from_euler_t(0.0,0,0))
cam.look_at([0,0,0])

cam.set_R_axisAngle(1.0,  0.0,  0.0, np.deg2rad(110.0))
cam.set_t(0.0,-0.3,0.1, frame='world')

## Define a Display plane
pl =  Plane(origin=np.array([0, 0, 0]), normal = np.array([0, 0, 1]), size=(0.3,0.3), n = (2,2))
pl = CircularPlane()
pl.random(n =number_of_points, r = 0.01, min_sep = 0.01)


#objectPoints = pl.get_points()

#x1,y1,x2,y2,x3,y3,x4,y4 = gd.extract_objectpoints_vars(objectPoints)
#imagePoints_true = np.array(cam.project(objectPoints, False))
#imagePoints_measured = cam.addnoise_imagePoints(imagePoints_true, mean = 0, sd = 4)
#[repro, imagePoints_repro] =  gd.repro_error_autograd(x1,y1,x2,y2,x3,y3,x4,y4,cam.P, imagePoints_measured)
#%%



Ejemplo n.º 54
0
def simple_contour(g, xc, yc, nom_bord, i, j, c=0, delta=0.001, eps=2**(-26)):
    abscisses, ordonnées = [], []
    dic_fonction = {
        "UP": find_seed_U,
        "LEFT": find_seed_L,
        "RIGHT": find_seed_R,
        "DOWN": find_seed_D
    }
    position = dic_fonction[nom_bord](g, c, [xc[i], xc[i + 1]],
                                      [yc[j], yc[j + 1]])
    print(position)
    gradg = autograd.grad(g)
    if not isinstance(position, list):
        return [], []
    else:
        position = np.array(position)
        abscisses.append(position[0])
        ordonnées.append(position[1])
        print(position)

        def test(position):
            return xc[i] <= position[0] <= xc[
                i + 1] and yc[j] <= position[1] <= yc[j + 1]

        while test(position):
            gradX = gradg(position)
            norme = np.sqrt(gradX[1]**2 + gradX[0]**2)
            vect = np.array([gradX[1] / norme, -1 * gradX[0] / norme])
            position_faux = position + vect * delta
            position1 = position

            def h1(x, y):
                return g(np.array([x, y])) - c

            def h2(x, y):
                return (x - position1[0])**2 + (y - position1[1])**2 - delta**2

            def H(x, y):
                return np.array([h1(x, y), h2(x, y)])

            def J_H(x, y):

                j = autograd.jacobian
                return np.c_[j(H, 0)(x, y), j(H, 1)(x, y)]

            J = J_H(position_faux[0], position_faux[1])
            if np.linalg.det(J) == 0:
                abscisses.append(position_faux[0])
                ordonnées.append(position_faux[1])
            else:

                J_inv = np.linalg.inv(J)
                position = position_faux - J_inv @ H(position_faux[0],
                                                     position_faux[1])
                while np.linalg.norm(position - position_faux) > eps:
                    position_faux = position
                    J = J_H(position_faux[0], position_faux[1])
                    if np.linalg.det(J) == 0:
                        break
                    else:
                        J_inv = np.linalg.inv(J)
                        position = position_faux - J_inv @ H(
                            position_faux[0], position_faux[1])
                abscisses.append(position[0])
                ordonnées.append(position[1])

        return abscisses, ordonnées
Ejemplo n.º 55
0
    ntrials = 20
    #theta = 1.2
    #A = np.array([[np.cos(theta), np.sin(theta)], [-np.sin(theta), np.cos(theta)]])

    # the same for convenience. constructing reasonable C from small to large
    # dimensions is tricky

    d = 10
    D = 120

    #d = 05
    #D = 6

    # A = rand_stable(d)
    # A = np.stack([A for _ in range(T)], axis=0)
    A = np.array([0.5*np.eye(d) for _ in range(T)])
    f01 = np.sin(np.linspace(0., 2*np.pi, num=T))
    f10 = -np.sin(np.linspace(0., 2*np.pi, num=T) + 1.2)*f01
    A[:,0,1] = f01*np.random.randn()*np.sign(np.random.randn())
    A[:,1,0] = f10*np.random.randn()*np.sign(np.random.randn())
    A[-1] = np.zeros((d, d))


    #C = np.eye(D)
    C, _ = np.linalg.qr(np.random.randn(D, d))

    #Q0 = 0.5*np.eye(d)
    #Q = 0.5*np.eye(d)
    # Q = np.diag([2.9, 3.5, 3., 10.])
    Q0 = rand_psd(d, maxew=0.5)
    Q = rand_psd(d, maxew=0.5)
Ejemplo n.º 56
0
 def H(x, y):
     return np.array([h1(x, y), h2(x, y)])
Ejemplo n.º 57
0

def logistic_predictions(weights, inputs):
    # Outputs probability of a label being true according to logistic model.
    return sigmoid(np.dot(inputs, weights))


def training_loss(weights):
    # Training loss is the negative log-likelihood of the training labels.
    preds = logistic_predictions(weights, inputs)
    label_probabilities = preds * targets + (1 - preds) * (1 - targets)
    return -np.sum(np.log(label_probabilities))


# Build a toy dataset.
inputs = np.array([[0.52, 1.12, 0.77], [0.88, -1.08, 0.15],
                   [0.52, 0.06, -1.30], [0.74, -2.49, 1.39]])
targets = np.array([True, True, False, True])

# Build a function that returns gradients of training loss using autograd.
training_gradient_fun = grad(training_loss)

# Check the gradients numerically, just to be safe.
weights = np.array([0.0, 0.0, 0.0])
quick_grad_check(training_loss, weights)

# Optimize weights using gradient descent.
print("Initial loss:", training_loss(weights))
for i in range(100):
    weights -= training_gradient_fun(weights) * 0.01

print("Trained loss:", training_loss(weights))
Ejemplo n.º 58
0
 def h1(x, y):
     return g(np.array([x, y])) - c
Ejemplo n.º 59
0
    # dLpoiss = (df / f)[:,None] * ygrid[None,:] - df[:,None] # deriv of Poisson log likelihood
    # gwts = np.sum(np.exp(logjoint-logli[:,None]) * dLpoiss, axis=1) # gradient weights
    # gradient = -X.T@gwts

    # # Hessian
    # ddLpoiss = (ddf / f - (df / f)**2)[:,None] * ygrid[None,:] - ddf[:,None]
    # ddL = (ddLpoiss + dLpoiss**2)
    # hwts = np.sum(np.exp(logjoint-logli[:,None]) * ddL, axis=1) - gwts**2 # hessian weights
    # H = -X.T @ (X * hwts[:,None])

    # return negL, gradient, H


# Set calcium model hyperparams
p = 2
ar_coefs = np.array([1.51, -0.6])  # decay in one time bin
alpha = 1.0  # gain
# sig = 0.2      # stdev of Gaussian noise (in spike train space)
sig2 = 0.001  # stdev of Gaussian noise (in spike train space)
sig = np.sqrt(sig2)  # variance of noise
hyperparams = [ar_coefs, np.log(alpha), np.log(sig2)]

S = 10  # max spike count to consider
ygrid = np.arange(0, S + 1)

# Set up GLM
D_in = 19  # dimension of stimulus
D = D_in + 1  # total dims with bias
T = 50000
dt = 0.5
bias = npr.randn(T)
Ejemplo n.º 60
0
    a1 = params[0]
    b1 = params[1]
    theta = generate_kumaraswamy(params, U)
    E = np.log(
        kumaraswamy_pdf(theta, params) /
        kumaraswamy_pdf(theta, np.array([a2, b2])))
    E = np.mean(E)
    return E


if __name__ == '__main__':
    n = 100
    k = 80
    params = np.random.uniform(10, 100, 2)
    params = np.append(params, 1.)
    m = np.array([0., 0., 0.])
    v = np.array([0., 0., 0.])
    for i in range(50000):
        params, m, v = iterate(params, n, k, i, m, v)
        if i % 100 == 0:
            print params
            #print m,v
            #U1=np.random.uniform(0,1,100)
            #U2=np.random.uniform(0,1,100)
            #U3=np.random.uniform(0,1,n)
            #print lower_bound(params,n,k,U1,U2,U3)
    print params
    plt.clf()
    print "true mean"
    print(k + 1.) / (n + 2.)
    U = np.random.uniform(0, 1, 100000)