I need to add an arbitrary penalization to the weights of my network but the normal way of doing this, i.e.

pen=0
for p in net.parameters():
pen+=penaltyfunc ( p )

is a bit too slow at high layer counts
to solve this I want to create a view(-1) of all the weight/bias matrices and then combine them into a new tensor. From there I can the whole penalization in one step. Normally with a view, the value of the view changes along with whatever you are viewing. However I’m not sure how to combine all these views into a single view. if I use the cat command, it returns a snapshot of the values of the views, i.e. it won’t change as the actual weights change.
Is there a way to do this or do i just need to execute the cat command each time I want to calculate the penalty?
I’ve tried searching for view multiple tensors, multiview, view+cat, but nothing really relevant comes up.