0

I'm new in pytorch and just try to write a network. The data.shape is (204,6170) and the last 5 cols are some labels. The number in data is float number like 0.030822.

#%%
from sklearn.feature_selection import RFE
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from sklearn.model_selection import train_test_split
import torch.functional as F

#%%
data = pd.read_table("table.log")
data_x = data.iloc[:, 0:(data.shape[1]-5)]
data_y = data.loc[:, 'target']
X_train, X_test, y_train, y_test = train_test_split(data_x,data_y,test_size=0.2,random_state=0)

#%%
from sklearn.linear_model import LinearRegression
lr = LinearRegression(normalize=True)
lr.fit(X_train,y_train)
rfe1 = RFE(estimator=lr,n_features_to_select=2000)
rfe1 = rfe1.fit(X_train,y_train)

#%%
x_train_rfe1 = X_train[X_train.columns[rfe1.support_]]
print(x_train_rfe1.head())
class testmodel(nn.Module):
    def __init__(self):
        super(testmodel,self).__init__()
        self.conv = nn.Sequential(
            nn.Conv1d(1500, 500, 1500, 0, 0),
            nn.ReLU(),
            nn.Conv1d(500, 100, 500, 0),
            nn.ReLU(),
            nn.Conv1d(100, 20, 100, 0),
            nn.Sigmoid()
        )
    def forward(self,x):
        x = self.conv
        return x
#%%
x_train_rfe1 = torch.Tensor(x_train_rfe1.values)
y_train = torch.Tensor(y_train.values.astype(np.int64))
model = testmodel()
y = model(x_train_rfe1)

criterion = nn.MSELoss()
loss = criterion(y, y_train)
print(loss)

error message:

Traceback (most recent call last):
  File "<input>", line 7, in <module>
  File "/miniconda3/envs/ml/lib/python3.8/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/miniconda3/envs/ml/lib/python3.8/site-packages/torch/nn/modules/loss.py", line 431, in forward
    return F.mse_loss(input, target, reduction=self.reduction)
  File "/miniconda3/envs/ml/lib/python3.8/site-packages/torch/nn/functional.py", line 2203, in mse_loss
    if not (target.size() == input.size()):
  File "/miniconda3/envs/ml/lib/python3.8/site-packages/torch/nn/modules/module.py", line 575, in __getattr__
    raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'Sequential' object has no attribute 'size'

Where can the error was? Is the internet usually written like this? How could I improve it?

1

1 Answer 1

2

You never run your input tensor x through your conv sequential layer in forward.

def forward(self, x):
    x = self.conv(x)
    return x

Do some PyTorch tutorials they will help you get the basics down: https://pytorch.org/tutorials/

Sign up to request clarification or add additional context in comments.

Comments

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.