本文整理汇总了Python中numpy.zeros方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.zeros方法的具体用法?Python numpy.zeros怎么用?Python numpy.zeros使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.zeros方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wordbag2mat
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import zeros [as 别名]
def wordbag2mat(self, wordbag): #testing
if self.model==None:
raise Exception("no model")
matrix=np.empty((len(wordbag),self.len_vector))
#如果词典中不存在该词,抛出异常,但暂时还没有自定义词典的办法,所以暂时不那么严格
#try:
# for i in range(len(wordbag)):
# matrix[i,:]=self.model[wordbag[i]]
#except:
# raise Exception("'%s' can not be found in dictionary." % wordbag[i])
#如果词典中不存在该词,则push进一列零向量
for i in range(len(wordbag)):
try:
matrix[i,:]=self.model.wv.__getitem__(wordbag[i])#[wordbag[i]]
except:
matrix[i,:]=np.zeros((1,self.len_vector))
return matrix
################################ problem #####################################
示例2: similarity_label
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import zeros [as 别名]
def similarity_label(self, words, normalization=True):
"""
you can calculate more than one word at the same time.
"""
if self.model==None:
raise Exception('no model.')
if isinstance(words, string_types):
words=[words]
vectors=np.transpose(self.model.wv.__getitem__(words))
if normalization:
unit_vector=unitvec(vectors,ax=0) # 这样写比原来那样速度提升一倍
#unit_vector=np.zeros((len(vectors),len(words)))
#for i in range(len(words)):
# unit_vector[:,i]=matutils.unitvec(vectors[:,i])
dists=np.dot(self.Label_vec_u, unit_vector)
else:
dists=np.dot(self.Label_vec, vectors)
return dists
示例3: get_a_datum
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import zeros [as 别名]
def get_a_datum(self):
if self._compressed:
datum = extract_sample(
self._data[self._cur], self._mean, self._resize)
else:
datum = self._data[self._cur]
# start parsing labels
label_elems = parse_label(self._label[self._cur])
label = np.zeros(self._label_dim)
if not self._multilabel:
label[0] = label_elems[0]
else:
for i in label_elems:
label[i] = 1
self._cur = (self._cur + 1) % self._sample_count
return datum, label
示例4: load_keypoints
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import zeros [as 别名]
def load_keypoints(image_filepath, image_height, image_width):
"""Load facial keypoints of one image."""
fp_keypoints = "%s.cat" % (image_filepath,)
if not os.path.isfile(fp_keypoints):
raise Exception("Could not find keypoint coordinates for image '%s'." \
% (image_filepath,))
else:
coords_raw = open(fp_keypoints, "r").readlines()[0].strip().split(" ")
coords_raw = [abs(int(coord)) for coord in coords_raw]
keypoints = []
#keypoints_arr = np.zeros((9*2,), dtype=np.int32)
for i in range(1, len(coords_raw), 2): # first element is the number of coords
x = np.clip(coords_raw[i], 0, image_width-1)
y = np.clip(coords_raw[i+1], 0, image_height-1)
keypoints.append((x, y))
return keypoints
示例5: wer
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import zeros [as 别名]
def wer(self, r, h):
# initialisation
d = np.zeros((len(r)+1)*(len(h)+1), dtype=np.uint8)
d = d.reshape((len(r)+1, len(h)+1))
for i in range(len(r)+1):
for j in range(len(h)+1):
if i == 0:
d[0][j] = j
elif j == 0:
d[i][0] = i
# computation
for i in range(1, len(r)+1):
for j in range(1, len(h)+1):
if r[i-1] == h[j-1]:
d[i][j] = d[i-1][j-1]
else:
substitution = d[i-1][j-1] + 1
insertion = d[i][j-1] + 1
deletion = d[i-1][j] + 1
d[i][j] = min(substitution, insertion, deletion)
return d[len(r)][len(h)]
示例6: compliance_function_fdiff
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import zeros [as 别名]
def compliance_function_fdiff(self, x, dc):
obj = self.compliance_function(x, dc)
x0 = x.copy()
dc0 = dc.copy()
dcf = np.zeros(dc.shape)
for i, v in enumerate(x):
x = x0.copy()
x[i] += 1e-6
o1 = self.compliance_function(x, dc)
x[i] = x0[i] - 1e-6
o2 = self.compliance_function(x, dc)
dcf[i] = (o1 - o2) / (2e-6)
print("finite differences: {:g}".format(np.linalg.norm(dcf - dc0)))
dc[:] = dc0
return obj
示例7: calculate_fdiff_stress
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import zeros [as 别名]
def calculate_fdiff_stress(self, x, u, nu, side=1, dx=1e-6):
"""
Calculate the derivative of the Von Mises stress using finite
differences given the densities x, displacements u, and young modulus
nu. Optionally, provide the side length (default: 1) and delta x
(default: 1e-6).
"""
ds = self.calculate_diff_stress(x, u, nu, side)
dsf = numpy.zeros(x.shape)
x = numpy.expand_dims(x, -1)
for i in range(x.shape[0]):
delta = scipy.sparse.coo_matrix(([dx], [[i], [0]]), shape=x.shape)
s1 = self.calculate_stress((x + delta.A).squeeze(), u, nu, side)
s2 = self.calculate_stress((x - delta.A).squeeze(), u, nu, side)
dsf[i] = ((s1 - s2) / (2. * dx))[i]
print("finite differences: {:g}".format(numpy.linalg.norm(dsf - ds)))
return dsf
示例8: test_add_uniform_time_weights
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import zeros [as 别名]
def test_add_uniform_time_weights():
time = np.array([15, 46, 74])
data = np.zeros((3))
ds = xr.DataArray(data,
coords=[time],
dims=[TIME_STR],
name='a').to_dataset()
units_str = 'days since 2000-01-01 00:00:00'
cal_str = 'noleap'
ds[TIME_STR].attrs['units'] = units_str
ds[TIME_STR].attrs['calendar'] = cal_str
with pytest.raises(KeyError):
ds[TIME_WEIGHTS_STR]
ds = add_uniform_time_weights(ds)
time_weights_expected = xr.DataArray(
[1, 1, 1], coords=ds[TIME_STR].coords, name=TIME_WEIGHTS_STR)
time_weights_expected.attrs['units'] = 'days'
assert ds[TIME_WEIGHTS_STR].identical(time_weights_expected)
示例9: ds_time_encoded_cf
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import zeros [as 别名]
def ds_time_encoded_cf():
time_bounds = np.array([[0, 31], [31, 59], [59, 90]])
bounds = np.array([0, 1])
time = np.array([15, 46, 74])
data = np.zeros((3))
ds = xr.DataArray(data,
coords=[time],
dims=[TIME_STR],
name='a').to_dataset()
ds[TIME_BOUNDS_STR] = xr.DataArray(time_bounds,
coords=[time, bounds],
dims=[TIME_STR, BOUNDS_STR],
name=TIME_BOUNDS_STR)
units_str = 'days since 2000-01-01 00:00:00'
cal_str = 'noleap'
ds[TIME_STR].attrs['units'] = units_str
ds[TIME_STR].attrs['calendar'] = cal_str
return ds
示例10: ds_with_time_bounds
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import zeros [as 别名]
def ds_with_time_bounds(alt_lat_str, var_name):
time_bounds = np.array([[0, 31], [31, 59], [59, 90]])
bounds = np.array([0, 1])
time = np.array([15, 46, 74])
data = np.zeros((3, 1, 1))
lat = [0]
lon = [0]
ds = xr.DataArray(data,
coords=[time, lat, lon],
dims=[TIME_STR, alt_lat_str, LON_STR],
name=var_name).to_dataset()
ds[TIME_BOUNDS_STR] = xr.DataArray(time_bounds,
coords=[time, bounds],
dims=[TIME_STR, BOUNDS_STR],
name=TIME_BOUNDS_STR)
units_str = 'days since 2000-01-01 00:00:00'
ds[TIME_STR].attrs['units'] = units_str
ds[TIME_BOUNDS_STR].attrs['units'] = units_str
return ds
示例11: test_sel_var
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import zeros [as 别名]
def test_sel_var():
time = np.array([0, 31, 59]) + 15
data = np.zeros((3))
ds = xr.DataArray(data,
coords=[time],
dims=[TIME_STR],
name=convection_rain.name).to_dataset()
condensation_rain_alt_name, = condensation_rain.alt_names
ds[condensation_rain_alt_name] = xr.DataArray(data, coords=[ds.time])
result = _sel_var(ds, convection_rain)
assert result.name == convection_rain.name
result = _sel_var(ds, condensation_rain)
assert result.name == condensation_rain.name
with pytest.raises(LookupError):
_sel_var(ds, precip)
示例12: build
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import zeros [as 别名]
def build(self):
#{{{
import numpy as np;
self.W = shared((self.input_dim, 4 * self.output_dim),
name='{}_W'.format(self.name))
self.U = shared((self.output_dim, 4 * self.output_dim),
name='{}_U'.format(self.name))
self.b = K.variable(np.hstack((np.zeros(self.output_dim),
K.get_value(self.forget_bias_init(
(self.output_dim,))),
np.zeros(self.output_dim),
np.zeros(self.output_dim))),
name='{}_b'.format(self.name))
#self.c_0 = shared((self.output_dim,), name='{}_c_0'.format(self.name) )
#self.h_0 = shared((self.output_dim,), name='{}_h_0'.format(self.name) )
self.c_0=np.zeros(self.output_dim).astype(theano.config.floatX);
self.h_0=np.zeros(self.output_dim).astype(theano.config.floatX);
self.params=[self.W,self.U,
self.b,
# self.c_0,self.h_0
];
#}}}
示例13: ctc_update_log_p
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import zeros [as 别名]
def ctc_update_log_p(skip_idxs, zeros, active, log_p_curr, log_p_prev):
active_skip_idxs = skip_idxs[(skip_idxs < active).nonzero()]
active_next = T.cast(T.minimum(
T.maximum(
active + 1,
T.max(T.concatenate([active_skip_idxs, [-1]])) + 2 + 1
), log_p_curr.shape[0]), 'int32')
common_factor = T.max(log_p_prev[:active])
p_prev = T.exp(log_p_prev[:active] - common_factor)
_p_prev = zeros[:active_next]
# copy over
_p_prev = T.set_subtensor(_p_prev[:active], p_prev)
# previous transitions
_p_prev = T.inc_subtensor(_p_prev[1:], _p_prev[:-1])
# skip transitions
_p_prev = T.inc_subtensor(_p_prev[active_skip_idxs + 2], p_prev[active_skip_idxs])
updated_log_p_prev = T.log(_p_prev) + common_factor
log_p_next = T.set_subtensor(
zeros[:active_next],
log_p_curr[:active_next] + updated_log_p_prev
)
return active_next, log_p_next
示例14: ctc_path_probs
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import zeros [as 别名]
def ctc_path_probs(predict, Y, alpha=1e-4):
smoothed_predict = (1 - alpha) * predict[:, Y] + alpha * np.float32(1.) / Y.shape[0]
L = T.log(smoothed_predict)
zeros = T.zeros_like(L[0])
log_first = zeros
f_skip_idxs = ctc_create_skip_idxs(Y)
b_skip_idxs = ctc_create_skip_idxs(Y[::-1]) # there should be a shortcut to calculating this
def step(log_f_curr, log_b_curr, f_active, log_f_prev, b_active, log_b_prev):
f_active_next, log_f_next = ctc_update_log_p(f_skip_idxs, zeros, f_active, log_f_curr, log_f_prev)
b_active_next, log_b_next = ctc_update_log_p(b_skip_idxs, zeros, b_active, log_b_curr, log_b_prev)
return f_active_next, log_f_next, b_active_next, log_b_next
[f_active, log_f_probs, b_active, log_b_probs], _ = theano.scan(
step, sequences=[L, L[::-1, ::-1]], outputs_info=[np.int32(1), log_first, np.int32(1), log_first])
idxs = T.arange(L.shape[1]).dimshuffle('x', 0)
mask = (idxs < f_active.dimshuffle(0, 'x')) & (idxs < b_active.dimshuffle(0, 'x'))[::-1, ::-1]
log_probs = log_f_probs + log_b_probs[::-1, ::-1] - L
return log_probs, mask
示例15: _project_im_rois
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import zeros [as 别名]
def _project_im_rois(im_rois, scales):
"""Project image RoIs into the image pyramid built by _get_image_blob.
Arguments:
im_rois (ndarray): R x 4 matrix of RoIs in original image coordinates
scales (list): scale factors as returned by _get_image_blob
Returns:
rois (ndarray): R x 4 matrix of projected RoI coordinates
levels (list): image pyramid levels used by each projected RoI
"""
im_rois = im_rois.astype(np.float, copy=False)
if len(scales) > 1:
widths = im_rois[:, 2] - im_rois[:, 0] + 1
heights = im_rois[:, 3] - im_rois[:, 1] + 1
areas = widths * heights
scaled_areas = areas[:, np.newaxis] * (scales[np.newaxis, :] ** 2)
diff_areas = np.abs(scaled_areas - 224 * 224)
levels = diff_areas.argmin(axis=1)[:, np.newaxis]
else:
levels = np.zeros((im_rois.shape[0], 1), dtype=np.int)
rois = im_rois * scales[levels]
return rois, levels
开发者ID:Sunarker,项目名称:Collaborative-Learning-for-Weakly-Supervised-Object-Detection,代码行数:26,代码来源:test.py