Skip to content

Commit 9eedd53

Browse files
authored
Merge pull request #1089 from cxysteven/traffic_demo_branch
Traffic demo branch
2 parents 5c0178b + f45b45e commit 9eedd53

File tree

7 files changed

+293
-0
lines changed

7 files changed

+293
-0
lines changed

demo/traffic_prediction/README

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
run by:
2+
cd ./data
3+
sh get_data.sh
4+
cd ..
5+
sh train.sh
6+
sh predict.sh
7+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
# Copyright (c) 2016 PaddlePaddle Authors, Inc. All Rights Reserved
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -e
17+
set -x
18+
19+
DIR="$( cd "$(dirname "$0")" ; pwd -P )"
20+
cd $DIR
21+
22+
#download the dataset
23+
echo "Downloading traffic data..."
24+
wget http://paddlepaddle.cdn.bcebos.com/demo/traffic/traffic_data.tar.gz
25+
26+
#extract package
27+
echo "Unzipping..."
28+
tar -zxvf traffic_data.tar.gz
29+
30+
echo "data/speeds.csv" > train.list
31+
echo "data/speeds.csv" > test.list
32+
echo "data/speeds.csv" > pred.list
33+
34+
echo "Done."
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Copyright (c) 2016 PaddlePaddle Authors, Inc. All Rights Reserved
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from paddle.trainer.PyDataProvider2 import *
16+
import sys
17+
import numpy as np
18+
TERM_NUM = 24
19+
FORECASTING_NUM = 24
20+
LABEL_VALUE_NUM = 4
21+
22+
23+
def initHook(settings, file_list, **kwargs):
24+
"""
25+
Init hook is invoked before process data. It will set obj.slots and store data meta.
26+
27+
:param settings: global object. It will passed to process routine.
28+
:type obj: object
29+
:param file_list: the meta file object, which passed from trainer_config.py,but unused in this function.
30+
:param kwargs: unused other arguments.
31+
"""
32+
del kwargs #unused
33+
34+
settings.pool_size = sys.maxint
35+
#Use a time seires of the past as feature.
36+
#Dense_vector's expression form is [float,float,...,float]
37+
settings.input_types = [dense_vector(TERM_NUM)]
38+
#There are next FORECASTING_NUM fragments you need predict.
39+
#Every predicted condition at time point has four states.
40+
for i in range(FORECASTING_NUM):
41+
settings.input_types.append(integer_value(LABEL_VALUE_NUM))
42+
43+
44+
@provider(
45+
init_hook=initHook, cache=CacheType.CACHE_PASS_IN_MEM, should_shuffle=True)
46+
def process(settings, file_name):
47+
with open(file_name) as f:
48+
#abandon fields name
49+
f.next()
50+
for row_num, line in enumerate(f):
51+
speeds = map(int, line.rstrip('\r\n').split(",")[1:])
52+
# Get the max index.
53+
end_time = len(speeds)
54+
# Scanning and generating samples
55+
for i in range(TERM_NUM, end_time - FORECASTING_NUM):
56+
# For dense slot
57+
pre_spd = map(float, speeds[i - TERM_NUM:i])
58+
59+
# Integer value need predicting, values start from 0, so every one minus 1.
60+
fol_spd = [j - 1 for j in speeds[i:i + FORECASTING_NUM]]
61+
62+
# Predicting label is missing, abandon the sample.
63+
if -1 in fol_spd:
64+
continue
65+
yield [pre_spd] + fol_spd
66+
67+
68+
def predict_initHook(settings, file_list, **kwargs):
69+
settings.pool_size = sys.maxint
70+
settings.input_types = [dense_vector(TERM_NUM)]
71+
72+
73+
@provider(init_hook=predict_initHook, should_shuffle=False)
74+
def process_predict(settings, file_name):
75+
with open(file_name) as f:
76+
#abandon fields name
77+
f.next()
78+
for row_num, line in enumerate(f):
79+
speeds = map(int, line.rstrip('\r\n').split(","))
80+
end_time = len(speeds)
81+
pre_spd = map(float, speeds[end_time - TERM_NUM:end_time])
82+
yield pre_spd

demo/traffic_prediction/gen_result.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright (c) 2016 PaddlePaddle Authors, Inc. All Rights Reserved
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
res = []
16+
with open('./rank-00000') as f:
17+
for line in f:
18+
pred = map(int, line.strip('\r\n;').split(";"))
19+
#raw prediction range from 0 to 3
20+
res.append([i + 1 for i in pred])
21+
22+
file_name = open('./data/pred.list').read().strip('\r\n')
23+
24+
FORECASTING_NUM = 24
25+
header = [
26+
'id',
27+
'201604200805',
28+
'201604200810',
29+
'201604200815',
30+
'201604200820',
31+
'201604200825',
32+
'201604200830',
33+
'201604200835',
34+
'201604200840',
35+
'201604200845',
36+
'201604200850',
37+
'201604200855',
38+
'201604200900',
39+
'201604200905',
40+
'201604200910',
41+
'201604200915',
42+
'201604200920',
43+
'201604200925',
44+
'201604200930',
45+
'201604200935',
46+
'201604200940',
47+
'201604200945',
48+
'201604200950',
49+
'201604200955',
50+
'201604201000',
51+
]
52+
###################
53+
## To CSV format ##
54+
###################
55+
with open(file_name) as f:
56+
f.next()
57+
print ','.join(header)
58+
for row_num, line in enumerate(f):
59+
fields = line.rstrip('\r\n').split(',')
60+
linkid = fields[0]
61+
print linkid + ',' + ','.join(map(str, res[row_num]))

demo/traffic_prediction/predict.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
# Copyright (c) 2016 PaddlePaddle Authors, Inc. All Rights Reserved
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
set -e
16+
17+
cfg=trainer_config.py
18+
# pass choice
19+
model="output/pass-00000"
20+
paddle train \
21+
--config=$cfg \
22+
--use_gpu=false \
23+
--job=test \
24+
--init_model_path=$model \
25+
--config_args=is_predict=1 \
26+
--predict_output_dir=.
27+
28+
python gen_result.py > result.txt
29+
30+
rm -rf rank-00000

demo/traffic_prediction/train.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
# Copyright (c) 2016 PaddlePaddle Authors, Inc. All Rights Reserved
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
set -e
16+
17+
cfg=trainer_config.py
18+
paddle train \
19+
--config=$cfg \
20+
--save_dir=./output \
21+
--trainer_count=4 \
22+
--log_period=1000 \
23+
--dot_period=10 \
24+
--num_passes=10 \
25+
--use_gpu=false \
26+
--show_parameter_stats_period=3000 \
27+
2>&1 | tee 'train.log'
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright (c) 2016 PaddlePaddle Authors, Inc. All Rights Reserved
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from paddle.trainer_config_helpers import *
15+
16+
################################### DATA Configuration #############################################
17+
is_predict = get_config_arg('is_predict', bool, False)
18+
trn = './data/train.list' if not is_predict else None
19+
tst = './data/test.list' if not is_predict else './data/pred.list'
20+
process = 'process' if not is_predict else 'process_predict'
21+
define_py_data_sources2(
22+
train_list=trn, test_list=tst, module="dataprovider", obj=process)
23+
################################### Parameter Configuaration #######################################
24+
TERM_NUM = 24
25+
FORECASTING_NUM = 24
26+
emb_size = 16
27+
batch_size = 128 if not is_predict else 1
28+
settings(
29+
batch_size=batch_size,
30+
learning_rate=1e-3,
31+
learning_method=RMSPropOptimizer())
32+
################################### Algorithm Configuration ########################################
33+
34+
output_label = []
35+
36+
link_encode = data_layer(name='link_encode', size=TERM_NUM)
37+
for i in xrange(FORECASTING_NUM):
38+
# Each task share same weight.
39+
link_param = ParamAttr(
40+
name='_link_vec.w', initial_max=1.0, initial_min=-1.0)
41+
link_vec = fc_layer(input=link_encode, size=emb_size, param_attr=link_param)
42+
score = fc_layer(input=link_vec, size=4, act=SoftmaxActivation())
43+
if is_predict:
44+
maxid = maxid_layer(score)
45+
output_label.append(maxid)
46+
else:
47+
# Multi-task training.
48+
label = data_layer(name='label_%dmin' % ((i + 1) * 5), size=4)
49+
cls = classification_cost(
50+
input=score, name="cost_%dmin" % ((i + 1) * 5), label=label)
51+
output_label.append(cls)
52+
outputs(output_label)

0 commit comments

Comments
 (0)