简体   繁体   中英

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

If I train the.network like this:

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:

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

As @Plagon commented, you can't create an MLPClassifier from weights and biases. Instead, you should import pickle and use it like:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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