简体   繁体   English

如何根据权重和偏差创建 MPLClassifier? (巨蟒 3)

[英]How to create an MPLClassifier from weights and biases? (Python 3)

I am trying to create an MPLClassifier with predefined weights and biases so that I can save them to a file and then我正在尝试创建一个具有预定义权重和偏差的 MPLClassifier,以便我可以将它们保存到一个文件中,然后

If I train the.network like this:如果我像这样训练 the.network:

import numpy as np
from sklearn.neural_network import MLPClassifier

data = np.load("data.npy")
labels = np.load("labels.npy")

clf = MLPClassifier()
clf.fit(data, labels)

np.save("weights.npy", clf.coefs_)
np.save("biases.npy", clf.intercepts_)

and then access the weights an biases like this:然后像这样访问权重和偏差:

import numpy as np
from sklearn.neural_network import MLPClassifier

weights = np.load("weights.npy")
biases = np.load("biases.npy")

I want to be able to create a new.network like:我希望能够像这样创建一个 new.network:

clf = MLPClassifier(weights=weights, biases=biases)

As @Plagon commented, you can't create an MLPClassifier from weights and biases.正如@Plagon 评论的那样,您不能根据权重和偏差创建 MLPClassifier。 Instead, you should import pickle and use it like:相反,您应该导入 pickle 并像这样使用它:

with open("network.pkl", "wb") as network:
    pickle.dump(clf, network)

and access it like:并像这样访问它:

with open("network.pkl", "wb") as network:
    clf = pickle.load(network)

For more information on pickle you can go its documentation at https://docs.python.org/3/library/pickle.html .有关 pickle 的更多信息,您可以 go 在https://docs.python.org/3/library/pickle.html查看其文档。

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

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