UserWarning: This overload of add_ , addcmul_, addcdiv_ is deprecated: errors while implementing SharedAdam

granth_jain: <p>Hi,</p>
<p>I am new to pytorch and implementing A3C.<br>
For this I have got a code for SharedAdam optimizer which is of pytorch version 0.4 which is producing above error in pytorch 1.6.0</p>
<p>Can someone please help to let me know what should be correct implementations of the functionality of above 3 functions in pytorch 1.6.0</p>
<p>Below is the code for pytorch 0.4</p>
<blockquote>
<p>class SharedAdam(optim.Adam):</p>
<pre><code>def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8,
             weight_decay=0):
    super(SharedAdam, self).__init__(params, lr, betas, eps, weight_decay)

    for group in self.param_groups:
        for p in group['params']:
            state = self.state[p]
            state['step'] = torch.zeros(1)
            state['exp_avg'] = p.data.new().resize_as_(p.data).zero_()
            state['exp_avg_sq'] = p.data.new().resize_as_(p.data).zero_()

def share_memory(self):
    for group in self.param_groups:
        for p in group['params']:
            state = self.state[p]
            state['step'].share_memory_()
            state['exp_avg'].share_memory_()
            state['exp_avg_sq'].share_memory_()

def step(self, closure=None):
    """Performs a single optimization step.
    Arguments:
        closure (callable, optional): A closure that reevaluates the model
            and returns the loss.
    """
    loss = None
    if closure is not None:
        loss = closure()

    for group in self.param_groups:
        for p in group['params']:
            if p.grad is None:
                continue
            grad = p.grad.data
            state = self.state[p]

            exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq']
            beta1, beta2 = group['betas']

            state['step'] += 1

            if group['weight_decay'] != 0:
                grad = grad.add(group['weight_decay'], p.data)

            # Decay the first and second moment running average coefficient
            exp_avg.mul_(beta1).add_(1 - beta1, grad)
            exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad)

            denom = exp_avg_sq.sqrt().add_(group['eps'])

            bias_correction1 = 1 - beta1 ** state['step'][0]
            bias_correction2 = 1 - beta2 ** state['step'][0]
            step_size = group['lr'] * math.sqrt(bias_correction2) / bias_correction1

            p.data.addcdiv_(-step_size, exp_avg, denom)

    return loss
</code></pre>
</blockquote>
<p>Is this the correct implementation-</p>
<pre><code>            exp_avg.mul_(beta1).add_(grad, alpha = 1 - beta1)
            exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value = 1 - beta2)


            p.data.addcdiv_(exp_avg, denom, value = -step_size)
</code></pre>

ptrblck: <p>It seems you are calling the mentioned method as:</p>
<pre><code class="lang-python">add_(Number alpha, Tensor other)
</code></pre>
<p>which is deprecated and should be changed to:</p>
<pre><code class="lang-python">add_(Tensor other, *, Number alpha)
</code></pre>
<p>so in your case, you could use</p>
<pre><code class="lang-python">exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1)
</code></pre>

Brando_Miranda: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="99802">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p><code>add_(Tensor other, *, Number alpha)</code></p>
</blockquote>
</aside>
<p>what does that mean? What does the star mean?</p>

Brando_Miranda: <p>I’m still getting an issue:</p>
<pre><code class="lang-auto">/Users/miranda9/RAdam/radam/radam.py:62: UserWarning: This overload of add_ is deprecated:
	add_(Number alpha, Tensor other)
Consider using one of the following signatures instead:
	add_(Tensor other, *, Number alpha) (Triggered internally at  /Users/distiller/project/conda/conda-bld/pytorch_1623459065530/work/torch/csrc/utils/python_arg_parser.cpp:1025.)
  exp_avg.mul_(beta1).add_(1 - beta1, grad)
</code></pre>
<p>see my attempts:</p>
<pre><code class="lang-auto">                # exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad)
                # exp_avg_sq.mul_(beta2).addcmul_(grad, grad, 1 - beta2)
                # exp_avg_sq.mul_(beta2).addcmul_(grad, 1 - beta2)
                exp_avg_sq.mul_(beta2).addcmul_(grad, torch.tensor(1 - beta2, device=grad.device))
</code></pre>
<aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/32861">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/32861" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/32861" target="_blank" rel="noopener nofollow ugc">add_, addcmul_, addcdiv_ deprecated</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2020-01-31" data-time="09:25:24" data-timezone="UTC">09:25AM - 31 Jan 20 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/jecampagne" target="_blank" rel="noopener nofollow ugc">
          <img alt="jecampagne" src="https://avatars.githubusercontent.com/u/20539759?v=4" class="onebox-avatar-inline" width="20" height="20">
          jecampagne
        </a>
      </div>
    </div>

    <div class="labels">
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: deprecation
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          triaged
        </span>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">## Do someone else experiences many messages due to deprecated functions?

Usi<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">ng `torch 1.5.0.dev20200129` to track a bug, I experience these Warning. Do the library will change its API???

/opt/conda/conda-bld/pytorch_1580285301452/work/torch/csrc/autograd/generated/python_variable_methods.cpp:1334: UserWarning: This overload of add_ is deprecated:
add_(Number alpha, Tensor other)
Consider using one of the following signatures instead:
add_(Tensor other, Number alpha)
/opt/conda/conda-bld/pytorch_1580285301452/work/torch/csrc/autograd/generated/python_variable_methods.cpp:1550: UserWarning: This overload of addcmul_ is deprecated:
addcmul_(Number value, Tensor tensor1, Tensor tensor2)
Consider using one of the following signatures instead:
addcmul_(Tensor tensor1, Tensor tensor2, Number value)
/opt/conda/conda-bld/pytorch_1580285301452/work/torch/csrc/autograd/generated/python_variable_methods.cpp:1480: UserWarning: This overload of addcdiv_ is deprecated:
addcdiv_(Number value, Tensor tensor1, Tensor tensor2)
Consider using one of the following signatures instead:
addcdiv_(Tensor tensor1, Tensor tensor2, Number value)</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


optimoose: <p>Hi, did you figure out how to convert from the old usage to the new usage. I don’t understand the solutions posed in the links. If you have it would be great if you could post the updated code to the code that was failing above. TY.</p>

optimoose: <aside class="quote no-group" data-username="Brando_Miranda" data-post="4" data-topic="99802">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/brando_miranda/48/14355_2.png" class="avatar"> Brando_Miranda:</div>
<blockquote>
<p><code>exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad)</code></p>
</blockquote>
</aside>
<p>=&gt;</p>
<p><code>exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1-beta2) </code></p>
<p>Seems to work…</p>