Giter VIP home page Giter VIP logo

Comments (5)

greed2411 avatar greed2411 commented on May 12, 2024

For multi label classification one is supposed to use sigmoid over softmax, because softmax makes sure the output of all values add up to zero. Therefore, you can get it to predict only one value. Whereas sigmoid there is no such restriction.

from pytorch-sentiment-analysis.

enod avatar enod commented on May 12, 2024

@greed2411
Thanks for the prompt answer. So in that case, does model should look like following?


class CNN(nn.Module):
    def __init__(self, vocab_size, embedding_dim, n_filters, filter_sizes, output_dim, dropout):
        super().__init__()
        self.embedding = nn.Embedding(vocab_size, embedding_dim)
        self.conv_0 = nn.Conv2d(in_channels=1, out_channels=n_filters, kernel_size=(filter_sizes[0],embedding_dim))
        self.conv_1 = nn.Conv2d(in_channels=1, out_channels=n_filters, kernel_size=(filter_sizes[1],embedding_dim))
        self.conv_2 = nn.Conv2d(in_channels=1, out_channels=n_filters, kernel_size=(filter_sizes[2],embedding_dim))
        self.fc = nn.Linear(len(filter_sizes)*n_filters, output_dim)
        self.dropout = nn.Dropout(dropout)
        
    def forward(self, x):
        
        #x = [sent len, batch size]
        
        x = x.permute(1, 0)
                
        #x = [batch size, sent len]
        
        embedded = self.embedding(x)
                
        #embedded = [batch size, sent len, emb dim]
        
        embedded = embedded.unsqueeze(1)
        
        #embedded = [batch size, 1, sent len, emb dim]
        
        conved_0 = F.relu(self.conv_0(embedded).squeeze(3))
        conved_1 = F.relu(self.conv_1(embedded).squeeze(3))
        conved_2 = F.relu(self.conv_2(embedded).squeeze(3))
            
        #conv_n = [batch size, n_filters, sent len - filter_sizes[n]]
        
        pooled_0 = F.max_pool1d(conved_0, conved_0.shape[2]).squeeze(2)
        pooled_1 = F.max_pool1d(conved_1, conved_1.shape[2]).squeeze(2)
        pooled_2 = F.max_pool1d(conved_2, conved_2.shape[2]).squeeze(2)
        
        #pooled_n = [batch size, n_filters]
        
        cat = self.dropout(torch.cat((pooled_0, pooled_1, pooled_2), dim=1))

        #cat = [batch size, n_filters * len(filter_sizes)]
            
        return F.sigmoid(self.fc(cat))

from pytorch-sentiment-analysis.

greed2411 avatar greed2411 commented on May 12, 2024

Yes that's how it should be.

On another case, can't think of a reason why @bentrevett used Conv2d instead of Conv1d for text data.

from pytorch-sentiment-analysis.

bentrevett avatar bentrevett commented on May 12, 2024

@enod, I believe that implementation is correct.

@greed2411, I'm not sure why I used it either. I believe I found it found it easier to get my head around it when thinking about it as 2d.

I'm currently in the process of updating these tutorials to TorchText 0.3 (as it has better integration with PyTorch 0.4) and will change to a Conv1d.

from pytorch-sentiment-analysis.

enod avatar enod commented on May 12, 2024

@greed2411 @bentrevett
Thanks for the feedback. Using sigmoid didn't work.
It turns out BCEWithLogitsLoss has sigmoid layer in it already. So if I'm not wrong, there is no need to add sigmoid layer in the model.

from pytorch-sentiment-analysis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.