ValueError: only one element tensors can be converted to Python scalars - while using SVM

a_d: <p>Hello all,<br>
I have an autoencoder based model, from which I extract the encoded state and further put it into a support vector classifier(sklearn.svm.SVC) to classify. However, when converting the list of the encoded states(either explicitly or as scikit learn does implicitly) I get the error as mentioned in the subject.<br>
Encoded tensor is of the shape - [1, 1, 313]</p>
<pre><code class="lang-auto">ValueError: only one element tensors can be converted to Python scalars
</code></pre>
<p>And here is my entire pipeline -</p>
<pre><code class="lang-auto">train_numpy = []
train_labels = []
model = Encoder()
model.load_state_dict(torch.load('path/encoder.pth'))
model.eval()
for param in model.parameters():
    param.requires_grad = False

for (x,y) in train_Data:
    encoded, out = model(x)
    encoded = encoded.squeeze().detach().numpy()
    y = y.squeeze().detach().numpy()
   train_numpy.append(x)
    train_labels.append(y)

train_numpy = numpy.asarray(train_numpy)
train_labels = numpy.asarray(train_labels)

SVM = svm.SVC(kernel='rbf')
SVM.fit(X=train_numpy, y=train_labels)
</code></pre>
<p>Could someone please point what could be going wrong,<br>
Thanks in Advance</p>

Nikronic: <p>Hi,</p>
<p>Could you please add the error stacktrace?</p>
<p>Bests</p>

a_d: <p>Sure, here it is</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "/usr/lib/python3.8/code.py", line 90, in runcode
    exec(code, self.locals)
  File "&lt;input&gt;", line 1, in &lt;module&gt;
  File "/pycharm/pycharm/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/pycharm/pycharm/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/main.py", line 50, in &lt;module&gt;
    train_numpy = numpy.array(train_numpy)
ValueError: only one element tensors can be converted to Python scalars
</code></pre>

Nikronic: <p>Unfortunately I still cannot figure it out, but here are some problems that may lead to this:</p>
<aside class="quote no-group quote-modified" data-username="a_d" data-post="1" data-topic="84796">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/e19b73/48.png" class="avatar"> a_d:</div>
<blockquote>
<pre><code class="lang-auto">encoded, out = model(x)
    encoded = encoded.squeeze().detach().numpy()
...
    train_numpy.append(x)  # should not be `train_numpy.append(encoded)` ??
</code></pre>
</blockquote>
</aside>
<p>You have never used <code>encoded</code>. I think you probably is passing <code>x</code> instead of <code>encoded</code> to the <code>train_numpy.append(x)</code>? I think because of these, <code>x</code> has same structure as <code>y</code> and needs <code>.detach().numpy()</code> but you have done these on <code>encoded</code> but never passed it to <code>train_numpy</code>.</p>

a_d: <p>Thanks a lot for pointing it out <a class="mention" href="/u/nikronic">@Nikronic</a>, I completely overlooked it and does not throw the same error.</p>