0

i build a classifying model about phishing site.

1. first, about my data

num_dataset: i have about 16000 dataset

num_feature: my dataset has 12 characterisitics.

label: if it's a phishing site, i set it's label -1. else 1

batch_size: set batch_size 128, for cnn model

kernel_size: 3

2. I try

from torch.utils.data import DataLoader, TensorDataset

train_dataset = TensorDataset(x_train, y_train)
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)

#-------------------------------------------------------#
class CNN(nn.Module):
  def __init__(self, kernel_size):
    super().__init__()  
    self.layer1 = nn.Sequential(
        nn.Conv1d(12, 32, 3,
                  stride=1, padding=1)
...

  def forward(self, x):
    out = self.layer1(x)
...
#-------------------------------------------------------#
model = CNN()

for epoch in range(epochs):
  avg_cost = 0

  for x_train, y_train in train_loader:

    Hypothesis = model(x_train)

x_train.shape

torch.Size([16072, 12])

y_train.shape

torch.Size([16072, 1])

3. Error

at Hypothesis = model(x_train)

RuntimeError: Expected 3-dimensional input for 3-dimensional weight [32, 12, 3], but got 2-dimensional input of size [128, 12] instead

4. Finnally

i think it's because I'm confused between conv1d and conv2d but i can't figure it out...

plz, i want to know the cause of this problem

1 Answer 1

1

You are using a nn.Conv1d which should receive a 3-dimensional input shaped (batch_size, n_channels, sequence_length). This being said your input has n_channels=12 (since you've initialized your 1d conv with 12 input channels) and a sequence_length=1. To match the requirements, you need to have an additional dimension on your input. Passing something like x_train.unsqueeze(-1) to your network should work.

See the following example:

>>> x = torch.rand(100, 12, 1)
>>> L = nn.Conv1d(12, 32, 3, stride=1, padding=1)
>>> L(x).shape
torch.Size([100, 32, 1])
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so very very very much. it worked out! I wish i could help someone else like you soon!!!!!
have you a nice day, and hope you had a great day

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.