Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CNN 모델 #116

Open
arabae opened this issue Jun 15, 2021 · 0 comments
Open

CNN 모델 #116

arabae opened this issue Jun 15, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@arabae
Copy link
Contributor

arabae commented Jun 15, 2021

1dConv를 사용하여 구축한 모델 공유합니다 :)

  • best feature를 사용했을 때, LB 0.8095 나왔습니다.

class DeepConv(nn.Module):
    def __init__(self, args):
        super(DeepConv, self).__init__()
        self.args = args
        self.device = args.device

        self.hidden_dim = self.args.hidden_dim

        # categorical features
        # ========================== nn.Embedding에 들어갈 self.embedding_dims의 개수를 수정해주세요 ===============================
        self.in_channels = self.args.n_cates + self.args.n_conts
        # =========================================================================================================================

        self.conv1 = nn.Sequential(
            nn.Conv1d(in_channels=self.in_channels,
                    out_channels=self.hidden_dim,
                    kernel_size=3,
                    padding=1,
                    padding_mode='zeros'),
            nn.BatchNorm1d(self.hidden_dim),
            nn.ReLU()
        )
        
        self.avg_pool = nn.AvgPool1d(kernel_size=2)

        self.conv2 = nn.Sequential(
            nn.Conv1d(in_channels=self.hidden_dim//2,
                    out_channels=self.hidden_dim,
                    kernel_size=3, 
                    padding=1,
                    padding_mode='zeros'),
            nn.BatchNorm1d(self.hidden_dim),
            nn.ReLU()
        )
        
        
        self.conv3 = nn.Sequential(
            nn.Conv1d(in_channels=self.hidden_dim,
                    out_channels=self.hidden_dim,
                    kernel_size=3, 
                    padding=1,
                    padding_mode='zeros'),
            nn.BatchNorm1d(self.hidden_dim),
            nn.ReLU()
        )
        
        self.conv4 = nn.Sequential(
            nn.Conv1d(in_channels=self.hidden_dim,
                    out_channels=self.hidden_dim,
                    kernel_size=3, 
                    padding=1,
                    padding_mode='zeros'),
            nn.BatchNorm1d(self.hidden_dim),
            nn.ReLU()
        )

        self.max_pool = nn.MaxPool1d(kernel_size=2)

        # Fully connected layer
        self.fc = nn.Linear(self.hidden_dim//2, 1)
        self.activation = nn.Sigmoid()
  

    def forward(self, input):
        categorical, continuous, mask, __ = input

        batch_size = categorical[0].size(0)
        seq_len = categorical[0].size(1)

        # concat Catgegorical & Continuous Feature
        x_cat = torch.stack(categorical) # [17, 128, 250]
        x_cont = torch.stack(continuous) # [3, 128, 250]

        embed = torch.cat([x_cat, x_cont], 0) # [20, 128, 250]
        embed = embed.permute(1, 0, 2)   # [128, 20, 250]

        conv1_output = self.conv1(embed.type(torch.FloatTensor).to(self.device))
        conv1_output = conv1_output.permute(0, 2, 1)

        avg_output = self.avg_pool(conv1_output)
        avg_output = avg_output.permute(0, 2, 1)

        conv2_output = self.conv2(avg_output)
        conv3_output = self.conv3(conv2_output)
        conv4_output = self.conv3(conv3_output)

        res = conv2_output + conv4_output
        res = res.permute(0, 2, 1)
        max_output = self.max_pool(res)

        out = self.fc(max_output)
        pred = self.activation(out).view(batch_size, -1)

        return pred
@arabae arabae added the enhancement New feature or request label Jun 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant