def trainmodel(trainloader, numepochs, learningrate): criterion = nn.BCELoss() optimizer…
- User
def train_model(train_loader, num_epochs, learning_rate): criterion = nn.BCELoss() optimizer = optim.Adam(model.parameters(), lr=learning_rate)
codetrain_losses = [] for epoch in range(num_epochs): total_loss = 0 for inputs, labels in train_loader: inputs = inputs.float() labels = labels.float() optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() total_loss += loss.item() avg_loss = total_loss / len(train_loader) train_losses.append(avg_loss) print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {avg_loss:.4f}') return train_lossesnum_epochs = 20 learning_rate = 0.001 train_losses = train_model(train_loader, num_epochs, learning_rate)
plt.figure(figsize=(10, 5)) plt.plot(range(1, num_epochs+1), train_losses, marker='o') plt.title('Training Loss Curve') plt.xlabel('Epoch #') plt.ylabel('Loss') plt.grid(True) plt.show() why is my shit not working it is saying a mitch match of dimensions
- Assistant
- User
File c:\Users<redacted>\anaconda3\Anaconda\Lib\site-packages\torch\nn\modules\module.py:1739, in Module._wrapped_call_impl(self, *args, **kwargs) 1737 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc] 1738 else: -> 1739 return self._call_impl(*args, **kwargs)
File c:\Users<redacted>\anaconda3\Anaconda\Lib\site-packages\torch\nn\modules\module.py:1750, in Module._call_impl(self, *args, **kwargs) 1745 # If we don't have any hooks, we want to skip the rest of the logic in 1746 # this function, and just call forward. 1747 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks ... 3563 ) 3565 if weight is not None: 3566 new_size = _infer_size(target.size(), weight.size())
ValueError: Using a target size (torch.Size([1])) that is different to the input size (torch.Size([1, 1])) is deprecated. Please ensure they have the same size. here is the error message
- Assistant
The error message you're seeing indicates that there's a mismatch between the dimensions of the output from your model and the dimensions of your target labels. Specifically, it states that the target size is
(torch.Size([1])), while the input size is . This means that your model is outputting a tensor with shape , but the labels are in the shape . - User
- Assistant
- User
without the section headers
- Assistant
- Usercode
Learning Rate Optimizer Layers Hidden Size Test Accuracy (%)0 0.001 adam 2 64 60.0 1 0.001 sgd 2 64 47.0 2 0.010 adam 3 128 76.0 3 0.010 sgd 3 128 51.0 4 0.001 adam 3 256 67.0 can you return this as a markdown table
- Assistant
Certainly! Here is your data formatted as a markdown table:
markdown| Learning Rate | Optimizer | Layers | Hidden Size | Test Accuracy (%) | |---------------|-----------|--------|-------------|-------------------| | 0.001 | adam | 2 | 64 | 60.0 | | 0.001 | sgd | 2 | 64 | 47.0 | | 0.010 | adam | 3 | 128 | 76.0 | | 0.010 | sgd | 3 | 128 | 51.0 | | 0.001 | adam | 3 | 256 | 67.0 |You can copy and paste the above markdown code into any markdown editor or viewer to see it rendered as a table.