当前位置: 首页>>代码示例>>Python>>正文


Python nd.one_hot方法代码示例

本文整理汇总了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) 
开发者ID:tonysy,项目名称:CapsuleNet-Gluon,代码行数:15,代码来源:CapsuleNet.py

示例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 
开发者ID:d2l-ai,项目名称:d2l-zh,代码行数:7,代码来源:utils.py

示例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] 
开发者ID:d2l-ai,项目名称:d2l-zh,代码行数:5,代码来源:utils.py

示例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
        )) 
开发者ID:sxhxliang,项目名称:CapsNet_Mxnet,代码行数:41,代码来源:utils.py

示例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) 
开发者ID:sxhxliang,项目名称:CapsNet_Mxnet,代码行数:16,代码来源:CapsLayers.py


注:本文中的mxnet.nd.one_hot方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。