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

Hi There,

I can’t seem to get my y_train tensor to match my X_train tensor shape

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]]))
print(y_train.shape)

print(torch_train_y[0])

print(torch_train_y[0].shape)

print(torch_train_X[0].shape)

OUT
(12271,)
tensor([4])
torch.Size([1])
torch.Size([8100])

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


net = NeuralNetClassifier(
    MLP,
    max_epochs=20,
    lr=0.1,
    device='cuda'
) 

net.fit(torch_train_X,torch_train_y)

Re-initializing module.
Re-initializing optimizer.
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-154-e0cb7063e59c> in <module>()
----> 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
--> 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 
--> 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:
--> 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(
--> 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         """
-> 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 
-> 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:
--> 169                 raise ValueError("X and y have inconsistent lengths.")
    170         self._len = len_X
    171 

ValueError: X and y have inconsistent lengths.

How can I get my torch tensors to match dim? Thank you all.

There might be a batch dimension expected here where X and y are supposed to match. What is torch_train_y.shape[0] and torch_train_X.shape[0]?

Thank you for taking a look, I get a attribute error:

AttributeError                            Traceback (most recent call last)
<ipython-input-403-c2300dd8f822> in <module>()
----> 1 print(torch_train_y.shape[0])
      2 print(torch_train_X.shape[0])

AttributeError: 'list' object has no attribute 'shape'

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.)

Like so?:

# 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)])