如何为新的训练模型初始化coef_init和intercept_init? [英] How to initialize coef_init and intercept_init for a new training model?

查看:177
本文介绍了如何为新的训练模型初始化coef_init和intercept_init?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如此处指定, https://stackoverflow.com/a/35662770/5757129 ,我存储了截取我的第一个模型.稍后,我将它们作为初始值设定项传递给我的第二个fit(),如下所示,以便在旧模型之上学习新数据.

As specified here, https://stackoverflow.com/a/35662770/5757129, i stored the coef and intercept of my first model. Later, i am passing them as initializers to my second fit() as shown below for learning new data on top of old model.

from sklearn import neighbors, linear_model
import numpy as np
import pickle
import os

def train_data():

    x1 = [[8, 9], [20, 22], [16, 18], [8,4]]
    y1 = [0, 1, 2, 3]

    #classes = np.arange(10)

    #sgd_clf = linear_model.SGDClassifier(learning_rate = 'constant', eta0 = 0.1, shuffle = False, n_iter = 1,warm_start=True)

    sgd_clf = linear_model.SGDClassifier(loss="hinge",max_iter=10000)

    sgd_clf.fit(x1,y1)

    coef = sgd_clf.coef_
    intercept = sgd_clf.intercept_

    return coef, intercept


def train_new_data(coefs,intercepts):

    x2 = [[18, 19],[234,897],[20, 122], [16, 118]]
    y2 = [4,5,6,7]

    sgd_clf1 = linear_model.SGDClassifier(loss="hinge",max_iter=10000)

    new_model = sgd_clf1.fit(x2,y2,coef_init=coefs,intercept_init=intercepts)

    return new_model


if __name__ == "__main__":

    coefs,intercepts= train_data()

    new_model = train_new_data(coefs,intercepts)

    print(new_model.predict([[16, 118]]))
    print(new_model.predict([[18, 19]]))
    print(new_model.predict([[8,9]]))
    print(new_model.predict([[20,22]]))

运行此命令时,我只能从new_model获得训练的标签.例如,print(new_model.predict([[8,9]]))必须将标签打印为0,而print(new_model.predict([[20,22]]))必须将标签打印为1,但是它将打印从4到7匹配的标签.

When i run this, i get the lables that are trained only from new_model. For instance, print(new_model.predict([[8,9]])) has to print label as 0 and print(new_model.predict([[20,22]])) has to print label as 1. But it prints lables matching from 4 to 7.

我是不是通过错误的方式通过了折角并从旧模型截取到新模型?

Am i passing the coef and intercepts from old model to the new one in wrong way ?

按照@vital_dml答案重新构建问题

Reframed the question as per @vital_dml answer

推荐答案

我不确定为什么需要传递系数并从第一个模型到第二个模型进行拦截,但是,由于第一个模型是经过训练的,因此会出现此类错误针对4个类y1 = [0, 1, 2, 3],而第二个具有2个类y2 = [4,5],这是有争议的.

I'm not sure why you need to pass coefficients and intercept from 1st model to the 2nd, however, you are getting such error because your 1st model is trained against 4 classes y1 = [0, 1, 2, 3], while 2nd one has 2 classes y2 = [4,5], which is controversial.

根据 scikit-learn文档,您的linear_model.SGDClassifier()返回:

coef_:如果n_classes == 2,则数组的形状为(1,n_features),否则为(n_classes,n_features)-分配给要素的权重.

coef_ : array, shape (1, n_features) if n_classes == 2 else (n_classes, n_features) - Weights assigned to the features.

intercept_:如果n_classes == 2,则数组的形状为(1,),否则为(n_classes,)- 决策函数中的常量.

intercept_ : array, shape (1,) if n_classes == 2 else (n_classes,) - Constants in decision function.

因此,在您的问题中,两个模型中的类和功能的数量必须相同.

So, within your question, the number of classes and features in both models have to be the same.

无论如何,我鼓励您考虑是否真的需要这样做?也许您可以串联这些向量.

Anyway, I encourage you to think do you really need to do that? Maybe you could just concatenate those vectors.

这篇关于如何为新的训练模型初始化coef_init和intercept_init?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆