GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||
GeezerVison is showing github.com โ Text Classification using CNN Text Classification using CNN
Yutong Wang edited this page
For each tweets record, it needs three labeler:
For more details of building dataset and Use this sql to select 468 tweets records from database which are labeled to be true(0). SELECT text from records where label1 = 0 Use this sql to select 532 tweets records from database which are labeled to be false(1). SELECT text from records where label1 = 1 So in all the training dataset contains 1000 different tweet records. Use similar sql statement to select 528 tweets records from database where The architecture of CNN used to classifying tweets text follows the work in Convolutional neural networks for sentence classification. The following code defines the architecture of CNN.
class CNN_Text(nn.Module): def __init__(self, args, vocab_len, weights): super(CNN_Text, self).__init__() self.args = args # parameters V = vocab_len D = args.embed_dim C = 2 Ci = 1 # conv layer input dimention Co = args.kernel_num # conv layer output dimention Ks_str = args.kernel_sizes Ks = [int(k) for k in Ks_str.split(',')] # embedding layer, input_size = V, output_size = D self.embed = nn.Embedding(V, D) if args.glove_embed: self.embed.weight = nn.Parameter(torch.FloatTensor(weights), requires_grad=args.glove_embed_train) if args.multichannel: self.embed1 = nn.Embedding(V, D) self.embed1.weight = nn.Parameter(torch.FloatTensor(weights), requires_grad=False) Ci = 2 # self.convs1 = nn.ModuleList([nn.Conv2d(Ci, Co, (K, D)) for K in Ks]) self.convs1 = nn.ModuleList([nn.Conv2d(Ci, Co, (1, 300)) for K in Ks]) self.dropout = nn.Dropout(args.dropout) self.fc1 = nn.Linear(len(Ks) * Co, C) def forward(self, input): x = self.embed(input) x = x.unsqueeze(1) if self.args.multichannel: x1 = self.embed1(input) x1 = x1.unsqueeze(1) x = torch.cat((x, x1), 1) x = [F.relu(conv(x)).squeeze(3) for conv in self.convs1] x = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in x] x = torch.cat(x, 1) x = self.dropout(x) logit = F.softmax(self.fc1(x), dim=-1) return logit To embed each word as a vector, I download an existed embedding file gensim_model = KeyedVectors.load_word2vec_format(self.read_path + 'GoogleNews-vectors-negative300.bin', binary=True) vocab = gensim_model.vocab vocab_len = len(vocab) weights = gensim_model.vectors For more details of training and testing process, please checkout to Here are several highlighted training parameters:
To train the model, use following code to call the train method: import train train.train(train_loader, validate_loader, test_loader, model, my_loss, weight, args) To test the model during training process, call
Yutong Wang / @RainyTong | ||||||||||||||
| You are visitor 01402775 โ GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800ร600. No cookies required. |