Optimizer won't update weights

Liviu_Rotaru: <p>So I’m trying to write my own optimizer and I found out that the parameters are’t being updated. I tried following the example of optim.SGD <a href="https://github.com/pytorch/pytorch/blob/master/torch/optim/sgd.py" rel="nofollow noopener">https://github.com/pytorch/pytorch/blob/master/torch/optim/sgd.py</a> .</p>
<p>class AGCD(Optimizer):<br>
def <strong>init</strong>(self, params, theta=1):<br>
defaults = dict(theta=theta)<br>
super(AGCD, self).<strong>init</strong>(params, defaults)<br>
self.x_update_params = deepcopy(self.param_groups)<br>
self.z_update_params = deepcopy(self.param_groups)<br>
self.theta_update_params = deepcopy(self.param_groups)<br>
for group in self.theta_update_params:<br>
for i in range(len(group[‘params’])):<br>
group[‘params’][i] = torch.ones(1)</p>
<pre><code>def __setstate__(self, state):
	super(AGCD, self).__setstate__(state)
@torch.no_grad()
def step(self, closure=None):
	loss = None
	if closure is not None:
		with torch.enable_grad():
			loss=closure()


	for group_y, group_x, group_z, group_theta in zip(self.param_groups,
						self.x_update_params,
						self.z_update_params,
						self.theta_update_params):
		
		for y, x, z, theta in zip(group_y['params'],
						group_x['params'],
						group_z['params'],
						group_theta['params']):
			if y.grad is None:
				continue
		
			d_y = y.grad
			j1 = j2 = my_argmax(d_y)
			y = (x*(1-theta).expand_as(x)).add(z*theta.expand_as(z)) #use pytorch functions for math
			temp = torch.zeros(x.size())
			#print(temp.size())
			#print(x.size())
			#print(d_y.size())
			temp[j1] = 1*0.2*x[j1]
			x = y.sub(temp)
			temp = torch.zeros(z.size())
			temp[j2] = 5*0.2*z[j2]
			#print(theta)
			theta = update_theta(theta)
			#print(theta)
				
	return loss			
</code></pre>
<p>BTW this is my first time posting on a forum so please excuse any sins.<br>
Edit: To be more specific, the parameters used to update aren’t themselves updated (theta stays at the value of 1 at all times).<br>
Edit2: I think it’s because I have to use pytorch operations instead of using “=” so that the object gets modified, not just the copy of that object</p>

JuanFMontesinos: <p>Hi, just shot coment, you are doing a deep copy of param groups. They no longer point to the memory address of the model weights.</p>

Liviu_Rotaru: <p>Thank you <a class="mention" href="/u/juanfmontesinos">@JuanFMontesinos</a> but that’s why i’m doing the deepcopy. The copies (x,z and theta) are used to update the orginal parameters (y) in the step(), and they each need to be their own separate thing. Everytime I call step() each of the parameter groups should be updated but they stay the same.</p>

JuanFMontesinos: <p>I’m a bit loss with your code<br>
What is see is that you are assigning y  for example here</p>
<pre><code class="lang-auto">y = (x*(1-theta).expand_as(x)).add(z*theta.expand_as(z)) #use pytorch functions for math
</code></pre>
<p>Thus y is no longer pointing to the deepcopy you did but a inner variable in the scope.</p>
<pre><code class="lang-auto">x = y.sub(temp)
</code></pre>
<p>same there, you are reasigning x, thus, overwritting param group again.<br>
You can wheter use in-place operators (in pytorch some functions has 2 versions w and w/o underscore, the second one is in-place.)<br>
Example:</p>
<pre><code class="lang-auto">a=5 # a tensor
a.add(4) 
#a is still 5
a.add_(4)
#a is now 9
</code></pre>
<p>or just assign y like y.grad=whatever_you_want</p>

Liviu_Rotaru: <p>Well, y should pointing to the inner variable. But every step I update the inner variables based on the deepcopies. Anyways, I found the problem. Thanks a lot!!!<br>
The thing is I was using. eg:<br>
y = x+3<br>
instead of<br>
x.copy_(x+3)<br>
so none of the variables were beeing updated.</p>