Getting a value error as I try to pass tensor to MLP

username: Jbrec
cooked: <p>Hi There,</p>
<p>I can’t seem to get my y_train tensor to match my X_train tensor shape</p>
<pre><code class="lang-auto">torch_train_X = []
for i,j in enumerate(HOG_X_train):
  torch_train_X.append(torch.from_numpy(HOG_X_train[i].astype(np.float32)))

torch_test_X = []
for i,j in enumerate(HOG_X_test):
  torch_test_X.append(torch.from_numpy(HOG_X_test[i].astype(np.float32)))

torch_train_y = []
for i,j in enumerate(y_train):
  torch_train_y.append(torch.as_tensor([y_train[i]]))

torch_test_y = []
for i,j in enumerate(y_test):
  torch_test_y.append(torch.as_tensor([y_test[i]]))
</code></pre>
<pre><code class="lang-auto">print(y_train.shape)

print(torch_train_y[0])

print(torch_train_y[0].shape)

print(torch_train_X[0].shape)
</code></pre>
<p>OUT<br>
(12271,)<br>
tensor([4])<br>
torch.Size([1])<br>
torch.Size([8100])</p>
<pre><code class="lang-auto">from skorch import NeuralNetClassifier

class MLP(nn.Module):
    def __init__(
        self,
        num_units=100,
        nonlin=F.relu,
        dropout=0.1,
        momentum = .01
        
    ):
        super(MLP, self).__init__()
        self.fc1 = nn.Linear(8100,num_units)
        self.fc2 = nn.Linear(num_units,7)
        self.relu=nn.ReLU()
        self.dropout = nn.Dropout(dropout)
            
        
    def forward(self,X):
        X = self.relu(self.fc1(X))
        X = self.dropout(X)
        X = self.fc2(X)
        X = self.relu(X)
        return X


</code></pre>
<pre><code class="lang-auto">net = NeuralNetClassifier(
    MLP,
    max_epochs=20,
    lr=0.1,
    device='cuda'
) 
</code></pre>
<p><code>net.fit(torch_train_X,torch_train_y)</code></p>
<pre><code class="lang-auto">Re-initializing module.
Re-initializing optimizer.
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-154-e0cb7063e59c&gt; in &lt;module&gt;()
----&gt; 1 net.fit(torch_train_X,y_train)

6 frames
/usr/local/lib/python3.7/dist-packages/skorch/classifier.py in fit(self, X, y, **fit_params)
    140         # this is actually a pylint bug:
    141         # https://github.com/PyCQA/pylint/issues/1085
--&gt; 142         return super(NeuralNetClassifier, self).fit(X, y, **fit_params)
    143 
    144     def predict_proba(self, X):

/usr/local/lib/python3.7/dist-packages/skorch/net.py in fit(self, X, y, **fit_params)
    915             self.initialize()
    916 
--&gt; 917         self.partial_fit(X, y, **fit_params)
    918         return self
    919 

/usr/local/lib/python3.7/dist-packages/skorch/net.py in partial_fit(self, X, y, classes, **fit_params)
    874         self.notify('on_train_begin', X=X, y=y)
    875         try:
--&gt; 876             self.fit_loop(X, y, **fit_params)
    877         except KeyboardInterrupt:
    878             pass

/usr/local/lib/python3.7/dist-packages/skorch/net.py in fit_loop(self, X, y, epochs, **fit_params)
    778 
    779         dataset_train, dataset_valid = self.get_split_datasets(
--&gt; 780             X, y, **fit_params)
    781         on_epoch_kwargs = {
    782             'dataset_train': dataset_train,

/usr/local/lib/python3.7/dist-packages/skorch/net.py in get_split_datasets(self, X, y, **fit_params)
   1304 
   1305         """
-&gt; 1306         dataset = self.get_dataset(X, y)
   1307         if not self.train_split:
   1308             return dataset, None

/usr/local/lib/python3.7/dist-packages/skorch/net.py in get_dataset(self, X, y)
   1259             return dataset
   1260 
-&gt; 1261         return dataset(X, y, **kwargs)
   1262 
   1263     def get_split_datasets(self, X, y=None, **fit_params):

/usr/local/lib/python3.7/dist-packages/skorch/dataset.py in __init__(self, X, y, length)
    167             len_y = get_len(y)
    168             if len_y != len_X:
--&gt; 169                 raise ValueError("X and y have inconsistent lengths.")
    170         self._len = len_X
    171 

ValueError: X and y have inconsistent lengths.

</code></pre>
<p>How can I get my torch tensors to match dim? Thank you all.</p>

username: eqy
cooked: <p>There might be a batch dimension expected here where <code>X</code> and <code>y</code> are supposed to match. What is <code>torch_train_y.shape[0]</code> and <code>torch_train_X.shape[0]</code>?</p>

username: Jbrec
cooked: <aside class="quote no-group" data-username="eqy" data-post="2" data-topic="120572">
<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/eqy/48/46233_2.png" class="avatar"> eqy:</div>
<blockquote>
<p><code>torch_train_y.shape[0]</code> and <code>torch_train_X.shape[0]</code>?</p>
</blockquote>
</aside>
<p>Thank you for taking a look, I get a attribute error:</p>
<pre><code class="lang-auto">AttributeError                            Traceback (most recent call last)
&lt;ipython-input-403-c2300dd8f822&gt; in &lt;module&gt;()
----&gt; 1 print(torch_train_y.shape[0])
      2 print(torch_train_X.shape[0])

AttributeError: 'list' object has no attribute 'shape'
</code></pre>

username: eqy
cooked: <p>In this case, try converting your data to tensors first. (However they should have the same length at this step as that will become the first dimension after they are converted.)</p>

username: Jbrec
cooked: <p>Like so?:</p>
<pre><code class="lang-auto"># Extract HOG features 
from skimage.feature import hog
def HOG_features(data):
    num_samples = len(data)
    hog_features = []
    for i in range(num_samples):
        img = data[i]
        feature = hog(img, orientations=9)
        hog_features.append(feature)
    return np.array(hog_features)

HOG_X_train = torch.tensor([HOG_features(X_train)])
HOG_X_test = torch.tensor([HOG_features(X_test)])
</code></pre>