This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Sentiment #24
Merged
Merged
Sentiment #24
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8a54241
Merge pull request #1 from PaddlePaddle/develop
schinger dddc150
add sentiment
79fb9ac
add sentiment.
5f61b0c
add *.sh and *.py, add readme.md other sections
7d77257
mini. error
5272642
Merge pull request #2 from PaddlePaddle/develop
schinger ce71f96
del one paragraph
01e08be
complete
5ec489c
revised but unfinished
9d6d8fe
run all code, revised completed.
f561dcc
revised according to reviews
44ed444
according to comments
c164305
finished
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
data/aclImdb | ||
data/imdb | ||
data/pre-imdb | ||
data/mosesdecoder-master | ||
logs/ | ||
model_output | ||
dataprovider_copy_1.py | ||
model.list | ||
test.log | ||
train.log | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test.log和train.log没放在logs下面?那logs是放什么呢 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
*.pyc | ||
.DS_Store |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/bin/bash | ||
# Copyright (c) 2016 PaddlePaddle 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. | ||
|
||
set -e | ||
set -x | ||
|
||
DIR="$( cd "$(dirname "$0")" ; pwd -P )" | ||
cd $DIR | ||
|
||
#download the dataset | ||
echo "Downloading aclImdb..." | ||
#http://ai.stanford.edu/%7Eamaas/data/sentiment/ | ||
wget http://ai.stanford.edu/%7Eamaas/data/sentiment/aclImdb_v1.tar.gz | ||
|
||
echo "Downloading mosesdecoder..." | ||
#https://github.com/moses-smt/mosesdecoder | ||
wget https://github.com/moses-smt/mosesdecoder/archive/master.zip | ||
|
||
#extract package | ||
echo "Unzipping..." | ||
tar -zxvf aclImdb_v1.tar.gz | ||
unzip master.zip | ||
|
||
#move train and test set to imdb_data directory | ||
#in order to process when traing | ||
mkdir -p imdb/train | ||
mkdir -p imdb/test | ||
|
||
cp -r aclImdb/train/pos/ imdb/train/pos | ||
cp -r aclImdb/train/neg/ imdb/train/neg | ||
|
||
cp -r aclImdb/test/pos/ imdb/test/pos | ||
cp -r aclImdb/test/neg/ imdb/test/neg | ||
|
||
#remove compressed package | ||
rm aclImdb_v1.tar.gz | ||
rm master.zip | ||
|
||
echo "Done." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright (c) 2016 PaddlePaddle 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. | ||
from paddle.trainer.PyDataProvider2 import * | ||
|
||
|
||
def hook(settings, dictionary, **kwargs): | ||
settings.word_dict = dictionary | ||
settings.input_types = [ | ||
integer_value_sequence(len(settings.word_dict)), integer_value(2) | ||
] | ||
settings.logger.info('dict len : %d' % (len(settings.word_dict))) | ||
|
||
|
||
@provider(init_hook=hook) | ||
def process(settings, file_name): | ||
with open(file_name, 'r') as fdata: | ||
for line_count, line in enumerate(fdata): | ||
label, comment = line.strip().split('\t\t') | ||
label = int(label) | ||
words = comment.split() | ||
word_slot = [ | ||
settings.word_dict[w] for w in words if w in settings.word_dict | ||
] | ||
yield word_slot, label |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
# Copyright (c) 2016 PaddlePaddle 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. | ||
|
||
import os, sys | ||
import numpy as np | ||
from optparse import OptionParser | ||
from py_paddle import swig_paddle, DataProviderConverter | ||
from paddle.trainer.PyDataProvider2 import integer_value_sequence | ||
from paddle.trainer.config_parser import parse_config | ||
""" | ||
Usage: run following command to show help message. | ||
python predict.py -h | ||
""" | ||
|
||
|
||
class SentimentPrediction(): | ||
def __init__(self, train_conf, dict_file, model_dir=None, label_file=None): | ||
""" | ||
train_conf: trainer configure. | ||
dict_file: word dictionary file name. | ||
model_dir: directory of model. | ||
""" | ||
self.train_conf = train_conf | ||
self.dict_file = dict_file | ||
self.word_dict = {} | ||
self.dict_dim = self.load_dict() | ||
self.model_dir = model_dir | ||
if model_dir is None: | ||
self.model_dir = os.path.dirname(train_conf) | ||
|
||
self.label = None | ||
if label_file is not None: | ||
self.load_label(label_file) | ||
|
||
conf = parse_config(train_conf, "is_predict=1") | ||
self.network = swig_paddle.GradientMachine.createFromConfigProto( | ||
conf.model_config) | ||
self.network.loadParameters(self.model_dir) | ||
input_types = [integer_value_sequence(self.dict_dim)] | ||
self.converter = DataProviderConverter(input_types) | ||
|
||
def load_dict(self): | ||
""" | ||
Load dictionary from self.dict_file. | ||
""" | ||
for line_count, line in enumerate(open(self.dict_file, 'r')): | ||
self.word_dict[line.strip().split('\t')[0]] = line_count | ||
return len(self.word_dict) | ||
|
||
def load_label(self, label_file): | ||
""" | ||
Load label. | ||
""" | ||
self.label = {} | ||
for v in open(label_file, 'r'): | ||
self.label[int(v.split('\t')[1])] = v.split('\t')[0] | ||
|
||
def get_index(self, data): | ||
""" | ||
transform word into integer index according to the dictionary. | ||
""" | ||
words = data.strip().split() | ||
word_slot = [self.word_dict[w] for w in words if w in self.word_dict] | ||
return word_slot | ||
|
||
def batch_predict(self, data_batch): | ||
input = self.converter(data_batch) | ||
output = self.network.forwardTest(input) | ||
prob = output[0]["value"] | ||
labs = np.argsort(-prob) | ||
for idx, lab in enumerate(labs): | ||
if self.label is None: | ||
print("predicting label is %d" % (lab[0])) | ||
else: | ||
print("predicting label is %s" % (self.label[lab[0]])) | ||
|
||
|
||
def option_parser(): | ||
usage = "python predict.py -n config -w model_dir -d dictionary -i input_file " | ||
parser = OptionParser(usage="usage: %s [options]" % usage) | ||
parser.add_option( | ||
"-n", | ||
"--tconf", | ||
action="store", | ||
dest="train_conf", | ||
help="network config") | ||
parser.add_option( | ||
"-d", | ||
"--dict", | ||
action="store", | ||
dest="dict_file", | ||
help="dictionary file") | ||
parser.add_option( | ||
"-b", | ||
"--label", | ||
action="store", | ||
dest="label", | ||
default=None, | ||
help="dictionary file") | ||
parser.add_option( | ||
"-c", | ||
"--batch_size", | ||
type="int", | ||
action="store", | ||
dest="batch_size", | ||
default=1, | ||
help="the batch size for prediction") | ||
parser.add_option( | ||
"-w", | ||
"--model", | ||
action="store", | ||
dest="model_path", | ||
default=None, | ||
help="model path") | ||
return parser.parse_args() | ||
|
||
|
||
def main(): | ||
options, args = option_parser() | ||
train_conf = options.train_conf | ||
batch_size = options.batch_size | ||
dict_file = options.dict_file | ||
model_path = options.model_path | ||
label = options.label | ||
swig_paddle.initPaddle("--use_gpu=0") | ||
predict = SentimentPrediction(train_conf, dict_file, model_path, label) | ||
|
||
batch = [] | ||
for line in sys.stdin: | ||
batch.append([predict.get_index(line)]) | ||
if len(batch) == batch_size: | ||
predict.batch_predict(batch) | ||
batch = [] | ||
if len(batch) > 0: | ||
predict.batch_predict(batch) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/bin/bash | ||
# Copyright (c) 2016 PaddlePaddle 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. | ||
set -e | ||
|
||
#Note the default model is pass-00002, you shold make sure the model path | ||
#exists or change the mode path. | ||
model=model_output/pass-00002/ | ||
config=trainer_config.py | ||
label=data/pre-imdb/labels.list | ||
cat ./data/aclImdb/test/pos/10007_10.txt | python predict.py \ | ||
--tconf=$config\ | ||
--model=$model \ | ||
--label=$label \ | ||
--dict=./data/pre-imdb/dict.txt \ | ||
--batch_size=1 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我看train.sh和test.sh打印的日志都没放在logs下面,所以这里把
logs/
改成*.log
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done