本文整理汇总了Python中numpy.may_share_memory方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.may_share_memory方法的具体用法?Python numpy.may_share_memory怎么用?Python numpy.may_share_memory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.may_share_memory方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_creation_maskcreation
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import may_share_memory [as 别名]
def test_creation_maskcreation(self):
# Tests how masks are initialized at the creation of Maskedarrays.
data = arange(24, dtype=float)
data[[3, 6, 15]] = masked
dma_1 = MaskedArray(data)
assert_equal(dma_1.mask, data.mask)
dma_2 = MaskedArray(dma_1)
assert_equal(dma_2.mask, dma_1.mask)
dma_3 = MaskedArray(dma_1, mask=[1, 0, 0, 0] * 6)
fail_if_equal(dma_3.mask, dma_1.mask)
x = array([1, 2, 3], mask=True)
assert_equal(x._mask, [True, True, True])
x = array([1, 2, 3], mask=False)
assert_equal(x._mask, [False, False, False])
y = array([1, 2, 3], mask=x._mask, copy=False)
assert_(np.may_share_memory(x.mask, y.mask))
y = array([1, 2, 3], mask=x._mask, copy=True)
assert_(not np.may_share_memory(x.mask, y.mask))
示例2: test_empty
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import may_share_memory [as 别名]
def test_empty(self):
# Tests empty/like
datatype = [('a', int), ('b', float), ('c', '|S8')]
a = masked_array([(1, 1.1, '1.1'), (2, 2.2, '2.2'), (3, 3.3, '3.3')],
dtype=datatype)
assert_equal(len(a.fill_value.item()), len(datatype))
b = empty_like(a)
assert_equal(b.shape, a.shape)
assert_equal(b.fill_value, a.fill_value)
b = empty(len(a), dtype=datatype)
assert_equal(b.shape, a.shape)
assert_equal(b.fill_value, a.fill_value)
# check empty_like mask handling
a = masked_array([1, 2, 3], mask=[False, True, False])
b = empty_like(a)
assert_(not np.may_share_memory(a.mask, b.mask))
b = a.view(masked_array)
assert_(np.may_share_memory(a.mask, b.mask))
示例3: check_may_share_memory_exact
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import may_share_memory [as 别名]
def check_may_share_memory_exact(a, b):
got = np.may_share_memory(a, b, max_work=MAY_SHARE_EXACT)
assert_equal(np.may_share_memory(a, b),
np.may_share_memory(a, b, max_work=MAY_SHARE_BOUNDS))
a.fill(0)
b.fill(0)
a.fill(1)
exact = b.any()
err_msg = ""
if got != exact:
err_msg = " " + "\n ".join([
"base_a - base_b = %r" % (a.__array_interface__['data'][0] - b.__array_interface__['data'][0],),
"shape_a = %r" % (a.shape,),
"shape_b = %r" % (b.shape,),
"strides_a = %r" % (a.strides,),
"strides_b = %r" % (b.strides,),
"size_a = %r" % (a.size,),
"size_b = %r" % (b.size,)
])
assert_equal(got, exact, err_msg=err_msg)
示例4: may_share_memory
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import may_share_memory [as 别名]
def may_share_memory(a, b):
# This is Fred suggestion for a quick and dirty way of checking
# aliasing .. this can potentially be further refined (ticket #374)
if _is_sparse(a) and _is_sparse(b):
return (SparseType.may_share_memory(a, b.data) or
SparseType.may_share_memory(a, b.indices) or
SparseType.may_share_memory(a, b.indptr))
if _is_sparse(b) and isinstance(a, numpy.ndarray):
a, b = b, a
if _is_sparse(a) and isinstance(b, numpy.ndarray):
if (numpy.may_share_memory(a.data, b) or
numpy.may_share_memory(a.indices, b) or
numpy.may_share_memory(a.indptr, b)):
# currently we can't share memory with a.shape as it is a tuple
return True
return False
示例5: test_borrow_input
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import may_share_memory [as 别名]
def test_borrow_input(self):
"""
Tests that the contract for io.In is respected. When borrow=False, it should be
impossible for outputs to be aliased to the input variables provided by the user,
either through a view-map or a destroy map. New tests should be added in the future
when borrow=True is implemented.
"""
a = T.dmatrix()
aval = numpy.random.rand(3, 3)
# when borrow=False, test that a destroy map cannot alias output to input
f = theano.function([In(a, borrow=False)], Out(a + 1, borrow=True))
assert numpy.all(f(aval) == aval + 1)
assert not numpy.may_share_memory(aval, f(aval))
# when borrow=False, test that a viewmap cannot alias output to input
f = theano.function([In(a, borrow=False)], Out(a[0, :], borrow=True))
assert numpy.all(f(aval) == aval[0, :])
assert not numpy.may_share_memory(aval, f(aval))
示例6: test_no_aliasing_2
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import may_share_memory [as 别名]
def test_no_aliasing_2(self):
# B and A take one another's values
# no copying is necessary since each one is updated.
orig_a = numpy.zeros((2, 2)) + .5
orig_b = numpy.zeros((2, 2)) - .5
A = self.shared(orig_a)
B = self.shared(orig_b)
data_of_a = data_of(A)
data_of_b = data_of(B)
f = pfunc([], [], updates=[(A, B), (B, A)])
f()
# correctness
assert numpy.all(data_of(A) == -.5)
assert numpy.all(data_of(B) == +.5)
# shared vars may not be aliased
assert not numpy.may_share_memory(data_of(A), data_of(B))
# theano should have been smart enough to not make copies
assert numpy.may_share_memory(data_of(A), data_of_b)
assert numpy.may_share_memory(data_of(B), data_of_a)
示例7: final_view
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import may_share_memory [as 别名]
def final_view(self, a, axis=None):
"""
Returns a view of array `a` restricting it to only the
*final* results computed by this tree (not the intermediate
results).
Parameters
----------
a : ndarray
An array of results computed using this EvalTree,
such that the `axis`-th dimension equals the full
length of the tree. The other dimensions of `a` are
unrestricted.
axis : int, optional
Specified the axis along which the selection of the
final elements is performed. If None, than this
selection if performed on flattened `a`.
Returns
-------
ndarray
Of the same shape as `a`, except for along the
specified axis, whose dimension has been reduced
to filter out the intermediate (non-final) results.
"""
if axis is None:
return a[0:self.num_final_strings()]
else:
sl = [slice(None)] * a.ndim
sl[axis] = slice(0, self.num_final_strings())
ret = a[tuple(sl)]
assert(ret.base is a or ret.base is a.base) # check that what is returned is a view
assert(ret.size == 0 or _np.may_share_memory(ret, a))
return ret
示例8: test_array_memory_sharing
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import may_share_memory [as 别名]
def test_array_memory_sharing(self):
assert_(np.may_share_memory(self.a, self.a.ravel()))
assert_(not np.may_share_memory(self.a, self.a.flatten()))
示例9: test_matrix_memory_sharing
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import may_share_memory [as 别名]
def test_matrix_memory_sharing(self):
assert_(np.may_share_memory(self.m, self.m.ravel()))
assert_(not np.may_share_memory(self.m, self.m.flatten()))
示例10: test_basic
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import may_share_memory [as 别名]
def test_basic(self):
d = np.ones((50, 60))
d2 = np.ones((30, 60, 6))
assert_(np.may_share_memory(d, d))
assert_(np.may_share_memory(d, d[::-1]))
assert_(np.may_share_memory(d, d[::2]))
assert_(np.may_share_memory(d, d[1:, ::-1]))
assert_(not np.may_share_memory(d[::-1], d2))
assert_(not np.may_share_memory(d[::2], d2))
assert_(not np.may_share_memory(d[1:, ::-1], d2))
assert_(np.may_share_memory(d2[1:, ::-1], d2))
# Utility
示例11: _compare_index_result
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import may_share_memory [as 别名]
def _compare_index_result(self, arr, index, mimic_get, no_copy):
"""Compare mimicked result to indexing result.
"""
arr = arr.copy()
indexed_arr = arr[index]
assert_array_equal(indexed_arr, mimic_get)
# Check if we got a view, unless its a 0-sized or 0-d array.
# (then its not a view, and that does not matter)
if indexed_arr.size != 0 and indexed_arr.ndim != 0:
assert_(np.may_share_memory(indexed_arr, arr) == no_copy)
# Check reference count of the original array
if HAS_REFCOUNT:
if no_copy:
# refcount increases by one:
assert_equal(sys.getrefcount(arr), 3)
else:
assert_equal(sys.getrefcount(arr), 2)
# Test non-broadcast setitem:
b = arr.copy()
b[index] = mimic_get + 1000
if b.size == 0:
return # nothing to compare here...
if no_copy and indexed_arr.ndim != 0:
# change indexed_arr in-place to manipulate original:
indexed_arr += 1000
assert_array_equal(arr, b)
return
# Use the fact that the array is originally an arange:
arr.flat[indexed_arr.ravel()] += 1000
assert_array_equal(arr, b)
示例12: check_may_share_memory_easy_fuzz
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import may_share_memory [as 别名]
def check_may_share_memory_easy_fuzz(get_max_work, same_steps, min_count):
# Check that overlap problems with common strides are solved with
# little work.
x = np.zeros([17,34,71,97], dtype=np.int16)
feasible = 0
infeasible = 0
pair_iter = iter_random_view_pairs(x, same_steps)
while min(feasible, infeasible) < min_count:
a, b = next(pair_iter)
bounds_overlap = np.may_share_memory(a, b)
may_share_answer = np.may_share_memory(a, b)
easy_answer = np.may_share_memory(a, b, max_work=get_max_work(a, b))
exact_answer = np.may_share_memory(a, b, max_work=MAY_SHARE_EXACT)
if easy_answer != exact_answer:
# assert_equal is slow...
assert_equal(easy_answer, exact_answer)
if may_share_answer != bounds_overlap:
assert_equal(may_share_answer, bounds_overlap)
if bounds_overlap:
if exact_answer:
feasible += 1
else:
infeasible += 1
示例13: test_may_share_memory_bad_max_work
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import may_share_memory [as 别名]
def test_may_share_memory_bad_max_work():
x = np.zeros([1])
assert_raises(OverflowError, np.may_share_memory, x, x, max_work=10**100)
assert_raises(OverflowError, np.shares_memory, x, x, max_work=10**100)
示例14: test_fast_power
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import may_share_memory [as 别名]
def test_fast_power(self):
x = np.array([1, 2, 3], np.int16)
res = x**2.0
assert_((x**2.00001).dtype is res.dtype)
assert_array_equal(res, [1, 4, 9])
# check the inplace operation on the casted copy doesn't mess with x
assert_(not np.may_share_memory(res, x))
assert_array_equal(x, [1, 2, 3])
# Check that the fast path ignores 1-element not 0-d arrays
res = x ** np.array([[[2]]])
assert_equal(res.shape, (1, 1, 3))
示例15: __array_finalize__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import may_share_memory [as 别名]
def __array_finalize__(self, obj):
if hasattr(obj, '_mmap') and np.may_share_memory(self, obj):
self._mmap = obj._mmap
self.filename = obj.filename
self.offset = obj.offset
self.mode = obj.mode
else:
self._mmap = None
self.filename = None
self.offset = None
self.mode = None