【问题标题】:Reconstructing Sklearn MLP Regression in MatLab在 MatLab 中重构 Sklearn MLP 回归
【发布时间】:2022-10-31 13:22:46
【问题描述】:

我正在使用 Sklearn 在 12 个特征和一个输出上训练多层感知器回归。 StandardScalar() 适合训练数据并应用于所有输入数据。经过一段时间的架构优化训练后,我得到了一个看起来相当准确的模型(<10% 误差)。我现在需要提取权重和偏差,以便在与人交互的系统上实时实现预测。这是通过 my_model.coefs_ 的权重和 my_model.intercepts_ 的偏差来完成的。权重针对我的模型中的节点数量进行了适当的调整,并且偏差对于每一层都有适当的长度。

现在的问题是,我在 MatLab 中实现了矩阵代数,并得到了与 my_model.predict() 产生的完全不同的预测。

我对 2 层 MLP 的重建过程(第一层有 11 个节点,第二层有 10 个节点):

scale()             % elementwise subtract feature mean and divide by feature stdev
scaled_obs = scale(raw_obs)  
% Up to this point results from MatLab == Sklearn

weight1 = [12x11]   % weights to transition from the input layer to the first hidden layer
weight2 = [11x10]
weight3 = [10x1]
bias1 = [11x1]      % bias to add to the first layer after weight1 has been applied
bias2 = [10x1]
bias3 = [1x1]

my_prediction = ((( scaled_obs * w1 + b1') * w2  + b2') * w3  + b3);

我也试过

my_prediction2 = ((( scaled_obs * w1 .* b1') * w2  .* b2') * w3  .* b3);   % because nothing worked...```

对于我的具体数据:

Sklearn prediction = 1.731
my_prediction = -50.347
my_prediction2 = -3.2075

从 my_model 提取相关参数时,我是否跳过了另一个权重/偏差?我在重建中的操作顺序是否有缺陷?

【问题讨论】:

    标签: matlab machine-learning scikit-learn neural-network


    【解决方案1】:

    在我看来my_prediction = ((( scaled_obs * w1 + b1') * w2 + b2') * w3 + b3); 是正确的,但只有一个缺失的部分,那就是激活函数。您为模型传递的激活函数是什么。默认情况下,MLPRegressorrelu 作为从第一层到倒数第三层(包括)的激活函数。倒数第二层没有任何激活功能。并且输出层有一个单独的激活函数,它是identity 函数,基本上是f(x) = x,所以你不必为此做任何事情。

    如果您选择了relu 或者您根本没有选择激活(然后relu 是默认值),那么您必须在numpy 中像np.maximum(0, your_layer1_calculation) 那样做类似的事情,我不确定这是如何完成的matlab

    所以最终的公式是:

    layer1 = np.dot(scaled_inputs, weight0) + bias0
    layer2 = np.dot(np.maximum(0, layer1), weight1) + bias1
    layer......
    layer(n-1) = np.dot(np.maximum(0, layer(n-2), weight(n-1)) + bias(n-1)
    layer(n) = layer(n-1) # identity function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-26
      • 2014-08-06
      • 2020-03-28
      • 2020-04-11
      • 2018-05-16
      • 2018-11-17
      • 2017-02-25
      • 2012-03-17
      相关资源
      最近更新 更多