Hello.
Using Python, we are writing a code to obtain the slope of the neural network loss function.
Here's the code.
def num_grad(self, f, key):
h = 0.001
W = self.params[key]
result = [[0 for j in range(len(W[0]))] for i in range(len(W))]
for i in range(len(W)):
for j in range(len(W[0])):
tmp_val = W[i][j]
self.params[key][i][j] = tmp_val + h
f1 = f(self.params[key])
self.params[key][i][j] = tmp_val - h
f2 = f(self.params[key])
result[i][j] = (f1-f2)/(2*h)
self.params[key] = tmp_val
return result
At this time, self.The parameters [key]
are two-dimensional list
indicating weights or biases
self.If you try to access the parameters [key][i][j]
, the value appears
TypeError: Cannot proceed with 'float' object is not writable
error.
I looked it up on the Internet and found that it is an error that appears when accessing variables other than list
as indexes
In my case, self normally.The corresponding error appears at the same time as the value of param[key][i][j]
appears.
What should I do?
python
At first, the parameters [key] are two dimensions. However, at the end of the for statement, the type changes when you assign param [key] = tmp_val.
© 2024 OneMinuteCode. All rights reserved.