【问题标题】:Implementing Matlab's weights and bias on Neurolab (Neural Network)在 Neurolab(神经网络)上实现 Matlab 的权重和偏差
【发布时间】:2015-05-11 01:58:30
【问题描述】:

我将 Matlab 中的权重和偏差导出到 python 中以用于神经实验室。我的网络有 8 个输入、3 个输入权重数组、4 个分层权重数组和 4 个输出神经元。这是我第一次这样做,我真的需要帮助才能完成它。下面是我的实现和我得到的错误。

import numpy as np
import neurolab as nl

net = nl.net.newff([[0, 1]], [3, 4])
input_w=[[-24.1874,24.1622,0.0755,-0.2521,4.4625,-10.7961,6.2183,0.2680],...]
input_w = np.array(input_w)
input_w = np.reshape(input_w, (8,3))

layer_w=[[-3.7940,-0.0336,-14.9024],......]
layer_w = [np.array(x) for x in (layer_w)]
layer_w = np.reshape(layer_w, (3,4))

input_bias =[0.4747,-1.2475,-1.2470]
bias_2=np.array([-10.9982,1.9692,5.0705,-0.1236])
bias_2 = np.reshape(bias_2, (4))

net.layers[0].np['w'][:] = input_w.
net.layers[1].np['w'][:] = layer_w.
net.layers[1].np['b'][:] = np.array([input_bias])
net.layers[0].np['b'][:] = bias_2

print net.sim([[0.015,0.022,0.0,0.0,0.432,0.647,0.831]])

Traceback (most recent call last):
  File "C:\Python27\neural13.py", line 206, in <module>
    net.layers[0].np['w'][:] = input_w
ValueError: could not broadcast input array from shape (8,3) into shape (3,1)

感谢您的建议,如果您需要更多信息,请随时询问。

【问题讨论】:

    标签: python matlab neural-network


    【解决方案1】:

    这里有几个问题。

    第一个问题是您需要 8 个输入神经元。为此,您需要 newff 中的第一个列表的长度为 8(8 个输入中的每一个输入一个最小值和一个最大值)。所以你最终只有一个输入,而不是 8 个(因此是 3x1 数组而不是 3x8 数组)。这可以通过更改来解决:

    net = nl.net.newff([[0, 1]], [3, 4])
    

    到:

    net = nl.net.newff([[0, 1]]*8, [3, 4])
    

    这是一种更短的书写方式:

    net = nl.net.newff([[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]], [3, 4])
    

    下一个问题是 python 使用与 matlab 不同的维度排序(默认情况下)。因此,形状为 (8,3) 的二维数组在 numpy 中的形状为 (3,8)。所以你所有的重塑都是不必要的。

    第三个问题是 input_bias 和 bias_2 的维度混淆了。我对neurlab的了解还不够多,不能说是要把开头的[3, 4]改成[4, 3],还是要切换bias。我会假设后者。

    最后一个问题是 net.sim 的输入需要 8 个元素,但你只有 7 个。

    这是您的代码的固定版本,假设您混合了偏差并使用一些虚拟值来填充您遗漏的内容:

    import numpy as np
    import neurolab as nl
    
    net = nl.net.newff([[0, 1]]*8, [3, 4])
    
    input_w = np.array([[-24.1874,24.1622,0.0755,-0.2521,4.4625,-10.7961,6.2183,0.2680],
                        [-24.1874,24.1622,0.0755,-0.2521,4.4625,-10.7961,6.2183,0.2680],
                        [-24.1874,24.1622,0.0755,-0.2521,4.4625,-10.7961,6.2183,0.2680]])
    
    layer_w = np.array([[-3.7940,-0.0336,-14.9024],
                        [-3.7940,-0.0336,-14.9024],
                        [-3.7940,-0.0336,-14.9024],
                        [-3.7940,-0.0336,-14.9024]])
    
    input_bias = np.array([0.4747,-1.2475,-1.2470])
    bias_2 = np.array([-10.9982,1.9692,5.0705,-0.1236])
    
    net.layers[0].np['w'][:] = input_w
    net.layers[1].np['w'][:] = layer_w
    net.layers[0].np['b'][:] = input_bias
    net.layers[1].np['b'][:] = bias_2
    
    print net.sim([[0.015,0.022,0.0,0.0,0.432,0.647,0.831]])
    

    【讨论】:

      猜你喜欢
      • 2018-03-01
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 2010-12-02
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      相关资源
      最近更新 更多