本文整理汇总了Python中numpy.float64方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.float64方法的具体用法?Python numpy.float64怎么用?Python numpy.float64使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.float64方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: draw_heatmap
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import float64 [as 别名]
def draw_heatmap(img, heatmap, alpha=0.5):
"""Draw a heatmap overlay over an image."""
assert len(heatmap.shape) == 2 or \
(len(heatmap.shape) == 3 and heatmap.shape[2] == 1)
assert img.dtype in [np.uint8, np.int32, np.int64]
assert heatmap.dtype in [np.float32, np.float64]
if img.shape[0:2] != heatmap.shape[0:2]:
heatmap_rs = np.clip(heatmap * 255, 0, 255).astype(np.uint8)
heatmap_rs = ia.imresize_single_image(
heatmap_rs[..., np.newaxis],
img.shape[0:2],
interpolation="nearest"
)
heatmap = np.squeeze(heatmap_rs) / 255.0
cmap = plt.get_cmap('jet')
heatmap_cmapped = cmap(heatmap)
heatmap_cmapped = np.delete(heatmap_cmapped, 3, 2)
heatmap_cmapped = heatmap_cmapped * 255
mix = (1-alpha) * img + alpha * heatmap_cmapped
mix = np.clip(mix, 0, 255).astype(np.uint8)
return mix
示例2: _convert_dict_to_sparse_matrix
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import float64 [as 别名]
def _convert_dict_to_sparse_matrix(self, feature_rows):
if len(feature_rows) == 0:
raise Exception('ERROR: something went wrong, empty features.')
data, row, col = [], [], []
for i, feature_row in enumerate(feature_rows):
if len(feature_row) == 0:
# case of empty feature set for a specific instance
row.append(i)
col.append(0)
data.append(0)
else:
for feature in feature_row:
row.append(i)
col.append(feature)
data.append(feature_row[feature])
shape = (max(row) + 1, self.feature_size)
data_matrix = csr_matrix((data, (row, col)),
shape=shape, dtype=np.float64)
return data_matrix
示例3: _maybe_cast_to_float64
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import float64 [as 别名]
def _maybe_cast_to_float64(da):
"""Cast DataArrays to np.float64 if they are of type np.float32.
Parameters
----------
da : xr.DataArray
Input DataArray
Returns
-------
DataArray
"""
if da.dtype == np.float32:
logging.warning('Datapoints were stored using the np.float32 datatype.'
'For accurate reduction operations using bottleneck, '
'datapoints are being cast to the np.float64 datatype.'
' For more information see: https://github.com/pydata/'
'xarray/issues/1346')
return da.astype(np.float64)
else:
return da
示例4: read_common_mat
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import float64 [as 别名]
def read_common_mat(fd):
"""
Read common matrix(for class Matrix in kaldi setup)
see matrix/kaldi-matrix.cc::
void Matrix<Real>::Read(std::istream & is, bool binary, bool add)
Return a numpy ndarray object
"""
mat_type = read_token(fd)
print_info(f'\tType of the common matrix: {mat_type}')
if mat_type not in ["FM", "DM"]:
raise RuntimeError(f"Unknown matrix type in kaldi: {mat_type}")
float_size = 4 if mat_type == 'FM' else 8
float_type = np.float32 if mat_type == 'FM' else np.float64
num_rows = read_int32(fd)
num_cols = read_int32(fd)
print_info(f'\tSize of the common matrix: {num_rows} x {num_cols}')
mat_data = fd.read(float_size * num_cols * num_rows)
mat = np.fromstring(mat_data, dtype=float_type)
return mat.reshape(num_rows, num_cols)
示例5: read_float_vec
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import float64 [as 别名]
def read_float_vec(fd, direct_access=False):
"""
Read float vector(for class Vector in kaldi setup)
see matrix/kaldi-vector.cc
"""
if direct_access:
expect_binary(fd)
vec_type = read_token(fd)
print_info(f'\tType of the common vector: {vec_type}')
if vec_type not in ["FV", "DV"]:
raise RuntimeError(f"Unknown matrix type in kaldi: {vec_type}")
float_size = 4 if vec_type == 'FV' else 8
float_type = np.float32 if vec_type == 'FV' else np.float64
dim = read_int32(fd)
print_info(f'\tDim of the common vector: {dim}')
vec_data = fd.read(float_size * dim)
return np.fromstring(vec_data, dtype=float_type)
示例6: test_ndarray_elementwise
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import float64 [as 别名]
def test_ndarray_elementwise():
np.random.seed(0)
nrepeat = 10
maxdim = 4
all_type = [np.float32, np.float64, np.float16, np.uint8, np.int32]
real_type = [np.float32, np.float64, np.float16]
for repeat in range(nrepeat):
for dim in range(1, maxdim):
check_with_uniform(lambda x, y: x + y, 2, dim, type_list=all_type)
check_with_uniform(lambda x, y: x - y, 2, dim, type_list=all_type)
check_with_uniform(lambda x, y: x * y, 2, dim, type_list=all_type)
check_with_uniform(lambda x, y: x / y, 2, dim, type_list=real_type)
check_with_uniform(lambda x, y: x / y, 2, dim, rmin=1, type_list=all_type)
check_with_uniform(mx.nd.sqrt, 1, dim, np.sqrt, rmin=0)
check_with_uniform(mx.nd.square, 1, dim, np.square, rmin=0)
check_with_uniform(lambda x: mx.nd.norm(x).asscalar(), 1, dim, np.linalg.norm)
示例7: test_random_seed_setting
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import float64 [as 别名]
def test_random_seed_setting():
ctx = mx.context.current_context()
seed_to_test = 1234
num_temp_seeds = 25
probs = [0.125, 0.25, 0.25, 0.0625, 0.125, 0.1875]
num_samples = 100000
for dtype in ['float16', 'float32', 'float64']:
seed = set_seed_variously(1, num_temp_seeds, seed_to_test)
samples1 = mx.nd.random.multinomial(data=mx.nd.array(probs, ctx=ctx, dtype=dtype),
shape=num_samples)
seed = set_seed_variously(seed, num_temp_seeds, seed_to_test)
samples2 = mx.nd.random.multinomial(data=mx.nd.array(probs, ctx=ctx, dtype=dtype),
shape=num_samples)
samples1np = samples1.asnumpy()
set_seed_variously(seed, num_temp_seeds, seed_to_test+1)
samples2np = samples2.asnumpy()
assert same(samples1np, samples2np), \
"seed-setting test: `multinomial` should give the same result with the same seed"
# Tests that seed setting of parallel rng is synchronous w.r.t. rng use before and after.
示例8: test_normal_generator
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import float64 [as 别名]
def test_normal_generator():
ctx = mx.context.current_context()
samples = 1000000
# Default success rate is 0.25, so 2 successes of 8 trials will pass.
trials = 8
num_buckets = 5
for dtype in ['float16', 'float32', 'float64']:
for mu, sigma in [(0.0, 1.0), (1.0, 5.0)]:
print("ctx=%s, dtype=%s, Mu=%g, Sigma=%g:" % (ctx, dtype, mu, sigma))
buckets, probs = gen_buckets_probs_with_ppf(lambda x: ss.norm.ppf(x, mu, sigma), num_buckets)
# Quantize bucket boundaries to reflect the actual dtype and adjust probs accordingly
buckets = np.array(buckets, dtype=dtype).tolist()
probs = [(ss.norm.cdf(buckets[i][1], mu, sigma) -
ss.norm.cdf(buckets[i][0], mu, sigma)) for i in range(num_buckets)]
generator_mx = lambda x: mx.nd.random.normal(mu, sigma, shape=x, ctx=ctx, dtype=dtype).asnumpy()
verify_generator(generator=generator_mx, buckets=buckets, probs=probs,
nsamples=samples, nrepeat=trials)
generator_mx_same_seed =\
lambda x: np.concatenate(
[mx.nd.random.normal(mu, sigma, shape=x // 10, ctx=ctx, dtype=dtype).asnumpy()
for _ in range(10)])
verify_generator(generator=generator_mx_same_seed, buckets=buckets, probs=probs,
nsamples=samples, nrepeat=trials)
示例9: test_uniform_generator
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import float64 [as 别名]
def test_uniform_generator():
ctx = mx.context.current_context()
for dtype in ['float16', 'float32', 'float64']:
for low, high in [(-1.0, 1.0), (1.0, 3.0)]:
print("ctx=%s, dtype=%s, Low=%g, High=%g:" % (ctx, dtype, low, high))
scale = high - low
buckets, probs = gen_buckets_probs_with_ppf(lambda x: ss.uniform.ppf(x, loc=low, scale=scale), 5)
# Quantize bucket boundaries to reflect the actual dtype and adjust probs accordingly
buckets = np.array(buckets, dtype=dtype).tolist()
probs = [(buckets[i][1] - buckets[i][0])/scale for i in range(5)]
generator_mx = lambda x: mx.nd.random.uniform(low, high, shape=x, ctx=ctx, dtype=dtype).asnumpy()
verify_generator(generator=generator_mx, buckets=buckets, probs=probs)
generator_mx_same_seed = \
lambda x: np.concatenate(
[mx.nd.random.uniform(low, high, shape=x // 10, ctx=ctx, dtype=dtype).asnumpy()
for _ in range(10)])
verify_generator(generator=generator_mx_same_seed, buckets=buckets, probs=probs)
示例10: test_infer_multiout_op2
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import float64 [as 别名]
def test_infer_multiout_op2():
def test_func(a):
q, l = mx.nd.linalg.gelqf(a)
return mx.nd.sum(l)
data32 = mx.nd.random.normal(shape=(2, 3), ctx=mx.cpu(), dtype=np.float32)
data32.attach_grad()
with autograd.record():
test32 = test_func(data32)
test32.backward()
data64 = mx.nd.Cast(data32, dtype=np.float64)
data64.attach_grad()
with autograd.record():
test64 = test_func(data64)
test64.backward()
assert_almost_equal(data64.grad.asnumpy(), data32.grad.asnumpy(), atol=1e-5, rtol=1e-5)
示例11: test_deconvolution_large_c
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import float64 [as 别名]
def test_deconvolution_large_c():
problematic_c = 64 * 1024
# The deconvolution accumulates many values, so set large tolerances.
tol = {np.dtype(np.float32): 1,
np.dtype(np.float64): 1}
def test_1D_with_width(width, grad_req):
ctx_list = [{'ctx': mx.gpu(0), 'deconv_data': (1, 8, width), 'type_dict': {'deconv_data': np.float32}},
{'ctx': mx.gpu(0), 'deconv_data': (1, 8, width), 'type_dict': {'deconv_data': np.float64}}]
sym = mx.sym.Deconvolution(layout='NCW', num_filter=problematic_c, kernel=(2,), name='deconv')
check_consistency([sym, sym], ctx_list, tol=tol, grad_req=grad_req)
def test_2D_with_width(width, grad_req):
ctx_list = [{'ctx': mx.gpu(0), 'deconv_data': (1, 8, 2, width), 'type_dict': {'deconv_data': np.float32}},
{'ctx': mx.gpu(0), 'deconv_data': (1, 8, 2, width), 'type_dict': {'deconv_data': np.float64}}]
sym = mx.sym.Deconvolution(layout='NCHW', num_filter=problematic_c, kernel=(2,2), name='deconv')
check_consistency([sym, sym], ctx_list, tol=tol, grad_req=grad_req)
# Run with different data tensor shapes to run cudnnFind() multiple times.
# First, populate algo and op caches with models that always use cudnnFind() (req == 'write').
# Then run models that must avoid cached cudnnFind() results in some cases (req == 'add').
widths = [4, 16, 64]
for req in ['write', 'add']:
for width in widths:
test_1D_with_width(width, req)
test_2D_with_width(width, req)
示例12: test_bilinear_sampler_with_type
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import float64 [as 别名]
def test_bilinear_sampler_with_type():
data = mx.sym.Variable('data')
grid = mx.sym.Variable('grid')
sym = mx.sym.BilinearSampler(data=data, grid=grid)
ctx_list = [{'ctx': mx.gpu(0), 'data': (1, 5, 10, 10), 'grid': (1, 2, 10, 10),
'type_dict': {'data': np.float64}},
{'ctx': mx.gpu(0), 'data': (1, 5, 10, 10), 'grid': (1, 2, 10, 10),
'type_dict': {'data': np.float32}},
{'ctx': mx.gpu(0), 'data': (1, 5, 10, 10), 'grid': (1, 2, 10, 10),
'type_dict': {'data': np.float16}},
{'ctx': mx.cpu(0), 'data': (1, 5, 10, 10), 'grid': (1, 2, 10, 10),
'type_dict': {'data': np.float64}},
{'ctx': mx.cpu(0), 'data': (1, 5, 10, 10), 'grid': (1, 2, 10, 10),
'type_dict': {'data': np.float32}}]
check_consistency(sym, ctx_list)
check_consistency(sym, ctx_list, grad_req="add")
示例13: test_pooling_with_type2
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import float64 [as 别名]
def test_pooling_with_type2():
ctx_list = [{'ctx': mx.gpu(0), 'pool_data': (10, 2, 10, 10), 'type_dict': {'pool_data': np.float64}},
{'ctx': mx.gpu(0), 'pool_data': (10, 2, 10, 10), 'type_dict': {'pool_data': np.float32}},
{'ctx': mx.gpu(0), 'pool_data': (10, 2, 10, 10), 'type_dict': {'pool_data': np.float16}},
{'ctx': mx.cpu(0), 'pool_data': (10, 2, 10, 10), 'type_dict': {'pool_data': np.float64}},
{'ctx': mx.cpu(0), 'pool_data': (10, 2, 10, 10), 'type_dict': {'pool_data': np.float32}}]
sym = mx.sym.Pooling(name='pool', kernel=(3,3), stride=(2,2), pool_type='max')
check_consistency(sym, ctx_list, rand_type=np.float16)
sym = mx.sym.Pooling(name='pool', kernel=(3,3), pad=(1,1), pool_type='avg')
check_consistency(sym, ctx_list)
sym = mx.sym.Pooling(name='pool', kernel=(5,5), pad=(2,2), pool_type='max')
check_consistency(sym, ctx_list, rand_type=np.float16)
sym = mx.sym.Pooling(name='pool', kernel=(3,3), pad=(1,1), pool_type='sum')
check_consistency(sym, ctx_list)
示例14: test_elementwisesum_with_type
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import float64 [as 别名]
def test_elementwisesum_with_type():
dev_types = [[mx.gpu(0), [np.float64, np.float32, np.float16]],
[mx.cpu(0), [np.float64, np.float32]] ]
for num_args in range(1, 6):
ews_arg_shape = {}
for i in range(num_args):
ews_arg_shape['ews_arg'+str(i)] = (2, 10)
sym = mx.sym.ElementWiseSum(name='ews', num_args=num_args)
ctx_list = []
for dev, types in dev_types:
for dtype in types:
ews_arg_dtype = {'type_dict':{}}
for i in range(num_args):
ews_arg_dtype['type_dict']['ews_arg'+str(i)] = dtype
ctx_elem = {'ctx': dev}
ctx_elem.update(ews_arg_shape)
ctx_elem.update(ews_arg_dtype)
ctx_list.append(ctx_elem)
check_consistency(sym, ctx_list)
示例15: test_embedding_with_type
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import float64 [as 别名]
def test_embedding_with_type():
def test_embedding_helper(data_types, weight_types, low_pad, high_pad):
NVD = [[20, 10, 20], [200, 10, 300]]
for N, V, D in NVD:
sym = mx.sym.Embedding(name='embedding', input_dim=V, output_dim=D)
ctx_list = []
for data_type in data_types:
for weight_type in weight_types:
ctx_list.append({'ctx': mx.gpu(0), 'embedding_data': (N,),
'type_dict': {'embedding_data': data_type, 'embedding_weight': weight_type}})
ctx_list.append({'ctx': mx.cpu(0), 'embedding_data': (N,),
'type_dict': {'embedding_data': data_type, 'embedding_weight': weight_type}})
arg_params = {'embedding_data': np.random.randint(low=-low_pad, high=V+high_pad, size=(N,))}
check_consistency(sym, ctx_list, grad_req={'embedding_data': 'null','embedding_weight': 'write'},
arg_params=arg_params)
data_types = [np.float16, np.float32, np.float64, np.int32]
weight_types = [np.float16, np.float32, np.float64]
test_embedding_helper(data_types, weight_types, 5, 5)
data_types = [np.uint8]
weight_types = [np.float16, np.float32, np.float64]
test_embedding_helper(data_types, weight_types, 0, 5)