Dimension mis-match error after editing gradients

username: afshin67
cooked: <p>Hi,</p>
<p>I changed the gradients of the network in a function starting by <code>torch::autograd::GradMode::set_enabled(false);</code>. Then, when I call <code>optimizer-&gt;step();</code>, I am getting a dimension mis-match error. Without calling the function, everything works fine.</p>
<p>The error is: (note that my network is <code>{10,32,64,64,64,32,12}</code>).</p>
<pre><code class="lang-auto">terminate called after throwing an instance of 'c10::Error'
  what():  The size of tensor a (32) must match the size of tensor b (31) at non-singleton dimension 0 (infer_size at /pytorch/aten/src/ATen/ExpandUtils.cpp:23)
frame #0: std::function&lt;std::string ()&gt;::operator()() const + 0x11 (0x7f5f4ab877d1 in /opt/libtorch/lib/libc10.so)
frame #1: c10::Error::Error(c10::SourceLocation, std::string const&amp;) + 0x2a (0x7f5f4ab8710a in /opt/libtorch/lib/libc10.so)
frame #2: at::infer_size(c10::ArrayRef&lt;long&gt;, c10::ArrayRef&lt;long&gt;) + 0x487 (0x7f5f3db0ebe7 in /opt/libtorch/lib/libcaffe2.so)
frame #3: at::TensorIterator::compute_shape() + 0x85 (0x7f5f3dd23045 in /opt/libtorch/lib/libcaffe2.so)
frame #4: at::TensorIterator::Builder::build() + 0x2f (0x7f5f3dd24aaf in /opt/libtorch/lib/libcaffe2.so)
frame #5: at::TensorIterator::binary_op(at::Tensor&amp;, at::Tensor const&amp;, at::Tensor const&amp;) + 0x31f (0x7f5f3dd2594f in /opt/libtorch/lib/libcaffe2.so)
frame #6: at::native::add_out(at::Tensor&amp;, at::Tensor const&amp;, at::Tensor const&amp;, c10::Scalar) + 0x21d (0x7f5f3db85afd in /opt/libtorch/lib/libcaffe2.so)
frame #7: at::TypeDefault::add_(at::Tensor&amp;, at::Tensor const&amp;, c10::Scalar) const + 0x68 (0x7f5f3e0025a8 in /opt/libtorch/lib/libcaffe2.so)
frame #8: torch::autograd::VariableType::add_(at::Tensor&amp;, at::Tensor const&amp;, c10::Scalar) const + 0x3ac (0x7f5f4b3bee3c in /opt/libtorch/lib/libtorch.so.1)
frame #9: torch::optim::Adam::step() + 0x27b (0x7f5f4b9af34b in /opt/libtorch/lib/libtorch.so.1)
frame #10: /net/ge.unx.sas.com/vol/vol110/u11/aforoo/RL/rllib/cmake-build-debug/rllib() [0x475662]
frame #11: /net/ge.unx.sas.com/vol/vol110/u11/aforoo/RL/rllib/cmake-build-debug/rllib() [0x46fa47]
frame #12: /net/ge.unx.sas.com/vol/vol110/u11/aforoo/RL/rllib/cmake-build-debug/rllib() [0x46a541]
frame #13: /net/ge.unx.sas.com/vol/vol110/u11/aforoo/RL/rllib/cmake-build-debug/rllib() [0x41ad3d]
frame #14: __libc_start_main + 0xf5 (0x7f5f0a1723d5 in /lib64/libc.so.6)
frame #15: /net/ge.unx.sas.com/vol/vol110/u11/aforoo/RL/rllib/cmake-build-debug/rllib() [0x419dc9]

Signal: SIGABRT (Aborted)
</code></pre>
<p>I printed the number of weights and gradients in each layer, before and after calling the function and they are equal:</p>
<pre><code class="lang-auto">(gdb) p shape_w0
$3 = std::vector of length 12, capacity 16 = {320, 32, 2048, 64, 4096, 64, 4096, 64, 2048, 32, 384, 12}
(gdb) p shape_g0
$4 = std::vector of length 12, capacity 16 = {320, 32, 2048, 64, 4096, 64, 4096, 64, 2048, 32, 384, 12}
(gdb) p shape_w1
$5 = std::vector of length 12, capacity 16 = {320, 32, 2048, 64, 4096, 64, 4096, 64, 2048, 32, 384, 12}
(gdb) p shape_g1
$6 = std::vector of length 12, capacity 16 = {320, 32, 2048, 64, 4096, 64, 4096, 64, 2048, 32, 384, 12}
</code></pre>
<p>I appreciate any help or comment.<br>
Afshin</p>

username: yf225
cooked: <p>Can you provide a minimal example for reproducing this error?</p>

username: afshin67
cooked: <p>Actually, this is solved. Thanks for the follow up.</p>