Skip to content
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
c575f12
LZQ:Add Classification Task__CNN
ziqian98 Dec 11, 2020
aac73a6
LZQ: Reader and Reader Test
ziqian98 Dec 11, 2020
c9a4771
LZQ:Update main train
ziqian98 Dec 11, 2020
ff24adc
LZQ:Update CNN model
ziqian98 Dec 11, 2020
9b28b8d
LZQ: Update minor bugs in main train
ziqian98 Dec 11, 2020
f2acf1a
LZQ: Update minor bugs in main predict
ziqian98 Dec 11, 2020
b74d77c
LZQ: Update for minor changes
ziqian98 Dec 16, 2020
9ae5fb4
LZQ: update minor changes in main_train
ziqian98 Dec 16, 2020
a73d7e6
LZQ: Add Bert in Classification Task
ziqian98 Dec 16, 2020
3b215e9
LZQ: Add Bert in Classification Prediction
ziqian98 Dec 16, 2020
7480e5a
LZQ: Merge data augmentation feature with main_train_cnn
ziqian98 Dec 16, 2020
83cbea7
LZQ: Add reader for merging data_augmentation feature
ziqian98 Dec 16, 2020
456f66d
Merge branch 'master' into lzq_new_classification
hunterhector Dec 17, 2020
7ccffee
Reform main train
ziqian98 Dec 19, 2020
9b8d1aa
LZQ: Reform redaer
ziqian98 Dec 20, 2020
7f560d7
LZQ: Delete old files
ziqian98 Dec 20, 2020
9ba6808
LZQ: Delete old augmentation reader
ziqian98 Dec 20, 2020
cad9fb7
LZQ: Upload imdb data samples to lzq_new_classification
ziqian98 Dec 20, 2020
63740ec
LZQ: Format
ziqian98 Dec 20, 2020
7830f81
LZQ: Format
ziqian98 Dec 20, 2020
d1cac8d
LZQ: Format main_train
ziqian98 Dec 20, 2020
92354a0
LZQ: Add Classification Data
ziqian98 Dec 20, 2020
fe70efc
LZQ: Add config_data yml file
ziqian98 Dec 20, 2020
c2efe83
LZQ: Add config model yml file
ziqian98 Dec 20, 2020
8897bdc
LZQ: Add config predict yml
ziqian98 Dec 20, 2020
a116684
LZQ: Add cnn model
ziqian98 Dec 20, 2020
2b9bc33
LZQ: Add prediction
ziqian98 Dec 20, 2020
04b0992
LZQ: Add utilility function
ziqian98 Dec 20, 2020
1ccd9b1
LZQ: Format cnn
ziqian98 Dec 20, 2020
e91633d
LZQ: Format main predict
ziqian98 Dec 20, 2020
f0d09df
LZQ: Format util
ziqian98 Dec 20, 2020
fd5141c
LZQ: Format cnn
ziqian98 Dec 20, 2020
ff779c8
LZQ: format CNN
ziqian98 Dec 20, 2020
acaf94e
LZQ: Format IMDB Reader
ziqian98 Dec 20, 2020
87a8bd5
LZQ: Format imdb reader
ziqian98 Dec 20, 2020
37f9b4d
LZQ: Format imdb reader
ziqian98 Dec 20, 2020
80fbef4
LZQ: Modify inputs of collect in reader
ziqian98 Dec 20, 2020
084f841
LZQ: Format reader
ziqian98 Dec 21, 2020
5c7e7a0
LZQ: Add Readme file
ziqian98 Dec 21, 2020
7d18628
LZQ: Update Train
ziqian98 Jan 5, 2021
d905307
LZQ: Fix label prediction problems
ziqian98 Jan 6, 2021
2ae2621
LZQ: Add testing accuracy
ziqian98 Jan 6, 2021
e6f3aea
LZQ: train format
ziqian98 Jan 6, 2021
dc45084
LZQ: update predict
ziqian98 Jan 6, 2021
a6e2388
LZQ: Reader for new API
ziqian98 Jan 10, 2021
2b5da59
LZQ: update train for new API
ziqian98 Jan 10, 2021
f19a07b
LZQ: Format train for new API
ziqian98 Jan 10, 2021
ab56bb2
LZQ: update reader test for new API
ziqian98 Jan 10, 2021
8eba3c0
LZQ: Format train
ziqian98 Jan 12, 2021
08bb2ad
LZQ: Format model cnn
ziqian98 Jan 12, 2021
045bdda
LZQ: Format main train
ziqian98 Jan 12, 2021
3b4b10e
LZQ: Remove a util function
ziqian98 Jan 12, 2021
692faa3
Reform the extractor and add mask
ziqian98 Jan 12, 2021
1f94612
LZQ: both pisitvie and negetavie keys are contained
ziqian98 Jan 12, 2021
75c5ab6
LZQ: Update for sentiment dictionary keys
ziqian98 Jan 12, 2021
59d6464
LZQ: change the way of adding data aug processor
ziqian98 Jan 12, 2021
bcaf655
LZQ: Add a data_aug flag
ziqian98 Jan 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions examples/Classification_new/cnn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2020 The Forte Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This file predict the ner tag for conll03 dataset."""

import torch
from texar.torch.modules.embedders import WordEmbedder
from texar.torch.modules.classifiers.conv_classifiers import Conv1DClassifier
from torch import nn
from texar.torch.data import Batch

def pad_each_bach(word, max_sen_len):
batch_size = word.shape[0]
curr_len = word.shape[1]
word_list = word.tolist()

# Line 0 in word_embedding_table is padding vec
for i in range(batch_size):
for j in range(max_sen_len-curr_len):
word_list[i].append(0)

return torch.LongTensor(word_list)


class CNN_Classifier(nn.Module):
def __init__(self, in_channels, word_embedding_table):
super().__init__()
self.embedder = WordEmbedder(init_value=word_embedding_table)

self.classifier = \
Conv1DClassifier(in_channels=in_channels,
in_features=word_embedding_table.size()[0])

self.max_sen_len = in_channels

def forward(self, batch: Batch):
word = batch["text_tag"]["data"]

word_pad = pad_each_bach(word, self.max_sen_len)

word_pad_embed = self.embedder(word_pad)

logits, pred = self.classifier(word_pad_embed)

return logits, pred

72 changes: 72 additions & 0 deletions examples/Classification_new/main_predict_bert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Copyright 2020 The Forte Authors. All Rights Reserved.
Comment thread
ziqian98 marked this conversation as resolved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This file predict the ner tag for conll03 dataset."""

import yaml
import torch
from forte.pipeline import Pipeline
from forte.predictor import Predictor
from ft.onto.base_ontology import Sentence
from forte.data.readers.imdb_reader import IMDBReader

def pad_each_bach(word, max_sen_len):
Comment thread
ziqian98 marked this conversation as resolved.
Outdated
batch_size = word.shape[0]
curr_len = word.shape[1]
word_list = word.tolist()

# Line 0 in word_embedding_table is padding vec
for i in range(batch_size):
for j in range(max_sen_len-curr_len):
word_list[i].append(0)

return torch.LongTensor(word_list)


def predict_forward_fn(model, batch):
'''Use model and batch data to predict label.'''
word = batch["text_tag"]["data"]
logits, pred = model(pad_each_bach(word, 500))
pred = pred.numpy()
print(pred)
Comment thread
ziqian98 marked this conversation as resolved.
Outdated
return {"label_tag": pred}


config_predict = yaml.safe_load(open("config_predict.yml", "r"))
Comment thread
ziqian98 marked this conversation as resolved.
saved_model = torch.load(config_predict['model_path'])
Comment thread
ziqian98 marked this conversation as resolved.
train_state = torch.load(config_predict['train_state_path'])

reader = IMDBReader()
predictor = Predictor(batch_size=config_predict['batch_size'],
model=saved_model,
predict_forward_fn=predict_forward_fn,
feature_resource=train_state['feature_resource'])
#evaluator = CoNLLNEREvaluator()
Comment thread
ziqian98 marked this conversation as resolved.
Outdated

pl = Pipeline()
pl.set_reader(reader)
pl.add(predictor)
#pl.add(evaluator)
pl.initialize()

for pack in pl.process_dataset(config_predict['test_path']):
print("---- pack ----")
for instance in pack.get(Sentence):
sentence = instance.text
predicts = []
for entry in pack.get(Sentence, instance):
predicts.append(entry.speaker)
print('---- example -----')
print("sentence: ", sentence)
print("predict sentiment: ", predicts)
#print(evaluator.get_result())
60 changes: 60 additions & 0 deletions examples/Classification_new/main_predict_cnn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 2020 The Forte Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This file predict the ner tag for conll03 dataset."""
Comment thread
ziqian98 marked this conversation as resolved.
Outdated

import yaml
import torch
import examples.Classification_new.cnn
from forte.pipeline import Pipeline
from forte.predictor import Predictor
from ft.onto.base_ontology import Sentence
from forte.data.readers.imdb_reader import IMDBReader


def predict_forward_fn(model, batch):
'''Use model and batch data to predict label.'''
logits, pred = model(batch)
pred = pred.numpy()
print(pred)
return {"label_tag": pred}


config_predict = yaml.safe_load(open("config_predict.yml", "r"))
saved_model = torch.load(config_predict['model_path'])
train_state = torch.load(config_predict['train_state_path'])

reader = IMDBReader()
predictor = Predictor(batch_size=config_predict['batch_size'],
model=saved_model,
predict_forward_fn=predict_forward_fn,
feature_resource=train_state['feature_resource'])
#evaluator = CoNLLNEREvaluator()

pl = Pipeline()
pl.set_reader(reader)
pl.add(predictor)
#pl.add(evaluator)
pl.initialize()

for pack in pl.process_dataset(config_predict['test_path']):
print("---- pack ----")
for instance in pack.get(Sentence):
sentence = instance.text
predicts = []
for entry in pack.get(Sentence, instance):
predicts.append(entry.speaker)
print('---- example -----')
print("sentence: ", sentence)
print("predict sentiment: ", predicts)
#print(evaluator.get_result())
Loading