本文整理汇总了Python中mxnet.ndarray.array方法的典型用法代码示例。如果您正苦于以下问题:Python ndarray.array方法的具体用法?Python ndarray.array怎么用?Python ndarray.array使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mxnet.ndarray
的用法示例。
在下文中一共展示了ndarray.array方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: draw
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import array [as 别名]
def draw(self, true_classes):
"""Draw samples from log uniform distribution and returns sampled candidates,
expected count for true classes and sampled classes."""
range_max = self.range_max
num_sampled = self.num_sampled
ctx = true_classes.context
log_range = math.log(range_max + 1)
num_tries = 0
true_classes = true_classes.reshape((-1,))
sampled_classes, num_tries = self.sampler.sample_unique(num_sampled)
true_cls = true_classes.as_in_context(ctx).astype('float64')
prob_true = ((true_cls + 2.0) / (true_cls + 1.0)).log() / log_range
count_true = self._prob_helper(num_tries, num_sampled, prob_true)
sampled_classes = ndarray.array(sampled_classes, ctx=ctx, dtype='int64')
sampled_cls_fp64 = sampled_classes.astype('float64')
prob_sampled = ((sampled_cls_fp64 + 2.0) / (sampled_cls_fp64 + 1.0)).log() / log_range
count_sampled = self._prob_helper(num_tries, num_sampled, prob_sampled)
return [sampled_classes, count_true, count_sampled]
示例2: reset
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import array [as 别名]
def reset(self):
"""Resets the iterator to the beginning of the data."""
self.curr_idx = 0
#shuffle data in each bucket
random.shuffle(self.idx)
for i, buck in enumerate(self.sentences):
self.indices[i], self.sentences[i], self.characters[i], self.label[i] = shuffle(self.indices[i],
self.sentences[i],
self.characters[i],
self.label[i])
self.ndindex = []
self.ndsent = []
self.ndchar = []
self.ndlabel = []
#for each bucket of data
for i, buck in enumerate(self.sentences):
#append the lists with an array
self.ndindex.append(ndarray.array(self.indices[i], dtype=self.dtype))
self.ndsent.append(ndarray.array(self.sentences[i], dtype=self.dtype))
self.ndchar.append(ndarray.array(self.characters[i], dtype=self.dtype))
self.ndlabel.append(ndarray.array(self.label[i], dtype=self.dtype))
示例3: test_grad_with_stype
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import array [as 别名]
def test_grad_with_stype():
def check_grad_with_stype(array_stype, grad_stype, expected_stype):
x = mx.nd.zeros((1, 1), stype=array_stype)
x.attach_grad(stype=grad_stype)
# check grad attached
assert x.grad.stype == expected_stype
y = x.detach()
# check array detached
assert y.stype == array_stype
stypes = ['default', 'csr', 'row_sparse']
for stype in stypes:
# check the default stype of the gradient (same as the array stype)
check_grad_with_stype(stype, None, stype)
for grad_stype in stypes:
# check the stype of the gradient when provided
check_grad_with_stype(stype, grad_stype, grad_stype)
示例4: test_out_grads
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import array [as 别名]
def test_out_grads():
x = nd.ones((3, 5))
dx = nd.zeros_like(x)
mark_variables([x], [dx])
da = None
db = nd.array([1,2,3,4,5])
dc = nd.array([5,4,3,2,1])
with train_section():
a, b, c = nd.split(x, axis=0, num_outputs=3, squeeze_axis=True)
backward([a, b, c], [da, db, dc])
assert (dx.asnumpy() == np.array(
[[1,1,1,1,1],
[1,2,3,4,5],
[5,4,3,2,1]])).all()
示例5: next_sample
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import array [as 别名]
def next_sample(self):
"""Helper function for reading in next sample."""
if self.cur >= len(self.seq):
raise StopIteration
idx = self.seq[self.cur]
self.cur += 1
uv_path = self.uv_file_list[idx]
image_path = self.image_file_list[idx]
uvmap = np.load(uv_path)
img = cv2.imread(image_path)[:,:,::-1]#to rgb
hlabel = uvmap
#print(hlabel.shape)
#hlabel = np.array(header.label).reshape( (self.output_label_size, self.output_label_size, self.num_classes) )
hlabel /= self.input_img_size
return img, hlabel
示例6: pairwise_dists
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import array [as 别名]
def pairwise_dists(self, embeddings):
nd_embedding_list = []
for i in xrange(self.ctx_num):
nd_embedding = mx.nd.array(embeddings, mx.gpu(i))
nd_embedding_list.append(nd_embedding)
nd_pdists = []
pdists = []
for idx in xrange(embeddings.shape[0]):
emb_idx = idx%self.ctx_num
nd_embedding = nd_embedding_list[emb_idx]
a_embedding = nd_embedding[idx]
body = mx.nd.broadcast_sub(a_embedding, nd_embedding)
body = body*body
body = mx.nd.sum_axis(body, axis=1)
nd_pdists.append(body)
if len(nd_pdists)==self.ctx_num or idx==embeddings.shape[0]-1:
for x in nd_pdists:
pdists.append(x.asnumpy())
nd_pdists = []
return pdists
示例7: generate_anchors_fpn
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import array [as 别名]
def generate_anchors_fpn(cfg):
"""
Generate anchor (reference) windows by enumerating aspect ratios X
scales wrt a reference (0, 0, 15, 15) window.
"""
RPN_FEAT_STRIDE = []
for k in cfg:
RPN_FEAT_STRIDE.append( int(k) )
RPN_FEAT_STRIDE = sorted(RPN_FEAT_STRIDE, reverse=True)
anchors = []
for k in RPN_FEAT_STRIDE:
v = cfg[str(k)]
bs = v['BASE_SIZE']
__ratios = np.array(v['RATIOS'])
__scales = np.array(v['SCALES'])
stride = int(k)
#print('anchors_fpn', bs, __ratios, __scales, file=sys.stderr)
r = generate_anchors(bs, __ratios, __scales, stride)
#print('anchors_fpn', r.shape, file=sys.stderr)
anchors.append(r)
return anchors
示例8: next_sample
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import array [as 别名]
def next_sample(self):
"""Helper function for reading in next sample."""
if self.cur >= len(self.seq):
raise StopIteration
idx = self.seq[self.cur]
self.cur += 1
s = self.imgrec.read_idx(idx)
header, img = recordio.unpack(s)
img = mx.image.imdecode(img).asnumpy()
hlabel = np.array(header.label).reshape( (self.num_classes,2) )
if not config.label_xfirst:
hlabel = hlabel[:,::-1] #convert to X/W first
annot = {'scale': config.base_scale}
#ul = np.array( (50000,50000), dtype=np.int32)
#br = np.array( (0,0), dtype=np.int32)
#for i in range(hlabel.shape[0]):
# h = int(hlabel[i][0])
# w = int(hlabel[i][1])
# key = np.array((h,w))
# ul = np.minimum(key, ul)
# br = np.maximum(key, br)
return img, hlabel, annot
示例9: pad_packed_tensor
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import array [as 别名]
def pad_packed_tensor(input, lengths, value, l_min=None):
old_shape = input.shape
if isinstance(lengths, nd.NDArray):
max_len = as_scalar(input.max())
else:
max_len = builtins.max(lengths)
if l_min is not None:
max_len = builtins.max(max_len, l_min)
batch_size = len(lengths)
ctx = input.context
dtype = input.dtype
x = nd.full((batch_size * max_len, *old_shape[1:]), value, ctx=ctx, dtype=dtype)
index = []
for i, l in enumerate(lengths):
index.extend(range(i * max_len, i * max_len + l))
index = nd.array(index, ctx=ctx)
return scatter_row(x, index, input).reshape(batch_size, max_len, *old_shape[1:])
示例10: face_create_lib
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import array [as 别名]
def face_create_lib(args, npz_embs, npz_emb_len, embedding, item, fd, max_nums):
# 得到当前ID的总个数
id_sum = npz_embs.shape[0]
# 为新的人脸入库,创建一个新的文件夹
new_lib_dir = os.path.join(args.outdir, '%08d' % id_sum)
os.mkdir(new_lib_dir)
# 为了统一,扩展一个维度
embedding = np.expand_dims(embedding, axis=0)
# 特征向量以及对应的ID图片的数目,都进行垂直拼接
npz_embs = np.vstack((npz_embs, embedding.reshape(1, -1)))
npz_emb_len = np.vstack((npz_emb_len, np.array([[1]])))
new_img_path = os.path.join(new_lib_dir, '00000' + args.encoding)
old_img_path = os.path.join(args.indir, item[1])
fd.write(old_img_path + '\t' + new_img_path + '\t' + str(max_nums) + '\n\n')
shutil.copyfile(old_img_path, new_img_path)
if args.delete:
os.remove(old_img_path)
return npz_embs,npz_emb_len
示例11: SGD
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import array [as 别名]
def SGD(sym, data_inputs, X, Y, X_test, Y_test, total_iter_num,
lr=None,
lr_scheduler=None, prior_precision=1,
out_grad_f=None,
initializer=None,
minibatch_size=100, dev=mx.gpu()):
if out_grad_f is None:
label_key = list(set(data_inputs.keys()) - set(['data']))[0]
exe, params, params_grad, _ = get_executor(sym, dev, data_inputs, initializer)
optimizer = mx.optimizer.create('sgd', learning_rate=lr,
rescale_grad=X.shape[0] / minibatch_size,
lr_scheduler=lr_scheduler,
wd=prior_precision)
updater = mx.optimizer.get_updater(optimizer)
start = time.time()
for i in range(total_iter_num):
indices = numpy.random.randint(X.shape[0], size=minibatch_size)
X_batch = X[indices]
Y_batch = Y[indices]
exe.arg_dict['data'][:] = X_batch
if out_grad_f is None:
exe.arg_dict[label_key][:] = Y_batch
exe.forward(is_train=True)
exe.backward()
else:
exe.forward(is_train=True)
exe.backward(out_grad_f(exe.outputs, nd.array(Y_batch, ctx=dev)))
for k in params:
updater(k, params_grad[k], params[k])
if (i + 1) % 500 == 0:
end = time.time()
print("Current Iter Num: %d" % (i + 1), "Time Spent: %f" % (end - start))
sample_test_acc(exe, X=X_test, Y=Y_test, label_num=10, minibatch_size=100)
start = time.time()
return exe, params, params_grad
示例12: prepare_sequence
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import array [as 别名]
def prepare_sequence(seq, word2idx):
return nd.array([word2idx[w] for w in seq])
# Compute log sum exp is numerically more stable than multiplying probabilities
示例13: _score_sentence
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import array [as 别名]
def _score_sentence(self, feats, tags):
# Gives the score of a provided tag sequence
score = nd.array([0])
tags = nd.concat(nd.array([self.tag2idx[START_TAG]]), *tags, dim=0)
for i, feat in enumerate(feats):
score = score + \
self.transitions.data()[to_scalar(tags[i+1]), to_scalar(tags[i])] + feat[to_scalar(tags[i+1])]
score = score + self.transitions.data()[self.tag2idx[STOP_TAG],
to_scalar(tags[int(tags.shape[0]-1)])]
return score
示例14: calculate_avg_q
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import array [as 别名]
def calculate_avg_q(samples, qnet):
total_q = 0.0
for i in range(len(samples)):
state = nd.array(samples[i:i + 1], ctx=qnet.ctx) / float(255.0)
total_q += qnet.forward(is_train=False, data=state)[0].asnumpy().max(axis=1).sum()
avg_q_score = total_q / float(len(samples))
return avg_q_score
示例15: calculate_avg_reward
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import array [as 别名]
def calculate_avg_reward(game, qnet, test_steps=125000, exploartion=0.05):
game.force_restart()
action_num = len(game.action_set)
total_reward = 0
steps_left = test_steps
episode = 0
while steps_left > 0:
# Running New Episode
episode += 1
episode_q_value = 0.0
game.begin_episode(steps_left)
start = time.time()
while not game.episode_terminate:
# 1. We need to choose a new action based on the current game status
if game.state_enabled:
do_exploration = (npy_rng.rand() < exploartion)
if do_exploration:
action = npy_rng.randint(action_num)
else:
# TODO Here we can in fact play multiple gaming instances simultaneously and make actions for each
# We can simply stack the current_state() of gaming instances and give prediction for all of them
# We need to wait after calling calc_score(.), which makes the program slow
# TODO Profiling the speed of this part!
current_state = game.current_state()
state = nd.array(current_state.reshape((1,) + current_state.shape),
ctx=qnet.ctx) / float(255.0)
action = int(nd.argmax_channel(
qnet.forward(is_train=False, data=state)[0]).asscalar())
else:
action = npy_rng.randint(action_num)
# 2. Play the game for a single mega-step (Inside the game, the action may be repeated for several times)
game.play(action)
end = time.time()
steps_left -= game.episode_step
print('Episode:%d, FPS:%s, Steps Left:%d, Reward:%d' \
% (episode, game.episode_step / (end - start), steps_left, game.episode_reward))
total_reward += game.episode_reward
avg_reward = total_reward / float(episode)
return avg_reward