Python神经网络权重

时间:2018-12-04 01:50:25

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

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

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

我的问题是我不知道如何将进入每个神经元的随机生成的权重保存到2D矩阵中。有人可以帮我吗?

我正在尝试这样的事情:

$y

但是我觉得这不是减轻MLP权重的最佳方法,也不是对我有用。

你们能帮我吗?

感谢您的咨询。

PS:不能使用tensorflow或keras。

1 个答案:

答案 0 :(得分:0)

#!/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])]