Python mxnet.ndarray.softmax() Examples
The following are 13
code examples of mxnet.ndarray.softmax().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
mxnet.ndarray
, or try the search function
.
Example #1
Source File: rl_controller.py From autogluon with Apache License 2.0 | 6 votes |
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
Example #2
Source File: rl_controller.py From autogluon with Apache License 2.0 | 6 votes |
def inference(self): actions = [] for idx in range(len(self.num_tokens)): logits = self.decoders[idx](1) probs = F.softmax(logits, axis=-1) action = mx.nd.argmax(probs, 1) actions.append(action) config = {} for i, action in enumerate(actions): choice = action.asscalar() k, space = self.spaces[i] config[k] = int(choice) return config
Example #3
Source File: actor_critic.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def forward(self, x): x = self.dense(x) probs = self.action_pred(x) values = self.value_pred(x) return F.softmax(probs), values
Example #4
Source File: tensor.py From dgl with Apache License 2.0 | 5 votes |
def softmax(input, dim=-1): return nd.softmax(input, axis=dim)
Example #5
Source File: __init__.py From dgl with Apache License 2.0 | 5 votes |
def softmax(x, dim): return nd.softmax(x, axis=dim)
Example #6
Source File: test_recognizer.py From gluon-cv with Apache License 2.0 | 5 votes |
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)
Example #7
Source File: custom_layers.py From d-SNE with Apache License 2.0 | 5 votes |
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)
Example #8
Source File: actor_critic.py From training_results_v0.6 with Apache License 2.0 | 5 votes |
def forward(self, x): x = self.dense(x) probs = self.action_pred(x) values = self.value_pred(x) return F.softmax(probs), values
Example #9
Source File: mxnet_model.py From char-rnn-text-generation with MIT License | 5 votes |
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
Example #10
Source File: rl_controller.py From autogluon with Apache License 2.0 | 5 votes |
def inference(self): # self-attention x = self.embedding(1).reshape(-3, 0)#.squeeze() # b x action x h kshape = (1, self.num_total_tokens, self.hidden_size) vshape = (1, self.num_total_tokens, 1) querry = self.querry(x).reshape(*kshape) # b x actions x h key = self.key(x).reshape(*kshape) #b x actions x h value = self.value(x).reshape(*vshape) # b x actions x 1 atten = mx.nd.linalg_gemm2(querry, key, transpose_b=True).softmax(axis=1) alphas = mx.nd.linalg_gemm2(atten, value).squeeze(axis=-1) actions = [] for idx in range(len(self.num_tokens)): i0 = sum(self.num_tokens[:idx]) i1 = sum(self.num_tokens[:idx+1]) logits = alphas[:, i0: i1] probs = F.softmax(logits, axis=-1) action = mx.nd.argmax(probs, 1) actions.append(action) config = {} for i, action in enumerate(actions): choice = action.asscalar() k, space = self.spaces[i] config[k] = int(choice) return config
Example #11
Source File: actor_critic.py From SNIPER-mxnet with Apache License 2.0 | 5 votes |
def forward(self, x): x = self.dense(x) probs = self.action_pred(x) values = self.value_pred(x) return F.softmax(probs), values
Example #12
Source File: rl_controller.py From autogluon with Apache License 2.0 | 4 votes |
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
Example #13
Source File: rl_controller.py From autogluon with Apache License 2.0 | 4 votes |
def sample(self, batch_size=1, with_details=False, with_entropy=False): # self-attention x = self.embedding(batch_size).reshape(-3, 0)#.squeeze() # b x action x h kshape = (batch_size, self.num_total_tokens, self.hidden_size) vshape = (batch_size, self.num_total_tokens, 1) querry = self.querry(x).reshape(*kshape) # b x actions x h key = self.key(x).reshape(*kshape) #b x actions x h value = self.value(x).reshape(*vshape) # b x actions x 1 atten = mx.nd.linalg_gemm2(querry, key, transpose_b=True).softmax(axis=1) alphas = mx.nd.linalg_gemm2(atten, value).squeeze(axis=-1) actions = [] entropies = [] log_probs = [] for idx in range(len(self.num_tokens)): i0 = sum(self.num_tokens[:idx]) i1 = sum(self.num_tokens[:idx+1]) logits = alphas[:, i0: i1] 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) 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