简体   繁体   English

简单的神经网络 - 如何存储权重?

[英]Simple neural network - how to store weights?

I recently started learning Python and am trying to implement my first neural network.我最近开始学习 Python 并尝试实现我的第一个神经网络。 My goal is to write a function that generates a neural net with a variable amount of layers and nodes.我的目标是编写一个 function 生成具有可变层数和节点数的神经网络。 All necessary information for that is stored in layerStructure (eg: First layer has four nodes, third layer has three nodes).所有必要的信息都存储在layerStructure中(例如:第一层有四个节点,第三层有三个节点)。

import numpy as np

#Vector of input layer
input = np.array([1,2,3,4])

#Amount of nodes in each layer
layerStructure = np.array([len(input),2,3])

#Generating empty weight matrix container
weightMatrix_arr = np.array([])

#Initialsing random weights matrices
for ii in range(len(layerStructure[0:-1])):
    randmatrix = np.random.rand(layerStructure[ii+1],layerStructure[ii])
    print(randmatrix)

The code above generates the following output:上面的代码生成如下 output:

[[0.6067148  0.66445212 0.54061231 0.19334004]
 [0.22385007 0.8391435  0.73625366 0.86343394]]
[[0.61794333 0.9114799 ]
 [0.10626486 0.95307027]
 [0.50567023 0.57246852]]

My first attempt was to store each random weight matrix in a container array called weightMatrix_arr .我的第一次尝试是将每个随机权重矩阵存储在一个名为weightMatrix_arr的容器数组中。 However, since the shape of individual matrices varies, I cannot use np.append() to store them all in the matrix container.但是,由于各个矩阵的形状各不相同,我不能使用 np.append() 将它们全部存储在矩阵容器中。 How can I save these matrices in order to access them during the backpropagation?如何保存这些矩阵以便在反向传播期间访问它们?

You can use a list instead of an np.array :您可以使用list而不是np.array

#Generating empty weight LIST container
weightMatrixes = []

#Initialsing random weights matrices
for ii in range(len(layerStructure[0:-1])):
    randmatrix = np.random.rand(layerStructure[ii+1],layerStructure[ii])
    weightMatrixes.append(randmatrix)
    print(randmatrix)

Otherwise you can set the weightMatrix_arr dtype to object : :否则,您可以将weightMatrix_arr dtype设置为object : :

#Generating empty weight LIST container
weightMatrixes = np.array([], dtype=object)

#Initialsing random weights matrices
for ii in range(len(layerStructure[0:-1])):
   randmatrix = np.random.rand(layerStructure[ii+1],layerStructure[ii])
   weightMatrixes = np.append(weightMatrixes, randmatrix)

Note both ways you can't access the inner layer indexes without accessing the layer matrix:请注意,如果不访问层矩阵,您就无法访问内层索引的两种方式:

weightMatrixes[layer, 0, 3] # ERROR
weightMatrixes[layer][0, 3] # OK

If memory consumption is not a problem, you can shape all layers as a longest one, and just ignore extra cells according to a layerStructure value.如果layerStructure消耗没有问题,您可以将所有层塑造为最长的层,并根据层结构值忽略额外的单元格。

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

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