コード例 #1
0
 def backward(self, top, propagate_down, bottom):
     with pu.caffe_cuda_context():
         h = caffe.cublas_handle()
         import scikits.cuda.linalg as linalg
         top_diff = top[0].diff_as_pycuda_gpuarray()
         ts = [self.t1_, self.t2_]
         for i in xrange(len(bottom)):
             if not propagate_down[i]:
                 continue
             diff = bottom[i].diff_as_pycuda_gpuarray()
             data = bottom[(i + 1) % 2].data_as_pycuda_gpuarray()
             # Belew 3 conditions are complicated and might be hard to
             # understand.
             swap = ts[i] ^ bool(i)
             t1 = ts[i]
             t2 = (not t1) ^ ts[(i + 1) % 2]
             for b in xrange(bottom[0].shape[0]):
                 x = top_diff[b]
                 y = data[b]
                 t1_, t2_ = t1, t2
                 if swap:
                     x, y = y, x
                     t1_, t2_ = t2_, t1_
                 linalg.dot(x,
                            y,
                            transa=blas_trans(t1_),
                            transb=blas_trans(t2_),
                            handle=h,
                            out=diff[b])
コード例 #2
0
    def forward(self, bottom, top):
        """

        """
        with pu.caffe_cuda_context():
            h = caffe.cublas_handle()
            batch_size = bottom[0].shape[0]
            dim = bottom[0].count / bottom[0].shape[0]
            pred = bottom[0].data_as_pycuda_gpuarray()
            label = bottom[1].data_as_pycuda_gpuarray()
            mask = bottom[2].data_as_pycuda_gpuarray()
            # Use bottom[0,1].diff as temporary buffer
            diff = bottom[0].diff_as_pycuda_gpuarray()
            diff2 = bottom[1].diff_as_pycuda_gpuarray()
            # Compute diff
            self.k_masked_diff_(diff, pred, label, mask)
            self.k_squared_(diff, diff2)
            import scikits.cuda.linalg as linalg
            # This needs scikits.cuda 0.5.0a3 or later
            # (sudo) pip install scikits.cuda=>0.5.0a3
            linalg.dot(diff.reshape(batch_size, dim), self.multipier_sum_,
                       handle=h, out=self.diff_sum_)
            linalg.dot(diff2.reshape(batch_size, dim), self.multipier_sum_,
                       handle=h, out=self.diff2_sum_)
            linalg.dot(mask.reshape(batch_size, dim), self.multipier_sum_,
                       handle=h, out=self.mask_sum_)
            self.k_ensure_mask_sum_(self.mask_sum_)
            term1 = self.k_div_sum_(self.diff2_sum_, self.mask_sum_)
            term2 = self.k_div_squared_sum_(self.diff_sum_, self.mask_sum_)
            top[0].data[...] = (term1.get() - self.lambda_ * term2.get()) \
                / batch_size
コード例 #3
0
 def backward(self, top, propagate_down, bottom):
     with pu.caffe_cuda_context():
         h = caffe.cublas_handle()
         import scikits.cuda.linalg as linalg
         top_diff = top[0].diff_as_pycuda_gpuarray()
         ts = [self.t1_, self.t2_]
         for i in xrange(len(bottom)):
             if not propagate_down[i]:
                 continue
             diff = bottom[i].diff_as_pycuda_gpuarray()
             data = bottom[(i + 1) % 2].data_as_pycuda_gpuarray()
             # Belew 3 conditions are complicated and might be hard to
             # understand.
             swap = ts[i] ^ bool(i)
             t1 = ts[i]
             t2 = (not t1) ^ ts[(i + 1) % 2]
             for b in xrange(bottom[0].shape[0]):
                 x = top_diff[b]
                 y = data[b]
                 t1_, t2_ = t1, t2
                 if swap:
                     x, y = y, x
                     t1_, t2_ = t2_, t1_
                 linalg.dot(x, y,
                            transa=blas_trans(t1_), transb=blas_trans(t2_),
                            handle=h, out=diff[b])
コード例 #4
0
 def forward(self, bottom, top):
     with pu.caffe_cuda_context():
         h = caffe.cublas_handle()
         import scikits.cuda.linalg as linalg
         mat1 = bottom[0].data_as_pycuda_gpuarray()
         mat2 = bottom[1].data_as_pycuda_gpuarray()
         mato = top[0].data_as_pycuda_gpuarray()
         for b in xrange(bottom[0].shape[0]):
             linalg.dot(mat1[b], mat2[b],
                        transa=blas_trans(self.t1_),
                        transb=blas_trans(self.t2_),
                        handle=h, out=mato[b])
コード例 #5
0
 def forward(self, bottom, top):
     with pu.caffe_cuda_context():
         h = caffe.cublas_handle()
         import scikits.cuda.linalg as linalg
         mat1 = bottom[0].data_as_pycuda_gpuarray()
         mat2 = bottom[1].data_as_pycuda_gpuarray()
         mato = top[0].data_as_pycuda_gpuarray()
         for b in xrange(bottom[0].shape[0]):
             linalg.dot(mat1[b],
                        mat2[b],
                        transa=blas_trans(self.t1_),
                        transb=blas_trans(self.t2_),
                        handle=h,
                        out=mato[b])
コード例 #6
0
    def forward(self, bottom, top):
        """

        """
        with pu.caffe_cuda_context():
            h = caffe.cublas_handle()
            batch_size = bottom[0].shape[0]
            dim = bottom[0].count / bottom[0].shape[0]
            pred = bottom[0].data_as_pycuda_gpuarray()
            label = bottom[1].data_as_pycuda_gpuarray()
            # Use bottom[0,1].diff as temporary buffer
            diff = bottom[0].diff_as_pycuda_gpuarray()
            diff2 = bottom[1].diff_as_pycuda_gpuarray()
            mask = bottom[0].diff_as_pycuda_gpuarray()
            # Compute diff
            self.k_masked_diff_(diff, pred, label)
            self.k_squared_(diff, diff2)
            import scikits.cuda.linalg as linalg
            # This needs scikits.cuda 0.5.0a3 or later
            # (sudo) pip install scikits.cuda=>0.5.0a3
            linalg.dot(diff.reshape(batch_size, dim),
                       self.multipier_sum_,
                       handle=h,
                       out=self.diff_sum_)
            linalg.dot(diff2.reshape(batch_size, dim),
                       self.multipier_sum_,
                       handle=h,
                       out=self.diff2_sum_)
            mask.fill(dtype(1.0))
            linalg.dot(mask.reshape(batch_size, dim),
                       self.multipier_sum_,
                       handle=h,
                       out=self.mask_sum_)
            self.k_ensure_mask_sum_(self.mask_sum_)
            term1 = self.k_div_sum_(self.diff2_sum_, self.mask_sum_)
            term2 = self.k_div_squared_sum_(self.diff_sum_, self.mask_sum_)
            top[0].data[...] = (term1.get() - self.lambda_ * term2.get()) \
                / batch_size