本文整理汇总了Python中numpy.copyto函数的典型用法代码示例。如果您正苦于以下问题:Python copyto函数的具体用法?Python copyto怎么用?Python copyto使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了copyto函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: guess_data_type
def guess_data_type(orig_values, namask=None):
"""
Use heuristics to guess data type.
"""
valuemap, values = None, orig_values
is_discrete = is_discrete_values(orig_values)
if is_discrete:
valuemap = sorted(is_discrete)
coltype = DiscreteVariable
else:
# try to parse as float
orig_values = np.asarray(orig_values)
if namask is None:
namask = isnastr(orig_values)
values = np.empty_like(orig_values, dtype=float)
values[namask] = np.nan
try:
np.copyto(values, orig_values, where=~namask, casting="unsafe")
except ValueError:
tvar = TimeVariable('_')
try:
values[~namask] = [tvar.parse(i) for i in orig_values[~namask]]
except ValueError:
coltype = StringVariable
# return original_values
values = orig_values
else:
coltype = TimeVariable
else:
coltype = ContinuousVariable
return valuemap, values, coltype
示例2: testTensorAccessor
def testTensorAccessor(self):
"""Check that tensor returns a reference."""
array_ref = self.interpreter.tensor(self.input0)
np.copyto(array_ref(), self.initial_data)
self.assertAllEqual(array_ref(), self.initial_data)
self.assertAllEqual(
self.interpreter.get_tensor(self.input0), self.initial_data)
示例3: _copyto
def _copyto(a, val, mask):
"""
Replace values in `a` with NaN where `mask` is True. This differs from
copyto in that it will deal with the case where `a` is a numpy scalar.
Parameters
----------
a : ndarray or numpy scalar
Array or numpy scalar some of whose values are to be replaced
by val.
val : numpy scalar
Value used a replacement.
mask : ndarray, scalar
Boolean array. Where True the corresponding element of `a` is
replaced by `val`. Broadcasts.
Returns
-------
res : ndarray, scalar
Array with elements replaced or scalar `val`.
"""
if isinstance(a, np.ndarray):
np.copyto(a, val, where=mask, casting='unsafe')
else:
a = a.dtype.type(val)
return a
示例4: npArrayToReadOnlySharedArray
def npArrayToReadOnlySharedArray(npArray):
'''Returns a shared memory array for a numpy array. Used to reduce memory footprint when passing parameters to multiprocess pools'''
SharedBase = multiprocessing.sharedctypes.RawArray(ctypes.c_float, npArray.shape[0] * npArray.shape[1])
SharedArray = np.ctypeslib.as_array(SharedBase)
SharedArray = SharedArray.reshape(npArray.shape)
np.copyto(SharedArray, npArray)
return SharedArray
示例5: __call__
def __call__(self, key, value):
key = self.path + key.lstrip('/')
if not self.strict and key not in self.npz:
return value
if isinstance(self.ignore_names, (tuple, list)):
ignore_names = self.ignore_names
else:
ignore_names = (self.ignore_names,)
for ignore_name in ignore_names:
if isinstance(ignore_name, str):
if key == ignore_name:
return value
elif callable(ignore_name):
if ignore_name(key):
return value
else:
raise ValueError(
'ignore_names needs to be a callable, string or '
'list of them.')
dataset = self.npz[key]
if dataset[()] is None:
return None
if value is None:
return dataset
elif isinstance(value, numpy.ndarray):
numpy.copyto(value, dataset)
elif isinstance(value, cuda.ndarray):
value.set(numpy.asarray(dataset))
else:
value = type(value)(numpy.asarray(dataset))
return value
示例6: learn_parameters
def learn_parameters(adj_list, q, c_max, nb_iterations, crit_infer = 0.2, crit_learn = 0.2, tmax_infer = 12, tmax_learn = 8):
"""
Learns the true group parameters of a given graph, and returns the optimal group assignment given by the belief
propagation algorithm
"""
# Number of nodes on the graph
N = len(adj_list)
# Optimal values given by the algorithm
f_min = 0
groups_opt = np.zeros(N, dtype = np.int8)
for _ in range(nb_iterations):
# Random initialization of each group's size
n = np.random.rand(q)
n /= np.sum(n)
# Random initialization of the edge matrix
c = np.random.rand(q, q)
for i in range(q - 1):
for j in range(i + 1, q):
c[j, i] = c[i, j]
c *= c_max
# Application of the BP_learning algorithm for these initialized values
groups, f_BP = BP_learning(q, n, c, adj_list, crit_infer, crit_learn, tmax_infer, tmax_learn)
# Updates the optimal values found
if f_BP < f_min:
f_min = f_BP
np.copyto(groups_opt, groups)
# Returns the optimal group assignment found
return groups_opt
示例7: binary_to_net
def binary_to_net(weights, spm_stream, ind_stream, codebook, num_nz):
bits = np.log2(codebook.size)
if bits == 4:
slots = 2
elif bits == 8:
slots = 1
else:
print "Not impemented,", bits
sys.exit()
code = np.zeros(weights.size, np.uint8)
# Recover from binary stream
spm = np.zeros(num_nz, np.uint8)
ind = np.zeros(num_nz, np.uint8)
if slots == 2:
spm[np.arange(0, num_nz, 2)] = spm_stream % (2**4)
spm[np.arange(1, num_nz, 2)] = spm_stream / (2**4)
else:
spm = spm_stream
ind[np.arange(0, num_nz, 2)] = ind_stream% (2**4)
ind[np.arange(1, num_nz, 2)] = ind_stream/ (2**4)
# Recover the matrix
ind = np.cumsum(ind+1)-1
code[ind] = spm
data = np.reshape(codebook[code], weights.shape)
np.copyto(weights, data)
示例8: apply_qt_elements_filtering
def apply_qt_elements_filtering(image):
#
# apply bilateral filter on IMAGE to smooth colors while keeping edges
cv2.bilateralFilter(UtilityOperations.crop_to_720p(image), 3, 255, 50,
dst=MultiprocessOperations.shared_memory_image)
#
# crop elements from IMAGE
MultiprocessOperations.wait_for_element_cropping()
#
# upload RED channels to GPU
TheanoOperations.__shared_red.set_value(
MultiprocessOperations.shared_memory_red_channel, borrow=True)
#
# upload GREEN channels to GPU
TheanoOperations.__shared_green.set_value(
MultiprocessOperations.shared_memory_green_channel, borrow=True)
#
# upload BLUE channels to GPU
TheanoOperations.__shared_blue.set_value(
MultiprocessOperations.shared_memory_blue_channel, borrow=True)
#
# download FILTERING result from GPU
np.copyto(MultiprocessOperations.shared_memory_qt_filtered_elements,
TheanoOperations.__apply_binary_filtering)
#
# apply IMAGE threshold CLASSIFICATION
MultiprocessOperations.wait_for_qt_image_classification()
return MultiprocessOperations.shared_memory_qt_filtered_elements
示例9: set_rho
def set_rho(self, rho):
"""
Set the initial density matrix
:param rho: 2D numpy array or sting containing the density matrix
:return: self
"""
if isinstance(rho, str):
# density matrix is supplied as a string
ne.evaluate("%s + 0j" % rho, local_dict=vars(self), out=self.rho)
elif isinstance(rho, np.ndarray):
# density matrix is supplied as an array
# perform the consistency checks
assert rho.shape == self.rho.shape,\
"The grid size does not match with the density matrix"
# make sure the density matrix is stored as a complex array
np.copyto(self.rho, rho.astype(np.complex))
else:
raise ValueError("density matrix must be either string or numpy.array")
# normalize
self.rho /= self.rho.trace() * self.dX
return self
示例10: _load_flat_grad
def _load_flat_grad(self, flat_grad):
start = 0
for g in self._grad_buffers:
size = g.size
np.copyto(g, np.reshape(flat_grad[start:start + size], g.shape))
start += size
return
示例11: serialize
def serialize(self, serializer):
"""Serializes the link object.
Args:
serializer (~chainer.AbstractSerializer): Serializer object.
"""
d = self.__dict__
for name in self._params:
serializer(name, d[name].data)
for name in self._persistent:
d[name] = serializer(name, d[name])
if (self.has_uninitialized_params and
isinstance(serializer, chainer.serializer.Serializer)):
raise ValueError("uninitialized parameters cannot be serialized")
for name in self._uninitialized_params.copy():
# Note: There should only be uninitialized parameters
# during deserialization.
initialized_value = serializer(name, None)
self.add_param(name, initialized_value.shape)
uninitialized_value = d[name].data
if isinstance(uninitialized_value, numpy.ndarray):
numpy.copyto(uninitialized_value, initialized_value)
elif isinstance(uninitialized_value, cuda.ndarray):
uninitialized_value.set(numpy.asarray(initialized_value))
示例12: use
def use(self, dataset):
"""
Computes and returns the outputs of the Learner for
``dataset``:
- the outputs should be a Numpy 2D array of size
len(dataset) by (nb of classes + 1)
- the ith row of the array contains the outputs for the ith example
- the outputs for each example should contain
the predicted class (first element) and the
output probabilities for each class (following elements)
Argument ``dataset`` is an MLProblem object.
"""
outputs = np.zeros((len(dataset), self.n_classes + 1))
errors = np.zeros((len(dataset), 2))
## PUT CODE HERE ##
# row[0] is input.csv image (array), row[1] actual target class for that image
for ind, row in enumerate(dataset):
# fill 2nd element with loss
errors[ind, 1] = self.fprop(row[0], row[1])
# predicted class
outputs[ind, 0] = np.argmax(self.hs[-1])
# 0/1 classification error
errors[ind, 0] = (outputs[ind, 0] != row[1])
# print "errors: ", errors[ind, ]
# add output probs
np.copyto(outputs[ind, 1:], self.hs[-1])
# print "outputs: ", outputs[ind,]
# time.sleep(5)
return outputs, errors
示例13: lieberfit
def lieberfit(Data,Order=5):
import numpy as np
NewCurve = np.zeros(shape=(Data.shape[0]))
OldCurve = np.array(Data)
Diff = NewCurve-OldCurve
Convergence = np.dot(Diff,Diff)
m = 0
while Convergence > 1: # Suggest setting convergence criteria == pixel resolution
P = np.polyfit(range(len(Data)),OldCurve,Order)
NewCurve = np.polyval(P,range(len(Data)))
np.copyto(OldCurve, NewCurve, where = NewCurve < OldCurve)
m+=1
Diff = NewCurve - OldCurve
Convergence = np.dot(Diff,Diff)
#print('Iterations needed for convergence: ',m,sep='')
CurveFit=np.copy(NewCurve)
return (CurveFit)
示例14: revert_all
def revert_all(self, clear=False):
'''return the image to it's original state'''
np.copyto(self.image, self.ref_image)
if clear:
self.clear_history()
self.drawing.reset()
示例15: draw_bounding_box_on_image_array
def draw_bounding_box_on_image_array(image,
ymin,
xmin,
ymax,
xmax,
color='red',
thickness=4,
display_str_list=(),
use_normalized_coordinates=True):
"""Adds a bounding box to an image (numpy array).
Bounding box coordinates can be specified in either absolute (pixel) or
normalized coordinates by setting the use_normalized_coordinates argument.
Args:
image: a numpy array with shape [height, width, 3].
ymin: ymin of bounding box.
xmin: xmin of bounding box.
ymax: ymax of bounding box.
xmax: xmax of bounding box.
color: color to draw bounding box. Default is red.
thickness: line thickness. Default value is 4.
display_str_list: list of strings to display in box
(each to be shown on its own line).
use_normalized_coordinates: If True (default), treat coordinates
ymin, xmin, ymax, xmax as relative to the image. Otherwise treat
coordinates as absolute.
"""
image_pil = Image.fromarray(np.uint8(image)).convert('RGB')
draw_bounding_box_on_image(image_pil, ymin, xmin, ymax, xmax, color,
thickness, display_str_list,
use_normalized_coordinates)
np.copyto(image, np.array(image_pil))