繁体   English   中英

在 python 中加载机器学习 model 时出错

[英]error while load machine learning model in python

我创建了 format.model 格式的机器学习模型。 我需要加载我的机器学习模型以便从模型中获取权重。 但是每当我尝试加载 model 时,它都会显示错误:

Pa1 instance has no attribute 'load'`

它发生在我所有的模型上(Pa1、Ogd、Arow)

这是我的程序的一部分:

global clfs

clfs = [Pa1(), Ogd(), Arow()] #this is my model pool
print 'model pool size: ', len(clfs)
filedir = '/home/myfile'

for i in xrange(len(clfs)): # for each model in the model pool

    clfs[i].load( filedir + '_' + str(i) + '.model')
    
    # get original model weight
    w = clfs[i].coef_[1:]
    weights = []
    weight = [] # [i][j]: i = model index, j = feature index
    for w_num in xrange(len(w)):
        weight.append(w[w_num])
    weights.append(weight)

我保存 model 的方式:

for i in xrange(len(clfs)): # i = every model in model pool
        print clfs[i]
        print 'training'
        train_accuracy=clfs[i].fit(X_train,Y_train)
        ori_train_acc.append(train_accuracy)
        clfs[i].save( filedir + '_' + str(i) + '.model')

这是我的 myfile_1.model (只是其中的一部分,因为它太长了)

model info:
{
    "clf_num" : 1,
    "cls_num" : 2,
    "loss" : "hinge",
    "model" : "pa1",
    "norm" : 0,
    "online" : 
    {
        "C" : 1,
        "bias_eta" : 0,
        "dim" : 36599,
        "lazy_update" : "false",
        "t" : 200
    }
}
model parameters:
weight[0]:
[ 0 -0.219641 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.0523282 -0.0452756 -0.0523282 -0.0043332 -0.000975608 9.32486e-05 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.00150336 -0.0131961 -0.00150336 -9.93554e-05 -0.0160645 -0.00972448 -0.00161313 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.0116229 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.0337869 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.00150336 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.000975608 -0.00618885 -0.0265526 -0.0265526 0.0138585 -0.00191089 0.00196821 -0.000975608 -0.000975608 0.00223302 -0.0121794 -0.000975608 -0.000975608 -0.0195 -0.00972448 -0.00180573 -0.00108076 -0.000975608 -0.000975608 -0.000975608]

我正在使用 python 2.7。 有什么解决办法吗? 我应该改变保存 model 的方式吗?

You can use model.save('path/to/location') to save your model in .h5 or .keras format and load this model by using keras.models.load_model('path/to/location') . 您还可以使用model.get_layer()提取 model 的层/权重

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM