Running error for converting libtorch to C++

username: Chen0729
cooked: <p>Hi, All</p>
<p>I met a running error when converting python software to C++<br>
The error is:</p>
<p>“terminate called after throwing an instance of ‘c10::Error’<br>
what():  Expected a Tensor of type Variable but found an undefined Tensor for argument <span class="hashtag">#0</span> ‘self’<br>
Exception raised from checked_cast_variable at …/torch/csrc/autograd/VariableTypeManual.cpp:39 (most recent call first):<br>
frame <span class="hashtag">#0:</span> c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits, std::allocator &gt;) + 0x58 (0x7f120c808268 in /sas3rd/conan/dev/mva-vbviya/LAXNO/pytorch/lib/libc10.so)<br>
frame <span class="hashtag">#1:</span>  + 0x2e6ad19 (0x7f1207d6cd19 in /sas3rd/conan/dev/mva-vbviya/LAXNO/pytorch/lib/libtorch_cpu.so)<br>
frame <span class="hashtag">#2:</span>  + 0x2603b6a (0x7f1207505b6a in /sas3rd/conan/dev/mva-vbviya/LAXNO/pytorch/lib/libtorch_cpu.so)<br>
frame <span class="hashtag">#3:</span>  + 0x80927e (0x7f120570b27e in /sas3rd/conan/dev/mva-vbviya/LAXNO/pytorch/lib/libtorch_cpu.so)<br>
frame <span class="hashtag">#4:</span>  + 0x11c0b4c (0x7f12060c2b4c in /sas3rd/conan/dev/mva-vbviya/LAXNO/pytorch/lib/libtorch_cpu.so)<br>
frame <span class="hashtag">#5:</span> at::mean(at::Tensor const&amp;, c10::ArrayRef, bool, c10::optional<a>c10::ScalarType</a>) + 0xf2 (0x7f1205fe3912 in /sas3rd/conan/dev/mva-vbviya/LAXNO/pytorch/lib/libtorch_cpu.so)”</p>
<p>It happens at the line:<br>
auto mom_gen = discriminator-&gt;forward(fake, true, cuda)[0];</p>
<p>The C++ code for discriminator is (and the discriminator is good when the option true is set to false) namely only has one output.<br>
std::vector<a>torch::Tensor</a> forward(torch::Tensor x1, bool feature = false, bool cuda = false) {<br>
torch::Tensor x = x1.view({-1, input_dim});<br>
torch::Tensor noise = torch::randn({x.size(0),x.size(1)}) * 0.3;<br>
if (cuda)<br>
noise = noise.cuda();<br>
torch::Tensor x_f;<br>
// x = x + torch::autograd::Variable(noise);//get rid of Variable<br>
x=x+noise;<br>
x.options().requires_grad(false);//some more work to do here<br>
for (int64_t i = 0; i &lt; 5; i++)<br>
{<br>
torch::nn::Module m;<br>
auto a=layers[0+i]-&gt;as()-&gt;forward(x);<br>
torch::Tensor x_f = torch::nn::functional::relu(a);<br>
noise = torch::randn({x_f.size(0),x_f.size(1)}) * 0.5;<br>
if (cuda)<br>
noise = noise.cuda();</p>
<pre><code>        x = x_f + torch::autograd::Variable(noise);
        x.options().requires_grad(false);
    }

    if (feature) {
        return {x_f, final-&gt;as&lt;LinearWeightNorm&gt;()-&gt;forward(x)};
    } else {
        return {final-&gt;as&lt;LinearWeightNorm&gt;()-&gt;forward(x)};
    }

}
</code></pre>
<p>};</p>
<p>And this is the discriminator software:</p>
<p>def forward(self, x, feature = False, cuda = False):<br>
x = x.view(-1, self.input_dim)<br>
noise = torch.randn(x.size()) * 0.3 if self.training else torch.Tensor([0])<br>
if cuda:<br>
noise = noise.cuda()<br>
x = x + Variable(noise, requires_grad = False)<br>
for i in range(len(self.layers)):<br>
m = self.layers[i]<br>
x_f = F.relu(m(x))<br>
noise = torch.randn(x_f.size()) * 0.5 if self.training else torch.Tensor([0])<br>
if cuda:<br>
noise = noise.cuda()<br>
x = (x_f + Variable(noise, requires_grad = False))<br>
if feature:<br>
return x_f, self.final(x)<br>
return self.final(x)</p>

username: glaringlee
cooked: <p>Hi, It seems to me that your sample code is pretty old, it still use Variable. I believe you should use Tensor class directly instead of using Variable. Can you try.</p>

username: Chen0729
cooked: <p>Thanks a lot for the comments.</p>
<p>Much appreciated.</p>