コード例 #1
0
 def compute_displace(self, amp, r, buffer):
     r = self.map_coordinates(r)
     ex = cp.exp(self._iqx * r.x() + self._iqxz * r.z(),
                 dtype=cp.complex64)
     ey = cp.exp(self._iqy * r.y() + self._iqyz * r.z(),
                 dtype=cp.complex64)
     cp.outer(amp * ey, ex, buffer)
コード例 #2
0
def wint(n, t):

    N = len(t)
    s = cp.linspace(1e-40, 1, n)
    # Inverse vandermonde matrix
    tmp1 = cp.arange(n)
    tmp2 = cp.arange(1, n + 2)
    iv = cp.linalg.inv(cp.exp(cp.outer(tmp1, cp.log(s))))
    u = cp.diff(
        cp.exp(cp.outer(tmp2, cp.log(s))) *
        cp.tile(1.0 / tmp2[..., cp.newaxis],
                [1, n]))  # integration over short intervals
    W1 = cp.matmul(iv, u[1:n + 1, :])  # x*pn(x) term
    W2 = cp.matmul(iv, u[0:n, :])  # const*pn(x) term

    # Compensate for overlapping short intervals
    tmp1 = cp.arange(1, n)
    tmp2 = (n - 1) * cp.ones((N - 2 * (n - 1) - 1))
    tmp3 = cp.arange(n - 1, 0, -1)
    p = 1 / cp.concatenate((tmp1, tmp2, tmp3))
    w = cp.zeros(N)
    for j in range(N - n + 1):
        # Change coordinates, and constant and linear parts
        W = ((t[j + n - 1] - t[j])**2) * W1 + (t[j + n - 1] - t[j]) * t[j] * W2

        for k in range(n - 1):
            w[j:j + n] = w[j:j + n] + p[j + k] * W[:, k]

    wn = w
    wn[-40:] = (w[-40]) / (N - 40) * cp.arange(N - 40, N)
    return wn
コード例 #3
0
ファイル: dA_gpu.py プロジェクト: msabr027/AD-KitNET
    def train(self, x):
        self.n = self.n + 1
        # update norms
        self.norm_max[x > self.norm_max] = x[x > self.norm_max]
        self.norm_min[x < self.norm_min] = x[x < self.norm_min]

        # 0-1 normalize
        x = (x - self.norm_min) / (self.norm_max - self.norm_min +
                                   0.0000000000000001)

        if self.params.corruption_level > 0.0:
            tilde_x = self.get_corrupted_input(x, self.params.corruption_level)
        else:
            tilde_x = x
        y = self.get_hidden_values(tilde_x)
        z = self.get_reconstructed_input(y)

        L_h2 = x - z
        L_h1 = cupy.dot(L_h2, self.W) * y * (1 - y)

        L_vbias = L_h2
        L_hbias = L_h1
        L_W = cupy.outer(tilde_x.T, L_h1) + cupy.outer(L_h2.T, y)

        self.W += self.params.lr * L_W
        self.hbias += self.params.lr * cupy.mean(L_hbias, axis=0)
        self.vbias += self.params.lr * cupy.mean(L_vbias, axis=0)
        return cupy.sqrt(cupy.mean(
            L_h2**2))  #the RMSE reconstruction error during training
コード例 #4
0
    def build_cbow_model(self):
        #Iterate over epochs
        for k in range(self.epochs):
            print("We are at epoch : ", k + 1)
            #For each training example
            for i in range(len(self.X_train)):

                #Forward propagation of the CBOW network-----

                #Take average
                x = np.zeros((self.vocab_size, 1))
                for word in self.X_train[i]:
                    x += self.one_hot(self.words_to_int[word])
                x /= len(self.X_train[i])

                h = np.dot(self.w_hidden.T, x)
                '''
				h = np.zeros((self.dim, 1))
				for word in self.X_train[i]:
					h += np.dot(self.w_hidden.T, self.one_hot(self.words_to_int[word]))
				h/=len(self.X_train[i])
				print ("Forward propagation done...",  i, k)

				'''
                print("-----------")
                print("Forward propagation done CBOW...: ", i, "Epoch: ",
                      k + 1)
                #h = np.dot(self.w_hidden.T , onehot(X_train[i])
                u = np.dot(self.w_output.T, h)
                pred = self.softmax(u)

                #Backward propagation------
                #err_sum = np.zeros((self.vocab_size,1))

                err = pred - self.one_hot(self.words_to_int[self.Y_train[i]])
                print("Calculated error..", i, k + 1)

                #Calculate dL/dW

                dw_hidden = np.outer(x, np.dot(self.w_output, err))

                #Calculate dL/dW'
                dw_output = np.outer(h, err)

                #Gradient descent
                self.w_hidden += -self.lr * dw_hidden
                self.w_output += -self.lr * dw_output
                print("Gradient descent done..", i, k + 1)

            #Update model after each epoch
            print("Saving model...")
            for key, value in self.words_to_int.items():
                self.model[key] = self.w_hidden[value].reshape(
                    1, self.w_hidden.shape[1])

            #Store model after every epoch

            print("Model to npy file...")
            np.save('./utils/cbow_new_' + str(k), self.model)
コード例 #5
0
    def build_skipgram_model(self):
        #Iterate over epochs
        print("No. of training samples are: ", len(self.X_train))
        for k in range(self.epochs):
            print("We are at epoch : ", k)
            print()
            print("No. of training samples: ", len(self.X_train))
            #For each training example
            for i in range(len(self.X_train)):

                #Forward propagation of the SkipGram network-----
                #Here X_train[i] is a Vx1 vector.
                #print "self.X_train[i] is ", self.X_train[i]
                #print "self.words_to_int[i] is ", self.words_to_int[self.X_train[i]]

                h = np.dot(self.w_hidden.T,
                           self.one_hot(self.words_to_int[self.X_train[i]]))
                output = np.dot(self.w_output.T, h)
                pred = self.softmax(output)
                print("---------------")
                print("Forward propagation done SKIPGRAM...",
                      str(i) + "/" + str(len(self.X_train)), " Epoch: ",
                      str(k + 1) + "/" + str(self.epochs))

                #Backward propagation------
                err_sum = np.zeros((self.vocab_size, 1))

                for word in self.Y_train[i]:
                    err_sum += (pred - self.one_hot(self.words_to_int[word]))

                #err_sum/= self.vocab_size
                print("Calculated error..", i, k + 1)

                #Calculate dL/dW
                dw_hidden = np.outer(
                    self.one_hot(self.words_to_int[self.X_train[i]]),
                    np.dot(self.w_output, err_sum))

                #Calculate dL/dW'
                dw_output = np.outer(h, err_sum)

                #Gradient descent
                self.w_hidden += -self.lr * dw_hidden
                self.w_output += -self.lr * dw_output

                print("Gradient descent done..", i, k + 1)

            #Update model after each epoch
            print("Saving model...")
            for key, value in self.int_to_words.items():
                self.model[value] = self.w_hidden[key].reshape(
                    1, self.w_hidden.shape[1])

            #Store model after every epoch
            #if (k!k%2==0):
            print("Model to npy file...")
            np.save('./utils/skipgram_' + str(k), self.model)
コード例 #6
0
ファイル: initsfwd.py プロジェクト: nikitinvv/lprec
def create_fwd(P):
    # convolution function
    fZ = cp.fft.fftshift(fzeta_loop_weights(
        P.Ntheta, P.Nrho, 2*P.beta, P.g-cp.log(P.am), 0, 4))
    # (lp2C1,lp2C2), transformed log-polar to Cartesian coordinates
    tmp1 = cp.outer(cp.exp(cp.array(P.rhosp)), cp.cos(cp.array(P.thsp))).flatten()
    tmp2 = cp.outer(cp.exp(cp.array(P.rhosp)), cp.sin(cp.array(P.thsp))).flatten()
    lp2C1 = [None]*P.Nspan
    lp2C2 = [None]*P.Nspan
    for k in range(P.Nspan):
        lp2C1[k] = ((tmp1-(1-P.aR))*cp.cos(k*P.beta+P.beta/2) -
                    tmp2*cp.sin(k*P.beta+P.beta/2))/P.aR
        lp2C2[k] = ((tmp1-(1-P.aR))*cp.sin(k*P.beta+P.beta/2) +
                    tmp2*cp.cos(k*P.beta+P.beta/2))/P.aR
        lp2C2[k] *= (-1)  # adjust for Tomopy
        cids = cp.where((lp2C1[k]**2+lp2C2[k]**2) <= 1)[0]
        lp2C1[k] = lp2C1[k][cids]
        lp2C2[k] = lp2C2[k][cids]
    # pids, index in polar grids after splitting by spans
    pids = [None]*P.Nspan
    [s0, th0] = cp.meshgrid(P.s, P.proj)
    th0 = th0.flatten()
    s0 = s0.flatten()
    for k in range(0, P.Nspan):
        pids[k] = cp.where((th0 >= k*P.beta-P.beta/2) &
                           (th0 < k*P.beta+P.beta/2))[0]

    # (p2lp1,p2lp2), transformed polar to log-polar coordinates
    p2lp1 = [None]*P.Nspan
    p2lp2 = [None]*P.Nspan
    for k in range(P.Nspan):
        th00 = th0[pids[k]]-k*P.beta
        s00 = s0[pids[k]]
        p2lp1[k] = th00
        p2lp2[k] = np.log(s00*P.aR+(1-P.aR)*np.cos(th00))

    # adapt for gpu interp
    for k in range(0, P.Nspan):
        lp2C1[k] = (lp2C1[k]+1)/2*(P.N-1)
        lp2C2[k] = (lp2C2[k]+1)/2*(P.N-1)
        p2lp1[k] = (p2lp1[k]-P.thsp[0])/(P.thsp[-1]-P.thsp[0])*(P.Ntheta-1)
        p2lp2[k] = (p2lp2[k]-P.rhosp[0])/(P.rhosp[-1]-P.rhosp[0])*(P.Nrho-1)
    const = cp.sqrt(P.N*P.osangles/P.Nproj)*cp.pi/4 / \
        P.aR/cp.sqrt(2)  # adjust constant
    fZgpu = fZ[:, :P.Ntheta//2+1]*const
    if(P.interp_type == 'cubic'):
        fZgpu = fZgpu/(P.B3com[:, :P.Ntheta//2+1])

    Pfwd0 = Pfwd(fZgpu, lp2C1, lp2C2, p2lp1, p2lp2, cids, pids)
    # array representation
    parsi, parsf = savePfwdpars(Pfwd0)
    return Pfwd0, parsi, parsf
コード例 #7
0
def dftUpsample(imageCorr, upsampleFactor, xyShift):
    """
    This performs a matrix multiply DFT around a small neighboring region of the inital
    correlation peak. By using the matrix multiply DFT to do the Fourier upsampling, the
    efficiency is greatly improved. This is adapted from the subfuction dftups found in
    the dftregistration function on the Matlab File Exchange.

    https://www.mathworks.com/matlabcentral/fileexchange/18401-efficient-subpixel-image-registration-by-cross-correlation

    The matrix multiplication DFT is from:

    Manuel Guizar-Sicairos, Samuel T. Thurman, and James R. Fienup, "Efficient subpixel
    image registration algorithms," Opt. Lett. 33, 156-158 (2008).
    http://www.sciencedirect.com/science/article/pii/S0045790612000778

    Args:
        imageCorr (complex valued ndarray):
            Correlation image between two images in Fourier space.
        upsampleFactor (int):
            Scalar integer of how much to upsample.
        xyShift (list of 2 floats):
            Coordinates in the UPSAMPLED GRID around which to upsample.
            These must be single-pixel IN THE UPSAMPLED GRID

    Returns:
        (ndarray):
            Upsampled image from region around correlation peak.
    """
    imageSize = imageCorr.shape
    pixelRadius = 1.5
    numRow = np.ceil(pixelRadius * upsampleFactor)
    numCol = numRow

    colKern = cp.exp(
        (-1j * 2 * cp.pi / (imageSize[1] * upsampleFactor))
        * cp.outer(
            (cp.fft.ifftshift((cp.arange(imageSize[1]))) - cp.floor(imageSize[1] / 2)),
            (cp.arange(numCol) - xyShift[1]),
        )
    )

    rowKern = cp.exp(
        (-1j * 2 * cp.pi / (imageSize[0] * upsampleFactor))
        * cp.outer(
            (cp.arange(numRow) - xyShift[0]),
            (cp.fft.ifftshift(cp.arange(imageSize[0])) - cp.floor(imageSize[0] / 2)),
        )
    )

    imageUpsample = cp.real(rowKern @ imageCorr @ colKern)
    return imageUpsample
コード例 #8
0
ファイル: pg-pong.py プロジェクト: claserken/pongpolicy
def policy_backward(eph, epdlogp):
    """ backward pass. (eph is array of intermediate hidden states) """
    dW2 = np.dot(eph.T, epdlogp).ravel()
    dh = np.outer(epdlogp, model['W2'])
    dh[eph <= 0] = 0  # backpro prelu
    dW1 = np.dot(dh.T, epx)
    return {'W1': dW1, 'W2': dW2}
コード例 #9
0
ファイル: corClust_gpu.py プロジェクト: msabr027/AD-KitNET
 def update(self,x):
     self.N += 1
     self.c += x
     c_rt = x - self.c/self.N
     self.c_r += c_rt
     self.c_rs += c_rt**2
     self.C += cupy.outer(c_rt,c_rt)
コード例 #10
0
ファイル: corClust_gpu.py プロジェクト: msabr027/AD-KitNET
 def corrDist(self):
     c_rs_sqrt = cupy.sqrt(self.c_rs)
     C_rs_sqrt = cupy.outer(c_rs_sqrt,c_rs_sqrt)
     C_rs_sqrt[C_rs_sqrt==0] = 1e-100 #this protects against dive by zero erros (occurs when a feature is a constant)
     D = 1-self.C/C_rs_sqrt #the correlation distance matrix
     D[D<0] = 0 #small negatives may appear due to the incremental fashion in which we update the mean. Therefore, we 'fix' them
     return D
コード例 #11
0
def ret_energy(Edge, Person):
    if GPU:
        return cp.sum(Edge * tri_array * (1.0 - cp.outer(Person, Person)) /
                      2.0)
    else:
        return np.sum(Edge * tri_array * (1.0 - np.outer(Person, Person)) /
                      2.0)
 def _derivativenorm(self):
     """Compute the derivative of the norm
     Returns
     -------
     derivative : numpy array, shape (m_parameters,)
     """
     w2 = cp.reshape(self.w,(self.n_features,self.d,self.D,self.D))
     derivative = cp.zeros((self.n_features,self.d,self.D,self.D)) 
     
     tmp=cp.zeros((self.n_features,self.D))
     tmp2=cp.zeros((self.n_features,self.D))
     tmp[0,:]=cp.sum(cp.square(w2[0,:,0,:]),0)
     for i in range(1,self.n_features-1):
         tmp[i,:]=cp.dot(tmp[i-1,:],cp.sum(cp.square(w2[i,:,:,:]),0)) 
     tmp[self.n_features-1,:]=cp.inner(tmp[self.n_features-2,:],
             cp.sum(cp.square(w2[self.n_features-1,:,:,0]),0))
     tmp2[self.n_features-1,:]=cp.sum(cp.square(w2[self.n_features-1,:,:,0]),0)
     for i in range(self.n_features-2,-1,-1):
         tmp2[i,:]=cp.dot(cp.sum(cp.square(w2[i,:,:,:]),0),tmp2[i+1,:])
     tmp2[0,:]=cp.inner(cp.sum(cp.square(w2[0,:,0,:]),0),tmp2[1,:])
 
     for j in range(self.d):
         derivative[0,j,0,:]=cp.multiply(tmp2[1,:],2*(w2[0,j,0,:]))
         derivative[self.n_features-1,j,:,0]=\
             cp.multiply(tmp[self.n_features-2,:],2*(w2[self.n_features-1,j,:,0]))
     for i in range(1,self.n_features-1):
         temp3=cp.outer(tmp[i-1,:],tmp2[i+1,:])
         for j in range(self.d):
             derivative[i,j,:,:]=cp.multiply(temp3,2*(w2[i,j,:,:]))
     return derivative.reshape(self.m_parameters)
コード例 #13
0
 def backward(self):
     W, t, output = self.get_ctx('W', 't', 'output')
     if output.grad is not None:
         grad_W = xp.outer(output.grad, t.data)
         grad_t = xp.dot(output.grad, W.data)
         W.accumulate_grad(grad_W)
         t.accumulate_grad(grad_t)
コード例 #14
0
    def _findCarrier_cupy(self, band0, band1, mask):
        band0 = cp.asarray(band0)
        band1 = cp.asarray(band1)
        mask = cp.asarray(mask)

        band = band0 * band1
        ixf = abs(cp.fft.fftshift(cp.fft.fft2(cp.fft.fftshift(band))))
        pyc0, pxc0 = self._findPeak_cupy(
            (ixf - gaussian_filter_cupy(ixf, 20)) * mask)

        ixfz, Kx, Ky = self._zoomf_cupy(band, self.N,
                                        cp.asarray(self._kx)[pyc0, pxc0],
                                        cp.asarray(self._ky)[pyc0, pxc0], 100,
                                        self._dk * self.N)
        pyc, pxc = self._findPeak_cupy(abs(ixfz))

        kx = Kx[pxc]
        ky = Ky[pyc]

        otf_exclude_min_radius = 0.5
        otf_exclude_max_radius = 1.5

        kr = cp.sqrt(cp.asarray(self._kx)**2 + cp.asarray(self._ky)**2)

        m = (kr < 2)
        otf = cp.fft.fftshift(self._tfm_cupy(kr, m) + (1 - m))

        otf_mask = (kr > otf_exclude_min_radius) & (kr <
                                                    otf_exclude_max_radius)
        otf_mask_for_band_common_freq = cp.fft.fftshift(
            otf_mask
            & cupyx.scipy.ndimage.shift(otf_mask, (pyc0 -
                                                   (self.N // 2 + 1), pxc0 -
                                                   (self.N // 2 + 1)),
                                        order=0))
        band0_common = cp.fft.ifft2(
            cp.fft.fft2(band0) / otf * otf_mask_for_band_common_freq)

        xx = cp.arange(-self.N / 2 * self._dx,
                       self.N / 2 * self._dx,
                       self._dx,
                       dtype=np.single)
        phase_shift_to_xpeak = cp.exp(-1j * kx * xx * 2 * pi * self.NA /
                                      self.wavelength)
        phase_shift_to_ypeak = cp.exp(-1j * ky * xx * 2 * pi * self.NA /
                                      self.wavelength)

        band1_common = cp.fft.ifft2(
            cp.fft.fft2(band1) /
            otf * otf_mask_for_band_common_freq) * cp.outer(
                phase_shift_to_ypeak, phase_shift_to_xpeak)

        scaling = 1 / cp.sum(band0_common * cp.conj(band0_common))

        cross_corr_result = cp.sum(band0_common * band1_common) * scaling

        ampl = cp.abs(cross_corr_result) * 2
        phase = cp.angle(cross_corr_result)
        return kx.get(), ky.get(), phase.get(), ampl.get()
コード例 #15
0
def gkern(kernlen=21, nsig=3):
    """Returns a 2D Gaussian kernel array."""
    interval = (2*nsig+1.)/(kernlen)
    x = np.linspace(-nsig-interval/2., nsig+interval/2., kernlen+1)
    kern1d = cp.array(np.diff(st.norm.cdf(x)))
    kernel_raw = cp.sqrt(cp.outer(kern1d, kern1d))
    kernel = kernel_raw/kernel_raw.sum()
    return kernel
コード例 #16
0
 def applyHann(self):
     width = self._image.shape[0]
     height = self._image.shape[1]
     row = cupy.hanning(width)
     col = cupy.hanning(height)
     window = cupy.outer(row, col)
     window = cupy.sqrt(window)
     image = cupy.multiply(window, self._image)
     self.setImage(image)
コード例 #17
0
ファイル: cupy_impl.py プロジェクト: we-taper/numq
def make_density_matrix_cupy(wf: cupy.ndarray):
    if wf.ndim == 1:
        return outer(wf, cupy.conj(wf))
    if wf.ndim == 2:
        wf_dim, num_wf = wf.shape
        try:
            ret = empty(shape=(num_wf, wf_dim, wf_dim), dtype=wf.dtype)
        except OutOfMemoryError:
            logger.critical(
                "OOM when creating density matrix for wavefunction "
                f"of shape {wf.shape}"
            )
            raise
        for wf_idx in range(num_wf):
            a_wf = wf[:, wf_idx]
            ret[wf_idx, :, :] = outer(a_wf, conj(a_wf))
        return ret
    raise NotImplementedError(wf.shape)
コード例 #18
0
ファイル: test_cublas.py プロジェクト: toslunar/cupy
 def test_ger(self):
     if self.dtype.char in 'FD':
         with pytest.raises(TypeError):
             cublas.ger(self.alpha, self.x, self.y, self.a)
         return
     ref = self.alpha * cupy.outer(self.x, self.y) + self.a
     if self.mode is not None:
         self.alpha = self.mode.array(self.alpha)
     cublas.ger(self.alpha, self.x, self.y, self.a)
     cupy.testing.assert_allclose(self.a, ref, rtol=self.tol, atol=self.tol)
コード例 #19
0
ファイル: lmafit_cuda.py プロジェクト: hlatkyd/vnmrjpy_old
def make_test_data():
    #a = cp.array([cp.sin(i/3) for i in range(100)])
    #b = cp.array([cp.sin(i/3) for i in range(100)])
    a = cp.array([i for i in range(100)])
    b = cp.array([i / 3 for i in range(100)])
    A = cp.outer(a, b)
    mask = cp.random.rand(A.shape[0], A.shape[1])
    mask[mask >= 0.5] = 1
    mask[mask < 0.5] = 0
    A_masked = cp.multiply(A, mask)

    return A, A_masked, mask
コード例 #20
0
ファイル: cupy_impl.py プロジェクト: we-taper/numq
def partial_trace_wf_cupy(iwf: cupy.ndarray, retain_qubits):
    nqb = int(math_log2(iwf.shape[0]))
    if len(retain_qubits) == nqb:
        return outer(iwf, iwf.conj())
    iwf = iwf.reshape([2] * nqb, order="C")
    retain_qubits = sorted(retain_qubits)
    for idx in range(len(retain_qubits)):
        r = retain_qubits[idx]
        if idx != r:
            iwf = iwf.swapaxes(idx, r)
    iwf = iwf.reshape((2 ** nqb,))
    return partial_trace_wf_keep_first_cupy(iwf, len(retain_qubits))
コード例 #21
0
ファイル: pong.py プロジェクト: andrecianflone/pong_pixels
def policy_backward(eph, epdlogp):

  """
  backward pass. (eph is array of intermediate hidden states)
  Args:
    eph: episode hidden states, stacked
    epdlogp: episode action gradient with advantage
  """
  dW2 = xp.dot(eph.T, epdlogp).ravel() # derivs wrt W2
  dh = xp.outer(epdlogp, model['W2']) # derivs wrt h post activation
  dh[eph <= 0] = 0 # backprop relu
  dW1 = xp.dot(dh.T, epx) # derivs wrt W1
  return {'W1':dW1, 'W2':dW2}
コード例 #22
0
ファイル: SemImage.py プロジェクト: kingsmedow/Auto-focus
    def applyHann(self):
        width = self._image.shape[1]
        height = self._image.shape[0]
        #print("Width,Height {:3d} {:3d} .".format(width,height))
        row = cupy.hanning(width)
        col = cupy.hanning(height)

        window = cupy.outer(col, row)
        window = cupy.sqrt(window)
        image = cupy.multiply(window, self._image)
        #print("array ", image)
        self._image = image.astype('uint8')
        self.setImage(self._image)
コード例 #23
0
def sigmasLancosTwo(n):
    """
    sigma Lancos coefficients for calculating inverse Fourier Transforms
    """
    temp = cp.zeros(2*n-1)
    for i in  cp.arange(2*n-1):
        k=i-(n-1)
        if k==0:
            temp[i] = 1
            continue
        else:
            temp[i] = cp.sin(PI*(k/n))/(PI*(k/n))

    return cp.outer(temp,temp)
コード例 #24
0
ファイル: datagen.py プロジェクト: emblixt/blixt_utils
    def deformation(self, prm):
        """
            Apply 2D Gaussian and Planar deformation.
            Computation is parallelized on GPU using cupy.
        """
        import cupy as cp
        xy_cp = cp.asarray(prm.xy)
        a_cp = cp.asarray(self.a)
        b_cp = cp.asarray(self.b)
        c_cp = cp.asarray(self.c)
        d_cp = cp.asarray(self.d)
        sigma_cp = cp.asarray(self.sigma)
        e_cp = cp.asarray(self.e)
        f_cp = cp.asarray(self.f)
        g_cp = cp.asarray(self.g)
        z_cp = cp.asarray(prm.z)

        func_planar = cp.ElementwiseKernel(
            in_params='T x, T y, T e, T f, T g',
            out_params='T z',
            operation= \
                '''
                z = e + f*x + g*y;
                ''',
            name='func_planar'
        )

        func_gauss2d = cp.ElementwiseKernel(
            in_params='T x, T y, T b, T c, T d, T sigma',
            out_params='T z',
            operation= \
                '''
                z = b*expf(-(powf(x-c,2) + powf(y-d,2))/(2*powf(sigma,2)));
                ''',
            name='func_gauss2d'
        )

        gauss_2d_cp = cp.zeros_like(xy_cp[:, 0])
        for i in range(len(self.b)):
            gauss_2d_cp += func_gauss2d(xy_cp[:, 0], xy_cp[:, 1], b_cp[i], c_cp[i], d_cp[i], sigma_cp[i])
        s1_cp = a_cp + (1.5 / z_cp) * cp.outer(cp.transpose(gauss_2d_cp), z_cp)
        s2_cp = func_planar(xy_cp[:, 0], xy_cp[:, 1], e_cp, f_cp, g_cp)

        refl_cp = cp.asarray(self.refl)
        for i in range(prm.nxy_tr):
            s = s1_cp[i, :] + s2_cp[i] + z_cp
            mat = cp.tile(z_cp, (len(s), 1)) - cp.tile(cp.expand_dims(s, 1), (1, len(z_cp)))
            refl_cp[i, :] = cp.dot(refl_cp[i, :], cp.sinc(mat))

        return np.reshape(cp.asnumpy(refl_cp), [prm.nxy_tr, prm.nz_tr])
コード例 #25
0
ファイル: test_order.py プロジェクト: stjordanis/gQuant
 def setUp(self):
     self.assets = 10
     self.samples = 5
     self.numbers = 30
     seq = 100
     self.distance = cupy.zeros(
         (self.samples, self.numbers, self.assets * (self.assets-1) // 2))
     cupy.random.seed(10)
     for i in range(self.samples):
         for j in range(self.numbers):
             cov = cupy.cov(cupy.random.rand(self.assets, seq))
             dia = cupy.diag(cov)
             corr = cov / cupy.sqrt(cupy.outer(dia, dia))
             dist = (1.0 - corr) / 2.0
             self.distance[i, j] = cupy.array(squareform(dist.get()))
コード例 #26
0
def outer(x1: Array, x2: Array, /) -> Array:
    """
    Array API compatible wrapper for :py:func:`np.outer <numpy.outer>`.

    See its docstring for more information.
    """
    # Note: the restriction to numeric dtypes only is different from
    # np.outer.
    if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes:
        raise TypeError('Only numeric dtypes are allowed in outer')

    # Note: the restriction to only 1-dim arrays is different from np.outer
    if x1.ndim != 1 or x2.ndim != 1:
        raise ValueError('The input arrays to outer must be 1-dimensional')

    return Array._new(np.outer(x1._array, x2._array))
コード例 #27
0
def hann(width, height, returnCupy=False):
    if cupy:
        row = cupy.hanning(width)
        col = cupy.hanning(height)
        window = cupy.outer(row, col)
        window = cupy.sqrt(window)
        if returnCupy:
            return window
        else:
            return cupy.asnumpy(window)
    else:
        row = numpy.hanning(width)
        col = numpy.hanning(height)
        window = numpy.outer(row, col)
        window = numpy.sqrt(window)
        return window
コード例 #28
0
ファイル: trainsingle.py プロジェクト: shy1/language-model
def train(inweights, v, variables, leak, bs, steps, testflag, s, count):
    T = len(s)
    N = 1024
    M = 1024
    x1 = cp.zeros(N * layerscales["L1"], dtype=np.float32)
    gradient = dict()
    softerr1 = 0
    err1 = 0
    skipfirst = 0
    t = step
    tm1 = (T - 1 - t - skipfirst)

    for k in range(skipfirst):
        step1 = s[t - step]
        x1 = (1.0 - leak) * x1 + leak * (inweights["U1"][:, step1] + cp.roll(x1, 1))
        x1 = x1 / cp.linalg.norm(x1)
        t += 1

    for b1 in range(tm1):
        step1 = s[t - step]

        x1 = (1.0 - leak) * x1 + leak * (inweights["U1"][:, step1] + cp.roll(x1, 1))
        x1 = x1 / cp.linalg.norm(x1)
        wx = cp.dot(variables["W1"], x1)
        wx = wx - cp.max(wx)
        p = cp.exp(wx)
        p1 = p / cp.sum(p)
        pred1 = cp.argmax(p1)

        target = s[t+1]
        target_prob1 = p1[target]
        softerr1 += 1 - target_prob1
        err1 = err1 + (pred1 != target)

        if testflag == 0:
            gradient["W1"] = cp.outer(v[:, target] - p1, x1)
            SGD.update_state(gradient)
            delta = SGD.get_delta()
            SGD.update_with_L1_regularization(variables, delta, L1)
        t += 1

    softerrors = dict()
    prederrors = dict()
    softerrors["lay1"] = softerr1 / (tm1)
    prederrors["lay1"] = err1 * 100.0 / (tm1)

    return prederrors, softerrors, variables
コード例 #29
0
def extract(noisyimg_gpu, imgweights_gpu, A_gpu):
    #- Set up the equation to solve (B&S eq 4)
    W_gpu = cpx.scipy.sparse.spdiags(data=imgweights_gpu.ravel(),
                                     diags=[
                                         0,
                                     ],
                                     m=npix,
                                     n=npix)

    iCov_gpu = A_gpu.T.dot(W_gpu.dot(A_gpu))

    y_gpu = A_gpu.T.dot(W_gpu.dot(noisyimg_gpu.ravel()))

    ##- Solve f (B&S eq 4)
    f_gpu = cp.linalg.solve(iCov_gpu.todense(),
                            y_gpu)  #requires array, not sparse object

    #- Eigen-decompose iCov to assist in upcoming steps
    u_gpu, v_gpu = cp.linalg.eigh(iCov_gpu.todense())

    #- Calculate C^-1 = QQ (B&S eq 10)
    d_gpu = cpx.scipy.sparse.spdiags(cp.sqrt(u_gpu), 0, len(u_gpu), len(u_gpu))

    Q_gpu = v_gpu.dot(d_gpu.dot(v_gpu.T))

    #- normalization vector (B&S eq 11)
    norm_vector_gpu = cp.sum(Q_gpu, axis=1)

    #- Resolution matrix (B&S eq 12)
    R_gpu = cp.outer(norm_vector_gpu**(-1), cp.ones(
        norm_vector_gpu.size)) * Q_gpu

    #- Decorrelated covariance matrix (B&S eq 13-15)
    udiags_gpu = cpx.scipy.sparse.spdiags(1 / u_gpu, 0, len(u_gpu), len(u_gpu))

    Cov_gpu = v_gpu.dot(udiags_gpu.dot(v_gpu.T))

    Cx_gpu = R_gpu.dot(Cov_gpu.dot(R_gpu.T))

    #- Decorrelated flux (B&S eq 16)
    fx_gpu = R_gpu.dot(f_gpu.ravel()).reshape(f_gpu.shape)

    #- Variance on f (B&S eq 13)
    varfx_gpu = cp.diagonal(Cx_gpu)

    return fx_gpu, varfx_gpu, R_gpu
コード例 #30
0
    def update(self, z):

        # Transform sigmas into measurement space
        for i in range(self.num_sp):
            self.sp_h[i] = self.H(self.sp_f[i])

        # Mean and covariance of the state in the measurement space
        Hx_bar, PHx = unscented_transform(self.sp_h, self.weights, self.R)
        # Cross variance of Fx and Hx -- used to calculate K
        cross_var = np.zeros((self.Nx, self.Nz))

        for i in range(self.num_sp):
            cross_var += self.weights[i] * np.outer(self.sp_f[i] - self.x_hat,
                                                    self.sp_h[i] - Hx_bar)
        # Calculate K and measurement residual
        K = np.dot(cross_var, inv(PHx))
        residual = self.difference(np.array(z), Hx_bar)
        # Update predicted to new values for state and variance
        self.x_hat += np.dot(K, residual)
        self.P -= np.dot(K, np.dot(PHx, K.T))