【问题标题】:error while load machine learning model in python在 python 中加载机器学习模型时出错
【发布时间】:2021-02-17 19:37:21
【问题描述】:

我创建了 .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)

我保存模型的方式:

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。有什么解决办法吗?我应该改变保存模型的方式吗?

【问题讨论】:

    标签: python python-2.7 machine-learning


    【解决方案1】:

    您可以使用model.save('path/to/location') 将模型保存为.h5.keras 格式,并使用keras.models.load_model('path/to/location') 加载此模型。您还可以使用 model.get_layer() 提取模型的层/权重

    【讨论】:

      猜你喜欢
      • 2020-03-01
      • 1970-01-01
      • 2016-10-27
      • 1970-01-01
      • 2019-12-21
      • 2017-06-08
      • 1970-01-01
      • 2019-10-27
      • 2020-12-03
      相关资源
      最近更新 更多