something-original
something-original

Reputation: 23

How to choose PyTorch Conv1d input parameters?

I am new to machine learning and I want to know how to specify Conv1d parameters for time series analysis. My input is a vector of 3 features - x, y, z acceleration respectively, and my target is only 0 or 1 (binary classification). My dataset contains 1200 1-dimension vectors with length of 3. Here's the code I've written:

class my_CNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.model = nn.Sequential(
            nn.Conv1d(5, 3, 1200),
            nn.ReLU(),
            nn.MaxPool1d(1, 3),
            nn.Conv1d(5, 3, 400),
            nn.ReLU(),
            nn.MaxPool2d(1, 2),
            nn.Flatten()
        )

    def forward(self, x):
        return self.model(x)

I've written nn.Conv1d(5, 3, 1200) because I've read that 1st parameter stands for number of batches (I have 5), then number of channels (I guess here I have 3) and input shape (here I have 1200 if I understood it correctly).

However, when I try to start learning, I receive an error: RuntimeError: Expected 2D (unbatched) or 3D (batched) input to conv1d, but got input of size: [3]

The code for learning is like this:

    for epoch in range(10):
        for X, y in zip(dataset, labels):
            X = [float(x) for x in X]
            X = tensor(np.array(X), dtype=torch.float32)
            y = tensor(np.array(y), dtype=torch.float32)
            X, y = X.to('cpu'), y.to('cpu')
            ypred = clf_acc(X)
            loss = loss_fn(ypred, y)

            opt.zero_grad()
            loss.backward()
            opt.step()
        print(f"Epoch:{epoch}, loss is {loss_fn.item()}")

I will highly appreciate your help and comments. I know there are same questions, but after reading the answers I receive even more questions since each case is individual.

Upvotes: 1

Views: 165

Answers (0)

Related Questions