Reputation:
let us suppose we have following list
w = [[[-0.82203424, -0.9185806 , 0.03494298]], [0., 0., 0.], [[1.0692896 ],[ 0.62761235],[-0.5426246 ]], [0]]
what i want is to increment each element of list by some amount using loop operator, this kind of operation we meet frequently in Neural Network Sketching, i know that there exist operation enumerate , for instance let us consider following code
import numpy as np
w = [[[-0.82203424, -0.9185806 , 0.03494298]], [0., 0., 0.], [[1.0692896 ],[ 0.62761235],[-0.5426246 ]], [0]]
for count,ele in enumerate(w):
print (count,ele)
0 [[-0.82203424, -0.9185806, 0.03494298]]
1 [0.0, 0.0, 0.0]
2 [[1.0692896], [0.62761235], [-0.5426246]]
3 [0]
it is clear what we can use ele arguments to access its lists, but ele has different format , there for we can also use ele to generate new enumeration right, for instance let us consider following code
import numpy as np
w = [[[-0.82203424, -0.9185806 , 0.03494298]], [0., 0., 0.], [[1.0692896 ],[ 0.62761235],[-0.5426246 ]], [0]]
for count,ele in enumerate(w):
for index, weight in enumerate(ele):
print (index, weight)
0 [-0.82203424, -0.9185806, 0.03494298]
0 0.0
1 0.0
2 0.0
0 [1.0692896]
1 [0.62761235]
2 [-0.5426246]
0 0
but how can i use this index for the original list?i have searched also and found that there exist such function numpy.ndenumerate, if i use this function in second list i will get
import numpy
w = [[[-0.82203424, -0.9185806 , 0.03494298]], [0., 0., 0.], [[1.0692896 ],[ 0.62761235],[-0.5426246 ]], [0]]
for count,ele in enumerate(w):
for index, weight in numpy.ndenumerate(ele):
print (index, weight)
result
(0, 0) -0.82203424
(0, 1) -0.9185806
(0, 2) 0.03494298
(0,) 0.0
(1,) 0.0
(2,) 0.0
(0, 0) 1.0692896
(1, 0) 0.62761235
(2, 0) -0.5426246
(0,) 0
so maybe accessing of original element will be following w[count][index] right?for instance accessing first element of first list will be like this
import numpy
w = [[[-0.82203424, -0.9185806 , 0.03494298]], [0., 0., 0.], [[1.0692896 ],[ 0.62761235],[-0.5426246 ]], [0]]
for count,ele in enumerate(w):
for index, weight in numpy.ndenumerate(ele):
print (index, weight)
print(w[count][index])
but it gives me following error
print(w[count][index])
TypeError: list indices must be integers, not tuple
how to fix? thanks in advance
Upvotes: 1
Views: 75
Reputation:
here is code which is connected to neural network model and data
from google.colab import drive
drive.mount('/content/drive')
from copy import deepcopy
import numpy as np
x = [[1],[2],[3],[4]]
y = [[2],[4],[6],[8]]
w = [[[-0.82203424, -0.9185806 , 0.03494298]], [0., 0., 0.], [[1.0692896 ],[ 0.62761235],[-0.5426246 ]], [0]]
print(len(w))
def feed_forward(inputs,outputs,weights):
pre_hidden =np.dot(inputs,weights[0])+weights[1]
hidden =np.where(pre_hidden<0,0,pre_hidden)
out =np.dot(hidden,weights[2])+weights[3]
squared_error =(np.square(out-outputs))
return squared_error
def update_weights(inputs, outputs, weights, epochs):
for epoch in range(epochs):
org_loss = feed_forward(inputs, outputs, weights)
wts_tmp= deepcopy(weights)
wts_tmp2 = deepcopy(weights)
for i, layer in enumerate(reversed(weights)):
for index, weight in np.ndenumerate(layer):
wts_tmp[-(i+1)][index] += 0.0001
loss = feed_forward(inputs, outputs, wts_tmp)
del_loss = np.sum(org_loss -loss)/(0.0001*len(inputs))
wts_tmp2[-(i+1)][index] += del_loss*0.01
wts_tmp = deepcopy(weights)
weights = deepcopy(wts_tmp2)
return wts_tmp2
update_weights(x,y,w,1)
Upvotes: 0
Reputation: 226694
The loop should likely be:
for i, ele in enumerate(w):
for j, x in enumerate(ele):
print(w[i][j]) # access original using indices
The test data given is in a inconsistent format (a extra layer of square brackets on the first element):
>>> from pprint import pprint
>>> pprint(w)
[[[-0.82203424, -0.9185806, 0.03494298]],
[0.0, 0.0, 0.0],
[[1.0692896], [0.62761235], [-0.5426246]],
[0]]
Perhaps this should be:
[[-0.82203424, -0.9185806, 0.03494298],
[0.0, 0.0, 0.0],
[1.0692896, 0.62761235, -0.5426246],
[0]]
If the data actually does have arbitrary and inconsistent nesting, then it isn't clear what is really needed.
Upvotes: 1