【问题标题】:nolearn for multi-label classificationnolearn 用于多标签分类
【发布时间】:2015-11-18 03:56:40
【问题描述】:

我尝试使用从 nolearn 包导入的 DBN 函数,这是我的代码:

from nolearn.dbn import DBN
import numpy as np
from sklearn import cross_validation

fileName = 'data.csv'
fileName_1 = 'label.csv'

data = np.genfromtxt(fileName, dtype=float, delimiter = ',')
label = np.genfromtxt(fileName_1, dtype=int, delimiter = ',')

clf = DBN(
    [data, 300, 10],
    learn_rates=0.3,
    learn_rate_decays=0.9,
    epochs=10,
    verbose=1,
    )

clf.fit(data,label)
score = cross_validation.cross_val_score(clf, data, label,scoring='f1', cv=10)
print score

由于我的数据具有形状 (1231, 229) 和带有形状 (1231,13) 的标签,因此标签集看起来像 ([0 0 1 0 1 0 1 0 0 0 1 1 0] ..., [....]),当我运行我的代码时,我收到了这个错误消息:输入形状错误(1231,13)。我想知道这里可能会出现两个问题:

  1. DBN 不支持多标签分类
  2. 我的标签不适合在 DBN 拟合函数中使用。

【问题讨论】:

    标签: python multilabel-classification dbn nolearn


    【解决方案1】:

    Fit 调用 BuildDBN,可以在这里找到 here 重要的一点是,dbn 已被弃用,您只能在 old_commits 找到它。无论如何,如果您正在寻找额外的信息,那么从我在您的 sn-p 中看到的内容中检查这两个可能是件好事,那就是 DBN 的第一个参数,即 [data, 300, 10] 应该是 [data.shape[1], 300, 10] 基于文档和源代码代码。希望这会有所帮助。

    【讨论】:

    • 谢谢。 DBN 是否支持多标签分类任务?我想知道这个问题也来自标签。
    【解决方案2】:

    正如弗朗西斯科·巴尔加斯所说,nolearn.dbn 已被弃用,您应该改用nolearn.lasagne(如果可以的话)。

    如果你想在千层面做多标签分类,那么你应该将你的regression参数设置为True,定义一个验证分数和一个自定义损失。

    这是一个例子:

    import numpy as np
    import theano.tensor as T
    from lasagne import layers
    from lasagne.updates import nesterov_momentum
    from nolearn.lasagne import NeuralNet
    from nolearn.lasagne import BatchIterator
    from lasagne import nonlinearities
    
    # custom loss: multi label cross entropy
    def multilabel_objective(predictions, targets):
        epsilon = np.float32(1.0e-6)
        one = np.float32(1.0)
        pred = T.clip(predictions, epsilon, one - epsilon)
        return -T.sum(targets * T.log(pred) + (one - targets) * T.log(one - pred), axis=1)
    
    
    net = NeuralNet(
        # customize "layers" to represent the architecture you want
        # here I took a dummy architecture
        layers=[(layers.InputLayer, {"name": 'input', 'shape': (None, 1, 229, 1)}),
    
                (layers.DenseLayer, {"name": 'hidden1', 'num_units': 20}),
                (layers.DenseLayer, {"name": 'output', 'nonlinearity': nonlinearities.sigmoid, 'num_units': 13})], #because you have 13 outputs
    
        # optimization method:
        update=nesterov_momentum,
        update_learning_rate=5*10**(-3),
        update_momentum=0.9,
    
        max_epochs=500,  # we want to train this many epochs
        verbose=1,
    
        #Here are the important parameters for multi labels
        regression=True,  
    
        objective_loss_function=multilabel_objective,
        custom_score=("validation score", lambda x, y: np.mean(np.abs(x - y)))
    
        )
    
    net.fit(X_train, labels_train)
    

    【讨论】:

    • 谢谢马西亚斯。当我尝试测试您的示例时,我遇到了导入错误:无法导入名称 mse。我在网上搜索了这个错误,很多人说Lasagne和nolearn有问题,不兼容。我正在使用 no learn 0.5。
    • @Fox 我也遇到了这个问题,有时theano 和 lasagne 的版本不同意。从命令行依次运行以下行:首先是pip install -r https://raw.githubusercontent.com/Lasagne/Lasagne/master/requirements.txt,然后是pip install https://github.com/Lasagne/Lasagne/archive/master.zip;之后它应该可以工作了
    • 我按照你的指示,仍然有这个问题。我检查了要求,我的 Theano 版本有问题吗?我的是 0.7.0。
    • @Fox 我有 theano 0.7.0、nolearn 0.6a0.dev0 和千层面 0.1。你也一样?您可以检查 pip show
    • 我有theano 0.7.0、nolearn 0.5和lasagne 0.2.dev1,看起来nolearn和lasagne都和你的不一样。
    猜你喜欢
    • 2017-04-16
    • 1970-01-01
    • 2018-02-16
    • 2020-02-21
    • 2018-05-26
    • 2019-04-01
    • 2018-08-01
    • 2015-09-22
    相关资源
    最近更新 更多