def copy_tensor(dst_tensor, src_tensor): """Copy the content from src_tensor to dst_tensor. Args: dst_tensor: the tensor to copy from. src_tensor: the tensor to copy to. Returns: None """ copied = True if isinstance(dst_tensor, cupy.ndarray) \ and isinstance(src_tensor, cupy.ndarray): cupy.copyto(dst_tensor, src_tensor) elif torch_available(): if isinstance(dst_tensor, torch.Tensor) and isinstance( src_tensor, torch.Tensor): dst_tensor.copy_(src_tensor) elif isinstance(dst_tensor, torch.Tensor) and isinstance( src_tensor, cupy.ndarray): t = torch.utils.dlpack.from_dlpack(src_tensor.toDlpack()) dst_tensor.copy_(t) elif isinstance(dst_tensor, cupy.ndarray) and isinstance( src_tensor, torch.Tensor): t = cupy.fromDlpack(torch.utils.dlpack.to_dlpack(src_tensor)) cupy.copyto(dst_tensor, t) else: copied = False else: copied = False if not copied: raise ValueError( "Unsupported tensor type. Got: {} and {}. Supported " "GPU tensor types are: torch.Tensor, cupy.ndarray.".format( type(dst_tensor), type(src_tensor)))
def copy(array, out=None, out_device=None, stream=None): """Copies a :class:`cupy.ndarray` object using the default stream. This function can copy the device array to the destination array on another device. Args: array (cupy.ndarray): Array to be copied. out (cupy.ndarray): Destination array. If it is not ``None``, then ``out_device`` argument is ignored. out_device: Destination device specifier. Actual device object is obtained by passing this value to :func:`get_device`. stream (cupy.cuda.Stream): CUDA stream. Returns: cupy.ndarray: Copied array. If ``out`` is not specified, then the array is allocated on the device specified by ``out_device`` argument. """ check_cuda_available() assert stream is None # TODO(beam2d): FIX IT if out is None: if out_device is None: out_device = array with get_device(out_device): out = cupy.empty_like(array) with get_device(array): cupy.copyto(out, array) return out
def copyto(dst, src): """Copies the elements of an ndarray to those of another one. This function can copy the CPU/GPU arrays to the destination arrays on another device. Args: dst (numpy.ndarray or cupy.ndarray): Destination array. src (numpy.ndarray or cupy.ndarray): Source array. """ if isinstance(dst, numpy.ndarray): numpy.copyto(dst, to_cpu(src)) elif isinstance(dst, ndarray): if isinstance(src, numpy.ndarray): if dst.flags.c_contiguous or dst.flags.f_contiguous: dst.set(src) else: cupy.copyto(dst, to_gpu(src, device=dst.device)) elif isinstance(src, ndarray): cupy.copyto(dst, src) else: raise TypeError( 'cannot copy from non-array object of type {}'.format( type(src))) else: raise TypeError('cannot copy to non-array object of type {}'.format( type(dst)))
def full(shape, fill_value, dtype=None, order='C'): """Returns a new array of given shape and dtype, filled with a given value. This function currently does not support ``order`` option. Args: shape (int or tuple of ints): Dimensionalities of the array. fill_value: A scalar value to fill a new array. dtype: Data type specifier. order ({'C', 'F'}): Row-major (C-style) or column-major (Fortran-style) order. Returns: cupy.ndarray: An array filled with ``fill_value``. .. seealso:: :func:`numpy.full` """ if dtype is None: if isinstance(fill_value, cupy.ndarray): dtype = fill_value.dtype else: dtype = numpy.array(fill_value).dtype a = cupy.ndarray(shape, dtype, order=order) cupy.copyto(a, fill_value, casting='unsafe') return a
def clip(self, indices, in_place=False, remove=False): ''' Removes the items of list, header, and data that do not have index in the given indices. Parameters ---------- indices : array-like indices of items to leave in_place : bool, default False If True, perform in-place array manipulations for ndarray. This saves memory, but the values of the original ndarray are not preserved. ''' if remove: indices = sorted(set(range(len(self))) - set(indices)) else: indices = sorted(indices) list = self.list head = self.header data = self.data self.list = (list[i] for i in indices) self.header = (head[i] for i in indices) tmpd = data[:len(indices)] if not in_place: tmpd = cp.empty_like(tmpd) for dst, j in zip(tmpd, indices): cp.copyto(dst, data[j]) self.data = tmpd
def full_like(a, fill_value, dtype=None, order='K', subok=None, shape=None): """Returns a full array with same shape and dtype as a given array. This function currently does not support ``subok`` option. Args: a (cupy.ndarray): Base array. fill_value: A scalar value to fill a new array. dtype: Data type specifier. The dtype of ``a`` is used by default. order ({'C', 'F', 'A', or 'K'}): Overrides the memory layout of the result. ``'C'`` means C-order, ``'F'`` means F-order, ``'A'`` means ``'F'`` if ``a`` is Fortran contiguous, ``'C'`` otherwise. ``'K'`` means match the layout of ``a`` as closely as possible. subok: Not supported yet, must be None. shape (int or tuple of ints): Overrides the shape of the result. If ``order='K'`` and the number of dimensions is unchanged, will try to keep order, otherwise, ``order='C'`` is implied. Returns: cupy.ndarray: An array filled with ``fill_value``. .. seealso:: :func:`numpy.full_like` """ if subok is not None: raise TypeError('subok is not supported yet') if dtype is None: dtype = a.dtype order, strides, memptr = _new_like_order_and_strides( a, dtype, order, shape) shape = shape if shape else a.shape a = cupy.ndarray(shape, dtype, memptr, strides, order) cupy.copyto(a, fill_value, casting='unsafe') return a
def fn(): with cupy.cuda.Device(node.device_id): out = chainer.functions.average_pooling_2d(x, kernel_size, stride=stride, pad=padding).array cupy.copyto(y, out)
def _check_copyto_where_multigpu_raises(self, dtype, ngpus): def get_numpy(): a = testing.shaped_arange((2, 3, 4), numpy, dtype) b = testing.shaped_reverse_arange((2, 3, 4), numpy, dtype) c = testing.shaped_arange((2, 3, 4), numpy, '?') numpy.copyto(a, b, where=c) return a for dev1, dev2, dev3, dev4 in itertools.product(*[range(ngpus)] * 4): if dev1 == dev2 == dev3 == dev4: continue if not dev1 <= dev2 <= dev3 <= dev4: continue with cuda.Device(dev1): a = testing.shaped_arange((2, 3, 4), cupy, dtype) with cuda.Device(dev2): b = testing.shaped_reverse_arange((2, 3, 4), cupy, dtype) with cuda.Device(dev3): c = testing.shaped_arange((2, 3, 4), cupy, '?') with cuda.Device(dev4): with self.assertRaisesRegex( ValueError, '^Array device must be same as the current device'): cupy.copyto(a, b, where=c)
def _check_copyto_where_multigpu_raises(self, dtype, ngpus): def get_numpy(): a = testing.shaped_arange((2, 3, 4), numpy, dtype) b = testing.shaped_reverse_arange((2, 3, 4), numpy, dtype) c = testing.shaped_arange((2, 3, 4), numpy, '?') numpy.copyto(a, b, where=c) return a for dev1, dev2, dev3, dev4 in itertools.product(*[range(ngpus)] * 4): if dev1 == dev2 == dev3 == dev4: continue if not dev1 <= dev2 <= dev3 <= dev4: continue with cuda.Device(dev1): a = testing.shaped_arange((2, 3, 4), cupy, dtype) with cuda.Device(dev2): b = testing.shaped_reverse_arange((2, 3, 4), cupy, dtype) with cuda.Device(dev3): c = testing.shaped_arange((2, 3, 4), cupy, '?') with cuda.Device(dev4): if all([(peer == dev4) or (cuda.runtime.deviceCanAccessPeer(dev4, peer) == 1) for peer in (dev1, dev2, dev3)]): with pytest.warns(cupy._util.PerformanceWarning): cupy.copyto(a, b, where=c) else: with pytest.raises(ValueError, match='Peer access is unavailable'): cupy.copyto(a, b, where=c)
def copy(array, out=None, out_device=None, stream=None): """Copies a cupy.ndarray object using the default stream. This function can copy the device array to the destination array on another device. Args: array (cupy.ndarray): Array to be copied. out (cupy.ndarray): Destination array. If it is not ``None``, then ``out_device`` argument is ignored. out_device: Destination device specifier. Actual device object is obtained by passing this value to :func:`get_device`. stream (cupy.cuda.Stream): CUDA stream. Returns: cupy.ndarray: Copied array. If ``out`` is not specified, then the array is allocated on the device specified by ``out_device`` argument. """ check_cuda_available() assert stream is None # TODO(beam2d): FIX IT if out is None: if out_device is None: out_device = array with get_device(out_device): out = cupy.empty_like(array) with get_device(array): cupy.copyto(out, array) return out
def copy_array(dst, src, casting='same_kind', where=None): if isinstance(dst, numpy.ndarray) and isinstance(src, numpy.ndarray): dst[:] = src elif isinstance(dst, cupy.ndarray): src = cupy.array(src, copy=False) cupy.copyto(dst, src) else: numpy.copyto(dst, src)
def copy_array(dst, src, casting="same_kind", where=None): if isinstance(dst, numpy.ndarray) and isinstance(src, numpy.ndarray): dst[:] = src elif is_cupy_array(dst): src = cupy.array(src, copy=False) cupy.copyto(dst, src) else: numpy.copyto(dst, src)
def copyto(dst, src, casting='same_kind', where=None): if isinstance(dst, np.ndarray) and isinstance(src, np.ndarray): dst[:] = src elif isinstance(dst, cupy.ndarray): src = cupy.array(src, copy=False) cupy.copyto(dst, src) else: np.copyto(dst, src)
def copy_array(dst: ArrayXd, src: ArrayXd) -> None: # pragma: no cover if isinstance(dst, numpy.ndarray) and isinstance(src, numpy.ndarray): dst[:] = src elif is_cupy_array(dst): src = cupy.array(src, copy=False) cupy.copyto(dst, src) else: numpy.copyto(dst, src)
def _copy(self, dst, src): for col in src.keys(): if col in dst: dst_col = self._sr_data_to_device_ary(dst[col]) src_col = self._sr_data_to_device_ary(src[col]) cupy.copyto(cupy.asarray(dst_col), cupy.asarray(src_col)) else: dst[col] = src[col].copy(True) return dst
def unwrap(p, discont=None, axis=-1, *, period=2 * numpy.pi): r"""Unwrap by taking the complement of large deltas w.r.t. the period. This unwraps a signal `p` by changing elements which have an absolute difference from their predecessor of more than ``max(discont, period/2)`` to their `period`-complementary values. For the default case where `period` is :math:`2\pi` and is ``discont`` is :math:`\pi`, this unwraps a radian phase `p` such that adjacent differences are never greater than :math:`\pi` by adding :math:`2k\pi` for some integer :math:`k`. Args: p (cupy.ndarray): Input array. discont (float): Maximum discontinuity between values, default is ``period/2``. Values below ``period/2`` are treated as if they were ``period/2``. To have an effect different from the default, ``discont`` should be larger than ``period/2``. axis (int): Axis along which unwrap will operate, default is the last axis. period: float, optional Size of the range over which the input wraps. By default, it is :math:`2\pi`. Returns: cupy.ndarray: The result array. .. seealso:: :func:`numpy.unwrap` """ p = cupy.asarray(p) nd = p.ndim dd = sumprod.diff(p, axis=axis) if discont is None: discont = period / 2 slice1 = [slice(None, None)] * nd # full slices slice1[axis] = slice(1, None) slice1 = tuple(slice1) dtype = numpy.result_type(dd.dtype, period) if numpy.issubdtype(dtype, numpy.integer): interval_high, rem = divmod(period, 2) boundary_ambiguous = rem == 0 else: interval_high = period / 2 boundary_ambiguous = True interval_low = -interval_high ddmod = cupy.mod(dd - interval_low, period) + interval_low if boundary_ambiguous: cupy.copyto(ddmod, interval_high, where=(ddmod == interval_low) & (dd > 0)) ph_correct = ddmod - dd cupy.copyto(ph_correct, 0, where=abs(dd) < discont) up = cupy.array(p, copy=True, dtype=dtype) up[slice1] = p[slice1] + cupy.cumsum(ph_correct, axis=axis) return up
def memcpy_htod(self, dest, src): """perform a host to device memory copy :param dest: A GPU memory allocation unit :type dest: cupy.ndarray :param src: A numpy array in host memory to store the data :type src: numpy.ndarray """ if isinstance(src, np.ndarray): src = cp.asarray(src) cp.copyto(dest, src)
def rmm_cupy_ary(cupy_fn, *args, **kwargs): """ Function to call CuPy functions with RMM memory management Parameters ---------- cupy_fn : cupy function, CuPy function to execute, for example cp.array *args : Non keyword arguments to pass to the CuPy function **kwargs : Keyword named arguments to pass to the CuPy function Note: this function should be used if the result of cupy_fn creates a new array. Functions to create a new CuPy array by reference to existing device array (through __cuda_array_interface__) can be used directly. Examples --------- .. code-block:: python from cuml.utils import rmm_cupy_ary import cupy as cp # Get a new array filled with 0, column major a = rmm_cupy_ary(cp.zeros, 5, order='F') """ # using_allocator was introduced in CuPy 7. Once 7+ is required, # this check can be removed alongside the else code path. if check_min_cupy_version("7.0"): with cp.cuda.memory.using_allocator(rmm.rmm_cupy_allocator): result = cupy_fn(*args, **kwargs) else: temp_res = cupy_fn(*args, **kwargs) result = \ _rmm_cupy6_array_like(temp_res, order=_strides_to_order(temp_res.strides, temp_res.dtype)) cp.copyto(result, temp_res) return result
def test_copyto_multigpu_noncontinguous(self, dtype): with cuda.Device(0): src = testing.shaped_arange((2, 3, 4), cupy, dtype) src = src.swapaxes(0, 1) with cuda.Device(1): dst = cupy.empty_like(src) cupy.copyto(dst, src) expected = testing.shaped_arange((2, 3, 4), numpy, dtype) expected = expected.swapaxes(0, 1) testing.assert_array_equal(expected, src.get()) testing.assert_array_equal(expected, dst.get())
def fn(): with cupy.cuda.Device(node.device_id): # Unfortunately we need to convert this cupy array to a tuple # this will force a copy back to host :( # TODO: support types on the frontend so we don't have to convert # here shape_tuple = list(cupy.asnumpy(shape).astype(np.int64)) # correct shape tuple to match batch size shape_tuple[0] = data.shape[0] shape_tuple = tuple(shape_tuple) cupy.copyto(reshaped, chainer.functions.reshape(data, shape_tuple).array)
def memcpy_dtoh(self, dest, src): """perform a device to host memory copy :param dest: A numpy array in host memory to store the data :type dest: numpy.ndarray :param src: A GPU memory allocation unit :type src: cupy.ndarray """ if isinstance(dest, np.ndarray): tmp = cp.asnumpy(src) np.copyto(dest, tmp) elif isinstance(dest, cp.ndarray): cp.copyto(dest, src) else: raise ValueError("dest type not supported")
def fn(): # time_st = datetime.datetime.now() if tz == numpy.ndarray: # to cpu np.copyto(z, cupy.asnumpy(x)) # assert cupy.testing.assert_array_equal(z,x) if tz == cupy.core.core.ndarray and tx != cupy.core.core.ndarray: # to gpu with cupy.cuda.Device(node.device_id): cupy.add(cupy.asarray(x), 0, out=z) if tz == cupy.core.core.ndarray and tx == cupy.core.core.ndarray: # to gpu tmp = None with cupy.cuda.Device(source_device_id): tmp = cupy.asnumpy(x) with cupy.cuda.Device(target_device_id): cupy.copyto(z, cupy.asarray(tmp))
def test_copy(self): c = Config(None, None, 4, 4) size = (4,3,224,224) io_in = InOut("in", "static", np.random.random(size), size) io_gpu = InOut("gpu", "dynamic", None, size) io_return = InOut("return", "dynamic", None, size) with cupy.cuda.Device(0): gpu_buffer = cupy.ndarray((size)) am = {"gpu": gpu_buffer, "return": np.ndarray((size))} inp_c0 = {"X": io_in} oup_c0 = {"Z": io_gpu} inp_c1 = {"X": io_gpu} oup_c1 = {"Z": io_return} c0 = Node(0, ops.O2P_COPY, inp_c0, oup_c0, {}, 0) c0.device_id = 0 c1 = Node(0, ops.O2P_COPY, inp_c1, oup_c1, {}, 0) c1.device_id = 0 fn_c0 = kernels.copy(c0, am, c) fn_c1 = kernels.copy(c1, am, c) #copy to gpu fn_c0() #execute +1 cupy.copyto(gpu_buffer,gpu_buffer + 1) #copy back fn_c1() ref_plus_one = io_in.get_data(am) + 1 cupy.testing.assert_array_equal(io_gpu.get_data(am), ref_plus_one) np.testing.assert_equal(io_return.get_data(am), ref_plus_one)
def test(self): test_images, test_labels = self.load_test() # Set test sample size self.SAMPLES_TEST = test_labels.shape[0] # Bias #test_inputs = np.c_[test_images, np.ones(self.SAMPLES_TEST)] # NOTE: Need to add bias for cupy test_inputs = np.column_stack((test_images, np.ones(self.SAMPLES_TEST))) # Copies test_target_array = np.zeros(test_labels.shape) np.copyto(test_target_array, test_labels) # Init predictions array test_predictions = np.zeros((self.SAMPLES_TEST,self.NEURONS)) # Test samples but this time only with forward prop for N in range(self.SAMPLES_TEST): test_o, test_tar_k, test_prediction_n = self.forward(test_inputs[N], test_target_array[N]) test_predictions[N] = test_prediction_n self.CORRECT_TEST_CONF, acc = self.get_confusion(test_target_array, test_predictions) self.CORRECT_TEST.append(acc)
def unwrap(p, discont=numpy.pi, axis=-1): """Unwrap by changing deltas between values to 2*pi complement. Args: p (cupy.ndarray): Input array. discont (float): Maximum discontinuity between values, default is ``pi``. axis (int): Axis along which unwrap will operate, default is the last axis. Returns: cupy.ndarray: The result array. .. seealso:: :func:`numpy.unwrap` """ p = cupy.asarray(p) nd = p.ndim dd = diff(p, axis=axis) slice1 = [slice(None, None)] * nd # full slices slice1[axis] = slice(1, None) slice1 = tuple(slice1) ddmod = cupy.mod(dd + numpy.pi, 2 * numpy.pi) - numpy.pi cupy.copyto(ddmod, numpy.pi, where=(ddmod == -numpy.pi) & (dd > 0)) ph_correct = ddmod - dd cupy.copyto(ph_correct, 0, where=cupy.abs(dd) < discont) up = cupy.array(p, copy=True, dtype='d') up[slice1] = p[slice1] + cupy.cumsum(ph_correct, axis=axis) return up
def fn(): with cupy.cuda.Device(node.device_id): if opt_mask: o, m = chainer.functions.dropout(data, ratio=ratio, return_mask=True) cupy.copyto(output, o.array) cupy.copyto(opt_mask, m.array) else: cupy.copyto(output, chainer.functions.dropout(data, ratio=ratio).array)
def select(condlist, choicelist, default=0): """Return an array drawn from elements in choicelist, depending on conditions. Args: condlist (list of bool arrays): The list of conditions which determine from which array in `choicelist` the output elements are taken. When multiple conditions are satisfied, the first one encountered in `condlist` is used. choicelist (list of cupy.ndarray): The list of arrays from which the output elements are taken. It has to be of the same length as `condlist`. default (scalar) : If provided, will fill element inserted in `output` when all conditions evaluate to False. default value is 0. Returns: cupy.ndarray: The output at position m is the m-th element of the array in `choicelist` where the m-th element of the corresponding array in `condlist` is True. .. seealso:: :func:`numpy.select` """ if len(condlist) != len(choicelist): raise ValueError( 'list of cases must be same length as list of conditions') if len(condlist) == 0: raise ValueError("select with an empty condition list is not possible") if not cupy.isscalar(default): raise TypeError("default only accepts scalar values") for i in range(len(choicelist)): if not isinstance(choicelist[i], cupy.ndarray): raise TypeError("choicelist only accepts lists of cupy ndarrays") cond = condlist[i] if cond.dtype.type is not cupy.bool_: raise ValueError( 'invalid entry {} in condlist: should be boolean ndarray'. format(i)) dtype = cupy.result_type(*choicelist) condlist = cupy.broadcast_arrays(*condlist) choicelist = cupy.broadcast_arrays(*choicelist, default) if choicelist[0].ndim == 0: result_shape = condlist[0].shape else: result_shape = cupy.broadcast_arrays(condlist[0], choicelist[0])[0].shape result = cupy.empty(result_shape, dtype) cupy.copyto(result, default) choicelist = choicelist[-2::-1] condlist = condlist[::-1] for choice, cond in zip(choicelist, condlist): cupy.copyto(result, choice, where=cond) return result
def fn(): with cupy.cuda.Device(node.device_id): cupy.copyto( output, chainer.functions.clip(inp, min_data[0], max_data[0]).array)
def fixpix(data,mask,out=None,dtype=None,fix_NaN=False): ''' fill the bad pixel with linear interpolation using surrounding pixels Parameters ---------- data : array-like An array of image If a 3D array containing multiple images, the images must be stacked along the 1st dimension (axis=0). mask : array-like An array indicates bad pixel positions The shape must be same as image. The value of bad pixel is nonzero, and the others is 0. If all pixels are bad, raise ValueError. out : cupy.ndarray, default None Alternate output array in which to place the result. The default is None; if provided, it must have the same shape as the expected output, but the type will be cast if necessary. dtype : str or dtype, default None dtype of ndarray used internally If None, use eclair.common.default_dtype. If the input dtype is different, use a casted copy. fix_NaN : bool, default False If true, fix NaN pixel even if it's not specified as bad pixel in mask Returns ------- fixed : ndarray An array of images fixed bad pixel Notes ----- NaN is ignored in interpolation calculations, but is not fixed if fix_NaN is False. ''' dtype = judge_dtype(dtype) data = cp.asarray(data,dtype=dtype) mask = cp.asarray(mask,dtype=dtype) dshape = data.shape imshape = data.shape[-2:] if imshape != mask.shape[-2:]: raise ValueError('shape differs between data and mask') if mask.all(axis=(-2,-1)).any(): raise ValueError('No available pixel') if out is None: out = data.copy() else: cp.copyto(out,data) tout = cp.array(out,copy=False,ndmin=3) filt = cp.array(mask,ndmin=3) elementwise_not(filt,filt) ternary_operation(filt,tout,0,tout) dconv = cp.empty_like(tout) nconv = cp.empty_like(filt) for _ in range(max(imshape)): conv_kernel(tout,dconv) conv_kernel(filt,nconv) fix_core(filt,dconv,nconv,fix_NaN,tout) if nconv.all(): break else: cp.sign(nconv,out=filt) return out
def imalign(data, shifts, interp='spline3', boundary='neighbor', trimimages=True, dtype=None): ''' Stack the images with aligning their relative positions Parameters ---------- data : 3D ndarray An array of images stacked along the 1st dimesion (axis=0). shifts : 2D ndarray An array of relative positions of images in units of pixel. Along the 1st axis, values of each images must be the same order as the 1st axis of "data". Along the 2nd axis, the 1st item is interpreted as the value of X, the 2nd item as the value of Y. interp : str, default 'spline3' Subpixel interpolation algorithm in subpixel image shift. The choices are: 'spline3' (bicubic spline), 'poly3' (3rd order interior polynomial), 'linear' (bilinear), and 'neighbor' (nearest neighbor). boundary : str, default 'neighbor' The choices are: 'neighbor', 'constant'. trimimages : bool, default True If True, the output images will be trimmed to include only the region over which they all overlap. dtype : str or dtype, default None dtype of array used internally. If None, use eclair.common.default_dtype. If the input dtype is different, use a casted copy. Returns ------- aligned : 3D cupy.ndarray An array of images aligned and stacked along the 1st axis ''' dtype = judge_dtype(dtype) data = cp.asarray(data, dtype=dtype) shifts = np.asarray(cp.asnumpy(shifts), dtype=dtype) try: nums, y_len, x_len = shape = np.array(data.shape) except ValueError: raise ValueError('data must have 3 dimensions') if nums != len(shifts): raise ValueError('data and shifts do not match') shift = Shift(x_len=x_len, y_len=y_len, interp=interp, boundary=boundary, dtype=dtype).interp xy_i = np.floor(shifts).astype(int) xy_d = shifts - xy_i xy_i -= xy_i.min(axis=0) x_u, y_u = xy_u = xy_i.max(axis=0) if trimimages: shape[1:] -= xy_u[::-1] copy_ = (lambda dst, src, ix, iy: cp.copyto( dst, src[y_u - iy:y_len - iy, x_u - ix:x_len - ix])) else: shape[1:] += xy_u[::-1] copy_ = (lambda dst, src, ix, iy: cp.copyto( dst[iy:iy + y_len, ix:ix + x_len], src)) aligned = cp.full(shape, cp.nan, dtype=dtype) for ixy, dxy, src, dst in zip(xy_i, xy_d, data, aligned): shifted = shift(src, *dxy) copy_(dst, shifted, *ixy) return aligned
def train(self): # Initialize prev update arrays u_h = np.zeros(self.w_h.shape) u_o = np.zeros(self.w_o.shape) for e in range(self.EPOCHS): x, t = self.load() # Check that our set of training data is evenly distributed # NOTE: check_balanced() won't work because CuPy doesn't have random.get_state # while self.check_balanced(t) == False: # x, t = self.load() # Add bias, copy arrays over #inputs = np.c_[x, np.ones(self.SAMPLES)] inputs = np.column_stack((x, np.ones(self.SAMPLES))) target_array = np.zeros((t.shape)) np.copyto(target_array, t) # Hold onto our predictions predictions = np.zeros((self.SAMPLES,self.NEURONS)) print("\n\nEpoch:%s lr:%s n:%s N:%s alpha:%s" % (e+1, self.LR, self.HIDDEN, self.SAMPLES, self.ALPHA)) for N in range(self.SAMPLES): # Forward prop n_o, target_k, prediction_n = self.forward(inputs[N], target_array[N]) # Backprop # Summations on weights use dots of hidden output errors # Output deltas o_deltas = n_o * (1-n_o) * (target_k-n_o) # Hidden deltas h_deltas = self.n_h * (1-self.n_h) * np.dot(o_deltas, np.transpose(self.w_o)) # Hidden to output weight update u_o = (self.LR * (np.dot((np.transpose(np.array(self.n_h)[np.newaxis])), np.array(o_deltas)[np.newaxis])) + (self.ALPHA * u_o)) # Update self.w_o += u_o # Input to hidden weight update u_h = (self.LR * (np.dot((np.transpose((inputs[N])[np.newaxis])), np.array(h_deltas[:-1])[np.newaxis])) + (self.ALPHA * u_h)) #- self.LAMBDA*u_h) self.w_h += u_h # Add predictions of sample to predictions array to compute confusion matrix predictions[N] = prediction_n # Append the returned accuracy to correctness array self.CORRECT_CONF, train_acc = self.get_confusion(target_array, predictions) self.CORRECT.append(train_acc) #print(self.CORRECT) # Run tests after every epoch print("\nRunning test") self.test()
def _unwrap_correct(dd, discont): ddmod = cupy.mod(dd + numpy.pi, 2*numpy.pi) - numpy.pi cupy.copyto(ddmod, numpy.pi, where=(ddmod == -numpy.pi) & (dd > 0)) ph_correct = ddmod - dd cupy.copyto(ph_correct, 0., where=cupy.abs(dd) < discont) return ph_correct