def garray_to_cudandarray_nested(lst): lst_flat = flatten(lst) lst_flat = [gput.garray_to_cudandarray(i) if not isinstance(i, theano.sandbox.cuda.CudaNdarray) else i for i in lst_flat] lst = unflatten(lst, lst_flat) return lst
def garray_to_cudandarray_nested(lst): lst_flat = flatten(lst) lst_flat = [ gput.garray_to_cudandarray(i) if not isinstance(i, theano.sandbox.cuda.CudaNdarray) else i for i in lst_flat ] lst = unflatten(lst, lst_flat) return lst
def Run_fmin_cg(self, maxiter): x0 = self.classifier.flat_params.get_value(borrow=True, return_internal_type=True) x0 = gnumpy_utils.cudandarray_to_garray(x0) f_op, params_opt = fmin_cg(f=self.ObjFun, x0=x0, fprime=self.ObjFunPrime, maxiter=maxiter) params_opt = gnumpy_utils.garray_to_cudandarray(params_opt) self.classifier.flat_params.set_value(params_opt)
def garray_to_cudandarray_nested(lst): #print "sizeof(lst): " + str(getsizeof(lst)) for idx,i in enumerate(lst): lst[idx].shape = tuple(int(q) for q in i.shape) lst_flat = flatten(lst) #print "len(lst_flat[0]): " + str(len(lst_flat[0])) #print "len(lst_flat[1]): " + str(len(lst_flat[1])) #print "len(lst_flat[2]): " + str(len(lst_flat[2])) #print "Size of vector in garray_to_cudandarray_nested:" + str(len(lst)) #print "Contents lst_flat[0]: " + str(lst[0].shape) #print "Contents lst_flat[1]: " + str(lst[1].shape) #print "Contents lst_flat[2]: " + str(lst[2].shape) #lst_flat = [gput.garray_to_cudandarray(i) for i in lst_flat[1:len(lst_flat)]] lst_flat = [gput.garray_to_cudandarray(i) for i in lst_flat] # #for idx,i in enumerate(lst_flat): # lst[idx].shape = tuple(int(q) for q in i.shape) # if lst_flat[idx].stride != None: # lst_flat[idx].stride = tuple(int(q) for q in i.stride) # print "Stride was long.\n" # else: # print "Stride was None.\n" # print "sizeof(i): " + str(getsizeof(i)) # tmp = [] # for i in lst_flat: # print "garray_to_cudandarray_nested:i.shape:" + str(i.shape) # tmp_ptr = gput.garray_to_cudandarray(i) # print "garray_to_cudandarray_nested:returned ptr:" + str(tmp_ptr) # print "garray_to_cudandarray_nested:returned ptr:" + str(tmp_ptr.shape) # # tmp.append(tmp_ptr) # print "loop" #for i in lst_flat: # print "sizeof(i): " + str(getsizeof(i)) #print "len(lst_flat[0]): " + str(len(lst_flat[0])) #print "len(lst_flat[1]): " + str(len(lst_flat[1])) #print "len(lst_flat[2]): " + str(len(lst_flat[2])) lst = unflatten(lst, lst_flat) #print "here" # lst = unflatten(lst, tmp) # print "here1" return lst
def test(shape=(3,4,5)): """ Make sure that the gnumpy conversion is exact. """ gpu = theano.sandbox.cuda.basic_ops.gpu_from_host U = gpu(theano.tensor.ftensor3('U')) ii = theano.function([U], gpu(U+1)) A = gnumpy.rand(*shape) A_cnd = garray_to_cudandarray(A) B_cnd = ii(A_cnd) B = cudandarray_to_garray(B_cnd) from numpy import array B2 = array(B_cnd) u = (A+1).asarray() v = B.asarray() w = B2 assert abs(u-v).max() == 0 assert abs(u-w).max() == 0
def test(shape=(3, 4, 5)): """ Make sure that the gnumpy conversion is exact from garray to CudaNdarray back to garray. """ gpu = theano.sandbox.cuda.basic_ops.gpu_from_host U = gpu(theano.tensor.ftensor3('U')) ii = theano.function([U], gpu(U + 1)) A = gnumpy.rand(*shape) A_cnd = garray_to_cudandarray(A) assert A_cnd.shape == A.shape # dtype always float32 # garray don't have strides B_cnd = ii(A_cnd) B = cudandarray_to_garray(B_cnd) assert A_cnd.shape == A.shape u = (A + 1).asarray() v = B.asarray() w = np.array(B_cnd) assert (u == v).all() assert (u == w).all()
def test2(shape=(3, 4, 5)): """ Make sure that the gnumpy conversion is exact from CudaNdarray to garray back to CudaNdarray. """ gpu = theano.sandbox.cuda.basic_ops.gpu_from_host U = gpu(theano.tensor.ftensor3('U')) theano.function([U], gpu(U + 1)) A = np.random.rand(*shape).astype('float32') A_cnd = theano.sandbox.cuda.CudaNdarray(A) A_gar = cudandarray_to_garray(A_cnd) assert A_cnd.shape == A_gar.shape # dtype always float32 # garray don't have strides B = garray_to_cudandarray(A_gar) assert A_cnd.shape == B.shape # dtype always float32 assert A_cnd._strides == B._strides assert A_cnd.gpudata == B.gpudata v = np.asarray(B) assert (v == A).all()
def garray_to_cudandarray_nested(lst): lst_flat = flatten(lst) lst_flat = [gput.garray_to_cudandarray(i) for i in lst_flat] lst = unflatten(lst, lst_flat) return lst
def garray_to_cudandarray_nested(lst): lst_flat = flatten(lst) lst_flat = [gput.garray_to_cudandarray(i) for i in lst_flat] lst = unflatten(lst, lst_flat) return lst
def ObjFunPrime(self, params): params = gnumpy_utils.garray_to_cudandarray(params) grad = self.grad_Tfunc(params) return gnumpy_utils.cudandarray_to_garray(grad)
def ObjFun(self, params): params = gnumpy_utils.garray_to_cudandarray(params) return self.get_cost_at_x(params)