用MLPClassifier分成三類並用矩陣演算演示了一下+用MLPRegressor去證實我的想法
阿新 • • 發佈:2021-01-11
技術標籤:機器學習
文章目錄
用MLPClassifier分成三類並用矩陣演算演示了一下
from sklearn.neural_network import MLPClassifier
import numpy as np
X = [[0., 0.], [1., 1.],[3,3]]
y = [0, 1,2]
clf = MLPClassifier(solver='lbfgs', alpha=1e-5,
hidden_layer_sizes= (2,),
random_state=1, activation='identity')
clf.fit(X, y)
print (clf.predict([[1.2,1]]))
a = np.matrix(clf.coefs_[0])
b = np.matrix(clf.coefs_[1])
theta1 = np.matrix(clf.intercepts_[0])
theta1 = theta1.T
theta2 = np.matrix(clf.intercepts_[1])
theta2 = theta2.T
x=np.matrix([[1.2 ],[1]])
result = np.exp(b.T * (a.T*x+ theta1)+theta2)
print (result/sum(result))
用MLPRegressor去證實我的想法!
- 我證實了輸出層的啟用函式果然是恆等對映呦呦!
from sklearn.neural_network import MLPRegressor
import numpy as np
X = [[0., 0.], [1., 1.],[3,3]]
y = [0., 1.,2.]
clf = MLPRegressor(solver='lbfgs', alpha=1e-5,tol = 1e-6,
hidden_layer_sizes= (2,),
random_state=1, activation='identity')
clf.fit(X, y)
print (clf.predict([[3,3]]))
a = np.matrix(clf.coefs_[0])
b = np.matrix(clf.coefs_[1])
theta1 = np.matrix(clf.intercepts_[0])
theta1 = theta1.T
theta2 = np.matrix(clf.intercepts_[1])
theta2 = theta2.T
x=np.matrix([[3.],[3.]])
result = b.T * (a.T*x+ theta1)+theta2
- 上面的predict的結果和我寫的result是一樣的!
- 真是太神奇啦!