简体   繁体   English

使用sci-kit Learn从ANN检索矩阵

[英]retrieve matrix from ANN using sci-kit learn

USing python 3.5, I do the following 使用python 3.5,我执行以下操作

from sklearn.neural_network import MLPRegressor
import pandas as pd

from sklearn import datasets from sklearn.decomposition import PCA 从sklearn导入数据集从sklearn.decomposition导入PCA

# import some data to play with
iris = datasets.load_iris()
X = iris.data[0:100, :2]  # we only take the first two features.
Y = iris.target[0:100]

X = StandardScaler().fit_transform(X)

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.15)
reg = MLPRegressor(hidden_layer_sizes=(10,),
                           solver="lbfgs", #solver="lbfgs",
                           max_iter = 10000,
                           activation = 'relu')
reg.fit(X_train,y_train)

i can get the matrix using reg.coefs_[0] but when I multiply it by a test case, I don't get 0 or 1 我可以使用reg.coefs_[0]获得矩阵,但是当我将其乘以测试用例时,我得不到0或1

what I'm trying to do is train an ANN based on lab data, and then implement it as matrix multiplication on a piece of hardware. 我要做的是根据实验室数据训练ANN,然后将其实现为硬件上的矩阵乘法。 The hardware can do basic arithmetic, so I need to deconstruct my ANN into a matrix, and then program matrix multiplication as iterated multiplication/addition 硬件可以执行基本算术运算,因此我需要将ANN解构为矩阵,然后将矩阵乘法编程为迭代乘法/加法

The short, "specific to this question" answer is 简短的“特定于此问题”的答案是

np.dot((np.dot(X_test[0],reg.coefs_[0]) +reg.intercepts_[0] ),reg.coefs_[1]) + reg.intercepts_[1]

The long answer is as follows (even longer version here: https://www.mohammadathar.com/blog/2017/2/15/a-different-look-at-neural-networks ) 长答案如下(甚至更长的版本在这里: https : //www.mohammadathar.com/blog/2017/2/15/a-different-look-at-neural-networks

A perceptron neural net can be modeled like this: 感知器神经网络可以这样建模: 在此处输入图片说明

What that really means, is "multiply input values by a weight, and add. then add in a bias value. Then activate. Then multiply by weights, and add. Then activate. And repeat!" 真正的意思是“将输入值乘以权重,然后相加。然后加上一个偏差值。然后激活。然后乘以权重并相加,然后激活。然后重复!”

As matrix math, it looks like this 作为矩阵数学,它看起来像这样

在此处输入图片说明

So the reg.coefs_ is the matrix coefficients (per layer) and the reg.intercepts_ is the activation values (again, per layer) 因此, reg.coefs_是矩阵系数(每层), reg.intercepts_是激活值(同样,每层)

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

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