【问题标题】:Python neural network weightsPython 神经网络权重
【发布时间】:2019-05-05 09:14:29
【问题描述】:

我正在向神经网络介绍自己,这是我第一次尝试编写神经网络。希望你能帮帮我:

假设我想编写一个通用的 MLP,这意味着我可以随时更改其自身的 layers_size。 例如,layers_size = [2,2,1] 或 layers_size = [5,40,40,3] [...,...,...]。

我的问题是我不知道如何将每个神经元的随机生成权重保存到二维矩阵中。有人能帮帮我吗?

我正在尝试这样的事情:

weights = []
length = len(layers_size)
#appreciate loop starting in 1 since you dont need 
#weights #in the entry layer

#runs layers_size times - 1
for i in range(1, length):
#Gives the amount of neurons for each layer
for j in range(0, layers_size[i]):
    #Get the amount of neurons from the previous layer to 
    # the actual neuron so it saves layers_size[i] - 1 
    # numWeights for the actual neuron...
    weights[i][j] = random...

但我觉得这不是保存 MLP 权重的最佳方法,也不适合我。

你们能帮帮我吗?

感谢您的建议。

PS:tensorflow和keras都不能用。

【问题讨论】:

  • 使用 numpy 保存矩阵可以节省时间

标签: python python-3.x neural-network conv-neural-network


【解决方案1】:
#!/bin/python

import numpy as np

layers_size = [5,40,40,3]

weights = []
length = len(layers_size)
#appreciate loop starting in 1 since you dont need 
#weights #in the entry layer

#runs layers_size times - 1
for i in range(0, length):
    weights.append([])
#Gives the amount of neurons for each layer
    for j in range(0, layers_size[i]):
        #Get the amount of neurons from the previous layer to 
        # the actual neuron so it saves layers_size[i] - 1 
        # numWeights for the actual neuron...
        weights[i].append(np.random.randint(1, 101))

print(np.array(weights))

输出:

[list([81, 53, 53, 55, 71])
 list([34, 75, 12, 14, 9, 69, 56, 1, 98, 73, 14, 82, 60, 52, 13, 7, 14, 9, 5, 8, 24, 61, 75, 52, 82, 91, 67, 75, 22, 77, 84, 71, 83, 77, 56, 99, 94, 49, 100, 84])
 list([45, 44, 71, 89, 16, 22, 41, 36, 42, 38, 53, 4, 25, 53, 16, 81, 47, 70, 9, 88, 81, 27, 66, 91, 97, 53, 41, 20, 20, 15, 77, 38, 60, 1, 30, 17, 55, 51, 33, 30])
 list([43, 60, 17])]

【讨论】:

  • 嗨康纳!这不正是我想要的,考虑到在神经网络中,一层的每个神经元通过权重连接到下一层的神经元。话虽如此,我在我的问题中的意思是:拥有 [2,2,1] layers_size 意味着在第一层和第二层之间将有一个 2x2 的权重来连接。以及要连接的第 2 层和第 3 层之间的 2x1 权重。现在这应该是通用的。 IE。 [5, 40, 40, 3] 将是 200(第二层的每个神经元 5 个权重),(第二层和第三层之间的每个神经元 1600, 40)和最后一层的每个神经元 120, 40跨度>
  • @a.br 您能否提供一些示例数据、示例代码和预期输出。例如,stackoverflow.com/help/mcve
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
  • 2011-05-10
  • 2018-11-27
  • 2016-08-17
  • 1970-01-01
相关资源
最近更新 更多