0

I downloaded a train dataset in json format and i read it in Google Colab. I have created a convolutional neural network and i want to train it with the train_set dataset. I also imported DataLoader because i want batches instead of images. So, i am running the following script in Google Colab:

import json

! wget -O train_set.json https://github.com/rslab-ntua/MSc_GBDA/blob/master/2020/Exercise_ML2/train_split.json

with open('train_set.json') as f:
  train_set = f.read()

from torch.utils.data import DataLoader

trainloader = DataLoader(train_set, batch_size=32, shuffle=True)


from torch import nn
from torch.nn import functional as F
from torchsummary import summary

# Convolutional Neural Network definition
class CNN(nn.Module):
  def __init__(self):
    super().__init__()
    self.conv_features = nn.Sequential(
        nn.Conv2d(in_channels=1, out_channels=4, kernel_size=3, stride=1, padding=1),
        nn.ReLU(inplace=True),
        nn.MaxPool2d(kernel_size=2, stride=2, padding=0),
        nn.Conv2d(in_channels=4, out_channels=16, kernel_size=3, stride=1, padding=1),
        nn.ReLU(inplace=True),
        nn.MaxPool2d(kernel_size=2, stride=2, padding=0),
        nn.Conv2d(in_channels=16, out_channels=24, kernel_size=3, stride=1, padding=1),
        nn.ReLU(inplace=True),
        nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
    )

    self.fc1 = nn.Linear(24*3*3,256)
    self.fc2 = nn.Linear(256,10)

  def forward(self, x):
    x = self.conv_features(x)
    x = x.view(x.size()[0], -1) # το κανω flattened
    x = F.relu(self.fc1(x))
    return self.fc2(x)

model = CNN()
summary(model, (1,28,28))

from torch.optim import SGD

num_epochs = 5

criterion = nn.CrossEntropyLoss()

optimizer = SGD(model.parameters(), lr=0.001, momentum = 0.9)

for epoch in range(num_epochs):
  total_loss = 0
  for images, labels in trainloader:
    optimizer.zero_grad()

    logits = model(images.to('cuda'))

    loss = criterion(logits, labels.to('cuda'))
    loss.backward()

    optimizer.step()
    total_loss += float(loss.cpu().detach())

print(f'Epoch {epoch+1}: total_mean_loss: {total_loss/len(trainloader)}')

and i get the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-082efbc4edf5> in <module>()
      9 for epoch in range(num_epochs):
     10   total_loss = 0
---> 11   for images, labels in trainloader:
     12     optimizer.zero_grad()
     13 

ValueError: too many values to unpack (expected 2)

I can not find the problem. Is it the object train_set that causes the problem?

George
  • 31
  • 1
  • 4
  • 1
    Does this answer your question? [ValueError: too many values to unpack while using torch tensors](https://stackoverflow.com/questions/62406283/valueerror-too-many-values-to-unpack-while-using-torch-tensors) – BoomBoxBoy Jun 14 '21 at 17:17
  • Try `print(train_set[0])` and see what it look like – Natthaphon Hongcharoen Jun 14 '21 at 18:39
  • ```print(train_set[0])``` does nothing. Neither prints a result nor gives an error. ```print(train_set(0))``` gives this error: _TypeError: 'str' object is not callable_ and ```print(train_set)``` gives this message: _IOPub data rate exceeded. The notebook server will temporarily stop sending output to the client in order to avoid crashing it. To change this limit, set the config variable `--NotebookApp.iopub_data_rate_limit`. Current values: NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec) NotebookApp.rate_limit_window=3.0 (secs)_ – George Jun 15 '21 at 06:01
  • could you provide us with the shape of your train_set? use: print(train_set.shape) Make sure that your train_set has the following dimension: (train_x, train_y) with dimension of train_x: (no_training_examples, image_width, image_height, color_channels) and dimension of train_y: (no_training_examples, 10) – yuki Jun 15 '21 at 12:58
  • The shape of my train_set is (18900, 64, 64, 3). I suppose that i only have train_x in my train_set and i must also have the train_y in it. – George Jun 16 '21 at 06:58

0 Answers0