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

时间:2021-02-17 04:44:28

标签: python python-2.7 machine-learning

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

...
</head>
<body>
...
    <canvas class="Wrapper vector-canvas" oncontextmenu="event.preventDefault()" style="width: 125px; 
     height: 95px;"></canvas>
...
</body>

我所有的模型(Pa1、Ogd、Arow)都会发生这种情况

这是我计划的一部分:

Pa1 instance has no attribute 'load'`

我保存模型的方式:

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)

这是我的 myfile_1.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')

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

1 个答案:

答案 0 :(得分:0)

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

提取模型的层/权重