Exemplo n.º 1
0
def rbf_kernel(X):

    XY = T.dot(X, X.T)
    x2 = T.sum(X**2, axis=1).dimshuffle(0, 'x')
    X2e = T.repeat(x2, X.shape[0], axis=1)
    H = X2e +  X2e.T - 2. * XY

    V = H.flatten()
    # median distance
    h = T.switch(T.eq((V.shape[0] % 2), 0),
        # if even vector
        T.mean(T.sort(V)[ ((V.shape[0] // 2) - 1) : ((V.shape[0] // 2) + 1) ]),
        # if odd vector
        T.sort(V)[V.shape[0] // 2])

    h = .5 * h / T.log(T.cast(H.shape[0] + 1., theano.config.floatX))

    # compute the rbf kernel
    kxy = T.exp(-H / h / 2.0)

    dxkxy = -T.dot(kxy, X)
    sumkxy = T.sum(kxy, axis=1).dimshuffle(0, 'x')
    dxkxy = T.add(dxkxy, T.mul(X, sumkxy)) / h

    return kxy, dxkxy
Exemplo n.º 2
0
def vgd_kernel_tensor(X0):

    XY = T.batched_dot(X0, X0.transpose(0,2,1))
    x2 = T.reshape(T.sum(T.square(X0),axis=2), (X0.shape[0], X0.shape[1], 1))
    X2e = T.repeat(x2, X0.shape[1], axis=2)
    H = T.sub(T.add(X2e, X2e.transpose(0,2,1)), 2 * XY)

    V = H.flatten(2)

    # median distance
    h = T.switch(T.eq((V.shape[1] % 2), 0),
            # if even vector
            T.mean(T.sort(V)[:, ((V.shape[1] // 2) - 1): ((V.shape[1] // 2) + 1)], axis=1),
             # if odd vector
            T.sort(V)[:, V.shape[1] // 2])

    h = T.sqrt(0.5 * h / T.log(X0.shape[1].astype('float32') + 1.0))
    # h = T.maximum(h, T.zeros_like(h) + 1e-4)

    # h = h / 2
    Kxy = T.exp(-H / T.tile(h.dimshuffle(0, 'x', 'x'), (1, X0.shape[1], X0.shape[1])) ** 2 / 2.0)

    dxkxy = - T.batched_dot(Kxy, X0)
    sumkxy = T.sum(Kxy, axis=2).dimshuffle(0, 1, 'x')
    dxkxy = T.add(dxkxy, T.mul(X0, sumkxy)) / (T.tile(h.dimshuffle(0, 'x', 'x'), (1, X0.shape[1], X0.shape[2])) ** 2)

    return (Kxy, dxkxy, h)
def vgd_kernel(X0):
    XY = T.dot(X0, X0.transpose())
    x2 = T.reshape(T.sum(T.square(X0), axis=1), (X0.shape[0], 1))
    X2e = T.repeat(x2, X0.shape[0], axis=1)
    H = T.sub(T.add(X2e, X2e.transpose()), 2 * XY)

    V = H.flatten()

    # median distance
    h = T.switch(
        T.eq((V.shape[0] % 2), 0),
        # if even vector
        T.mean(T.sort(V)[((V.shape[0] // 2) - 1):((V.shape[0] // 2) + 1)]),
        # if odd vector
        T.sort(V)[V.shape[0] // 2])

    h = T.sqrt(0.5 * h / T.log(X0.shape[0].astype('float32') + 1.0)) / 2.

    Kxy = T.exp(-H / h**2 / 2.0)

    dxkxy = -T.dot(Kxy, X0)
    sumkxy = T.sum(Kxy, axis=1).dimshuffle(0, 'x')
    dxkxy = T.add(dxkxy, T.mul(X0, sumkxy)) / (h**2)

    return (Kxy, dxkxy, h)
Exemplo n.º 4
0
 def median(x):
     return T.switch(
         T.eq((x.shape[0] % 2), 0),
         T.mean(
             T.sort(x)[((x.shape[0] // 2) - 1):((x.shape[0] // 2) +
                                                1)]),
         T.sort(x)[x.shape[0] // 2])
Exemplo n.º 5
0
 def _inferred_labels(self, s, M, threshold):
     inference = T.tensordot(s,
                             T.switch(T.eq(M, 0), 0, M / T.sum(M, axis=0)),
                             axes=[1, 1])
     BvSB = T.sort(inference, axis=1)[:, -1] - T.sort(inference, axis=1)[:,
                                                                         -2]
     L_inf = T.switch(T.gt(BvSB, threshold),
                      T.cast(T.argmax(inference, axis=1), 'int32'), -1)
     return L_inf
Exemplo n.º 6
0
 def median(tensor):
     """
     MAD tensor from https://groups.google.com/forum/#!topic/theano-users/I4eHjbAetEQ
     :param tensor: Input tensor
     :return: Median expression
     """
     tensor = tensor.flatten(1)
     return T.switch(T.eq((tensor.shape[0] % 2), 0),
                     # if even vector
                     T.mean(T.sort(tensor)[((tensor.shape[0] / 2) - 1): ((tensor.shape[0] / 2) + 1)]),
                     # if odd vector
                     T.sort(tensor)[tensor.shape[0] // 2])
Exemplo n.º 7
0
 def _inferred_labels(self, s, M, threshold):
     inference = T.tensordot(
             s,
             T.switch(T.eq(M,0), 0, M/T.sum(M, axis=0)),
             axes=[1,1])
     BvSB = T.sort(inference,axis=1)[:,-1]-T.sort(inference,axis=1)[:,-2]
     L_inf = T.switch(
         T.gt(BvSB,threshold),
         T.cast(T.argmax(inference,axis=1),'int32'),
         -1
         )
     return L_inf
Exemplo n.º 8
0
    def get_output_for(self, input, deterministic=False, **kwargs):
        if self.maxpool:
            input = T.signal.pool.pool_2d(input,
                                          ds=(self.pool_size, ) * 2,
                                          ignore_border=True,
                                          st=(1, 1),
                                          mode='max')
        if deterministic:
            input = T.signal.pool.pool_2d(input,
                                          ds=(self.pool_size, ) * 2,
                                          ignore_border=True,
                                          st=(self.pool_size, ) * 2,
                                          padding=(self.pool_size / 2, ) * 2,
                                          mode='average_exc_pad')

            return input
            # return input[:, :, ::self.pool_size, ::self.pool_size]

        else:
            w, h = self.input_shape[2:]
            n_w, n_h = w / self.grid_size, h / self.grid_size
            n_sample_per_grid = self.grid_size / self.pool_size
            idx_w = []
            idx_h = []

            for i in range(n_w):
                offset = self.grid_size * i
                if i < n_w - 1:
                    this_n = self.grid_size
                else:
                    this_n = input.shape[2] - offset
                this_idx = T.sort(
                    self.rng.permutation(size=(1, ),
                                         n=this_n)[0, :n_sample_per_grid])
                idx_w.append(offset + this_idx)

            for i in range(n_h):
                offset = self.grid_size * i
                if i < n_h - 1:
                    this_n = self.grid_size
                else:
                    this_n = input.shape[3] - offset
                this_idx = T.sort(
                    self.rng.permutation(size=(1, ),
                                         n=this_n)[0, :n_sample_per_grid])
                idx_h.append(offset + this_idx)
            idx_w = T.concatenate(idx_w, axis=0)
            idx_h = T.concatenate(idx_h, axis=0)

            output = input[:, :, idx_w][:, :, :, idx_h]

            return output
Exemplo n.º 9
0
def median_distance(H, e=1e-6):
    if H.ndim != 2:
        raise NotImplementedError

    V = H.flatten()
    # median distance
    h = T.switch(T.eq((V.shape[0] % 2), 0),
        # if even vector
        T.mean(T.sort(V)[ ((V.shape[0] // 2) - 1) : ((V.shape[0] // 2) + 1) ]),
        # if odd vector
        T.sort(V)[V.shape[0] // 2])
    #h = h / T.log(H.shape[0] + 1).astype(theano.config.floatX)
    return h
Exemplo n.º 10
0
    def setup(self, bottom, top):

        if len(bottom) != 2:
            raise Exception("The layer needs two inputs!")

        probs_tmp = T.ftensor4()
        stat_inp = T.ftensor4()

        stat = stat_inp[:, :, :, 1:]

        probs_bg = probs_tmp[:, 0, :, :]
        probs = probs_tmp[:, 1:, :, :]

        probs_max = T.max(probs, axis=3).max(axis=2)

        q_fg = 0.996
        probs_sort = T.sort(probs.reshape((-1, 20, 41 * 41)), axis=2)
        weights = np.array([q_fg**i
                            for i in range(41 * 41 - 1, -1, -1)])[None,
                                                                  None, :]
        Z_fg = np.sum(weights)
        weights = T.addbroadcast(theano.shared(weights), 0, 1)
        probs_mean = T.sum((probs_sort * weights) / Z_fg, axis=2)

        q_bg = 0.999
        probs_bg_sort = T.sort(probs_bg.reshape((-1, 41 * 41)), axis=1)
        weights_bg = np.array([q_bg**i
                               for i in range(41 * 41 - 1, -1, -1)])[None, :]
        Z_bg = np.sum(weights_bg)
        weights_bg = T.addbroadcast(theano.shared(weights_bg), 0)
        probs_bg_mean = T.sum((probs_bg_sort * weights_bg) / Z_bg, axis=1)

        stat_2d = stat[:, 0, 0, :] > 0.5

        loss_1 = -T.mean(
            T.sum((stat_2d * T.log(probs_mean) /
                   T.sum(stat_2d, axis=1, keepdims=True)),
                  axis=1))
        loss_2 = -T.mean(
            T.sum(((1 - stat_2d) * T.log(1 - probs_max) /
                   T.sum(1 - stat_2d, axis=1, keepdims=True)),
                  axis=1))
        loss_3 = -T.mean(T.log(probs_bg_mean))

        loss = loss_1 + loss_2 + loss_3

        self.forward_theano = theano.function([probs_tmp, stat_inp], loss)
        self.backward_theano = theano.function([probs_tmp, stat_inp],
                                               T.grad(loss, probs_tmp))
Exemplo n.º 11
0
def median_distance(H, e=1e-6, log_ratio_bandwidth=False):
    if H.ndim != 2:
        raise NotImplementedError

    V = H.flatten()
    # median distance
    h = T.switch(T.eq((V.shape[0] % 2), 0),
        # if even vector
        T.mean(T.sort(V)[ ((V.shape[0] // 2) - 1) : ((V.shape[0] // 2) + 1) ]),
        # if odd vector
        T.sort(V)[V.shape[0] // 2])

    if log_ratio_bandwidth:
        h /= T.log(H.shape[0].astype('float32') + 1.0)
    return h
    def kmaxpooling(self, input, k):
        """
        Записывет k максимальных значений по оси 3, отвечающей результату одного фильтра
        :param input: символичный тензор размера 4: (batch size, nfilters, output row, output col)
                      (output row = высота - итоговое количество окон предложения для одного фильтра)
        :param k: int, количество максимальных элементов
        """
        # axis=2 так как нам нужна сортировка внутри каждого фильтра
        pooling_args_sorted = T.argsort(input, axis=2)
        args_of_k_max = pooling_args_sorted[:, :, -k:, :]
        # не хочу терять порядок слов, поэтому ещё раз сортирую номера максимумов:
        args_of_k_max_sorted = T.sort(args_of_k_max, axis=2)

        # use nonsymbolic shape if possible
        input_shape = self.input_shape
        if any(s is None for s in input_shape):
            input_shape = input.shape

        dim0 = T.arange(input_shape[0]).repeat(input_shape[1] * k *
                                               input_shape[3])
        dim1 = T.arange(input_shape[1]).repeat(k * input_shape[3]).reshape((1, -1))\
            .repeat(input_shape[0], axis=0).flatten()
        dim2 = args_of_k_max_sorted.flatten()
        dim3 = T.arange(input_shape[3]).reshape((1, -1))\
            .repeat(input_shape[0] * input_shape[1] * k, axis=0).flatten()

        return input[dim0, dim1, dim2, dim3].reshape(
            (input_shape[0], input_shape[1], k, input_shape[3]))
Exemplo n.º 13
0
    def apply(self, x, y, prefix):
        W, = self.parameters

        mask = tensor.alloc(0, y.shape[0] * self.n_classes)
        ind = tensor.arange(y.shape[0]) * self.n_classes + y
        mask = tensor.set_subtensor(mask[ind], 1.0)
        mask = tensor.reshape(mask, (y.shape[0], self.n_classes))

        #Compute distance matrix
        D = ((W**2).sum(axis=1, keepdims=True).T +
             (x**2).sum(axis=1, keepdims=True) - 2 * tensor.dot(x, W.T))
        self.add_auxiliary_variable(D, name=prefix + '_D')

        d_correct = tensor.reshape(D[mask.nonzero()], (y.shape[0], 1))
        d_incorrect = tensor.reshape(D[(1.0 - mask).nonzero()],
                                     (y.shape[0], self.n_classes - 1))

        c = (d_correct - d_incorrect) / (d_correct + d_incorrect)
        c_sorted = tensor.sort(c, axis=1)[:, ::-1]

        c = (self.weighting * c_sorted).sum(axis=1, keepdims=True)
        self.add_auxiliary_variable(c, name=prefix + '_cost')
        if self.nonlin:
            c = tensor.exp(self.gamma * c)
        cost = c.mean()
        misclass = (tensor.switch(c_sorted[:, 0] < 0, 0.0, 1.0)).mean()
        return cost, misclass
Exemplo n.º 14
0
    def dynamic_kmaxPooling(self, curConv_out, k):
        neighborsForPooling = TSN.images2neibs(ten4=curConv_out, neib_shape=(1,curConv_out.shape[3]), mode='ignore_borders')
        self.neighbors = neighborsForPooling

        neighborsArgSorted = T.argsort(neighborsForPooling, axis=1)
        kNeighborsArg = neighborsArgSorted[:,-k:]
        #self.bestK = kNeighborsArg
        kNeighborsArgSorted = T.sort(kNeighborsArg, axis=1)

        ii = T.repeat(T.arange(neighborsForPooling.shape[0]), k)
        jj = kNeighborsArgSorted.flatten()
        pooledkmaxTmp = neighborsForPooling[ii, jj]
        new_shape = T.cast(T.join(0, 
                           T.as_tensor([neighborsForPooling.shape[0]]),
                           T.as_tensor([k])),
                           'int64')
        pooledkmax_matrix = T.reshape(pooledkmaxTmp, new_shape, ndim=2)

        rightWidth=self.unifiedWidth-k            
        right_padding = T.zeros((neighborsForPooling.shape[0], rightWidth), dtype=theano.config.floatX)
        matrix_padded = T.concatenate([pooledkmax_matrix, right_padding], axis=1)      
        #recover tensor form
        new_shape = T.cast(T.join(0, curConv_out.shape[:-2],
                           T.as_tensor([curConv_out.shape[2]]),
                           T.as_tensor([self.unifiedWidth])),
                           'int64')

        curPooled_out = T.reshape(matrix_padded, new_shape, ndim=4)
                
        return curPooled_out
Exemplo n.º 15
0
    def __call__(self, X):
        XY = X.dot(X.T)
        x2 = tt.sum(X ** 2, axis=1).dimshuffle(0, 'x')
        X2e = tt.repeat(x2, X.shape[0], axis=1)
        H = X2e + X2e.T - 2. * XY

        V = tt.sort(H.flatten())
        length = V.shape[0]
        # median distance
        m = tt.switch(tt.eq((length % 2), 0),
                      # if even vector
                      tt.mean(V[((length // 2) - 1):((length // 2) + 1)]),
                      # if odd vector
                      V[length // 2])

        h = .5 * m / tt.log(floatX(H.shape[0]) + floatX(1))

        #  RBF
        Kxy = tt.exp(-H / h / 2.0)

        # Derivative
        dxkxy = -tt.dot(Kxy, X)
        sumkxy = tt.sum(Kxy, axis=1).dimshuffle(0, 'x')
        dxkxy = tt.add(dxkxy, tt.mul(X, sumkxy)) / h

        return Kxy, dxkxy
Exemplo n.º 16
0
        def kmaxpooling_output(input):
            '''
                实现 k-max pooling
                    1. 先排序
                    2. 再分别取出前k个值
            :param k: k top higiest value
            :type k: int
            :return:
            '''
            input = T.transpose(input, axes=(0, 1, 3, 2))
            sorted_values = T.argsort(input, axis=3)
            topmax_indexes = sorted_values[:, :, :, -k:]
            # sort indexes so that we keep the correct order within the sentence
            topmax_indexes_sorted = T.sort(topmax_indexes)

            # given that topmax only gives the index of the third dimension, we need to generate the other 3 dimensions
            dim0 = T.arange(0, input.shape[0]).repeat(input.shape[1] *
                                                      input.shape[2] * k)
            dim1 = T.arange(0,
                            input.shape[1]).repeat(k * input.shape[2]).reshape(
                                (1, -1)).repeat(input.shape[0],
                                                axis=0).flatten()
            dim2 = T.arange(0, input.shape[2]).repeat(k).reshape(
                (1, -1)).repeat(input.shape[0] * input.shape[1],
                                axis=0).flatten()
            dim3 = topmax_indexes_sorted.flatten()
            return T.transpose(input[dim0, dim1, dim2, dim3].reshape(
                (input.shape[0], input.shape[1], input.shape[2], k)),
                               axes=(0, 1, 3, 2))
Exemplo n.º 17
0
    def k_max_pool(self, x, k):
        """
        perform k-max pool on the input along the rows

        input: theano.tensor.tensor4
           
        k: theano.tensor.iscalar
            the k parameter

        Returns: 
        4D tensor
        """
        x = T.reshape(x, (x.shape[0], x.shape[1], 1, x.shape[2] * x.shape[3]))
        ind = T.argsort(x, axis=3)

        sorted_ind = T.sort(ind[:, :, :, -k:], axis=3)

        dim0, dim1, dim2, dim3 = sorted_ind.shape

        indices_dim0 = T.arange(dim0).repeat(dim1 * dim2 * dim3)
        indices_dim1 = (
            T.arange(dim1).repeat(dim2 * dim3).reshape((dim1 * dim2 * dim3, 1)).repeat(dim0, axis=1).T.flatten()
        )
        indices_dim2 = T.arange(dim2).repeat(dim3).reshape((dim2 * dim3, 1)).repeat(dim0 * dim1, axis=1).T.flatten()

        result = x[indices_dim0, indices_dim1, indices_dim2, sorted_ind.flatten()].reshape(sorted_ind.shape)
        shape = (result.shape[0], result.shape[1], result.shape[2] * result.shape[3], 1)

        result = T.reshape(result, shape)

        return result
Exemplo n.º 18
0
 def softmax(self, D, I):
   D = D * T.constant(self.attrs['sharpening'], 'float32')
   if self.attrs['norm'] == 'exp':
     E = T.exp(-D) * I
     E = E / T.maximum(T.sum(E,axis=0,keepdims=True),T.constant(1e-20,'float32'))
   elif self.attrs['norm'] == 'sigmoid':
     E = (numpy.float32(1) - T.tanh(D)**2) * I
   elif self.attrs['norm'] == 'lstm':
     n_out = self.attrs['template']
     def lstm(z, i_t, s_p, h_p):
       z += T.dot(h_p, self.N_re)
       i = T.outer(i_t, T.alloc(numpy.cast['int8'](1), n_out))
       ingate = T.nnet.sigmoid(z[:,n_out: 2 * n_out])
       forgetgate = T.nnet.sigmoid(z[:,2 * n_out:3 * n_out])
       outgate = T.nnet.sigmoid(z[:,3 * n_out:])
       input = T.tanh(z[:,:n_out])
       s_t = input * ingate + s_p * forgetgate
       h_t = T.tanh(s_t) * outgate
       return theano.gradient.grad_clip(s_t * i, -50, 50), h_t * i
     E, _ = theano.scan(lstm, sequences=[D,I], outputs_info=[T.zeros((n_out,), 'float32'), T.zeros((n_out,), 'int32')])
     E = T.nnet.sigmoid(T.dot(E,self.N_out))
   else:
     raise NotImplementedError()
   if self.attrs['nbest'] > 1:
     opt = T.minimum(self.attrs['nbest'], E.shape[0])
     score = (T.sort(E, axis=0)[-opt]).dimshuffle('x',0).repeat(E.shape[0],axis=0)
     E = T.switch(T.lt(E,score), T.zeros_like(E), E)
   return E
Exemplo n.º 19
0
    def _pooling_function(self, inputs, pool_size, strides, border_mode, dim_ordering):

        if pool_size[0]<-1:
            # k-max pooling
            input_layer = T.transpose(inputs, axes=(0, 1, 3, 2))
            sorted_values = T.argsort(input_layer, axis=3)
            topmax_indexes = sorted_values[:, :, :, -self.k:]
            # sort indexes so that we keep the correct order within the sentence
            topmax_indexes_sorted = T.sort(topmax_indexes)

            # given that topmax only gives the index of the third dimension, we need to generate the other 3 dimensions
            dim0 = T.arange(0, input_layer.shape[0]).repeat(input_layer.shape[1] * input_layer.shape[2] * self.k)
            dim1 = T.arange(0, input_layer.shape[1]).repeat(self.k * input_layer.shape[2]).reshape((1, -1)).repeat(
                input_layer.shape[0],
                axis=0).flatten()
            dim2 = T.arange(0, input_layer.shape[2]).repeat(self.k).reshape((1, -1)).repeat(
                input_layer.shape[0] * input_layer.shape[1],
                axis=0).flatten()
            dim3 = topmax_indexes_sorted.flatten()
            x = T.transpose(
                input_layer[dim0, dim1, dim2, dim3].reshape(
                    (input_layer.shape[0], input_layer.shape[1], input_layer.shape[2], self.k)),
                axes=(0, 1, 3, 2))
            return x
        else:
            return super(MaxPooling2DWrapper, self)._pooling_function(inputs, pool_size, strides, border_mode, dim_ordering)
Exemplo n.º 20
0
        def kmaxpooling_output(input):
            '''
                实现 k-max pooling
                    1. 先排序
                    2. 再分别取出前k个值
            :param k: k top higiest value
            :type k: int
            :return:
            '''
            input = T.transpose(input, axes=(0, 1, 3, 2))
            sorted_values = T.argsort(input, axis=3)
            topmax_indexes = sorted_values[:, :, :, -k:]
            # sort indexes so that we keep the correct order within the sentence
            topmax_indexes_sorted = T.sort(topmax_indexes)

            # given that topmax only gives the index of the third dimension, we need to generate the other 3 dimensions
            dim0 = T.arange(0, input.shape[0]).repeat(input.shape[1] * input.shape[2] * k)
            dim1 = T.arange(0, input.shape[1]).repeat(k * input.shape[2]).reshape((1, -1)).repeat(input.shape[0],
                                                                                                  axis=0).flatten()
            dim2 = T.arange(0, input.shape[2]).repeat(k).reshape((1, -1)).repeat(input.shape[0] * input.shape[1],
                                                                                 axis=0).flatten()
            dim3 = topmax_indexes_sorted.flatten()
            return T.transpose(
                input[dim0, dim1, dim2, dim3].reshape((input.shape[0], input.shape[1], input.shape[2], k)),
                axes=(0, 1, 3, 2))
Exemplo n.º 21
0
    def output(self, x, index_selection_func=None):
        if self.n_out > 1:
            iWin = self.k

            if self.n_in == 1:
                iWin = 1

            rnd_proj = T.dot(
                x.reshape((x.shape[0], x.shape[1]*x.shape[2])),
                self.rand_proj_mat
            )

            if index_selection_func is not None:
                self.out_idxs = index_selection_func(rnd_proj)
            else:
                self.out_idxs = T.argsort(rnd_proj)
            self.out_idxs = T.sort(self.out_idxs[:, -self.k:])

            # self.out_idxs.set_value(
            #     np.random.randint(0, self.n_out, (self.batch_size, self.k))
            # )

        sparse = sparse_block_dot_SS(
            self.W,
            x,
            self.in_idxs,
            self.b,
            self.out_idxs
        )

        return (sparse if self.activation is None
                else self.activation(sparse))
Exemplo n.º 22
0
    def link(self, input):
        self.input = input

        # select the lines where we apply k-max pooling
        neighbors_for_pooling = TSN.images2neibs(
            ten4=self.input,
            neib_shape=(self.input.shape[2], 1),  # we look the max on every dimension
            mode='valid'  # 'ignore_borders'
        )

        neighbors_arg_sorted = T.argsort(neighbors_for_pooling, axis=1)
        k_neighbors_arg = neighbors_arg_sorted[:, -self.k_max:]
        k_neighbors_arg_sorted = T.sort(k_neighbors_arg, axis=1)

        ii = T.repeat(T.arange(neighbors_for_pooling.shape[0]), self.k_max)
        jj = k_neighbors_arg_sorted.flatten()
        flattened_pooled_out = neighbors_for_pooling[ii, jj]

        pooled_out_pre_shape = T.join(
            0,
            self.input.shape[:-2],
            [self.input.shape[3]],
            [self.k_max]
        )
        self.output = flattened_pooled_out.reshape(
            pooled_out_pre_shape,
            ndim=self.input.ndim
        ).dimshuffle(0, 1, 3, 2)
        return self.output
Exemplo n.º 23
0
 def _step(x, k, max_seq_len):
     tmp = x[
         T.arange(x.shape[0])[:, np.newaxis, np.newaxis],
         T.sort(T.argsort(x, axis=1)[:, -k:, :], axis=1),
         T.arange(x.shape[2])[np.newaxis, np.newaxis,:],
     ]
     return T.concatenate([tmp, T.zeros([x.shape[0], max_seq_len-k, x.shape[2]])], axis=1)
Exemplo n.º 24
0
    def dynamic_kmaxPooling(self, curConv_out, k):
        neighborsForPooling = TSN.images2neibs(
            ten4=curConv_out,
            neib_shape=(1, curConv_out.shape[3]),
            mode='ignore_borders')
        self.neighbors = neighborsForPooling

        neighborsArgSorted = T.argsort(neighborsForPooling, axis=1)
        kNeighborsArg = neighborsArgSorted[:, -k:]
        #self.bestK = kNeighborsArg
        kNeighborsArgSorted = T.sort(kNeighborsArg, axis=1)

        ii = T.repeat(T.arange(neighborsForPooling.shape[0]), k)
        jj = kNeighborsArgSorted.flatten()
        pooledkmaxTmp = neighborsForPooling[ii, jj]
        new_shape = T.cast(
            T.join(0, T.as_tensor([neighborsForPooling.shape[0]]),
                   T.as_tensor([k])), 'int64')
        pooledkmax_matrix = T.reshape(pooledkmaxTmp, new_shape, ndim=2)

        rightWidth = self.unifiedWidth - k
        right_padding = T.zeros((neighborsForPooling.shape[0], rightWidth),
                                dtype=theano.config.floatX)
        matrix_padded = T.concatenate([pooledkmax_matrix, right_padding],
                                      axis=1)
        #recover tensor form
        new_shape = T.cast(
            T.join(0, curConv_out.shape[:-2],
                   T.as_tensor([curConv_out.shape[2]]),
                   T.as_tensor([self.unifiedWidth])), 'int64')

        curPooled_out = T.reshape(matrix_padded, new_shape, ndim=4)

        return curPooled_out
Exemplo n.º 25
0
 def softmax(self, D, I):
   D = D * T.constant(self.attrs['sharpening'], 'float32')
   if self.attrs['norm'] == 'exp':
     E = T.exp(-D) * I
     E = E / T.maximum(T.sum(E,axis=0,keepdims=True),T.constant(1e-20,'float32'))
   elif self.attrs['norm'] == 'sigmoid':
     E = (numpy.float32(1) - T.tanh(D)**2) * I
   elif self.attrs['norm'] == 'lstm':
     n_out = self.attrs['template']
     def lstm(z, i_t, s_p, h_p):
       z += T.dot(h_p, self.N_re)
       i = T.outer(i_t, T.alloc(numpy.cast['int8'](1), n_out))
       ingate = T.nnet.sigmoid(z[:,n_out: 2 * n_out])
       forgetgate = T.nnet.sigmoid(z[:,2 * n_out:3 * n_out])
       outgate = T.nnet.sigmoid(z[:,3 * n_out:])
       input = T.tanh(z[:,:n_out])
       s_t = input * ingate + s_p * forgetgate
       h_t = T.tanh(s_t) * outgate
       return theano.gradient.grad_clip(s_t * i, -50, 50), h_t * i
     E, _ = theano.scan(lstm, sequences=[D,I], outputs_info=[T.zeros((n_out,), 'float32'), T.zeros((n_out,), 'int32')])
     E = T.nnet.sigmoid(T.dot(E,self.N_out))
   else:
     raise NotImplementedError()
   if self.attrs['nbest'] > 1:
     opt = T.minimum(self.attrs['nbest'], E.shape[0])
     score = (T.sort(E, axis=0)[-opt]).dimshuffle('x',0).repeat(E.shape[0],axis=0)
     E = T.switch(T.lt(E,score), T.zeros_like(E), E)
   return E
    def beam_search_forward(self, col, score, embedding, pre_hid_info,
                            s_embedding):
        n = col.shape[0]
        input_info = T.concatenate([embedding, col, pre_hid_info], axis=-1)
        u1 = get_output(self.gru_update_3, input_info)
        r1 = get_output(self.gru_reset_3, input_info)
        reset_h1 = pre_hid_info * r1
        c_in = T.concatenate([embedding, col, reset_h1], axis=1)
        c1 = get_output(self.gru_candidate_3, c_in)
        h1 = (1.0 - u1) * pre_hid_info + u1 * c1

        s = get_output(self.score, h1)
        sample_score = T.dot(s, s_embedding.T)
        k = sample_score.shape[-1]
        sample_score = sample_score.reshape((n, 1, k))
        sample_score += score
        sample_score = sample_score.reshape((n, 10 * k))
        sort_index = T.argsort(-sample_score, axis=-1)
        sample_score = T.sort(-sample_score, axis=-1)
        tops = sort_index[:, :10]
        sample_score = -sample_score[:, :10]
        tops = T.cast(T.divmod(tops, self.target_vocab_size), "int8")

        embedding = get_output(self.target_input_embedding, tops)
        d = embedding.shape[-1]
        embedding = embedding.reshape((n * 10, d))
        return sample_score, embedding, h1, tops
    def k_max_pool(self, x, k):
        """
        perform k-max pool on the input along the rows

        input: theano.tensor.tensor4
           
        k: theano.tensor.iscalar
            the k parameter

        Returns: 
        4D tensor
        """
        ind = T.argsort(x, axis=3)

        sorted_ind = T.sort(ind[:, :, :, -k:], axis=3)

        dim0, dim1, dim2, dim3 = sorted_ind.shape

        indices_dim0 = T.arange(dim0).repeat(dim1 * dim2 * dim3)
        indices_dim1 = T.arange(dim1).repeat(dim2 * dim3).reshape(
            (dim1 * dim2 * dim3, 1)).repeat(dim0, axis=1).T.flatten()
        indices_dim2 = T.arange(dim2).repeat(dim3).reshape(
            (dim2 * dim3, 1)).repeat(dim0 * dim1, axis=1).T.flatten()

        return x[indices_dim0, indices_dim1, indices_dim2,
                 sorted_ind.flatten()].reshape(sorted_ind.shape)
    def update_active_paths(self, l, scores, active_paths_current, N,
                            beam_size):

        best_scores_l_all = T.max(scores, axis=1)  # N * D

        best_scores_l = T.sort(best_scores_l_all,
                               axis=-1)[:, -beam_size:]  # N * B

        active_words_l = T.argsort(best_scores_l_all,
                                   axis=1)[:, -beam_size:]  # N * B

        best_paths_l_all = T.argmax(scores, axis=1)  # N * D

        best_paths_l_inds = best_paths_l_all[T.repeat(T.arange(N), beam_size),
                                             active_words_l.flatten()]
        best_paths_l_inds = best_paths_l_inds.reshape((N, beam_size))  # N * B

        best_paths_l = active_paths_current[
            T.repeat(T.arange(N), beam_size),
            best_paths_l_inds.flatten()].reshape(
                (N, beam_size, self.max_length))  # N * B * L

        active_paths_new = T.set_subtensor(best_paths_l[:, :, l],
                                           active_words_l)

        return best_scores_l, active_paths_new
Exemplo n.º 29
0
    def _pooling_function(self, inputs, pool_size, strides, border_mode,
                          dim_ordering):

        if pool_size[0] < -1:
            # k-max pooling
            input_layer = T.transpose(inputs, axes=(0, 1, 3, 2))
            sorted_values = T.argsort(input_layer, axis=3)
            topmax_indexes = sorted_values[:, :, :, -self.k:]
            # sort indexes so that we keep the correct order within the sentence
            topmax_indexes_sorted = T.sort(topmax_indexes)

            # given that topmax only gives the index of the third dimension, we need to generate the other 3 dimensions
            dim0 = T.arange(0, input_layer.shape[0]).repeat(
                input_layer.shape[1] * input_layer.shape[2] * self.k)
            dim1 = T.arange(0, input_layer.shape[1]).repeat(
                self.k * input_layer.shape[2]).reshape(
                    (1, -1)).repeat(input_layer.shape[0], axis=0).flatten()
            dim2 = T.arange(0, input_layer.shape[2]).repeat(self.k).reshape(
                (1, -1)).repeat(input_layer.shape[0] * input_layer.shape[1],
                                axis=0).flatten()
            dim3 = topmax_indexes_sorted.flatten()
            x = T.transpose(input_layer[dim0, dim1, dim2, dim3].reshape(
                (input_layer.shape[0], input_layer.shape[1],
                 input_layer.shape[2], self.k)),
                            axes=(0, 1, 3, 2))
            return x
        else:
            return super(MaxPooling2DWrapper,
                         self)._pooling_function(inputs, pool_size, strides,
                                                 border_mode, dim_ordering)
Exemplo n.º 30
0
 def apply(self, x, y, prefix):
     W, = self.parameters
     
     mask = tensor.alloc(0, y.shape[0]*self.n_classes)
     ind = tensor.arange(y.shape[0])*self.n_classes + y
     mask = tensor.set_subtensor(mask[ind], 1.0)
     mask = tensor.reshape(mask, (y.shape[0], self.n_classes))
     
     #Compute distance matrix
     D = ((W**2).sum(axis=1, keepdims=True).T + (x**2).sum(axis=1, keepdims=True) - 2*tensor.dot(x, W.T))
     self.add_auxiliary_variable(D, name=prefix+'_D')
           
     d_correct = tensor.reshape(D[mask.nonzero()], (y.shape[0], 1))
     d_incorrect = tensor.reshape(D[(1.0-mask).nonzero()], (y.shape[0], self.n_classes-1))
     
     c = (d_correct - d_incorrect)/(d_correct + d_incorrect)
     c_sorted = tensor.sort(c, axis=1)[:, ::-1]
     
     c = (self.weighting*c_sorted).sum(axis=1, keepdims=True)
     self.add_auxiliary_variable(c, name=prefix+'_cost')
     if self.nonlin:
         c = tensor.exp(self.gamma*c)
     cost = c.mean()
     misclass = (tensor.switch(c_sorted[:, 0] < 0, 0.0, 1.0)).mean()
     return cost, misclass
Exemplo n.º 31
0
    def get_stencil(self, t, r=None, texp=None):
        if r is None or texp is None:
            return tt.shape_padright(t)

        z = tt.zeros_like(self.a)
        r = tt.as_tensor_variable(r)
        R = self.r_star + z
        hp = 0.5 * self.period

        if self.ecc is None:
            # Equation 14 from Winn (2010)
            k = r / self.r_star
            arg1 = tt.square(1 + k) - tt.square(self.b)
            arg2 = tt.square(1 - k) - tt.square(self.b)
            factor = R / (self.a * self.sin_incl)
            hdur1 = hp * tt.arcsin(factor * tt.sqrt(arg1)) / np.pi
            hdur2 = hp * tt.arcsin(factor * tt.sqrt(arg2)) / np.pi
            ts = [-hdur1, -hdur2, hdur2, hdur1]
            flag = z

        else:
            M_contact1 = self.contact_points_op(self.a, self.ecc,
                                                self.cos_omega, self.sin_omega,
                                                self.cos_incl + z,
                                                self.sin_incl + z, R + r)
            M_contact2 = self.contact_points_op(self.a, self.ecc,
                                                self.cos_omega, self.sin_omega,
                                                self.cos_incl + z,
                                                self.sin_incl + z, R - r)

            flag = M_contact1[2] + M_contact2[2]

            ts = [
                tt.mod(
                    (M_contact1[0] - self.M0) / self.n + hp, self.period) - hp,
                tt.mod(
                    (M_contact2[0] - self.M0) / self.n + hp, self.period) - hp,
                tt.mod(
                    (M_contact2[1] - self.M0) / self.n + hp, self.period) - hp,
                tt.mod(
                    (M_contact1[1] - self.M0) / self.n + hp, self.period) - hp
            ]

        start = self.period * tt.floor((tt.min(t) - self.t0) / self.period)
        end = self.period * (tt.ceil((tt.max(t) - self.t0) / self.period) + 1)
        start += self.t0
        end += self.t0
        tout = []
        for i in range(4):
            if z.ndim < 1:
                tout.append(ts[i] + tt.arange(start, end, self.period))
            else:
                tout.append(
                    theano.scan(
                        fn=lambda t0, s0, e0, p0: t0 + tt.arange(s0, e0, p0),
                        sequences=[ts[i], start, end, self.period],
                    )[0].flatten())

        ts = tt.sort(tt.concatenate(tout))
        return ts, flag
    def dog_output(self, input_image):

        _, self.channels, self.height, self.width = input_image.shape
        conv_output = T.nnet.conv2d(input_image,
                                    dog_W,
                                    filter_flip=False,
                                    border_mode='half',
                                    subsample=(1, 1))

        conv_output2 = conv_output[:, ::-1, :, :]

        dog_maps = conv_output - conv_output2

        dog_maps = T.ge(dog_maps, 0) * dog_maps
        dog_maps = T.switch(T.ge(dog_maps, T.mean(dog_maps)), dog_maps, 0.0)
        #dog_maps=T.set_subtensor(dog_maps[:,1:2,:,:],np.float32(0.0))
        sorted_dog = T.sort(T.reshape(dog_maps, (-1, )))
        #sorted_dog=T.shape(sorted_dog)-T.sum(T.neq(sorted_dog,0.0))
        num_spikes = T.neq(sorted_dog, 0.0)
        num_spikes_per_bin = T.sum(num_spikes) // self.time_steps
        i = T.shape(sorted_dog)[0] - num_spikes_per_bin
        bin_limits = T.zeros(self.time_steps + 1)
        bin_limits = T.set_subtensor(bin_limits[0], sorted_dog[-1])
        for j in range(0, self.time_steps):
            bin_limits = T.set_subtensor(bin_limits[j + 1], sorted_dog[i])
            i = i - num_spikes_per_bin

        #return dog_maps,sorted_dog,bin_limits
#return self.temporal_encoding(dog_maps,bin_limits)
        return T.reshape(
            self.temporal_encoding(dog_maps, bin_limits) * np.float32(1.0), [
                self.time_steps, self.batch_size, self.channels * 2,
                self.height, self.width
            ])
Exemplo n.º 33
0
    def __call__(self, X):
        XY = X.dot(X.T)
        x2 = tt.sum(X**2, axis=1).dimshuffle(0, 'x')
        X2e = tt.repeat(x2, X.shape[0], axis=1)
        H = X2e + X2e.T - 2. * XY

        V = tt.sort(H.flatten())
        length = V.shape[0]
        # median distance
        m = tt.switch(
            tt.eq((length % 2), 0),
            # if even vector
            tt.mean(V[((length // 2) - 1):((length // 2) + 1)]),
            # if odd vector
            V[length // 2])

        h = .5 * m / tt.log(floatX(H.shape[0]) + floatX(1))

        #  RBF
        Kxy = tt.exp(-H / h / 2.0)

        # Derivative
        dxkxy = -tt.dot(Kxy, X)
        sumkxy = tt.sum(Kxy, axis=-1, keepdims=True)
        dxkxy = tt.add(dxkxy, tt.mul(X, sumkxy)) / h

        return Kxy, dxkxy
Exemplo n.º 34
0
    def link(self, input):
        self.input = input.dimshuffle(0, 1, 3, 2)
        # get the indexes that give the max on every line and sort them
        ind = T.argsort(self.input, axis=3)
        sorted_ind = T.sort(ind[:, :, :, -self.k_max:], axis=3)
        dim0, dim1, dim2, dim3 = sorted_ind.shape

        # prepare indices for selection
        indices_dim0 = T.arange(dim0)\
                        .repeat(dim1 * dim2 * dim3)
        indices_dim1 = T.arange(dim1)\
                        .repeat(dim2 * dim3)\
                        .reshape((dim1 * dim2 * dim3, 1))\
                        .repeat(dim0, axis=1)\
                        .T\
                        .flatten()
        indices_dim2 = T.arange(dim2)\
                        .repeat(dim3)\
                        .reshape((dim2 * dim3, 1))\
                        .repeat(dim0 * dim1, axis=1)\
                        .T\
                        .flatten()

        # output
        self.output = self.input[
            indices_dim0,
            indices_dim1,
            indices_dim2,
            sorted_ind.flatten()
        ].reshape(sorted_ind.shape).dimshuffle(0, 1, 3, 2)
        return self.output
Exemplo n.º 35
0
def kite_area(r, b):
    abc = tt.sort(tt.stack((r, b, tt.ones_like(r)), axis=0), axis=0)
    A = abc[0]
    B = abc[1]
    C = abc[2]
    return 0.5 * tt.sqrt(
        tt.abs_((A + (B + C)) * (C - (A - B)) * (C + (A - B)) * (A + (B - C))))
Exemplo n.º 36
0
 def fix_k_max(self, k, masked_data):
     # @ref: https://github.com/fchollet/keras/issues/373
     result = masked_data[
         T.arange(masked_data.shape[0]).dimshuffle(0, "x", "x"),
         T.sort(T.argsort(masked_data, axis=1)[:, -k:, :], axis=1),
         T.arange(masked_data.shape[2]).dimshuffle("x", "x", 0)
     ]
     return result
Exemplo n.º 37
0
    def hinge_cost(self, y, delta=0.1, outputs=T.arange(26)):

        c = self.p_y_given_x[T.arange(y.shape[0]), y]
        p_y_given_x_temp = T.sort(self.p_y_given_x)
        a = p_y_given_x_temp[:, -1]
        b = p_y_given_x_temp[:, -2]
        cost = T.mean(T.maximum(0, T.sub(2 * c, T.add(a, b)) + delta))
        return cost
Exemplo n.º 38
0
 def __call__(self,X):
     ind = T.argsort(X, axis = 3)
     sorted_ind = T.sort(ind[:,:,:, -self.poolsize:], axis = 3)
     dim0, dim1, dim2, dim3 = sorted_ind.shape
     indices_dim0 = T.arange(dim0).repeat(dim1 * dim2 * dim3)
     indices_dim1 = T.arange(dim1).repeat(dim2 * dim3).reshape((dim1*dim2*dim3, 1)).repeat(dim0, axis=1).T.flatten()
     indices_dim2 = T.arange(dim2).repeat(dim3).reshape((dim2*dim3, 1)).repeat(dim0 * dim1, axis = 1).T.flatten()
     return X[indices_dim0, indices_dim1, indices_dim2, sorted_ind.flatten()].reshape(sorted_ind.shape)
Exemplo n.º 39
0
def median(grid_split_image, grid_counts):
    shp = grid_split_image.shape
    reshape = grid_split_image.reshape((shp[0], shp[1], shp[2], -1), ndim=4)
    img_sort = T.sort(reshape)
    pixels = img_sort.shape[-1]
    indicies = (pixels + grid_counts) // 2
    indicies = T.cast(indicies, 'int32')
    medians = img_sort.take(indicies)
    return medians
Exemplo n.º 40
0
def _global_rank_pooling(input, **kwargs):
    [n_songs, n_fmaps, n_time, n_freq] = input.shape
    input2d = input.reshape((n_songs*n_fmaps, n_time*n_freq))
    
    weight1d = kwargs['weight1d']
    
    out2d = T.sort(input2d, axis=-1)[:, ::-1] * weight1d / T.sum(weight1d)
    out4d = out2d.reshape((n_songs, n_fmaps, n_time, n_freq))
    return T.sum(out4d, axis=(2,3))
Exemplo n.º 41
0
def median(grid_split_image, grid_counts):
    shp = grid_split_image.shape
    reshape = grid_split_image.reshape((shp[0], shp[1], shp[2], -1), ndim=4)
    img_sort = T.sort(reshape)
    pixels = img_sort.shape[-1]
    indicies = (pixels + grid_counts) // 2
    indicies = T.cast(indicies, 'int32')
    medians = img_sort.take(indicies)
    return medians
Exemplo n.º 42
0
def _k_max_pooling(input, kmax):
  pool = input.dimshuffle(0, 2, 1, 3).flatten(ndim=3).dimshuffle(1,0,2).flatten(ndim=2).dimshuffle(1,0)
  neighborsArgSorted = T.argsort(pool, axis=1)
  yy = T.sort(neighborsArgSorted[:, -kmax:], axis=1).flatten()
  xx = T.repeat(T.arange(neighborsArgSorted.shape[0]), kmax)
  pool_kmax = pool[xx, yy]
  pool_kmax_shape = T.join(0, T.as_tensor([input.shape[0], input.shape[1], input.shape[3], kmax]))
  pooled_out = pool_kmax.reshape(pool_kmax_shape, ndim=4).dimshuffle(0, 1, 3, 2)
  return pooled_out
Exemplo n.º 43
0
def sort():
#    cands = T.itensor3('cands')
    cands = T.imatrix('cands')
#    h_in = np.asarray([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype='int32')
#    h_in = np.asarray([[1, 2], [3, 4]], dtype='int32')
    h_in = np.asarray([[4, 2], [3, 1]], dtype='int32')

    y = T.sort(cands, axis=1)
    f = theano.function(inputs=[cands], outputs=[y], on_unused_input='ignore')
    print f(h_in)
Exemplo n.º 44
0
    def get_stencil(self, t, r=None, texp=None):
        if r is None or texp is None:
            return tt.shape_padright(t)

        z = tt.zeros_like(self.a)
        r = tt.as_tensor_variable(r)
        R = self.r_star + z
        hp = 0.5 * self.period

        if self.ecc is None:
            # Equation 14 from Winn (2010)
            k = r / self.r_star
            arg1 = tt.square(1 + k) - tt.square(self.b)
            arg2 = tt.square(1 - k) - tt.square(self.b)
            factor = R / (self.a * self.sin_incl)
            hdur1 = hp * tt.arcsin(factor * tt.sqrt(arg1)) / np.pi
            hdur2 = hp * tt.arcsin(factor * tt.sqrt(arg2)) / np.pi
            ts = [-hdur1, -hdur2, hdur2, hdur1]
            flag = z

        else:
            M_contact1 = self.contact_points_op(
                self.a, self.ecc, self.cos_omega, self.sin_omega,
                self.cos_incl + z, self.sin_incl + z, R + r)
            M_contact2 = self.contact_points_op(
                self.a, self.ecc, self.cos_omega, self.sin_omega,
                self.cos_incl + z, self.sin_incl + z, R - r)

            flag = M_contact1[2] + M_contact2[2]

            ts = [
                tt.mod((M_contact1[0]-self.M0)/self.n+hp, self.period)-hp,
                tt.mod((M_contact2[0]-self.M0)/self.n+hp, self.period)-hp,
                tt.mod((M_contact2[1]-self.M0)/self.n+hp, self.period)-hp,
                tt.mod((M_contact1[1]-self.M0)/self.n+hp, self.period)-hp
            ]

        start = self.period * tt.floor((tt.min(t) - self.t0) / self.period)
        end = self.period * (tt.ceil((tt.max(t) - self.t0) / self.period) + 1)
        start += self.t0
        end += self.t0
        tout = []
        for i in range(4):
            if z.ndim < 1:
                tout.append(ts[i] + tt.arange(start, end, self.period))
            else:
                tout.append(theano.scan(
                    fn=lambda t0, s0, e0, p0: t0 + tt.arange(s0, e0, p0),
                    sequences=[ts[i], start, end, self.period],
                )[0].flatten())

        ts = tt.sort(tt.concatenate(tout))
        return ts, flag
Exemplo n.º 45
0
    def kmaxPool(self, conv_out, pool_shape, k):
        '''
        Perform k-max Pooling.
        '''
        n0, n1, d, size = pool_shape
        imgs = images2neibs(conv_out, T.as_tensor_variable((1, size)))

        indices = T.argsort(T.mul(imgs, -1))
        k_max_indices = T.sort(indices[:, :k])
    
        S = T.arange(d*n1*n0).reshape((d*n1*n0, 1))
        return imgs[S, k_max_indices].reshape((n0, n1, d, k))
Exemplo n.º 46
0
    def __init__(self, conv_out, k=1):
        """
        Allocate a LeNetConvPoolLayer with shared variable internal parameters.

        :type rng: numpy.random.RandomState
        :param rng: a random number generator used to initialize weights

        :type input: theano.tensor.dtensor4
        :param input: symbolic image tensor, of shape image_shape

        :type filter_shape: tuple or list of length 4
        :param filter_shape: (number of filters, num input feature maps,
                              filter height,filter width)

        :type image_shape: tuple or list of length 4
        :param image_shape: (batch size, num input feature maps,
                             image height, image width)

        :type poolsize: tuple or list of length 2
        :param poolsize: the downsampling (pooling) factor (#rows,#cols)
        """
        
        #images2neibs produces a 2D matrix
        neighborsForPooling = TSN.images2neibs(ten4=conv_out, neib_shape=(conv_out.shape[2], 1), mode='ignore_borders')

        #k = poolsize[1]

        neighborsArgSorted = T.argsort(neighborsForPooling, axis=1)
        kNeighborsArg = neighborsArgSorted[:,-k:]
        kNeighborsArgSorted = T.sort(kNeighborsArg, axis=1)

        ii = T.repeat(T.arange(neighborsForPooling.shape[0]), k)
        jj = kNeighborsArgSorted.flatten()
        pooledkmaxTmp = neighborsForPooling[ii, jj]

        # reshape pooledkmaxTmp
        new_shape = T.cast(T.join(0, conv_out.shape[:-2],
                           T.as_tensor([conv_out.shape[3]]),
                           T.as_tensor([k])),
                           'int32')
        pooled_out = T.reshape(pooledkmaxTmp, new_shape, ndim=4)
        
        # downsample each feature map individually, using maxpooling
        '''
        pooled_out = downsample.max_pool_2d(input=conv_out,
                                            ds=poolsize, ignore_border=True)
        '''
        # add the bias term. Since the bias is a vector (1D array), we first
        # reshape it to a tensor of shape (1,n_filters,1,1). Each bias will
        # thus be broadcasted across mini-batches and feature map
        # width & height
        self.output = T.tanh(pooled_out)
Exemplo n.º 47
0
    def setup(self, bottom, top):

        if len(bottom) != 2:
            raise Exception("The layer needs two inputs!")

        probs_tmp = T.ftensor4()
        stat_inp = T.ftensor4()

        stat = stat_inp[:, :, :, 1:]

        probs_bg = probs_tmp[:, 0, :, :]
        probs = probs_tmp[:, 1:, :, :]

        probs_max = T.max(probs, axis=3).max(axis=2)

        q_fg = 0.996
        probs_sort = T.sort(probs.reshape((-1, 20, 41 * 41)), axis=2)
        weights = np.array([q_fg ** i for i in range(41 * 41 - 1, -1, -1)])[None, None, :]
        Z_fg = np.sum(weights)
        weights = T.addbroadcast(theano.shared(weights), 0, 1)
        probs_mean = T.sum((probs_sort * weights) / Z_fg, axis=2)

        q_bg = 0.999
        probs_bg_sort = T.sort(probs_bg.reshape((-1, 41 * 41)), axis=1)
        weights_bg = np.array([q_bg ** i for i in range(41 * 41 - 1, -1, -1)])[None, :]
        Z_bg = np.sum(weights_bg)
        weights_bg = T.addbroadcast(theano.shared(weights_bg), 0)
        probs_bg_mean = T.sum((probs_bg_sort * weights_bg) / Z_bg, axis=1)

        stat_2d = stat[:, 0, 0, :] > 0.5

        loss_1 = -T.mean(T.sum((stat_2d * T.log(probs_mean) / T.sum(stat_2d, axis=1, keepdims=True)), axis=1))
        loss_2 = -T.mean(T.sum(((1 - stat_2d) * T.log(1 - probs_max) / T.sum(1 - stat_2d, axis=1, keepdims=True)), axis=1))
        loss_3 = -T.mean(T.log(probs_bg_mean))

        loss = loss_1 + loss_2 + loss_3

        self.forward_theano = theano.function([probs_tmp, stat_inp], loss)
        self.backward_theano = theano.function([probs_tmp, stat_inp], T.grad(loss, probs_tmp))
Exemplo n.º 48
0
    def kmaxpooling(self,input,k):

        sorted_values = T.argsort(input,axis=3)
        topmax_indexes = sorted_values[:,:,:,-k:]
        # sort indexes so that we keep the correct order within the sentence
        topmax_indexes_sorted = T.sort(topmax_indexes)

        #given that topmax only gives the index of the third dimension, we need to generate the other 3 dimensions
        dim0 = T.arange(0,self.input_shape[0]).repeat(self.input_shape[1]*self.input_shape[2]*k)
        dim1 = T.arange(0,self.input_shape[1]).repeat(k*self.input_shape[2]).reshape((1,-1)).repeat(self.input_shape[0],axis=0).flatten()
        dim2 = T.arange(0,self.input_shape[2]).repeat(k).reshape((1,-1)).repeat(self.input_shape[0]*self.input_shape[1],axis=0).flatten()
        dim3 = topmax_indexes_sorted.flatten()
        return input[dim0,dim1,dim2,dim3].reshape((self.input_shape[0], self.input_shape[1], self.input_shape[2], k))
Exemplo n.º 49
0
 def softmax(self, D, I):
   D = D * T.constant(self.attrs['sharpening'], 'float32')
   if self.attrs['norm'] == 'exp':
     E = T.exp(-D)
   elif self.attrs['norm'] == 'sigmoid':
     E = T.nnet.sigmoid(D)
   else:
     raise NotImplementedError()
   E = E * I
   if self.attrs['nbest'] > 0:
     opt = T.minimum(self.attrs['nbest'], E.shape[0])
     score = (T.sort(E, axis=0)[-opt]).dimshuffle('x',0).repeat(E.shape[0],axis=0)
     E = T.switch(T.lt(E,score), T.zeros_like(E), E)
   return E / T.maximum(T.sum(E,axis=0,keepdims=True),T.constant(1e-20,'float32'))
    def calculate_cost(self):
        output1 = self.model1.output
        output2 = self.model2.output

        #Median loss with cross-entropy
        distances = ((output1 - output2) ** 2).sum(axis=1)
        sorted_distances = T.sort(distances)
        median = (sorted_distances[BATCH_SIZE/2] + sorted_distances[BATCH_SIZE/2 - 1]) / 2.0

        p = distances[0:BATCH_SIZE:2]  #pairs
        q = distances[1:BATCH_SIZE:2]  #non-pairs

        loss = (T.log(1.0 + T.exp(-60.0*(q - median)))).mean() + \
            (T.log(1.0 + T.exp(-60.0*(median - p)))).mean()          #cross-entropy
        return loss
Exemplo n.º 51
0
 def k_max_pool(conv, k):
     c_shape = conv.shape
     # c_shape = tPrint('conv_shape')(c_shape)
     pool_size = (1, conv.shape[-1])
     neighbors_to_pool = TSN.images2neibs(ten4=conv,
                                          neib_shape=pool_size,
                                          mode='ignore_borders')
     arg_sorted = T.argsort(neighbors_to_pool, axis=1)
     top_k = arg_sorted[:, -k:]
     top_k_sorted = T.sort(top_k, axis=1)
     ii = T.repeat(T.arange(neighbors_to_pool.shape[0], dtype='int32'), k)
     jj = top_k_sorted.flatten()
     values = neighbors_to_pool[ii, jj]
     pooled_out = T.reshape(values, (c_shape[0], c_shape[1], c_shape[2], k), ndim=4)
     return pooled_out
Exemplo n.º 52
0
def k_max_pooling(input, kmax):
  nbatches, nchannels, nwords, ndim = input.shape[0], input.shape[1], input.shape[2], input.shape[3]
  x = input.dimshuffle(0,1,3,2)
  neighborsArgSorted = T.argsort(x, axis=3)
  ax0 = T.repeat(T.arange(nbatches), nchannels*ndim*kmax)
  ax1 = T.repeat(T.arange(nchannels), ndim * kmax).dimshuffle('x', 0)
  ax1 = T.repeat(ax1, nbatches, axis=0).flatten()
  ax2 = T.repeat(T.arange(ndim), kmax, axis=0).dimshuffle('x', 'x', 0)
  ax2 = T.repeat(ax2, nchannels, axis=1)
  ax2 = T.repeat(ax2, nbatches, axis=0).flatten()
  ax3 = T.sort(neighborsArgSorted[:,:,:,-kmax:], axis=3).flatten()

  pooled_out = x[ax0, ax1, ax2, ax3]
  pooled_out = pooled_out.reshape((nbatches, nchannels, ndim, kmax)).dimshuffle(0,1,3,2)
  return pooled_out
Exemplo n.º 53
0
    def extract_ind_app(lookup):
        assert isinstance(lookup, LookupTable)
        param = lookup.W
        branches = VariableFilter(bricks=[lookup], name='output_0')(cg)
        assert all([
            isinstance(branch.owner.inputs[0].owner.inputs[0].owner.op,
                       tensor.subtensor.AdvancedSubtensor1)
            for branch in branches])
        outputs = [branch.owner.inputs[0].owner.inputs[0] for branch in branches]
        indices = [branch.owner.inputs[0].owner.inputs[0].owner.inputs[1] for branch in branches]
        canonized_indices = tensor.concatenate(indices, axis=0)
        canonized_indices = canonized_indices % lookup.length # Replace -1 with lookup.length - 1
        canonized_indices = tensor.extra_ops.Unique()(tensor.sort(canonized_indices))

        subparam = param[canonized_indices]
        return {param: (subparam, canonized_indices, outputs, indices)}
Exemplo n.º 54
0
def hard_bootstrapping_binary_crossentropy(pred,
                                           target,
                                           num_taken,
                                           num_skipped=0,
                                           weight=1):
    """
    from
    High-performance Semantic Segmentation Using Very Deep Fully Convolutional Networks
    http://arxiv.org/abs/1604.04339
    """
    pixel_loss = treeano.utils.weighted_binary_crossentropy(pred,
                                                            target,
                                                            weight=weight)
    flat_loss = pixel_loss.flatten(2)
    sorted_flat_loss = T.sort(flat_loss)
    chosen_loss = sorted_flat_loss[:, -(num_taken + num_skipped):-num_skipped]
    return chosen_loss
Exemplo n.º 55
0
    def fprop(self, inputs):

        # format inputs
        inputs = self.input_space.format_as(inputs, self.mlp.input_space)
        rval = self.mlp.fprop(inputs)
        if self.pooling_mode == 0:
            rval = tensor.max(rval, axis=0)
        elif self.pooling_mode == 1:
            rval = block_gradient(tensor.sort(rval, axis=0))[-3:][::-1]
            rval = tensor.sum(rval * self.probs, axis=0) / 3
            #rval = tensor.mean(rval, axis=0)
        else:
            raise Exception("Others are not implemented yet!")
        rval = rval.dimshuffle('x', 0)
        rval = self.final_layer.fprop(rval)

        return rval