本文整理汇总了Python中mxnet.ndarray.softmax方法的典型用法代码示例。如果您正苦于以下问题:Python ndarray.softmax方法的具体用法?Python ndarray.softmax怎么用?Python ndarray.softmax使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mxnet.ndarray
的用法示例。
在下文中一共展示了ndarray.softmax方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: inference
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import softmax [as 别名]
def inference(self):
inputs = self.static_inputs[1]
hidden = self.static_init_hidden[1]
actions = []
for block_idx in range(len(self.num_tokens)):
logits, hidden = self.forward(inputs, hidden,
block_idx, is_embed=(block_idx==0))
probs = F.softmax(logits, axis=-1)
action = mx.nd.argmax(probs, 1)
actions.append(action)
inputs = action + sum(self.num_tokens[:block_idx])
inputs.detach()
config = {}
for i, action in enumerate(actions):
choice = action.asscalar()
k, space = self.spaces[i]
config[k] = int(choice)
return config
示例2: forward
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import softmax [as 别名]
def forward(self, x):
x = self.dense(x)
probs = self.action_pred(x)
values = self.value_pred(x)
return F.softmax(probs), values
示例3: softmax
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import softmax [as 别名]
def softmax(input, dim=-1):
return nd.softmax(input, axis=dim)
示例4: softmax
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import softmax [as 别名]
def softmax(x, dim):
return nd.softmax(x, axis=dim)
示例5: test
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import softmax [as 别名]
def test(ctx, val_data, opt, net):
acc_top1 = mx.metric.Accuracy()
acc_top5 = mx.metric.TopKAccuracy(5)
for i, batch in enumerate(val_data):
data, label = batch_fn(batch, ctx)
outputs = []
for _, X in enumerate(data):
X = X.reshape((-1,) + X.shape[2:])
pred = net(X.astype(opt.dtype, copy=False))
if opt.use_softmax:
pred = F.softmax(pred, axis=1)
outputs.append(pred)
acc_top1.update(label, outputs)
acc_top5.update(label, outputs)
mx.ndarray.waitall()
_, cur_top1 = acc_top1.get()
_, cur_top5 = acc_top5.get()
if i > 0 and i % opt.log_interval == 0:
print('%04d/%04d is done: acc-top1=%f acc-top5=%f' % (i, len(val_data), cur_top1*100, cur_top5*100))
_, top1 = acc_top1.get()
_, top5 = acc_top5.get()
return (top1, top5)
示例6: hybrid_forward
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import softmax [as 别名]
def hybrid_forward(self, F, input_logits, target_logits, sample_weight=None):
input_softmax = F.softmax(input_logits, axis=1)
target_softmax = F.softmax(target_logits, axis=1)
loss = F.square(input_softmax - target_softmax)
return F.mean(loss, axis=self._batch_axis, exclude=True)
示例7: generate_text
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import softmax [as 别名]
def generate_text(model, seed, length=512, top_n=10):
"""
generates text of specified length from trained model
with given seed character sequence.
"""
logger.info("generating %s characters from top %s choices.", length, top_n)
logger.info('generating with seed: "%s".', seed)
generated = seed
encoded = mx.nd.array(encode_text(seed))
seq_len = encoded.shape[0]
x = F.expand_dims(encoded[:seq_len-1], 1)
# input shape: [seq_len, 1]
state = model.begin_state()
# get rnn state due to seed sequence
_, state = model(x, state)
next_index = encoded[seq_len-1].asscalar()
for i in range(length):
x = mx.nd.array([[next_index]])
# input shape: [1, 1]
logit, state = model(x, state)
# output shape: [1, vocab_size]
probs = F.softmax(logit)
next_index = sample_from_probs(probs.asnumpy().squeeze(), top_n)
# append to sequence
generated += ID2CHAR[next_index]
logger.info("generated text: \n%s\n", generated)
return generated
示例8: sample
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import softmax [as 别名]
def sample(self, batch_size=1, with_details=False, with_entropy=False):
"""
Returns
-------
configs : list of dict
list of configurations
"""
inputs = self.static_inputs[batch_size]
hidden = self.static_init_hidden[batch_size]
actions = []
entropies = []
log_probs = []
for idx in range(len(self.num_tokens)):
logits, hidden = self.forward(inputs, hidden,
idx, is_embed=(idx==0))
probs = F.softmax(logits, axis=-1)
log_prob = F.log_softmax(logits, axis=-1)
entropy = -(log_prob * probs).sum(1, keepdims=False) if with_entropy else None
action = mx.random.multinomial(probs, 1)
ind = mx.nd.stack(mx.nd.arange(probs.shape[0], ctx=action.context),
action.astype('float32'))
selected_log_prob = F.gather_nd(log_prob, ind)
actions.append(action[:, 0])
entropies.append(entropy)
log_probs.append(selected_log_prob)
inputs = action[:, 0] + sum(self.num_tokens[:idx])
inputs.detach()
configs = []
for idx in range(batch_size):
config = {}
for i, action in enumerate(actions):
choice = action[idx].asscalar()
k, space = self.spaces[i]
config[k] = int(choice)
configs.append(config)
if with_details:
entropies = F.stack(*entropies, axis=1) if with_entropy else entropies
return configs, F.stack(*log_probs, axis=1), entropies
else:
return configs