Skip to content

Commit 3d28964

Browse files
committed
follow comments
1 parent aaecfcc commit 3d28964

File tree

3 files changed

+57
-63
lines changed

3 files changed

+57
-63
lines changed

demo/sentiment/predict.py

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -66,42 +66,27 @@ def load_label(self, label_file):
6666
for v in open(label_file, 'r'):
6767
self.label[int(v.split('\t')[1])] = v.split('\t')[0]
6868

69-
def get_data(self, data):
69+
def get_index(self, data):
7070
"""
71-
Get input data of paddle format.
71+
transform word into integer index according to the dictionary.
7272
"""
73-
for line in data:
74-
words = line.strip().split()
75-
word_slot = [
76-
self.word_dict[w] for w in words if w in self.word_dict
77-
]
78-
if not word_slot:
79-
print "all words are not in dictionary: %s", line
80-
continue
81-
yield [word_slot]
82-
83-
def predict(self, batch_size):
84-
85-
def batch_predict(batch_data):
86-
input = self.converter(self.get_data(batch_data))
87-
output = self.network.forwardTest(input)
88-
prob = output[0]["value"]
89-
labs = np.argsort(-prob)
90-
for idx, lab in enumerate(labs):
91-
if self.label is None:
92-
print("predicting label is %d" % (lab[0]))
93-
else:
94-
print("predicting label is %s" %
95-
(self.label[lab[0]]))
96-
97-
batch = []
98-
for line in sys.stdin:
99-
batch.append(line)
100-
if len(batch) == batch_size:
101-
batch_predict(batch)
102-
batch=[]
103-
if len(batch) > 0:
104-
batch_predict(batch)
73+
words = data.strip().split()
74+
word_slot = [
75+
self.word_dict[w] for w in words if w in self.word_dict
76+
]
77+
return word_slot
78+
79+
def batch_predict(self, data_batch):
80+
input = self.converter(data_batch)
81+
output = self.network.forwardTest(input)
82+
prob = output[0]["value"]
83+
labs = np.argsort(-prob)
84+
for idx, lab in enumerate(labs):
85+
if self.label is None:
86+
print("predicting label is %d" % (lab[0]))
87+
else:
88+
print("predicting label is %s" %
89+
(self.label[lab[0]]))
10590

10691
def option_parser():
10792
usage = "python predict.py -n config -w model_dir -d dictionary -i input_file "
@@ -152,8 +137,15 @@ def main():
152137
label = options.label
153138
swig_paddle.initPaddle("--use_gpu=0")
154139
predict = SentimentPrediction(train_conf, dict_file, model_path, label)
155-
predict.predict(batch_size)
156140

141+
batch = []
142+
for line in sys.stdin:
143+
batch.append([predict.get_index(line)])
144+
if len(batch) == batch_size:
145+
predict.batch_predict(batch)
146+
batch=[]
147+
if len(batch) > 0:
148+
predict.batch_predict(batch)
157149

158150
if __name__ == '__main__':
159151
main()

doc/tutorials/sentiment_analysis/sentiment_analysis.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -293,20 +293,21 @@ predict.sh:
293293
model=model_output/pass-00002/
294294
config=trainer_config.py
295295
label=data/pre-imdb/labels.list
296-
python predict.py \
297-
-n $config\
298-
-w $model \
299-
-b $label \
300-
-d data/pre-imdb/dict.txt \
301-
-i data/aclImdb/test/pos/10007_10.txt
302-
```
303-
304-
* `predict.py`: predicting interface.
305-
* -n $config : set network configure.
306-
* -w $model: set model path.
307-
* -b $label: set dictionary about corresponding relation between integer label and string label.
308-
* -d data/pre-imdb/dict.txt: set dictionary.
309-
* -i data/aclImdb/test/pos/10014_7.txt: set one example file to predict.
296+
cat ./data/aclImdb/test/pos/10007_10.txt | python predict.py \
297+
--tconf=$config\
298+
--model=$model \
299+
--label=$label \
300+
--dict=./data/pre-imdb/dict.txt \
301+
--batch_size=1
302+
```
303+
304+
* `cat ./data/aclImdb/test/pos/10007_10.txt` : the input sample.
305+
* `predict.py` : predicting interface.
306+
* `--tconf=$config` : set network configure.
307+
* ` --model=$model` : set model path.
308+
* `--label=$label` : set dictionary about corresponding relation between integer label and string label.
309+
* `--dict=data/pre-imdb/dict.txt` : set dictionary.
310+
* `--batch_size=1` : set batch size.
310311

311312
Note you should make sure the default model path `model_output/pass-00002`
312313
exists or change the model path.

doc_cn/demo/sentiment_analysis/sentiment_analysis.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -291,20 +291,21 @@ predict.sh:
291291
model=model_output/pass-00002/
292292
config=trainer_config.py
293293
label=data/pre-imdb/labels.list
294-
python predict.py \
295-
-n $config\
296-
-w $model \
297-
-b $label \
298-
-d data/pre-imdb/dict.txt \
299-
-i data/aclImdb/test/pos/10007_10.txt
300-
```
301-
302-
* `predict.py`: 预测接口脚本。
303-
* -n $config : 设置网络配置。
304-
* -w $model: 设置模型路径。
305-
* -b $label: 设置标签类别字典,这个字典是整数标签和字符串标签的一个对应。
306-
* -d data/pre-imdb/dict.txt: 设置字典文件。
307-
* -i data/aclImdb/test/pos/10014_7.txt: 设置一个要预测的示例文件。
294+
cat ./data/aclImdb/test/pos/10007_10.txt | python predict.py \
295+
--tconf=$config\
296+
--model=$model \
297+
--label=$label \
298+
--dict=./data/pre-imdb/dict.txt \
299+
--batch_size=1
300+
```
301+
302+
* `cat ./data/aclImdb/test/pos/10007_10.txt` : 输入预测样本。
303+
* `predict.py` : 预测接口脚本。
304+
* `--tconf=$config` : 设置网络配置。
305+
* `--model=$model` : 设置模型路径。
306+
* `--label=$label` : 设置标签类别字典,这个字典是整数标签和字符串标签的一个对应。
307+
* `--dict=data/pre-imdb/dict.txt` : 设置字典文件。
308+
* `--batch_size=1` : 设置batch size。
308309

309310
注意应该确保默认模型路径`model_output / pass-00002`存在或更改为其它模型路径。
310311

0 commit comments

Comments
 (0)