commit
4b904a514e
@ -0,0 +1,86 @@
|
||||
set -e
|
||||
|
||||
function clock_to_seconds() {
|
||||
hours=`echo $1 | awk -F ':' '{print $1}'`
|
||||
mins=`echo $1 | awk -F ':' '{print $2}'`
|
||||
secs=`echo $1 | awk -F ':' '{print $3}'`
|
||||
echo `bc -l <<< "$secs + $mins * 60 + $hours * 3600"`
|
||||
}
|
||||
|
||||
function infer() {
|
||||
unset OMP_NUM_THREADS MKL_NUM_THREADS OMP_DYNAMIC KMP_AFFINITY
|
||||
topology=$1
|
||||
layer_num=$2
|
||||
bs=$3
|
||||
use_mkldnn=$4
|
||||
if [ $4 == "True" ]; then
|
||||
thread=1
|
||||
log="logs/infer-${topology}-${layer_num}-mkldnn-${bs}.log"
|
||||
elif [ $4 == "False" ]; then
|
||||
thread=`nproc`
|
||||
if [ $thread -gt $bs ]; then
|
||||
thread=$bs
|
||||
fi
|
||||
log="logs/infer-${topology}-${layer_num}-${thread}mklml-${bs}.log"
|
||||
else
|
||||
echo "Wrong input $4, use True or False."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
models_in="models/${topology}-${layer_num}/pass-00000/"
|
||||
if [ ! -d $models_in ]; then
|
||||
echo "Training model ${topology}_${layer_num}"
|
||||
paddle train --job=train \
|
||||
--config="${topology}.py" \
|
||||
--use_mkldnn=True \
|
||||
--use_gpu=False \
|
||||
--trainer_count=1 \
|
||||
--num_passes=1 \
|
||||
--save_dir="models/${topology}-${layer_num}" \
|
||||
--config_args="batch_size=128,layer_num=${layer_num}" \
|
||||
> /dev/null 2>&1
|
||||
echo "Done"
|
||||
fi
|
||||
log_period=$((256 / bs))
|
||||
paddle train --job=test \
|
||||
--config="${topology}.py" \
|
||||
--use_mkldnn=$use_mkldnn \
|
||||
--use_gpu=False \
|
||||
--trainer_count=$thread \
|
||||
--log_period=$log_period \
|
||||
--config_args="batch_size=${bs},layer_num=${layer_num},is_infer=True" \
|
||||
--init_model_path=$models_in \
|
||||
2>&1 | tee ${log}
|
||||
|
||||
# calculate the last 5 logs period time of 1280 samples,
|
||||
# the time before are burning time.
|
||||
start=`tail ${log} -n 7 | head -n 1 | awk -F ' ' '{print $2}' | xargs`
|
||||
end=`tail ${log} -n 2 | head -n 1 | awk -F ' ' '{print $2}' | xargs`
|
||||
start_sec=`clock_to_seconds $start`
|
||||
end_sec=`clock_to_seconds $end`
|
||||
fps=`bc <<< "scale = 2; 1280 / ($end_sec - $start_sec)"`
|
||||
echo "Last 1280 samples start: ${start}(${start_sec} sec), end: ${end}(${end_sec} sec;" >> ${log}
|
||||
echo "FPS: $fps images/sec" >> ${log}
|
||||
}
|
||||
|
||||
if [ ! -f "train.list" ]; then
|
||||
echo " " > train.list
|
||||
fi
|
||||
if [ ! -f "test.list" ]; then
|
||||
echo " " > test.list
|
||||
fi
|
||||
if [ ! -d "logs" ]; then
|
||||
mkdir logs
|
||||
fi
|
||||
if [ ! -d "models" ]; then
|
||||
mkdir -p models
|
||||
fi
|
||||
|
||||
# inference benchmark
|
||||
for use_mkldnn in True False; do
|
||||
for batchsize in 1 2 4 8 16; do
|
||||
infer googlenet v1 $batchsize $use_mkldnn
|
||||
infer resnet 50 $batchsize $use_mkldnn
|
||||
infer vgg 19 $batchsize $use_mkldnn
|
||||
done
|
||||
done
|
@ -0,0 +1,45 @@
|
||||
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
IF(MOBILE_INFERENCE)
|
||||
return()
|
||||
ENDIF()
|
||||
|
||||
include (ExternalProject)
|
||||
|
||||
# NOTE: c-ares is needed when linking with grpc.
|
||||
|
||||
SET(CARES_SOURCES_DIR ${THIRD_PARTY_PATH}/cares)
|
||||
SET(CARES_INSTALL_DIR ${THIRD_PARTY_PATH}/install/cares)
|
||||
SET(CARES_INCLUDE_DIR "${CARES_INSTALL_DIR}/include/" CACHE PATH "cares include directory." FORCE)
|
||||
|
||||
ExternalProject_Add(
|
||||
extern_cares
|
||||
GIT_REPOSITORY "https://github.com/c-ares/c-ares.git"
|
||||
GIT_TAG "cares-1_13_0"
|
||||
PREFIX ${CARES_SOURCES_DIR}
|
||||
UPDATE_COMMAND ""
|
||||
CONFIGURE_COMMAND ./buildconf && ./configure --disable-shared --prefix=${CARES_INSTALL_DIR}
|
||||
BUILD_IN_SOURCE 1
|
||||
BUILD_COMMAND make
|
||||
INSTALL_COMMAND make install
|
||||
)
|
||||
|
||||
ADD_LIBRARY(cares STATIC IMPORTED GLOBAL)
|
||||
SET_PROPERTY(TARGET cares PROPERTY IMPORTED_LOCATION
|
||||
"${CARES_INSTALL_DIR}/lib/libcares.a")
|
||||
|
||||
include_directories(${CARES_INCLUDE_DIR})
|
||||
ADD_DEPENDENCIES(cares extern_cares)
|
@ -0,0 +1,66 @@
|
||||
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
IF(MOBILE_INFERENCE)
|
||||
return()
|
||||
ENDIF()
|
||||
|
||||
include (ExternalProject)
|
||||
|
||||
SET(GRPC_SOURCES_DIR ${THIRD_PARTY_PATH}/grpc)
|
||||
SET(GRPC_INSTALL_DIR ${THIRD_PARTY_PATH}/install/grpc)
|
||||
SET(GRPC_INCLUDE_DIR "${GRPC_INSTALL_DIR}/include/" CACHE PATH "grpc include directory." FORCE)
|
||||
SET(GRPC_CPP_PLUGIN "${GRPC_INSTALL_DIR}/bin/grpc_cpp_plugin" CACHE FILEPATH "GRPC_CPP_PLUGIN" FORCE)
|
||||
IF(APPLE)
|
||||
SET(BUILD_CMD make -n HAS_SYSTEM_PROTOBUF=false -s -j8 static grpc_cpp_plugin | sed "s/-Werror//g" | sh)
|
||||
ELSE()
|
||||
SET(BUILD_CMD make HAS_SYSTEM_PROTOBUF=false -s -j8 static grpc_cpp_plugin)
|
||||
ENDIF()
|
||||
|
||||
ExternalProject_Add(
|
||||
extern_grpc
|
||||
DEPENDS protobuf zlib
|
||||
GIT_REPOSITORY "https://github.com/grpc/grpc.git"
|
||||
GIT_TAG "v1.7.x"
|
||||
PREFIX ${GRPC_SOURCES_DIR}
|
||||
UPDATE_COMMAND ""
|
||||
CONFIGURE_COMMAND ""
|
||||
BUILD_IN_SOURCE 1
|
||||
# NOTE(yuyang18):
|
||||
# Disable -Werror, otherwise the compile will fail in MacOS.
|
||||
# It seems that we cannot configure that by make command.
|
||||
# Just dry run make command and remove `-Werror`, then use a shell to run make commands
|
||||
BUILD_COMMAND ${BUILD_CMD}
|
||||
INSTALL_COMMAND make prefix=${GRPC_INSTALL_DIR} install
|
||||
)
|
||||
|
||||
# FIXME(typhoonzero): hack to get static lib path, try a better way like merge them.
|
||||
ADD_LIBRARY(grpc++_unsecure STATIC IMPORTED GLOBAL)
|
||||
SET_PROPERTY(TARGET grpc++_unsecure PROPERTY IMPORTED_LOCATION
|
||||
"${GRPC_INSTALL_DIR}/lib/libgrpc++_unsecure.a")
|
||||
|
||||
ADD_LIBRARY(grpc++ STATIC IMPORTED GLOBAL)
|
||||
SET_PROPERTY(TARGET grpc++ PROPERTY IMPORTED_LOCATION
|
||||
"${GRPC_INSTALL_DIR}/lib/libgrpc++.a")
|
||||
ADD_LIBRARY(gpr STATIC IMPORTED GLOBAL)
|
||||
SET_PROPERTY(TARGET gpr PROPERTY IMPORTED_LOCATION
|
||||
"${GRPC_INSTALL_DIR}/lib/libgpr.a")
|
||||
|
||||
ADD_LIBRARY(grpc_unsecure STATIC IMPORTED GLOBAL)
|
||||
SET_PROPERTY(TARGET grpc_unsecure PROPERTY IMPORTED_LOCATION
|
||||
"${GRPC_INSTALL_DIR}/lib/libgrpc_unsecure.a")
|
||||
|
||||
include_directories(${GRPC_INCLUDE_DIR})
|
||||
ADD_DEPENDENCIES(grpc++_unsecure extern_grpc)
|
File diff suppressed because it is too large
Load Diff
@ -1,101 +0,0 @@
|
||||
Simple Linear Regression
|
||||
========================
|
||||
|
||||
PaddlePaddle is a deep learning platform open-sourced by Baidu. With PaddlePaddle, you can easily train a classic neural network within a couple lines of configuration, or you can build sophisticated models that provide state-of-the-art performance on difficult learning tasks like sentiment analysis, machine translation, image caption and so on.
|
||||
|
||||
Problem Background
|
||||
------------------
|
||||
|
||||
Now, to give you a hint of what using PaddlePaddle looks like, let's start with a fundamental learning problem - `simple linear regression <https://en.wikipedia.org/wiki/Simple_linear_regression>`_: you have observed a set of two-dimensional data points of ``X`` and ``Y``, where ``X`` is an explanatory variable and ``Y`` is corresponding dependent variable, and you want to recover the underlying correlation between ``X`` and ``Y``. Linear regression can be used in many practical scenarios. For example, ``X`` can be a variable about house size, and ``Y`` a variable about house price. You can build a model that captures relationship between them by observing real estate markets.
|
||||
|
||||
Prepare the Data
|
||||
-----------------
|
||||
|
||||
Suppose the true relationship can be characterized as ``Y = 2X + 0.3``, let's see how to recover this pattern only from observed data. Here is a piece of python code that feeds synthetic data to PaddlePaddle. The code is pretty self-explanatory, the only extra thing you need to add for PaddlePaddle is a definition of input data types.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
# dataprovider.py
|
||||
from paddle.trainer.PyDataProvider2 import *
|
||||
import random
|
||||
|
||||
# define data types of input: 2 real numbers
|
||||
@provider(input_types=[dense_vector(1), dense_vector(1)],use_seq=False)
|
||||
def process(settings, input_file):
|
||||
for i in xrange(2000):
|
||||
x = random.random()
|
||||
yield [x], [2*x+0.3]
|
||||
|
||||
Train a NeuralNetwork
|
||||
----------------------
|
||||
|
||||
To recover this relationship between ``X`` and ``Y``, we use a neural network with one layer of linear activation units and a square error cost layer. Don't worry if you are not familiar with these terminologies, it's just saying that we are starting from a random line ``Y' = wX + b`` , then we gradually adapt ``w`` and ``b`` to minimize the difference between ``Y'`` and ``Y``. Here is what it looks like in PaddlePaddle:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
# trainer_config.py
|
||||
from paddle.trainer_config_helpers import *
|
||||
|
||||
# 1. read data. Suppose you saved above python code as dataprovider.py
|
||||
data_file = 'empty.list'
|
||||
with open(data_file, 'w') as f: f.writelines(' ')
|
||||
define_py_data_sources2(train_list=data_file, test_list=None,
|
||||
module='dataprovider', obj='process',args={})
|
||||
|
||||
# 2. learning algorithm
|
||||
settings(batch_size=12, learning_rate=1e-3, learning_method=MomentumOptimizer())
|
||||
|
||||
# 3. Network configuration
|
||||
x = data_layer(name='x', size=1)
|
||||
y = data_layer(name='y', size=1)
|
||||
y_predict = fc_layer(input=x, param_attr=ParamAttr(name='w'), size=1, act=LinearActivation(), bias_attr=ParamAttr(name='b'))
|
||||
cost = square_error_cost(input=y_predict, label=y)
|
||||
outputs(cost)
|
||||
|
||||
Some of the most fundamental usages of PaddlePaddle are demonstrated:
|
||||
|
||||
- The first part shows how to feed data into PaddlePaddle. In general cases, PaddlePaddle reads raw data from a list of files, and then do some user-defined process to get real input. In this case, we only need to create a placeholder file since we are generating synthetic data on the fly.
|
||||
|
||||
- The second part describes learning algorithm. It defines in what ways adjustments are made to model parameters. PaddlePaddle provides a rich set of optimizers, but a simple momentum based optimizer will suffice here, and it processes 12 data points each time.
|
||||
|
||||
- Finally, the network configuration. It usually is as simple as "stacking" layers. Three kinds of layers are used in this configuration:
|
||||
- **Data Layer**: a network always starts with one or more data layers. They provide input data to the rest of the network. In this problem, two data layers are used respectively for ``X`` and ``Y``.
|
||||
- **FC Layer**: FC layer is short for Fully Connected Layer, which connects all the input units to current layer and does the actual computation specified as activation function. Computation layers like this are the fundamental building blocks of a deeper model.
|
||||
- **Cost Layer**: in training phase, cost layers are usually the last layers of the network. They measure the performance of current model, and provide guidence to adjust parameters.
|
||||
|
||||
Now that everything is ready, you can train the network with a simple command line call:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
paddle train --config=trainer_config.py --save_dir=./output --num_passes=30
|
||||
|
||||
|
||||
This means that PaddlePaddle will train this network on the synthectic dataset for 30 passes, and save all the models under path ``./output``. You will see from the messages printed out during training phase that the model cost is decreasing as time goes by, which indicates we are getting a closer guess.
|
||||
|
||||
|
||||
Evaluate the Model
|
||||
-------------------
|
||||
|
||||
Usually, a different dataset that left out during training phase should be used to evalute the models. However, we are lucky enough to know the real answer: ``w=2, b=0.3``, thus a better option is to check out model parameters directly.
|
||||
|
||||
In PaddlePaddle, training is just to get a collection of model parameters, which are ``w`` and ``b`` in this case. Each parameter is saved in an individual file in the popular ``numpy`` array format. Here is the code that reads parameters from last pass.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
import numpy as np
|
||||
import os
|
||||
|
||||
def load(file_name):
|
||||
with open(file_name, 'rb') as f:
|
||||
f.read(16) # skip header for float type.
|
||||
return np.fromfile(f, dtype=np.float32)
|
||||
|
||||
print 'w=%.6f, b=%.6f' % (load('output/pass-00029/w'), load('output/pass-00029/b'))
|
||||
# w=1.999743, b=0.300137
|
||||
|
||||
.. image:: parameters.png
|
||||
:align: center
|
||||
|
||||
Although starts from a random guess, you can see that value of ``w`` changes quickly towards 2 and ``b`` changes quickly towards 0.3. In the end, the predicted line is almost identical with real answer.
|
||||
|
||||
There, you have recovered the underlying pattern between ``X`` and ``Y`` only from observed data.
|
Before Width: | Height: | Size: 43 KiB |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue