OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Pytorch Conv1d on simple 1d for Signal: The size of tensor a (100) must match the size of tensor b (16)

  • Thread starter Thread starter stevGates
  • Start date Start date
S

stevGates

Guest
I am learning Pytorch using signal classification where all values are binary (0 or 1). The input data is a vector of [ ,32] and the output is [ ,16]. I have a large dataset of more than 400K samples. This is the dataset: LINK

I want to build a CNN model to predict Y from X. I am always obtain this error :

Code:
> RuntimeError: The size of tensor a (100) must match the size of tensor b (16) at non-singleton dimension 1

There is only 1 dimension on all my work. This is the code:

Code:
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F  # Import torch.nn.functional as F
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error
import time
import matplotlib.pyplot as plt
from torch.utils.data import DataLoader, TensorDataset

# Load data
LinkOfDataset = 'NewNew.csv'
Data = pd.read_csv(LinkOfDataset, encoding='utf-8')

ChallengeLen = 32
ResponseLen = 16

NumberOfTraining = 0.6
NumberOfValidation = 0.2
NumberOfTesting = 0.2

# Split data
TrainData, TestData = train_test_split(Data, train_size=0.8)
TestData, ValidationData = train_test_split(TestData, train_size=0.8)

XTrainData = TrainData.iloc[:, :ChallengeLen].values
YTrainData = TrainData.iloc[:, ChallengeLen:].values

XValidationData = ValidationData.iloc[:, :ChallengeLen].values
YValidationData = ValidationData.iloc[:, ChallengeLen:].values

XTestData = TestData.iloc[:, :ChallengeLen].values
YTestData = TestData.iloc[:, ChallengeLen:].values

# Convert data to PyTorch tensors
XTrainTensor = torch.tensor(XTrainData, dtype=torch.float32).unsqueeze(1)
YTrainTensor = torch.tensor(YTrainData, dtype=torch.float32)

XValidationTensor = torch.tensor(XValidationData, dtype=torch.float32).unsqueeze(1)
YValidationTensor = torch.tensor(YValidationData, dtype=torch.float32)

XTestTensor = torch.tensor(XTestData, dtype=torch.float32).unsqueeze(1)
YTestTensor = torch.tensor(YTestData, dtype=torch.float32)

# Create dataloaders
train_dataset = TensorDataset(XTrainTensor, YTrainTensor)
validation_dataset = TensorDataset(XValidationTensor, YValidationTensor)
test_dataset = TensorDataset(XTestTensor, YTestTensor)

train_loader = DataLoader(train_dataset, batch_size=100, shuffle=True)
validation_loader = DataLoader(validation_dataset, batch_size=100, shuffle=False)
test_loader = DataLoader(test_dataset, batch_size=100, shuffle=False)

# Define the model
class CNNModel(nn.Module):
    def __init__(self):
        super(CNNModel, self).__init__()
        super(CNNModel, self).__init__()
        self.conv1 = nn.Conv1d(in_channels=1, out_channels=32, kernel_size=3, padding=1)
        self.conv2 = nn.Conv1d(in_channels=32, out_channels=64, kernel_size=3, padding=1)
        self.pool = nn.MaxPool1d(kernel_size=2)
        self.dropout = nn.Dropout(0.25)
        self.conv3 = nn.Conv1d(in_channels=64, out_channels=64, kernel_size=3, padding=1)
        self.conv4 = nn.Conv1d(in_channels=64, out_channels=128, kernel_size=3, padding=1)
        self.flatten = nn.Flatten()

        # Calculate the size of the input to the first fully connected layer
        # Initial length: 32
        # After conv1 + pool: 32 / 2 = 16
        # After conv2 + pool: 16 / 2 = 8
        # After conv3 + pool: 8 / 2 = 4
        # After conv4 + pool: 4 / 2 = 2
        # Output channels of conv4: 128
        self.fc1_input_dim = 128 * 2  # 128 channels * 2 length

        self.fc1 = nn.Linear(self.fc1_input_dim, 128)
        self.fc2 = nn.Linear(128, 16)  # Output length is 16

        
    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))  # After conv1 + pool: [batch_size, 32, 16]
        x = self.pool(F.relu(self.conv2(x)))  # After conv2 + pool: [batch_size, 64, 8]
        x = self.dropout(x)
        x = self.pool(F.relu(self.conv3(x)))  # After conv3 + pool: [batch_size, 64, 4]
        x = self.dropout(x)
        x = self.pool(F.relu(self.conv4(x)))  # After conv4 + pool: [batch_size, 128, 2]
        x = self.dropout(x)
        x = self.flatten(x)  # Flatten: [batch_size, 128*2 = 256]
        x = F.relu(self.fc1(x))  # Fully connected layer: [batch_size, 128]
        x = self.fc2(x)  # Output layer: [batch_size, 16]
        return x

model = CNNModel()

# Define loss and optimizer
criterion = nn.MSELoss()  # or nn.CrossEntropyLoss() if you are dealing with classification
optimizer = optim.Adam(model.parameters(), lr=0.001)

# Training loop
num_epochs = 1
history = {'loss': [], 'val_loss': [], 'accuracy': [], 'val_accuracy': []}

for epoch in range(num_epochs):
    model.train()
    running_loss = 0.0
    for X_batch, y_batch in train_loader:
        optimizer.zero_grad()
        outputs = model(X_batch)
        loss = criterion(outputs, y_batch)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()
    epoch_loss = running_loss / len(train_loader)
    history['loss'].append(epoch_loss)
    
    model.eval()
    val_loss = 0.0
    correct = 0
    total = 0
    with torch.no_grad():
        for X_batch, y_batch in validation_loader:
            outputs = model(X_batch)
            loss = criterion(outputs, y_batch)
            val_loss += loss.item()
            _, predicted = torch.max(outputs.data, 1)
            total += y_batch.size(0)
            correct += (predicted == y_batch).sum().item()
    epoch_val_loss = val_loss / len(validation_loader)
    epoch_val_accuracy = correct / total
    history['val_loss'].append(epoch_val_loss)
    history['val_accuracy'].append(epoch_val_accuracy)
    
    print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {epoch_loss:.4f}, Val Loss: {epoch_val_loss:.4f}, Val Accuracy: {epoch_val_accuracy:.4f}')

# Save the model
torch.save(model.state_dict(), "PAutoencoder.pth")

# Plotting the training and validation loss
plt.figure(figsize=(12, 5))

plt.subplot(1, 2, 1)
plt.plot(history['loss'], label='Training Loss')
plt.plot(history['val_loss'], label='Validation Loss')
plt.title('Training and Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()

plt.subplot(1, 2, 2)
plt.plot(history['accuracy'], label='Training Accuracy')
plt.plot(history['val_accuracy'], label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()

plt.show()

I don't understand how to solve this problem
<p>I am learning <code>Pytorch</code> using signal classification where all values are binary (0 or 1). The input data is a vector of [ ,32] and the output is [ ,16]. I have a large dataset of more than 400K samples. This is the dataset: <a href="https://www.dropbox.com/scl/fi/bx51...ey=mpqdttoq74f9s1xlwzrp5scct&st=km4ixm7e&dl=0" rel="nofollow noreferrer">LINK</a></p>
<p>I want to build a CNN model to predict Y from X. I am always obtain this error :</p>
<pre><code>> RuntimeError: The size of tensor a (100) must match the size of tensor b (16) at non-singleton dimension 1
</code></pre>
<p>There is only 1 dimension on all my work. This is the code:</p>
<pre><code>import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F # Import torch.nn.functional as F
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error
import time
import matplotlib.pyplot as plt
from torch.utils.data import DataLoader, TensorDataset

# Load data
LinkOfDataset = 'NewNew.csv'
Data = pd.read_csv(LinkOfDataset, encoding='utf-8')

ChallengeLen = 32
ResponseLen = 16

NumberOfTraining = 0.6
NumberOfValidation = 0.2
NumberOfTesting = 0.2

# Split data
TrainData, TestData = train_test_split(Data, train_size=0.8)
TestData, ValidationData = train_test_split(TestData, train_size=0.8)

XTrainData = TrainData.iloc[:, :ChallengeLen].values
YTrainData = TrainData.iloc[:, ChallengeLen:].values

XValidationData = ValidationData.iloc[:, :ChallengeLen].values
YValidationData = ValidationData.iloc[:, ChallengeLen:].values

XTestData = TestData.iloc[:, :ChallengeLen].values
YTestData = TestData.iloc[:, ChallengeLen:].values

# Convert data to PyTorch tensors
XTrainTensor = torch.tensor(XTrainData, dtype=torch.float32).unsqueeze(1)
YTrainTensor = torch.tensor(YTrainData, dtype=torch.float32)

XValidationTensor = torch.tensor(XValidationData, dtype=torch.float32).unsqueeze(1)
YValidationTensor = torch.tensor(YValidationData, dtype=torch.float32)

XTestTensor = torch.tensor(XTestData, dtype=torch.float32).unsqueeze(1)
YTestTensor = torch.tensor(YTestData, dtype=torch.float32)

# Create dataloaders
train_dataset = TensorDataset(XTrainTensor, YTrainTensor)
validation_dataset = TensorDataset(XValidationTensor, YValidationTensor)
test_dataset = TensorDataset(XTestTensor, YTestTensor)

train_loader = DataLoader(train_dataset, batch_size=100, shuffle=True)
validation_loader = DataLoader(validation_dataset, batch_size=100, shuffle=False)
test_loader = DataLoader(test_dataset, batch_size=100, shuffle=False)

# Define the model
class CNNModel(nn.Module):
def __init__(self):
super(CNNModel, self).__init__()
super(CNNModel, self).__init__()
self.conv1 = nn.Conv1d(in_channels=1, out_channels=32, kernel_size=3, padding=1)
self.conv2 = nn.Conv1d(in_channels=32, out_channels=64, kernel_size=3, padding=1)
self.pool = nn.MaxPool1d(kernel_size=2)
self.dropout = nn.Dropout(0.25)
self.conv3 = nn.Conv1d(in_channels=64, out_channels=64, kernel_size=3, padding=1)
self.conv4 = nn.Conv1d(in_channels=64, out_channels=128, kernel_size=3, padding=1)
self.flatten = nn.Flatten()

# Calculate the size of the input to the first fully connected layer
# Initial length: 32
# After conv1 + pool: 32 / 2 = 16
# After conv2 + pool: 16 / 2 = 8
# After conv3 + pool: 8 / 2 = 4
# After conv4 + pool: 4 / 2 = 2
# Output channels of conv4: 128
self.fc1_input_dim = 128 * 2 # 128 channels * 2 length

self.fc1 = nn.Linear(self.fc1_input_dim, 128)
self.fc2 = nn.Linear(128, 16) # Output length is 16


def forward(self, x):
x = self.pool(F.relu(self.conv1(x))) # After conv1 + pool: [batch_size, 32, 16]
x = self.pool(F.relu(self.conv2(x))) # After conv2 + pool: [batch_size, 64, 8]
x = self.dropout(x)
x = self.pool(F.relu(self.conv3(x))) # After conv3 + pool: [batch_size, 64, 4]
x = self.dropout(x)
x = self.pool(F.relu(self.conv4(x))) # After conv4 + pool: [batch_size, 128, 2]
x = self.dropout(x)
x = self.flatten(x) # Flatten: [batch_size, 128*2 = 256]
x = F.relu(self.fc1(x)) # Fully connected layer: [batch_size, 128]
x = self.fc2(x) # Output layer: [batch_size, 16]
return x

model = CNNModel()

# Define loss and optimizer
criterion = nn.MSELoss() # or nn.CrossEntropyLoss() if you are dealing with classification
optimizer = optim.Adam(model.parameters(), lr=0.001)

# Training loop
num_epochs = 1
history = {'loss': [], 'val_loss': [], 'accuracy': [], 'val_accuracy': []}

for epoch in range(num_epochs):
model.train()
running_loss = 0.0
for X_batch, y_batch in train_loader:
optimizer.zero_grad()
outputs = model(X_batch)
loss = criterion(outputs, y_batch)
loss.backward()
optimizer.step()
running_loss += loss.item()
epoch_loss = running_loss / len(train_loader)
history['loss'].append(epoch_loss)

model.eval()
val_loss = 0.0
correct = 0
total = 0
with torch.no_grad():
for X_batch, y_batch in validation_loader:
outputs = model(X_batch)
loss = criterion(outputs, y_batch)
val_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
total += y_batch.size(0)
correct += (predicted == y_batch).sum().item()
epoch_val_loss = val_loss / len(validation_loader)
epoch_val_accuracy = correct / total
history['val_loss'].append(epoch_val_loss)
history['val_accuracy'].append(epoch_val_accuracy)

print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {epoch_loss:.4f}, Val Loss: {epoch_val_loss:.4f}, Val Accuracy: {epoch_val_accuracy:.4f}')

# Save the model
torch.save(model.state_dict(), "PAutoencoder.pth")

# Plotting the training and validation loss
plt.figure(figsize=(12, 5))

plt.subplot(1, 2, 1)
plt.plot(history['loss'], label='Training Loss')
plt.plot(history['val_loss'], label='Validation Loss')
plt.title('Training and Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()

plt.subplot(1, 2, 2)
plt.plot(history['accuracy'], label='Training Accuracy')
plt.plot(history['val_accuracy'], label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()

plt.show()
</code></pre>
<p>I don't understand how to solve this problem</p>
 

Latest posts

Top