Thank you for your reply.
This is indeed a good idea…but when I try this comment in weight, I get the error: “bool value of Tensor with more than one value is ambiguous”…
I can get the max(abs(bias))…maybe I know the problem, but I don’t know how to solve it
Sorry for the late reply. You need to determine dimension along which you need the max value from the weight tensor becuase different layers will have different multiple dimensions, for example linear layers will have (out_features,in_features) shape with 2 dimensions.
Oh, no, no…don’t need to say sorry, your help is my pleasure.
Maybe I can understand your meaning, but I don’t know how to specify one dimension…
I am so sorry I am a new beginner
But I try…
consider x is your weight matrix. And the shape of x is torch.Size([3, 3]).
when you try to find max value with x.max(dim=0) where dim is the argument along which you are asking for max value, which in our case is 0 means we are asking for max values from all the columns so it will return
tensor([9, 4, 6])
And when you try to find max value with x.max(dim=1), in this case, dim is 1 means we are asking for max values across all the rows so it will return
You can flatten the model then the max of the numpy aray. Just pass your net to below function it will give you fattened numpy array of model and al it’s shapes. Then you can apply all numpy operations on that.
#############################################################################
# Flattening the NET
#############################################################################
def flattenNetwork(net):
flatNet = []
shapes = []
for param in net.parameters():
#if its WEIGHTS
curr_shape = param.cpu().data.numpy().shape
shapes.append(curr_shape)
if len(curr_shape) == 2:
param = param.cpu().data.numpy().reshape(curr_shape[0]*curr_shape[1])
flatNet.append(param)
elif len(curr_shape) == 4:
param = param.cpu().data.numpy().reshape(curr_shape[0]*curr_shape[1]*curr_shape[2]*curr_shape[3])
flatNet.append(param)
else:
param = param.cpu().data.numpy().reshape(curr_shape[0])
flatNet.append(param)
finalNet = []
for obj in flatNet:
for x in obj:
finalNet.append(x)
finalNet = np.array(finalNet)
return finalNet,shapes
Dear Sir, I am using your function flattenNetwork() to get the network parameters/weights and shape. I manipulate the weights and I don’t change the shape of the network. How can I get back the original network with manipulated weights? Please help. Thank you.