簡體   English   中英

Python神經網絡權重

[英]Python neural network weights

我正在向神經網絡自我介紹,這是我第一次嘗試編寫神經網絡。 希望你能幫助我:

因此,可以說我想編寫一個通用MLP,這意味着我可以隨時更改其自身的layers_size。 例如,layers_size = [2,2,1]或layers_size = [5,40,40,3] [...,...,...]。

我的問題是我不知道如何將隨機產生的權重保存到2D矩陣中。 有人可以幫我嗎?

我正在嘗試這樣的事情:

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。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM