def __call__(self, f_atoms, f_bonds, super_node_x,
           atom_label, mask_reagents, mask_reactants_reagents, pair_label, mask_pair_select,
           action, step_num,
           stop_idx,
           sample_index):

        atom_label -= 1
        mask_reagents -= 2
        mask_reactants_reagents -= 2
        action -= 1
        batch_size = action.shape[0]

        # atom
        loss_atom, acc_atom, atoms_selected = self.g_atom(f_atoms, cp.copy(f_bonds), super_node_x, atom_label, mask_reagents, mask_reactants_reagents, batch_size)

        # pair
        loss_pair = self.g_pair(f_atoms, cp.copy(f_bonds), super_node_x, action, pair_label, mask_pair_select, batch_size, atoms_selected)

        # action
        loss_action, acc_action = self.g_action(f_atoms, cp.copy(f_bonds), super_node_x, action, batch_size)

        # stop
        loss_stop, acc_stop = self.g_stop(f_atoms, cp.copy(f_bonds), super_node_x, stop_idx, action, batch_size)


        reporter.report({
            'loss_stop': loss_stop,
            'loss_atom': loss_atom,
            'loss_pair': loss_pair,
            'loss_action': loss_action,
            'acc_stop': acc_stop,
            'acc_atom': acc_atom,
            # 'acc_pair': acc_pair,   # acc_pair need to be further extended
            'acc_action': acc_action,
        }, self)
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
Exemple #3
0
    def exec_crossA0001(self) -> None:
        """exec_crossA0001.

        :rtype: None
        """
        x_population = cp.copy(self.get_population())
        x_aux = cp.copy(self.get_population())
        index_selection = int(self.get_n_samples() *
                              self.get_percent_selection())
        index_cross = int(self.get_n_samples() * self.get_percent_cross())
        cp.random.shuffle(x_population)
        if index_cross % 2 == 0:
            y_population = self._crossA0001(
                x_population[index_selection:, :][0:index_cross, :],
                self.get_n_jobs(),
                self.get_n_operations(),
                index_cross,
                self.get_percent_intra_cross(),
            )
            x_aux[index_selection:, :][-index_cross:, :] = y_population
            self.set_population(x_aux)
        else:
            index_cross -= 1
            y_population = self._crossA0001(
                x_population[index_selection:, :][0:index_cross, :],
                self.get_n_jobs(),
                self.get_n_operations(),
                self.get_n_samples(),
                self.get_percent_intra_cross(),
            )
            x_aux[index_selection:, :][-index_cross:, :] = y_population
            self.set_population(x_aux)
Exemple #4
0
def resetStocks():
    """
    reset global variables
    """
    global stockPool, hurstPool
    stockPool = np.copy(config._stockPool)
    hurstPool = np.copy(config._hurstPool)
Exemple #5
0
def train(X, W1, W2, W3, pad=0):

    # Conv 1
    H1, cache1 = conv.conv_forward(X, W1, pad)

    # ReLu 1
    H1_relu = np.copy(H1)
    H1_relu[H1 < 0] = 0
    
    # cifar10.plotH(H1_relu[:,:,:4])

    # Pool
    for m in range(15):
        for n in range(15):
            x_slice = H1_relu[2*m:2*m+2, 2*n:2*n+2]
            P1[m, n] = np.max(x_slice, axis=(0, 1))

    # Conv 2
    H2, cache2 = conv.conv_forward(P1, W2, pad)

    # ReLu 2
    H2_relu = np.copy(H2)
    H2_relu[H2 < 0] = 0

    # cifar10.plotH(H2_relu[:,:,:4])

    # FC 1
    x = H2_relu.flatten()
    scores = x.dot(W3)

    # Softmax
    ex = np.exp(scores)
    probs[sample] = ex/np.sum(ex, keepdims=True)
    loss = -np.log(probs[sample, Ytr[sample]])
    dscores = np.copy(probs)
    dscores[sample, Ytr[sample]] -= 1

    # Backprop FC 1
    dW3 = np.dot(H2_relu.reshape(3042, 1), dscores[sample].reshape(1, 10))
    dH2 = np.dot(dscores[sample], W3.T).reshape(13, 13, depth2)

    # Backprop ReLu 2
    dH2[H2 <= 0] = 0

    # Backprop Conv 2
    dP1, dW2 = conv.conv_backward(dH2, cache2)

    # Backprop Pool
    dH1 = np.zeros(H1.shape)
    for m in range(15):
        for n in range(15):
            dH1[2*m:2*m+2, 2*n:2*n+2] = dP1[m, n]

    # Backprop ReLu 1
    dH1[H1 <= 0] = 0

    # Backprop Conv 1
    dX, dW1 = conv.conv_backward(dH1, cache1)

    return loss, dW1, dW2, dW3
Exemple #6
0
def test_3d_inactive():
    n = 30
    lx, ly, lz = n, n, n
    data, labels = make_3d_syntheticdata(lx, ly, lz)
    old_labels = cp.copy(labels)
    labels[5:25, 26:29, 26:29] = -1
    after_labels = cp.copy(labels)
    labels = random_walker(data, labels, mode='cg')
    assert (labels.reshape(data.shape)[13:17, 13:17, 13:17] == 2).all()
    assert data.shape == labels.shape
    return data, labels, old_labels, after_labels
Exemple #7
0
 def __estimate_one_step(self, gamma: cp.float64):
     """
     Estimation routine for one given gamma parameter of (iterated) Tikhonov method.
     :param gamma: Regularization parameter.
     :type gamma: float
     """
     order = 1
     while order <= self.order:
         self.__iteration(gamma)
         self.previous = cp.copy(self.current)
         order += 1
     self.__update_solution()
     self.previous = cp.copy(self.initial)
Exemple #8
0
    def __init__(self,
                 dataframe,
                 train_indices,
                 test_indices,
                 prediction_model,
                 cond_ind_test=None,
                 data_transform=None,
                 verbosity=0):

        # Default value for the mask
        mask = dataframe.mask
        if mask is None:
            mask = np.zeros(dataframe.values.shape, dtype='bool')
        # Get the dataframe shape
        T = len(dataframe.values)
        # Have the default dataframe be the training data frame
        train_mask = np.copy(mask)
        train_mask[[t for t in range(T) if t not in train_indices]] = True
        self.dataframe = DataFrame(dataframe.values,
                                   mask=train_mask,
                                   missing_flag=dataframe.missing_flag)
        # Initialize the models baseclass with the training dataframe
        Models.__init__(self,
                        dataframe=self.dataframe,
                        model=prediction_model,
                        data_transform=data_transform,
                        mask_type='y',
                        verbosity=verbosity)

        # Build the testing dataframe as well
        self.test_mask = np.copy(mask)
        self.test_mask[[t for t in range(T) if t not in test_indices]] = True

        # Setup the PCMCI instance
        if cond_ind_test is not None:
            # Force the masking
            cond_ind_test.set_mask_type('y')
            cond_ind_test.verbosity = verbosity
            PCMCI.__init__(self,
                           dataframe=self.dataframe,
                           cond_ind_test=cond_ind_test,
                           selected_variables=None,
                           verbosity=verbosity)

        # Set the member variables
        self.cond_ind_test = cond_ind_test
        # Initialize member varialbes that are set outside
        self.target_predictors = None
        self.selected_targets = None
        self.fitted_model = None
        self.test_array = None
Exemple #9
0
 def load_mc(self, t_ij):
     for j, (l, h) in enumerate(
             zip(self.observables.lows, self.observables.highs)):
         in_bounds = np.logical_and(t_ij[:, j] > l, t_ij[:, j] < h)
         t_ij = t_ij[in_bounds]
     self.t_ij = cp.asarray(t_ij)
     self.w_i = cp.ones(t_ij.shape[0])
     if self.bootstrap_binning is not None:
         counts, _ = cp.histogramdd(cp.asarray(self.t_ij),
                                    bins=self.bin_edges,
                                    weights=cp.asarray(self.w_i))
         self.counts = (cp.asarray(counts).flatten() / self.bin_vol /
                        cp.sum(cp.asarray(counts))).reshape(counts.shape)
     self.sigma_j = cp.std(self.t_ij, axis=0)
     self.h_ij = self._adapt_bandwidth()
     for j, (l, h, refl) in enumerate(
             zip(self.observables.lows, self.observables.highs,
                 self.reflect_axes)):
         if not refl:
             continue
         if type(refl) == tuple:
             low, high = refl
             mask = self.t_ij[:, j] < low
             t_ij_reflected_low = cp.copy(self.t_ij[mask, :])
             h_ij_reflected_low = self.h_ij[mask, :]
             w_i_reflected_low = self.w_i[mask, :]
             t_ij_reflected_low[:, j] = 2 * l - t_ij_reflected_low[:, j]
             mask = self.t_ij[:, j] > high
             t_ij_reflected_high = cp.copy(self.t_ij[mask, :])
             h_ij_reflected_high = self.h_ij[mask, :]
             w_i_reflected_high = self.w_i[mask, :]
             t_ij_reflected_high[:, j] = 2 * h - t_ij_reflected_high[:, j]
         else:
             t_ij_reflected_low = cp.copy(self.t_ij)
             h_ij_reflected_low = self.h_ij
             w_i_reflected_low = self.w_i
             t_ij_reflected_low[:, j] = 2 * l - self.t_ij[:, j]
             t_ij_reflected_high = cp.copy(self.t_ij)
             h_ij_reflected_high = self.h_ij
             w_i_reflected_high = self.w_i
             t_ij_reflected_high[:, j] = 2 * h - self.t_ij[:, j]
         self.t_ij = cp.concatenate(
             [self.t_ij, t_ij_reflected_low, t_ij_reflected_high])
         self.h_ij = cp.concatenate(
             [self.h_ij, h_ij_reflected_low, h_ij_reflected_high])
         self.w_i = cp.concatenate(
             [self.w_i, w_i_reflected_low, w_i_reflected_high])
     self.t_ij = cp.ascontiguousarray(self.t_ij)
     self.h_ij = cp.ascontiguousarray(self.h_ij)
     self.w_i = cp.ascontiguousarray(self.w_i)
Exemple #10
0
def _divide_nonzero(array1, array2, cval=1e-10):
    """
    Divides two arrays.

    Denominator is set to small value where zero to avoid ZeroDivisionError and
    return finite float array.

    Parameters
    ----------
    array1 : (N, ..., M) ndarray
        Array 1 in the enumerator.
    array2 : (N, ..., M) ndarray
        Array 2 in the denominator.
    cval : float, optional
        Value used to replace zero entries in the denominator.

    Returns
    -------
    array : (N, ..., M) ndarray
        Quotient of the array division.
    """

    # Copy denominator
    denominator = cp.copy(array2)

    # Set zero entries of denominator to small value
    denominator[denominator == 0] = cval

    # Return quotient
    return cp.divide(array1, denominator)
Exemple #11
0
    def __call__(self, x, inst):
        h = relu(self.flat1_bn(self.flat1(x)))
        h = relu(self.down1_bn(self.down1(h)))
        h = relu(self.down2_bn(self.down2(h)))
        h = relu(self.down3_bn(self.down3(h)))
        h = relu(self.down4_bn(self.down4(h)))
        h = relu(self.up1_bn(self.up1(h)))
        h = relu(self.up2_bn(self.up2(h)))
        h = relu(self.up3_bn(self.up3(h)))
        h = relu(self.up4_bn(self.up4(h)))
        # outputs = tanh(self.flat2(h))
        outputs = self.flat2(h)

        # instance-wise average pooling
        outputs_mean = cp.copy(outputs.data)
        inst = inst.data.astype(int)
        ids = np.unique(inst)
        for id in ids:
            indices = list((inst == id).nonzero())
            for j in range(self.out_ch):
                outputs_ins = outputs_mean[indices[0], j, indices[2], indices[3]]
                mean_feat = cp.mean(outputs_ins)
                mean_feat = cp.ones(outputs_ins.shape) * mean_feat
                outputs_mean[indices[0], j, indices[2], indices[3]] = mean_feat

        return Variable(outputs_mean)
Exemple #12
0
def initial_pop(good_data, require_goods, non_require_goods, non_require_values, limit_weight):
    #good_data: 原始商品資料集
    #require_goods: 必要性的商品清單
    #non_require_goods: 不必要性的商品清單
    #non_require_values: 不必要性的商品數量
    #limit_weight: 最大重量限制
    while True:
#        temp_good = cp.copy(good_data) #先將原始資料暫時給另外一個變數使用    
        temp_good = cp.copy(good_data) #先將原始資料暫時給另外一個變數使用    
        for i in range(len(require_goods)):
            get_index = temp_good[(temp_good[:,8] == require_goods[i]) & (temp_good[:, 11] !=1)] #取得符合條件的資料
            temp_index = random.randint(0, (len(get_index)-1)) #隨機取得品項
            get_index = get_index[temp_index] #取得商品名稱
            get_index[11] = 1
            temp_good[temp_good[:,0] == get_index[0]] = get_index
    
        selected_require = np.random.choice(non_require_goods, non_require_values, replace=False)
        
        for i in range(len(selected_require)):
            get_index = temp_good[(temp_good[:,8] == selected_require[i]) & (temp_good[:, 11] !=1)] #取得符合條件的資料
            temp_index = random.randint(0, (len(get_index)-1)) #隨機取得品項
            get_index = get_index[temp_index] #取得商品名稱
            get_index[11] = 1
            temp_good[temp_good[:,0] == get_index[0]] = get_index

        selected_good = temp_good[temp_good[:, 11] == 1]
        sum_weight = np.sum(selected_good[:,10])
        if sum_weight <= limit_weight:
            break
    return(selected_good) #回傳結果
Exemple #13
0
    def _get_psi_k(self, phi, k):
        """Returns the linear causal effect matrices excluding variable k.

        Parameters
        ----------
        phi : array-like
            Coefficient matrices at different lags.

        k : int
            Variable index to exclude causal effects through.

        Returns
        -------
        psi_k : array-like, shape (tau_max + 1, N, N)
            Matrices of causal effects excluding k.
        """

        psi_k = np.zeros((self.tau_max + 1, self.N, self.N))

        psi_k[0] = np.identity(self.N)
        phi_k = np.copy(phi)
        phi_k[1:, k, :] = 0.
        for n in range(1, self.tau_max + 1):
            psi_k[n] = np.zeros((self.N, self.N))
            for s in range(1, n + 1):
                psi_k[n] += np.dot(phi_k[s], psi_k[n - s])

        return psi_k
Exemple #14
0
def heat3d_cp(alpha, Tcool, Thot, dx, dy, dz, dt, nx, ny, nz, nt, result='time'):
    '''
    Solves the 3 dimensional heat equation using cupy
    
    alpha -- thermal diffusivity
    Tcool -- initial cold temperature
    Thot -- initial hot temperature
    dx, dy, dz -- spacing in each dimension
    dt -- time step
    nx, ny, nz -- number of gridpoints in each dimension
    nt -- integration time
    result -- either 'time', 'field' or 'both', returning either the total time the computation took, the resulting field or both 
    '''
        
    in_field = Tcool*cp.ones((nx,ny,nz))
    in_field[nx//4:3*nx//4, ny//4:3*ny//4, nz//4:3*nz//4] = Thot
    
    out_field = cp.copy(in_field)

    # warm up
    op_np(in_field, out_field, alpha, Tcool, dx, dy, dz, dt)

    # timing
    tic = get_time()
    op_np(in_field, out_field, alpha, Tcool, dx, dy, dz, dt, nt=nt)
    #toc = time.time()
    time_cp = get_time() - tic
    
    if result == 'time':
        return time_cp
    elif result == 'field':
        return in_field
    elif result == 'both':
        return time_cp, in_field
Exemple #15
0
def imgshift_cupy(img_cupy,dxy):
    
    imgout_cupy=cp.copy(img_cupy)
    imgout_cupy=cp.roll(imgout_cupy,int(dxy[0]),axis=0)
    imgout_cupy=cp.roll(imgout_cupy,int(dxy[1]),axis=1)
    
    return imgout_cupy
Exemple #16
0
def median_blur(image_unbordered, rows, columns=None):
    if columns == None:
        columns = rows
    if (rows == 1 and columns == 1):
        raise "Filter to little, increase rows or columns"
    if is_even(rows) or is_even(columns):
        raise "Rows and columns must be odd numbers"
    width_array, height_array = generate_arrays(rows, columns)
    #print(width_array, height_array)
    padding = max(width_array + height_array)
    members = []
    #image = cv2.copyMakeBorder(image_unbordered, padding,padding,padding,padding,cv2.BORDER_REFLECT)
    image = cp.copy(image_unbordered)
    if len(image.shape) == 3:
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    width, height = image.shape
    # convolucion
    for i in range(padding, width - padding):
        for j in range(padding, height - padding):
            members = [
                image[i + alpha, j + beta] for alpha in width_array
                for beta in height_array
            ]
            image_unbordered[i - padding, j - padding] = median(members)
            #print(i,j,len(members))
    return image_unbordered
    """
Exemple #17
0
def csc_median_axis_0(X):
    """Find the median across axis 0 of a CSC matrix.
    It is equivalent to doing np.median(X, axis=0).

    Parameters
    ----------
    X : CSC sparse matrix, shape (n_samples, n_features)
        Input data.

    Returns
    -------
    median : ndarray, shape (n_features,)
        Median.

    """
    if not iscsc(X):
        raise TypeError("Expected matrix of CSC format, got %s" % X.format)

    indptr = X.indptr
    n_samples, n_features = X.shape
    median = np.zeros(n_features)

    for f_ind, (start, end) in enumerate(zip(indptr[:-1], indptr[1:])):

        # Prevent modifying X in place
        data = np.copy(X.data[start:end])
        nz = n_samples - data.size
        median[f_ind] = _get_median(data, nz)

    return median
Exemple #18
0
    def testBasicMatOpr(self):
        # ==============Test arrayfire basic operation test==========================

        raw_data = np.arange(20).reshape(4, 5)
        raw_data_cp = cp.copy(raw_data)

        assert np.array_equal(raw_data, raw_data_cp)
Exemple #19
0
 def test_permutation_sort_1dim(self, dtype):
     cupy_random = self._xp_random(cupy)
     a = cupy.arange(10, dtype=dtype)
     b = cupy.copy(a)
     c = cupy_random.permutation(a)
     testing.assert_allclose(a, b)
     testing.assert_allclose(b, cupy.sort(c))
Exemple #20
0
 def test_permutation_sort_ndim(self, dtype):
     cupy_random = self._xp_random(cupy)
     a = cupy.arange(15, dtype=dtype).reshape(5, 3)
     b = cupy.copy(a)
     c = cupy_random.permutation(a)
     testing.assert_allclose(a, b)
     testing.assert_allclose(b, cupy.sort(c, axis=0))
    def test_backward_case2(self):
        vertices = [[0.8, 0.8, 1.], [-0.5, -0.8, 1.], [0.8, -0.8, 1.]]
        faces = [[0, 1, 2]]
        pyi = 40
        pxi = 50

        renderer = neural_renderer.Renderer()
        renderer.image_size = 64
        renderer.anti_aliasing = False
        renderer.perspective = False

        vertices = chainer.Variable(cp.array(vertices, 'float32'))
        faces = cp.array(faces, 'int32')
        images = renderer.render_silhouettes(vertices[None, :, :],
                                             faces[None, :, :])
        loss = cf.sum(cf.absolute(images[:, pyi, pxi]))
        loss.backward()

        for i in range(3):
            for j in range(2):
                axis = 'x' if j == 0 else 'y'
                vertices2 = cp.copy(vertices.data)
                vertices2[i, j] -= 1. / vertices.grad[i, j]
                images = renderer.render_silhouettes(vertices2[None, :, :],
                                                     faces[None, :, :])
                image = np.tile(images[0].data.get()[:, :, None], (1, 1, 3))
                image[pyi, pxi] = [1, 0, 0]
                ref = scipy.misc.imread(
                    './tests/data/rasterize_silhouettes_case2_v%d_%s.png' %
                    (i, axis))
                ref = ref.astype('float32') / 255
                chainer.testing.assert_allclose(ref, image)
Exemple #22
0
    def exec_sortA0001(self) -> None:
        """exec_sortA0001.

        :rtype: None
        """
        x_population = cp.copy(self.get_population())
        x_sort = cp.copy(self.get_fitness())
        y_population, y_sort = self._sortA0001(
            x_population,
            x_sort,
            self.get_n_jobs(),
            self.get_n_operations(),
            self.get_n_samples(),
        )
        self.set_population(y_population)
        self._set_fitness(y_sort)
def voxelize(faces, size):
    faces = cp.copy(faces)
    faces *= size

    voxels = _voxelize_sub1(faces, size)
    voxels = _voxelize_sub4(voxels)

    return voxels
Exemple #24
0
def identifyClassFromProb(probs):
    if usegpu == True:
        probs = np.asarray(probs)

    probs_ = np.copy(probs)
    probs_[probs_ > 0.5] = 1
    probs_[probs_ <= 0.5] = 0
    return probs_
Exemple #25
0
    def exec_migrationA0001(self) -> None:
        """exec_migrationA0001.

        :rtype: None
        """
        x_population = cp.copy(self.get_population())
        x_aux = cp.copy(self.get_population())
        index_selection = int(self.get_n_samples() *
                              self.get_percent_selection())
        index_migration = int(self.get_n_samples() *
                              self.get_percent_migration())
        cp.random.shuffle(x_population)
        y_population = self._permutationA0001(self.get_n_jobs(),
                                              self.get_n_operations(),
                                              index_migration)
        x_aux[index_selection:, :][-index_migration:, :] = y_population
        self.set_population(x_aux)
Exemple #26
0
    def test_copy_orders(self, order):
        a = cupy.empty((2, 3, 4))
        b = cupy.copy(a, order)

        a_cpu = numpy.empty((2, 3, 4))
        b_cpu = numpy.copy(a_cpu, order)

        assert b.strides == b_cpu.strides
Exemple #27
0
    def test_copy_orders(self, order):
        a = cupy.empty((2, 3, 4))
        b = cupy.copy(a, order)

        a_cpu = numpy.empty((2, 3, 4))
        b_cpu = numpy.copy(a_cpu, order)

        self.assertEqual(b.strides, b_cpu.strides)
Exemple #28
0
        def sortAC0001() -> cp.core.core.ndarray:
            """sortAC0001.

            :rtype: cp.core.core.ndarray
            """

            X_AUX = cp.copy(X)
            return X_AUX[y.argsort()], cp.sort(y)
    def test_copy_orders(self, order):
        a = cupy.empty((2, 3, 4))
        b = cupy.copy(a, order)

        a_cpu = numpy.empty((2, 3, 4))
        b_cpu = numpy.copy(a_cpu, order)

        self.assertEqual(b.strides, b_cpu.strides)
Exemple #30
0
 def test_permutation_seed1(self, dtype):
     a = testing.shaped_random((10,), cupy, dtype)
     b = cupy.copy(a)
     cupy.random.seed(0)
     pa = cupy.random.permutation(a)
     cupy.random.seed(0)
     pb = cupy.random.permutation(b)
     testing.assert_allclose(pa, pb)
Exemple #31
0
 def test_shuffle_seed1(self, dtype):
     a = testing.shaped_random((10,), cupy, dtype)
     b = cupy.copy(a)
     cupy.random.seed(0)
     cupy.random.shuffle(a)
     cupy.random.seed(0)
     cupy.random.shuffle(b)
     testing.assert_allclose(a, b)
Exemple #32
0
	def forward(self, state, action, reward, new_state, is_terminal):
		q = self.get_q(Variable(state))
		q_target = self.get_target_q(Variable(new_state))

		max_target_q = cp.max(q_target.data, axis=1)

		target = cp.copy(q.data)

		for i in xrange(target.shape[0]):
			curr_action = int(action[i])
			if is_terminal[i]:
				target[i, curr_action] = reward[i]
			else:
				target[i, curr_action] = reward[i] + self.gamma * max_target_q[i]
		
		loss = F.mean_squared_error(Variable(target), q)
		return loss, 0.0 #cp.mean(q.data[:, action[i]])
Exemple #33
0
    def forward_onestep(self, x_data, y_data, state, train=True):
        batchsize = x_data.shape[0]
        x = chainer.Variable(x_data, volatile=not train)
        # lstm
        if y_data is not None:
            y = chainer.Variable(self.xp.array(y_data, dtype=self.xp.int32), volatile=not train)
            h_in = self.chain.lstm_xh(x) + \
                self.chain.lstm_yh(y) + \
                self.chain.lstm_rh(state['r']) + \
                self.chain.lstm_hh(state['h'])
        else:
            h_in = self.chain.lstm_xh(x) + \
                self.chain.lstm_rh(state['r']) + \
                self.chain.lstm_hh(state['h'])
            
        c_t, h_t = F.lstm(state['c'], h_in)            

        key = F.reshape(self.chain.l_key(h_t), (batchsize, self.nb_reads, self.memory_shape[1]))
        add = F.reshape(self.chain.l_add(h_t), (batchsize, self.nb_reads, self.memory_shape[1]))
        sigma = self.chain.l_sigma(h_t) 

        # Compute least used weight (not differentiable)
        if self.xp == cp:
            wu_tm1_data = cp.copy(state['used_weight'].data)
            lu_index = np.argsort(cuda.to_cpu(wu_tm1_data), axis=1)[:,:self.nb_reads]
        else:
            wu_tm1_data = state['used_weight'].data
            lu_index = np.argsort(wu_tm1_data, axis=1)[:,:self.nb_reads]
        wlu_tm1_data = self.xp.zeros((batchsize, self.memory_shape[0]), 
                                     dtype=self.xp.float32)
        for i in range(batchsize):
            for  j in range(self.nb_reads):                
                wlu_tm1_data[i,lu_index[i,j]] = 1.  # 1 for least used index
        wlu_tm1 = chainer.Variable(wlu_tm1_data, volatile=not train)

        # write weight
        _wlu_tm1 = F.broadcast_to(
            F.reshape(wlu_tm1, (batchsize, 1, self.memory_shape[0])),
            (batchsize, self.nb_reads, self.memory_shape[0]))
        _sigma = F.broadcast_to(F.reshape(sigma, (batchsize, 1, 1)), 
                                (batchsize, self.nb_reads, self.memory_shape[0]))
        ww_t = _sigma * state['read_weight'] + (1 - _sigma) * _wlu_tm1  
        
        # write to memory
        _lu_mask = 1 - F.broadcast_to(
            F.reshape(wlu_tm1, (batchsize, self.memory_shape[0], 1)),
            (batchsize, self.memory_shape[0], self.memory_shape[1]))
        M_t = state['M'] * _lu_mask + F.batch_matmul(ww_t, add, transa=True) 

        # read from memory
        K_t = cosine_similarity(key, M_t)    

        # read weight, used weight
        wr_t = F.reshape(F.softmax(F.reshape(
                    K_t, (-1, self.memory_shape[0]))), 
                         (batchsize, self.nb_reads, self.memory_shape[0]))
        wu_t = self.gamma * state['used_weight'] + F.sum(wr_t, axis=1) + F.sum(ww_t, axis=1)

        # read memory
        r_t = F.reshape(F.batch_matmul(wr_t, M_t), (batchsize, -1))  # (batchsize, nb_reads * memory_shape[1])

        # set to state
        state_new = {
            'M': M_t,
            'c': c_t,
            'h': h_t,
            'r': r_t,
            'read_weight': wr_t,
            'used_weight': wu_t,
            }
        return state_new