コード例 #1
0
    def backpropogate(self, vth, hn, xn, y_index):
        xn=cp.pad(xn, (1,0), 'constant', constant_values=1.0)
        #set up yn
        yn=cp.zeros(vth.shape)
        yn[y_index]=1

        #set up dlda (dl with respect to activation)
        dlda=-1*(yn-vth)


        #compute dldv (dl with respect to v)
        dldv=cp.expand_dims(dlda, axis=1)*hn.T

        #step down gradient for layer 2
        self.v_matrix-=self.learning_rate*dldv

        #set up matrix for computing layer 1 derivatives
        xn_matrix=cp.tile(xn, (self.h_matrix.shape[0],1))

        f_prime=self.tanh_derivative(xn)
        f_prime=cp.pad(f_prime, ((1,0), (0,0)), 'constant', constant_values=1.0)

        #set up l1 gradient matrix
        l1_gradient_matrix=cp.zeros(self.h_matrix.shape)
        for i, vi in enumerate(self.v_matrix):
            grad=((dlda[i]*vi)*f_prime.T[0])[1:]
            grad=cp.expand_dims(grad, axis=1)*xn_matrix
            l1_gradient_matrix+=grad

        #step dow gradient for layer 1
        self.h_matrix-=l1_gradient_matrix
def computeRT(item_path, xp=np):
    with open(item_path, 'r') as f:
        LINES = [line.strip("';\n") for line in f]

    param = {}
    for index, args in enumerate(LINES):
        name = args[:15].replace(" ", "")
        name = name[:].replace("=", "")
        val = args[15:]
        val = xp.asarray(val.strip("[]").split(","), dtype=xp.float64)
        param[name] = val

    cam_dir = param["cam_dir"]
    cam_pos = param["cam_pos"]
    cam_up = param["cam_up"]

    z = cam_dir / xp.linalg.norm(cam_dir)

    x = xp.cross(cam_up, z)
    x = x / xp.linalg.norm(x)

    y = xp.cross(z, x)

    x = xp.expand_dims(x, axis=1)
    y = xp.expand_dims(y, axis=1)
    z = xp.expand_dims(z, axis=1)

    R = xp.concatenate([x, y, z], axis=1)
    T = xp.expand_dims(cam_pos, axis=1)

    R_T = xp.concatenate([R, T], axis=1)

    return R_T
    def estimate_position_distance(place_bin_centers, positions, position_std):
        '''

        Parameters
        ----------
        place_bin_centers : ndarray, shape (n_position_bins, n_position_dims)
        positions : ndarray, shape (n_time, n_position_dims)
        position_std : float

        Returns
        -------
        position_distance : ndarray, shape (n_time, n_position_bins)

        '''
        n_time, n_position_dims = positions.shape
        n_position_bins = place_bin_centers.shape[0]

        position_distance = cp.ones((n_time, n_position_bins),
                                    dtype=cp.float32)

        for position_ind in range(n_position_dims):
            position_distance *= gaussian_pdf(
                cp.expand_dims(place_bin_centers[:, position_ind], axis=0),
                cp.expand_dims(positions[:, position_ind], axis=1),
                position_std)

        return position_distance
コード例 #4
0
def jitter(raster, window):
    ntrials, nbins = raster.shape
    # if needed, pad to be divisible by window
    if nbins % window:
        pad = cp.zeros((ntrials, -nbins % window))
        raster = cp.concatenate([raster, pad], axis=1)
    nbins_rounded = raster.shape[1]
    n_jitter_bins = nbins_rounded // window

    # get psth
    psth = raster.mean(axis=0)

    # bin over window and sum
    raster_binned = cp.reshape(raster, (ntrials, window, n_jitter_bins)).sum(axis=1)
    psth_binned = cp.reshape(psth, (window, n_jitter_bins)).sum(axis=0)

    # determine correction
    correction = raster_binned / cp.expand_dims(psth_binned, 0)
    correction = cp.tile(cp.expand_dims(correction, 1), [1, window, 1])
    correction = cp.reshape(correction, (ntrials, nbins_rounded))

    # apply correction
    raster_jittered = cp.expand_dims(psth, 0) * correction

    # trim off padding
    raster_jittered = raster_jittered[:, :nbins]
    raster_jittered[cp.isnan(raster_jittered)] = 0
    return raster_jittered
コード例 #5
0
def HaversineLocal(busMatrix, lineMatrix, haversine=True):
    MatrizOnibus = cp.copy(busMatrix)
    MatrizLinhas = cp.copy(lineMatrix)

    MatrizLinhas = cp.dsplit(MatrizLinhas, 2)
    MatrizOnibus = cp.dsplit(MatrizOnibus, 2)

    infVector = cp.squeeze(cp.sum(cp.isnan(MatrizLinhas[0]), axis=1), axis=-1)

    MatrizLinhas[0] = cp.expand_dims(MatrizLinhas[0], axis=-1)
    MatrizLinhas[1] = cp.expand_dims(MatrizLinhas[1], axis=-1)
    MatrizOnibus[0] = cp.expand_dims(MatrizOnibus[0], axis=-1)
    MatrizOnibus[1] = cp.expand_dims(MatrizOnibus[1], axis=-1)

    MatrizOnibus[0] *= cp.pi / 180
    MatrizOnibus[1] *= cp.pi / 180
    MatrizLinhas[1] = cp.transpose(MatrizLinhas[1], [2, 3, 0, 1]) * cp.pi / 180
    MatrizLinhas[0] = cp.transpose(MatrizLinhas[0], [2, 3, 0, 1]) * cp.pi / 180

    # Haversine or euclidian, based on <haversine>
    if haversine:
        results = 1000*2*6371.0088*cp.arcsin(
        cp.sqrt(
            (cp.sin((MatrizOnibus[0] - MatrizLinhas[0])*0.5)**2 + \
             cp.cos(MatrizOnibus[0])* cp.cos(MatrizLinhas[0]) * cp.sin((MatrizOnibus[1] - MatrizLinhas[1])*0.5)**2)
        ))
    else:
        results = cp.sqrt((MatrizOnibus[0] - MatrizLinhas[0])**2 +
                          (MatrizOnibus[1] - MatrizLinhas[1])**2)

    return results, infVector
コード例 #6
0
def preprocess(img, pos, half_size=15, device=0):
    with cp.cuda.Device(device):
        pos_x_left, pos_x_right = pos[:, 0] - half_size, pos[:, 0] + half_size
        pos_y_left, pos_y_right = pos[:, 1] - half_size, pos[:, 1] + half_size

        #TODO: current using filtering option, in the near future, change to padding option
        SELECT_MAP = (pos_x_left >= 0) * (pos_y_left >= 0) * (
            pos_x_right < 2048) * (pos_y_right < 2048)
        SELECT_INDEX = cp.where(SELECT_MAP > 0)[0]

        pos_x_left, pos_x_right, pos_y_left, pos_y_right = pos_x_left[
            SELECT_INDEX], pos_x_right[SELECT_INDEX], pos_y_left[
                SELECT_INDEX], pos_y_right[SELECT_INDEX]

        pos = pos[SELECT_INDEX]

        # shape should be dx * dy * dz
        pos_x = pos_x_left
        pos_x = cp.expand_dims(pos_x, axis=0)
        adding = cp.expand_dims(cp.arange(2 * half_size), 1)
        pos_x = pos_x + adding

        pos_y = pos_y_left
        pos_y = cp.expand_dims(pos_y, axis=0)
        adding = cp.expand_dims(cp.arange(2 * half_size), 1)
        pos_y = pos_y + adding

        # x * y * N
        _x = img[pos_x[:, cp.newaxis, :], pos_y]

    return _x.get(), pos.get()  #, groups_start, groups_end
コード例 #7
0
    def movepixels_3d(self, Iin, Tx):
        '''
        This function will translate the pixels of an image
        according to Tx translation images. 
        
        Inputs;
        Tx: The transformation image, dscribing the
                     (backwards) translation of every pixel in the x direction.
       
        Outputs,
          Iout : The transformed image
        
        '''

        nx, ny, nz = Iin.shape

        tmpx = cp.linspace(0, nx - 1, num=nx, dtype=np.float32)
        tmpy = cp.linspace(0, ny - 1, num=ny, dtype=np.float32)
        tmpz = cp.linspace(0, nz - 1, num=nz, dtype=np.float32)

        x, y, z = cp.meshgrid(tmpx, tmpy, tmpz, indexing='ij')
        Tlocalx = cp.expand_dims(x + Tx, axis=0)
        y = cp.expand_dims(y, axis=0)
        z = cp.expand_dims(z, axis=0)
        coordinates = cp.vstack((Tlocalx, y, z))

        return map_coordinates(Iin, coordinates, order=1, cval=0)
コード例 #8
0
ファイル: modules.py プロジェクト: haofengac/pyCapsNet
 def backward(self, grad): 
     self.s = cp.expand_dims(self.s.swapaxes(self.dim,-1), -1)
     grad = cp.expand_dims(grad.swapaxes(self.dim,-1), -1)
     mat = self.s @ self.s.swapaxes(-1,-2)
     mat = (-mat + cp.eye(mat.shape[-1]) * (mat**0.5))
     grad = mat @ grad
     self.s = self.s.swapaxes(self.dim,-1).squeeze(-1)
     return grad.swapaxes(self.dim,-2).squeeze(-1)
コード例 #9
0
 def gpd(points, fpoints=fpoints, cell=cell, radii=radii):
     points = cp.asarray(points)
     
     diff = cp.expand_dims(points, axis=1)-cp.expand_dims(fpoints, axis=0)
     diff = (diff - cp.around(diff)).reshape(-1,3)
     diff = cp.dot(cell, diff.T).T
     diff = cp.linalg.norm(diff, axis=1).reshape(-1,fpoints.shape[0])-radii.T
     return cp.min(diff, axis=1)
コード例 #10
0
        def fitnessAC0001():
            """fitnessAC0001."""
            @cuda.jit
            def kernel(x, T, M, digits, n_samples, n_machines, c_o, t_j, t_m):
                row = cuda.grid(1)
                if row < n_samples:
                    for i in range(x.shape[1]):
                        idx = int(math.ceil(x[row, i]))
                        t_aux = int(
                            math.ceil(T[row, idx,
                                        int(math.ceil(c_o[row, idx]))]))
                        m_aux = int(
                            math.ceil(M[row, idx,
                                        int(math.ceil(c_o[row, idx]))]))
                        c_o[row, idx] = c_o[row, idx] + 1

                        if t_m[row, m_aux] > t_j[row, idx]:
                            t_m[row, m_aux] = t_m[row, m_aux] + t_aux
                            t_j[row, idx] = t_m[row, m_aux]

                        else:
                            t_j[row, idx] = t_j[row, idx] + t_aux
                            t_m[row, m_aux] = t_j[row, idx]

            T_expand = cp.array(
                cp.repeat(cp.expand_dims(T, axis=0), n_samples, axis=0),
                dtype=cp.float32,
            )
            M_expand = cp.array(
                cp.repeat(cp.expand_dims(M, axis=0), n_samples, axis=0),
                dtype=cp.float32,
            )
            c_o_expand = cp.array(cp.zeros([n_samples, digits]),
                                  dtype=cp.float32)
            t_j_expand = cp.array(cp.zeros([n_samples, digits]),
                                  dtype=cp.float32)
            t_m_expand = cp.array(cp.zeros([n_samples, n_machines]),
                                  dtype=cp.float32)
            threadsperblock = 16
            blockspergrid_x = int(math.ceil(n_samples / threadsperblock))
            blockspergrid = blockspergrid_x

            cuda.synchronize()
            kernel[blockspergrid, threadsperblock](
                x,
                T_expand,
                M_expand,
                digits,
                n_samples,
                n_machines,
                c_o_expand,
                t_j_expand,
                t_m_expand,
            )
            cuda.synchronize()

            return t_j_expand
コード例 #11
0
def make_sequence_src_img(dirpath, rand_num=3, seq_num=3):
    img_name = 'scene_00_{:0>4}.png'.format(rand_num)
    path = os.path.join(dirpath, img_name)
    img = Image.open(path)
    img_mat = xp.asarray(img)
    img_mat = xp.asarray(img_mat, dtype='f').transpose(2, 0, 1)
    img_mat = xp.expand_dims(img_mat, axis=0)
    sequence_img_matrix = xp.expand_dims(img_mat, axis=0)

    return sequence_img_matrix
コード例 #12
0
def get_expanded_tensors(vertices: cp.ndarray = None,
                         edges: cp.ndarray = None,
                         normals: cp.ndarray = None,
                         norms: cp.ndarray = None,
                         normssq: cp.ndarray = None,
                         chunk_size: int = None,
                         num_verts: int = None) -> dict:
    #
    # Match the dimensions for subtraction etc
    #                                   REPEATED
    # [triangle_index, vert_index, querypoint_index, coordinates]
    vertices = cp.tile(
        cp.expand_dims(vertices, axis=2),
        (1, 1, chunk_size, 1),
    )
    #                                   REPEATED
    # [triangle_index, edge_index, querypoint_index, coordinates]
    # ===================
    # [:, 0, :, :] = u = p2 - p1
    # [:, 1, :, :] = v = p3 - p1
    # [:, 2, :, :] = w = p3 - p2
    edges = cp.tile(cp.expand_dims(edges, axis=2), (1, 1, chunk_size, 1))
    edge_norms = cp.linalg.norm(edges, axis=3)
    edge_normssq = cp.square(edge_norms)
    #                   REPEATED        REPEATED
    # [triangle_index, edge_index, querypoint_index, coordinates]
    normals = cp.tile(cp.expand_dims(normals, axis=(1, 2)),
                      (1, 3, chunk_size, 1))
    #                   REPEATED        REPEATED
    # [triangle_index, edge_index, querypoint_index]
    norms = cp.tile(cp.expand_dims(norms, axis=(1, 2)), (1, 3, chunk_size))
    #                   REPEATED        REPEATED
    # [triangle_index, edge_index, querypoint_index]
    normssq = cp.tile(cp.expand_dims(normssq, axis=(1, 2)), (1, 3, chunk_size))
    #
    # Zeros and ones for condition testing
    z = cp.zeros((num_verts, 3, chunk_size))
    o = cp.ones((num_verts, 3, chunk_size))
    #
    return {
        "vertices": vertices,
        "edges": edges,
        "edge_norms": edge_norms,
        "edge_normssq": edge_normssq,
        "normals": normals,
        "norms": norms,
        "normssq": normssq,
        "zero_tensor": z,
        "one_tensor": o,
        "chunk_size": chunk_size,
        "num_verts": num_verts
    }
コード例 #13
0
    def init_conc(self, img):
        bs, x, y, z = img.shape
        sh = 1 / (x * 2)
        vec = cp.linspace(self.top_bc + sh, self.bot_bc - sh, x)
        for i in range(2):
            vec = cp.expand_dims(vec, -1)
        vec = cp.expand_dims(vec, 0)
        vec = vec.repeat(z, -1)
        vec = vec.repeat(y, -2)
        vec = vec.repeat(bs, 0)
        vec = vec.astype(self.precision)

        return self.pad(img * vec, [self.top_bc * 2, self.bot_bc * 2])
コード例 #14
0
 def init_conc(self, img):
     bs, x, y, z = img.shape
     sh = 1 / (x + 1)
     vec = cp.linspace(self.top_bc + sh, self.bot_bc - sh, x)
     for i in range(2):
         vec = cp.expand_dims(vec, -1)
     vec = cp.expand_dims(vec, 0)
     vec = vec.repeat(z, -1)
     vec = vec.repeat(y, -2)
     vec = vec.repeat(bs, 0)
     vec = vec.astype(self.precision)
     img1 = cp.array(img)
     img1[img1 > 1] = 1
     return self.pad(img1 * vec, [self.top_bc, self.bot_bc])
コード例 #15
0
def _get_anchor_negative_triplet_mask(labels):
    """Return a 2D mask where mask[a, n] is True iff a and n have distinct labels.

    Args:
        labels: xp.array with shape=(batch_size)

    Returns:
        mask: xp.array with shape=(batch_size, batch_size), dtype=xp.bool
    """
    # Check if labels[i] == labels[j]
    # By using broadcasting:
    # Left side's shape (1, batch_size) => (*, batch_size)
    # Right side's shape (batch_size, 1) => (batch_size, *)
    return xp.expand_dims(labels, axis=0) != xp.expand_dims(labels, axis=1)
コード例 #16
0
def predict(args):
    classes = np.genfromtxt(os.path.join(args.dataset, "meta", "classes.txt"),
                            str,
                            delimiter="\n")
    model, preprocess, xp, test_dataset = prepare_setting(args)

    from chainer.exporters import caffe
    import onnx_chainer
    # x = [chainer.Variable(np.zeros((1, 3, 224, 224), np.float32))]
    # caffe.export(model, x, None, True, 'test')
    x = np.zeros((1, 3, 224, 224), dtype=np.float32)
    onnx_chainer.export(model, x, filename='text.onnx')

    top_1_counter = 0
    top_5_counter = 0
    top_10_counter = 0
    indices = list(range(len(test_dataset)))
    num_iteration = len(indices) if args.sample < 0 else args.sample
    random.shuffle(indices)
    with chainer.function.no_backprop_mode(), chainer.using_config(
            'train', False):
        for i in indices[:num_iteration]:
            img, label = test_dataset.get_example(i)
            h = model.predictor(xp.expand_dims(xp.array(img), axis=0))
            print(xp.expand_dims(xp.array(img), axis=0).shape)
            prediction = chainer.functions.softmax(h)
            if args.device >= 0:
                prediction = xp.asnumpy(prediction[0].data)
            else:
                prediction = prediction[0].data
            top_ten = np.argsort(-prediction)[:10]
            top_five = top_ten[:5]
            if top_five[0] == label:
                top_1_counter += 1
                top_5_counter += 1
                top_10_counter += 1
                msg = "Bingo!"
            elif label in top_five:
                top_5_counter += 1
                top_10_counter += 1
                msg = "matched top 5"
            elif label in top_ten:
                top_10_counter += 1
                msg = "matched top 10"
            else:
                msg = "Boo, actual {}".format(classes[label])
            print(classes[top_five], prediction[top_five], msg)
        print('top1 accuracy', top_1_counter / num_iteration)
        print('top5 accuracy', top_5_counter / num_iteration)
        print('top10 accuracy', top_10_counter / num_iteration)
def compute_similarity(src_batch_emb,
                       tgt_all_emb,
                       word2id,
                       batch_words,
                       indices,
                       include_self=False,
                       num_neighbors=10):

    # Normalize batch embeddings
    src_batch_emb_norm = src_batch_emb / cupy.linalg.norm(src_batch_emb,
                                                          axis=1)[:, None]

    # Compute cosine similarity
    cos_score = src_batch_emb_norm.dot(
        tgt_all_emb.T)  # [batch_size, num_words]

    sim_indices = indices
    # conc = cupy.concatenate([cupy.expand_dims(cos_score[i][sim_indices[i]], axis=0) for i in range(len(sim_indices))], axis=0)
    sim_cos_scores = cupy.concatenate([
        cupy.expand_dims(cos_score[i][sim_indices[i]], axis=0)
        for i in range(len(sim_indices))
    ],
                                      axis=0)
    sim_cos_scores = cupy.asnumpy(sim_cos_scores)
    return sim_cos_scores
コード例 #18
0
def get_feature(model, image):
    global inference_time

    start = time.time()
    image = xp.asarray(image)
    processed_image = model.feature_layer.prepare(image)
    resp, conf, x, y, w, h, e = model.predict(
        xp.expand_dims(processed_image, axis=0))
    resp = chainer.backends.cuda.to_cpu(resp.array)
    conf = chainer.backends.cuda.to_cpu(conf.array)
    w = chainer.backends.cuda.to_cpu(w.array)
    h = chainer.backends.cuda.to_cpu(h.array)
    x = chainer.backends.cuda.to_cpu(x.array)
    y = chainer.backends.cuda.to_cpu(y.array)
    e = chainer.backends.cuda.to_cpu(e.array)

    resp = np.squeeze(resp, axis=0)
    conf = np.squeeze(conf, axis=0)
    x = np.squeeze(x, axis=0)
    y = np.squeeze(y, axis=0)
    w = np.squeeze(w, axis=0)
    h = np.squeeze(h, axis=0)
    e = np.squeeze(e, axis=0)

    inference_time = time.time() - start
    logger.info('inference time {:.5f}'.format(inference_time))
    return resp, conf, x, y, w, h, e
コード例 #19
0
def compute3Dpositions(item_path, file_path, xp=np):
    K = getcamK(file_path, xp=xp)

    fx = K[0, 0]
    fy = K[1, 1]
    u0 = K[0, 2]
    v0 = K[1, 2]

    u = xp.tile(xp.arange(1, 641), (480, 1))
    v = xp.expand_dims(xp.arange(1, 481), axis=1)
    v = xp.tile(v, (1, 640))

    u_u0_by_fx = (u - u0) / fx
    v_v0_by_fy = (v - v0) / fy

    with open(item_path, 'r') as f:
        lines = f.read()
    lines = lines.split()
    str2mat = xp.array(lines, dtype=xp.float64)
    z = xp.reshape(str2mat, (480, 640))

    z = z / xp.sqrt(u_u0_by_fx**2 + v_v0_by_fy**2 + 1)

    x = ((u - u0) / fx) * z
    y = ((v - v0) / fy) * z

    return z
コード例 #20
0
 def Hpower(self, x):
     x = np.fft.ifft2(self.H *
                      np.fft.fft2(np.expand_dims(x, -1), axes=(0, 1)),
                      axes=(0, 1))
     x = np.sum(self.mask * self.crop(np.real(x)), 2)
     x = self.pad(x)
     return x
コード例 #21
0
def chw_cupy_random_flip(chw_imageset,
                         y_random=False,
                         x_random=False,
                         return_param=False,
                         copy=False):
    if len(chw_imageset.shape) == 3:
        chw_imageset = cupy.expand_dims(chw_imageset, axis=0)
    y_flip, x_flip = False, False
    if y_random:
        y_flip = random.choice([True, False])
    if x_random:
        x_flip = random.choice([True, False])

    if y_flip:
        chw_imageset = chw_imageset[:, :, ::-1, :]
    if x_flip:
        chw_imageset = chw_imageset[:, :, :, ::-1]

    if copy:
        chw_imageset = chw_imageset.copy()

    if return_param:
        return chw_imageset, {'y_flip': y_flip, 'x_flip': x_flip}
    else:
        return chw_imageset
コード例 #22
0
def _bss_decomp_mtifilt_cupy(reference_sources, estimated_source, j, flen,
                             nsrc):
    """Decomposition of an estimated source image into four components
    representing respectively the true source image, spatial (or filtering)
    distortion, interference and artifacts, derived from the true source
    images using multichannel time-invariant filters.
    """

    # decomposition
    # true source image

    s_true = cp.concatenate(
        (reference_sources[j], cp.zeros([flen - 1], dtype=cp.float64)), 0)

    # spatial (or filtering) distortion
    e_spat = _project_cupy(cp.expand_dims(reference_sources[j, :], 0),
                           estimated_source, flen, 1) - s_true

    # interference
    e_interf = _project_cupy(reference_sources, estimated_source, flen,
                             nsrc) - s_true - e_spat

    # artifacts
    e_artif = -s_true - e_spat - e_interf

    e_artif += cp.concatenate(
        (estimated_source, cp.zeros([flen - 1], dtype=cp.float64)), 0)

    return (s_true, e_spat, e_interf, e_artif)
コード例 #23
0
def process_2D(Volume_spectra: cp.ndarray, coordinates: cp.ndarray,
               dispersion: cp.array) -> np.array:
    """
    This function process 2D array of spectrum to return adjusted Bscan.

    :param Volume_spectra: 2nd order tensor containing spectras raw data. Last dimension is depth encoding.
    :type Volume_spectra: cp.ndarray
    :param coordinates: 2D array containing coordinates for k-linearization interpolation.
    :type coordinates: cp.ndarray
    :param dispersion: Array with value for dispersion compensation.
    :type dispersion: cp.array
    """

    if Arguments.dimension[1] == 1:
        Volume_spectra = cp.expand_dims(Volume_spectra, axis=0)
        coordinates = cp.array([coordinates[1]])
        Volume_spectra = detrend_1D(Volume_spectra)
        Volume_spectra = compensate_dispersion_2D(Volume_spectra, dispersion)
        Volume_spectra = linearize_spectra_1D(Volume_spectra, coordinates)

    else:
        Volume_spectra = detrend_2D(Volume_spectra)
        Volume_spectra = compensate_dispersion_2D(Volume_spectra, dispersion)
        Volume_spectra = linearize_spectra_2D(Volume_spectra, coordinates)

    if Arguments.shift:
        Volume_spectra = spectrum_shift_2D(Volume_spectra)

    Volume_spectra = fftpack.fft(
        Volume_spectra, axis=1,
        overwrite_x=True)[:, :Arguments.dimension[2] // 2]

    return cp.asnumpy(cp.absolute(Volume_spectra))
コード例 #24
0
ファイル: minnn.py プロジェクト: xuandif-cmu/minnn-assignment
 def backward(self):
     emb, t_sum, axis, reduce_size = self.get_ctx('emb', 't_sum', 'axis',
                                                  'reduce_size')
     if t_sum.grad is not None:
         g0 = xp.expand_dims(t_sum.grad, axis)
         g = xp.repeat(g0, reduce_size, axis=axis)
         emb.accumulate_grad(g)
コード例 #25
0
ファイル: data_processing.py プロジェクト: Blopeur/tigramite
def weighted_avg_and_std(values, axis, weights):
    """Returns the weighted average and standard deviation.

    Parameters
    ---------
    values : array
        Data array of shape (time, variables).

    axis : int
        Axis to average/std about

    weights : array
        Weight array of shape (time, variables).

    Returns
    -------
    (average, std) : tuple of arrays
        Tuple of weighted average and standard deviation along axis.
    """

    values[np.isnan(values)] = 0.
    average = np.ma.average(values, axis=axis, weights=weights)

    variance = np.sum(weights * (values - np.expand_dims(average, axis))**2,
                      axis=axis) / weights.sum(axis=axis)

    return (average, np.sqrt(variance))
コード例 #26
0
ファイル: CuPyImps.py プロジェクト: shrivastava-piyush/G-DICE
def stepGPU(stateG, newStateG, tG, oG, rG, currNodeG, aTransG, ntTransG, gamma, sampIndG, nodeIndG):
    nSim = stateG.shape[-1]
    nSamp = stateG.shape[0]
    fullSampIndices = cp.tile(cp.expand_dims(sampIndG, 1), (1, nSim))
    actionsG = aTransG[currNodeG, fullSampIndices]  #50*1000 actions at this timestep for each sample-sim combination
    #newStateG = cpr.multinomial(1, tG[s, a]) for s
    test = tG[stateG, actionsG]
    pass
コード例 #27
0
def iht3_py2(w1, w2, ax, shift, shape):

    C = 1. / np.sqrt(2.)
    y = np.zeros(shape)

    x1 = C * (w1 - w2)
    x2 = C * (w1 + w2)

    ind = ax + 2
    y = np.reshape(
        np.concatenate([np.expand_dims(x1, ind),
                        np.expand_dims(x2, ind)],
                       axis=ind), shape)

    if shift == True:
        y = np.roll(y, 1, axis=ax + 1)
    return y
コード例 #28
0
ファイル: modules.py プロジェクト: haofengac/pyCapsNet
 def backward(self, grads, optimizer=None):
     t = time.time()
     grads = self.squash.backward(grads)
     grads = grads.reshape((self.x_size[0],self.out_channels, self.mapsize, self.mapsize,-1))
     grads = cp.concatenate([cp.expand_dims(self.caps[i].backward(
         grads[:,:,:,:,i], optimizer=optimizer), -1) for i in range(self.ndim)], axis=-1)
     out = cp.sum(grads, axis=-1)
     return out
コード例 #29
0
    def Hadj(self, x):
        x = np.expand_dims(x, -1)
        x = x * self.mask
        x = self.pad(x)

        x = np.fft.fft2(x, axes=(0, 1))
        x = np.fft.ifft2(self.Hconj * x, axes=(0, 1))
        x = np.real(x)
        return x
コード例 #30
0
    def _process_feats(self, output_reshaped, mask):
        """Take in a reshaped YOLO output in height,width,3,85 format together with its
        corresponding YOLO mask and return the detected bounding boxes, the confidence,
        and the class probability in each cell/pixel.

        Keyword arguments:
        output_reshaped -- reshaped YOLO output as NumPy arrays with shape (3,height,width,85)
        mask -- 2-dimensional tuple with mask specification for this output
        """

        # Two in-line functions required for calculating the bounding box
        # descriptors:
        def sigmoid(value):
            """Return the sigmoid of the input."""
            return 1.0 / (1.0 + cp.exp(-value))

        def exponential(value):
            """Return the exponential of the input."""
            return cp.exp(value)

        # Vectorized calculation of above two functions:
        sigmoid_v = np.vectorize(sigmoid)
        exponential_v = np.vectorize(exponential)
        output_reshaped = cp.asarray(output_reshaped)
        grid_h, grid_w, _, _ = output_reshaped.shape

        anchors = cp.asarray(np.array([self.anchors[i] for i in mask]))

        # Reshape to N, height, width, num_anchors, box_params:
        anchors_tensor = cp.reshape(anchors, (1, 1, anchors.shape[0], 2))
        box_xy = sigmoid(output_reshaped[..., :2])
        box_wh = exponential(output_reshaped[..., 2:4]) * anchors_tensor
        box_confidence = sigmoid(output_reshaped[..., 4])

        box_confidence = cp.expand_dims(box_confidence, axis=-1)
        box_class_probs = sigmoid(output_reshaped[..., 5:])

        col = cp.tile(cp.arange(0, grid_w), grid_w).reshape(-1, grid_w)
        row = cp.tile(cp.arange(0, grid_h).reshape(-1, 1), grid_h)

        col = col.reshape(grid_h, grid_w, 1, 1).repeat(3, axis=-2)
        row = row.reshape(grid_h, grid_w, 1, 1).repeat(3, axis=-2)
        grid = cp.concatenate((col, row), axis=-1)

        box_xy += grid
        #box_xy /= (grid_w, grid_h)
        box_xy[..., 0] = box_xy[..., 0] / grid_w
        box_xy[..., 1] = box_xy[..., 1] / grid_h
        #box_wh /= self.input_resolution_yolo
        box_wh[..., 0] = box_wh[..., 0] / self.input_resolution_yolo[0]
        box_wh[..., 1] = box_wh[..., 1] / self.input_resolution_yolo[1]
        box_xy -= (box_wh / 2.)
        boxes = cp.concatenate((box_xy, box_wh), axis=-1)

        # boxes: centroids, box_confidence: confidence level, box_class_probs:
        # class confidence
        return boxes, box_confidence, box_class_probs
コード例 #31
0
ファイル: join.py プロジェクト: 2php/chainer
def stack(tup, axis=0):
    """Stacks arrays along a new axis.

    Args:
        tup (sequence of arrays): Arrays to be stacked.
        axis (int): Axis along which the arrays are stacked.

    Returns:
        cupy.ndarray: Stacked array.

    .. seealso:: :func:`numpy.stack`
    """
    return concatenate([cupy.expand_dims(x, axis) for x in tup], axis)
コード例 #32
0
ファイル: Agent.py プロジェクト: thinkother/DQN
    def train(self):
        # clear grads
        self.q_func.zerograds()

        # pull tuples from memory pool
        batch_tuples = self.replay.pull(Config.batch_size)
        if not len(batch_tuples):
            return

        # stack inputs
        cur_x = [self.env.getX(t.state) for t in batch_tuples]
        next_x = [self.env.getX(t.next_state) for t in batch_tuples]
        # merge inputs into one array
        if Config.gpu:
            cur_x = [cupy.expand_dims(t, 0) for t in cur_x]
            cur_x = cupy.concatenate(cur_x, 0)
            next_x = [cupy.expand_dims(t, 0) for t in next_x]
            next_x = cupy.concatenate(next_x, 0)
        else:
            cur_x = np.stack(cur_x)
            next_x = np.stack(next_x)

        # get cur outputs
        cur_output = self.QFunc(self.q_func, cur_x)
        # get next outputs, NOT target
        next_output = self.QFunc(self.q_func, next_x)
        # choose next action for each output
        next_action = [
            self.env.getBestAction(
                o.data,
                [t.next_state for t in batch_tuples]
            ) for o in next_output  # for each head in Model
        ]
        # get next outputs, target
        next_output = self.QFunc(self.target_q_func, next_x)

        # clear err of tuples
        for t in batch_tuples:
            t.err = 0.
        # store err count
        err_count_list = [0.] * len(batch_tuples)

        # compute grad's weights
        weights = np.array([t.P for t in batch_tuples], np.float32)
        if Config.gpu:
            weights = cuda.to_gpu(weights)
        if self.replay.getPoolSize():
            weights *= self.replay.getPoolSize()
        weights = weights ** -Config.beta
        weights /= weights.max()
        if Config.gpu:
            weights = cupy.expand_dims(weights, 1)
        else:
            weights = np.expand_dims(weights, 1)

        # update beta
        Config.beta = min(1, Config.beta + Config.beta_add)

        # compute grad for each head
        for k in range(Config.K):
            if Config.gpu:
                cur_output[k].grad = cupy.zeros_like(cur_output[k].data)
            else:
                cur_output[k].grad = np.zeros_like(cur_output[k].data)
            # compute grad from each tuples
            for i in range(len(batch_tuples)):
                if batch_tuples[i].mask[k]:
                    cur_action_value = \
                        cur_output[k].data[i][batch_tuples[i].action].tolist()
                    reward = batch_tuples[i].reward
                    next_action_value = \
                        next_output[k].data[i][next_action[k][i]].tolist()
                    target_value = reward
                    # if not empty position, not terminal state
                    if batch_tuples[i].next_state.in_game:
                        target_value += Config.gamma * next_action_value
                    loss = cur_action_value - target_value
                    cur_output[k].grad[i][batch_tuples[i].action] = 2 * loss
                    # count err
                    if cur_action_value:
                        batch_tuples[i].err += abs(loss / cur_action_value)
                        err_count_list[i] += 1

            # multiply weights with grad and clip
            if Config.gpu:
                cur_output[k].grad = cupy.multiply(
                    cur_output[k].grad, weights)
                cur_output[k].grad = cupy.clip(cur_output[k].grad, -1, 1)
            else:
                cur_output[k].grad = np.multiply(
                    cur_output[k].grad, weights)
                cur_output[k].grad = np.clip(cur_output[k].grad, -1, 1)
            # backward
            cur_output[k].backward()

        # adjust grads of shared
        for param in self.q_func.shared.params():
            param.grad /= Config.K

        # update params
        self.optimizer.update()

        # avg err
        for i in range(len(batch_tuples)):
            if err_count_list[i] > 0:
                batch_tuples[i].err /= err_count_list[i]

        self.replay.merge(Config.alpha)

        return np.mean([t.err for t in batch_tuples])