本文整理匯總了Python中mxnet.nd.one_hot方法的典型用法代碼示例。如果您正苦於以下問題:Python nd.one_hot方法的具體用法?Python nd.one_hot怎麽用?Python nd.one_hot使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mxnet.nd
的用法示例。
在下文中一共展示了nd.one_hot方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: hybrid_forward
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import one_hot [as 別名]
def hybrid_forward(self, F, images, num_classes, labels, X_l2norm,
lambda_value = 0.5, sample_weight=None):
self.num_classes = num_classes
labels_onehot = nd.one_hot(labels, num_classes)
first_term_base = F.square(nd.maximum(0.9-X_l2norm,0))
second_term_base = F.square(nd.maximum(X_l2norm -0.1, 0))
# import pdb; pdb.set_trace()
margin_loss = labels_onehot * first_term_base + lambda_value * (1-labels_onehot) * second_term_base
margin_loss = margin_loss.sum(axis=1)
loss = F.mean(margin_loss, axis=self._batch_axis, exclude=True)
loss = _apply_weighting(F, loss, self._weight/2, sample_weight)
return F.mean(loss, axis=self._batch_axis, exclude=True)
示例2: forward
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import one_hot [as 別名]
def forward(self, inputs, state):
X = nd.one_hot(inputs.T, self.vocab_size)
Y, state = self.rnn(X, state)
output = self.dense(Y.reshape((-1, Y.shape[-1])))
return output, state
示例3: to_onehot
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import one_hot [as 別名]
def to_onehot(X, size):
"""Represent inputs with one-hot encoding."""
return [nd.one_hot(x, size) for x in X.T]
示例4: train
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import one_hot [as 別名]
def train(train_data, test_data, net, loss, trainer, ctx, num_epochs, print_batches=100):
"""Train a network"""
for epoch in range(num_epochs):
train_loss = 0.
train_acc = 0.
n = 0
for i, (data, label) in tqdm(enumerate(train_data), total=len(train_data), ncols=70, leave=False, unit='b'):
# for i, batch in enumerate(train_data):
# data, label = batch
one_hot_label = nd.one_hot(label,10)
label = label.as_in_context(ctx)
one_hot_label = one_hot_label.as_in_context(ctx)
data = data.as_in_context(ctx)
with autograd.record():
output = net(data)
L = loss(output, one_hot_label)
L.backward()
trainer.step(data.shape[0])
train_loss += nd.mean(L).asscalar()
# print('nd.mean(L).asscalar()',nd.mean(L).asscalar())
train_acc += accuracy(output, label)
n = i + 1
if print_batches and n % print_batches == 0:
print('output',output)
print("Batch %d. Loss: %f, Train acc %f" % (
n, train_loss/n, train_acc/n
))
# print('train_loss',train_loss)
test_acc = evaluate_accuracy(test_data, net, ctx)
print("Epoch %d. Loss: %f, Train acc %f, Test acc %f" % (
epoch, train_loss/n, train_acc/n, test_acc
))
示例5: forward
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import one_hot [as 別名]
def forward(self,labels,y_pred):
labels_onehot = labels #nd.one_hot(labels, self.num_classes)
first_term_base = nd.square(nd.maximum(0.9-y_pred,0))
second_term_base = nd.square(nd.maximum(y_pred -0.1, 0))
# import pdb; pdb.set_trace()
margin_loss = labels_onehot * first_term_base + self.lambda_value * (1-labels_onehot) * second_term_base
margin_loss = margin_loss.sum(axis=1)
loss = nd.mean(margin_loss, axis=self._batch_axis, exclude=True)
loss = _apply_weighting(nd, loss, self._weight/2, self.sample_weight)
return nd.mean(loss, axis=self._batch_axis, exclude=True)