Add inline and bit manipulation in CpuId.h

avx_docs
liaogang 8 years ago
commit 880aa220be

3
.gitignore vendored

@ -9,3 +9,6 @@ build/
.pydevproject .pydevproject
Makefile Makefile
.test_env/ .test_env/
*~
bazel-*

@ -7,6 +7,7 @@
sha: v0.13.2 sha: v0.13.2
hooks: hooks:
- id: yapf - id: yapf
files: (.*\.(py|bzl)|BUILD|.*\.BUILD|WORKSPACE)$ # Bazel BUILD files follow Python syntax.
- repo: https://github.com/pre-commit/pre-commit-hooks - repo: https://github.com/pre-commit/pre-commit-hooks
sha: 7539d8bd1a00a3c1bfd34cdb606d3a6372e83469 sha: 7539d8bd1a00a3c1bfd34cdb606d3a6372e83469
hooks: hooks:

@ -8,10 +8,13 @@ os:
env: env:
- JOB=DOCS - JOB=DOCS
- JOB=BUILD_AND_TEST - JOB=BUILD_AND_TEST
- JOB=PRE_COMMIT
matrix: matrix:
exclude: exclude:
- os: osx - os: osx
env: JOB=DOCS # Only generate documentation in linux env: JOB=DOCS # Only generate documentation in linux.
- os: osx
env: JOB=PRE_COMMIT # Only check pre-commit hook in linux
addons: addons:
apt: apt:
@ -39,18 +42,23 @@ addons:
- lcov - lcov
- graphviz - graphviz
- swig - swig
- clang-format-3.8
before_install: before_install:
- | - |
if [ ${JOB} == "BUILD_AND_TEST" ]; then if [ ${JOB} == "BUILD_AND_TEST" ]; then
if ! git diff --name-only $TRAVIS_COMMIT_RANGE | grep -qvE '(\.md$)|(\.rst$)|(\.jpg$)|(\.png$)' local change_list=`git diff --name-only $TRAVIS_COMMIT_RANGE`
if [ $? -eq 0 ]; then # if git diff return no zero, then rerun unit test.
if ! echo ${change_list} | grep -qvE '(\.md$)|(\.rst$)|(\.jpg$)|(\.png$)'
then then
echo "Only markdown docs were updated, stopping build process." echo "Only markdown docs were updated, stopping build process."
exit exit
fi fi
fi fi
fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo paddle/scripts/travis/before_install.linux.sh; fi - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo paddle/scripts/travis/before_install.linux.sh; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then paddle/scripts/travis/before_install.osx.sh; fi - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then paddle/scripts/travis/before_install.osx.sh; fi
- pip install wheel protobuf sphinx breathe recommonmark virtualenv numpy sphinx_rtd_theme - if [[ "$JOB" == "PRE_COMMIT" ]]; then sudo ln -s /usr/bin/clang-format-3.8 /usr/bin/clang-format; fi
- pip install wheel protobuf sphinx recommonmark virtualenv numpy sphinx_rtd_theme pre-commit
script: script:
- paddle/scripts/travis/main.sh - paddle/scripts/travis/main.sh
notifications: notifications:

@ -69,7 +69,7 @@ include(coveralls)
find_package(Git REQUIRED) find_package(Git REQUIRED)
# version.cmake will get the current PADDLE_VERSION # version.cmake will get the current PADDLE_VERSION
include(version) include(version)
add_definitions(-DPADDLE_VERSION=\"${PADDLE_VERSION}\") add_definitions(-DPADDLE_VERSION=${PADDLE_VERSION})
if(NOT WITH_GPU) if(NOT WITH_GPU)
add_definitions(-DPADDLE_ONLY_CPU) add_definitions(-DPADDLE_ONLY_CPU)

@ -1,4 +1,4 @@
Copyright (c) 2016 Baidu, Inc. All Rights Reserved Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
Apache License Apache License
Version 2.0, January 2004 Version 2.0, January 2004
@ -188,7 +188,7 @@ Copyright (c) 2016 Baidu, Inc. All Rights Reserved
same "printed page" as the copyright notice for easier same "printed page" as the copyright notice for easier
identification within third-party archives. identification within third-party archives.
Copyright (c) 2016 Baidu, Inc. All Rights Reserve. Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.

@ -0,0 +1,15 @@
# External dependency to Google protobuf.
http_archive(
name="protobuf",
url="http://github.com/google/protobuf/archive/v3.1.0.tar.gz",
sha256="0a0ae63cbffc274efb573bdde9a253e3f32e458c41261df51c5dbc5ad541e8f7",
strip_prefix="protobuf-3.1.0", )
# External dependency to gtest 1.7.0. This method comes from
# https://www.bazel.io/versions/master/docs/tutorial/cpp.html.
new_http_archive(
name="gtest",
url="https://github.com/google/googletest/archive/release-1.7.0.zip",
sha256="b58cb7547a28b2c718d1e38aee18a3659c9e3ff52440297e965f5edffe34b6d0",
build_file="third_party/gtest.BUILD",
strip_prefix="googletest-release-1.7.0", )

@ -25,4 +25,3 @@ test 4 2 256 512
test 4 2 512 128 test 4 2 512 128
test 4 2 512 256 test 4 2 512 256
test 4 2 512 512 test 4 2 512 512

@ -30,7 +30,6 @@ if(WITH_DOC)
find_package(Sphinx REQUIRED) find_package(Sphinx REQUIRED)
find_package(Doxygen REQUIRED) find_package(Doxygen REQUIRED)
find_python_module(recommonmark REQUIRED) find_python_module(recommonmark REQUIRED)
find_python_module(breathe REQUIRED)
endif() endif()
if(WITH_SWIG_PY) if(WITH_SWIG_PY)

@ -1,4 +1,4 @@
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved # Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@ -15,4 +15,3 @@ set -e
wget https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz wget https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
tar zxf cifar-10-python.tar.gz tar zxf cifar-10-python.tar.gz
rm cifar-10-python.tar.gz rm cifar-10-python.tar.gz

@ -15,5 +15,3 @@ do
gunzip ${fname}.gz gunzip ${fname}.gz
fi fi
done done

@ -1,4 +1,4 @@
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved # Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@ -14,10 +14,9 @@
from paddle.trainer_config_helpers import * from paddle.trainer_config_helpers import *
mode = get_config_arg("mode", str, "generator") mode = get_config_arg("mode", str, "generator")
assert mode in set(["generator", assert mode in set([
"discriminator", "generator", "discriminator", "generator_training", "discriminator_training"
"generator_training", ])
"discriminator_training"])
is_generator_training = mode == "generator_training" is_generator_training = mode == "generator_training"
is_discriminator_training = mode == "discriminator_training" is_discriminator_training = mode == "discriminator_training"
@ -38,8 +37,8 @@ sample_dim = 2
settings( settings(
batch_size=128, batch_size=128,
learning_rate=1e-4, learning_rate=1e-4,
learning_method=AdamOptimizer(beta1=0.5) learning_method=AdamOptimizer(beta1=0.5))
)
def discriminator(sample): def discriminator(sample):
""" """
@ -50,71 +49,88 @@ def discriminator(sample):
of the sample is from real data. of the sample is from real data.
""" """
param_attr = ParamAttr(is_static=is_generator_training) param_attr = ParamAttr(is_static=is_generator_training)
bias_attr = ParamAttr(is_static=is_generator_training, bias_attr = ParamAttr(
initial_mean=1.0, is_static=is_generator_training, initial_mean=1.0, initial_std=0)
initial_std=0)
hidden = fc_layer(input=sample, name="dis_hidden", size=hidden_dim, hidden = fc_layer(
input=sample,
name="dis_hidden",
size=hidden_dim,
bias_attr=bias_attr, bias_attr=bias_attr,
param_attr=param_attr, param_attr=param_attr,
act=ReluActivation()) act=ReluActivation())
hidden2 = fc_layer(input=hidden, name="dis_hidden2", size=hidden_dim, hidden2 = fc_layer(
input=hidden,
name="dis_hidden2",
size=hidden_dim,
bias_attr=bias_attr, bias_attr=bias_attr,
param_attr=param_attr, param_attr=param_attr,
act=LinearActivation()) act=LinearActivation())
hidden_bn = batch_norm_layer(hidden2, hidden_bn = batch_norm_layer(
hidden2,
act=ReluActivation(), act=ReluActivation(),
name="dis_hidden_bn", name="dis_hidden_bn",
bias_attr=bias_attr, bias_attr=bias_attr,
param_attr=ParamAttr(is_static=is_generator_training, param_attr=ParamAttr(
initial_mean=1.0, is_static=is_generator_training, initial_mean=1.0,
initial_std=0.02), initial_std=0.02),
use_global_stats=False) use_global_stats=False)
return fc_layer(input=hidden_bn, name="dis_prob", size=2, return fc_layer(
input=hidden_bn,
name="dis_prob",
size=2,
bias_attr=bias_attr, bias_attr=bias_attr,
param_attr=param_attr, param_attr=param_attr,
act=SoftmaxActivation()) act=SoftmaxActivation())
def generator(noise): def generator(noise):
""" """
generator generates a sample given noise generator generates a sample given noise
""" """
param_attr = ParamAttr(is_static=is_discriminator_training) param_attr = ParamAttr(is_static=is_discriminator_training)
bias_attr = ParamAttr(is_static=is_discriminator_training, bias_attr = ParamAttr(
initial_mean=1.0, is_static=is_discriminator_training, initial_mean=1.0, initial_std=0)
initial_std=0)
hidden = fc_layer(input=noise, hidden = fc_layer(
input=noise,
name="gen_layer_hidden", name="gen_layer_hidden",
size=hidden_dim, size=hidden_dim,
bias_attr=bias_attr, bias_attr=bias_attr,
param_attr=param_attr, param_attr=param_attr,
act=ReluActivation()) act=ReluActivation())
hidden2 = fc_layer(input=hidden, name="gen_hidden2", size=hidden_dim, hidden2 = fc_layer(
input=hidden,
name="gen_hidden2",
size=hidden_dim,
bias_attr=bias_attr, bias_attr=bias_attr,
param_attr=param_attr, param_attr=param_attr,
act=LinearActivation()) act=LinearActivation())
hidden_bn = batch_norm_layer(hidden2, hidden_bn = batch_norm_layer(
hidden2,
act=ReluActivation(), act=ReluActivation(),
name="gen_layer_hidden_bn", name="gen_layer_hidden_bn",
bias_attr=bias_attr, bias_attr=bias_attr,
param_attr=ParamAttr(is_static=is_discriminator_training, param_attr=ParamAttr(
is_static=is_discriminator_training,
initial_mean=1.0, initial_mean=1.0,
initial_std=0.02), initial_std=0.02),
use_global_stats=False) use_global_stats=False)
return fc_layer(input=hidden_bn, return fc_layer(
input=hidden_bn,
name="gen_layer1", name="gen_layer1",
size=sample_dim, size=sample_dim,
bias_attr=bias_attr, bias_attr=bias_attr,
param_attr=param_attr, param_attr=param_attr,
act=LinearActivation()) act=LinearActivation())
if is_generator_training: if is_generator_training:
noise = data_layer(name="noise", size=noise_dim) noise = data_layer(name="noise", size=noise_dim)
sample = generator(noise) sample = generator(noise)
@ -126,7 +142,8 @@ if is_generator_training or is_discriminator_training:
label = data_layer(name="label", size=1) label = data_layer(name="label", size=1)
prob = discriminator(sample) prob = discriminator(sample)
cost = cross_entropy(input=prob, label=label) cost = cross_entropy(input=prob, label=label)
classification_error_evaluator(input=prob, label=label, name=mode+'_error') classification_error_evaluator(
input=prob, label=label, name=mode + '_error')
outputs(cost) outputs(cost)
if is_generator: if is_generator:

@ -1,4 +1,4 @@
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved # Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@ -15,10 +15,9 @@ from paddle.trainer_config_helpers import *
mode = get_config_arg("mode", str, "generator") mode = get_config_arg("mode", str, "generator")
dataSource = get_config_arg("data", str, "mnist") dataSource = get_config_arg("data", str, "mnist")
assert mode in set(["generator", assert mode in set([
"discriminator", "generator", "discriminator", "generator_training", "discriminator_training"
"generator_training", ])
"discriminator_training"])
is_generator_training = mode == "generator_training" is_generator_training = mode == "generator_training"
is_discriminator_training = mode == "discriminator_training" is_discriminator_training = mode == "discriminator_training"
@ -47,13 +46,22 @@ s8, s16 = int(sample_dim/8), int(sample_dim/16)
settings( settings(
batch_size=128, batch_size=128,
learning_rate=2e-4, learning_rate=2e-4,
learning_method=AdamOptimizer(beta1=0.5) learning_method=AdamOptimizer(beta1=0.5))
)
def conv_bn(input, channels, imgSize, num_filters, output_x, stride, name, def conv_bn(input,
param_attr, bias_attr, param_attr_bn, bn, trans=False, channels,
imgSize,
num_filters,
output_x,
stride,
name,
param_attr,
bias_attr,
param_attr_bn,
bn,
trans=False,
act=ReluActivation()): act=ReluActivation()):
""" """
conv_bn is a utility function that constructs a convolution/deconv layer conv_bn is a utility function that constructs a convolution/deconv layer
with an optional batch_norm layer with an optional batch_norm layer
@ -84,16 +92,27 @@ def conv_bn(input, channels, imgSize, num_filters, output_x, stride, name,
nameApx = "_convt" nameApx = "_convt"
if bn: if bn:
conv = img_conv_layer(input, filter_size=filter_size, conv = img_conv_layer(
input,
filter_size=filter_size,
num_filters=num_filters, num_filters=num_filters,
name=name + nameApx, num_channels=channels, name=name + nameApx,
act=LinearActivation(), groups=1, stride=stride, num_channels=channels,
padding=padding, bias_attr=bias_attr, act=LinearActivation(),
param_attr=param_attr, shared_biases=True, layer_attr=None, groups=1,
filter_size_y=None, stride_y=None, padding_y=None, stride=stride,
padding=padding,
bias_attr=bias_attr,
param_attr=param_attr,
shared_biases=True,
layer_attr=None,
filter_size_y=None,
stride_y=None,
padding_y=None,
trans=trans) trans=trans)
conv_bn = batch_norm_layer(conv, conv_bn = batch_norm_layer(
conv,
act=act, act=act,
name=name + nameApx + "_bn", name=name + nameApx + "_bn",
bias_attr=bias_attr, bias_attr=bias_attr,
@ -102,46 +121,57 @@ def conv_bn(input, channels, imgSize, num_filters, output_x, stride, name,
return conv_bn return conv_bn
else: else:
conv = img_conv_layer(input, filter_size=filter_size, conv = img_conv_layer(
input,
filter_size=filter_size,
num_filters=num_filters, num_filters=num_filters,
name=name + nameApx, num_channels=channels, name=name + nameApx,
act=act, groups=1, stride=stride, num_channels=channels,
padding=padding, bias_attr=bias_attr, act=act,
param_attr=param_attr, shared_biases=True, layer_attr=None, groups=1,
filter_size_y=None, stride_y=None, padding_y=None, stride=stride,
padding=padding,
bias_attr=bias_attr,
param_attr=param_attr,
shared_biases=True,
layer_attr=None,
filter_size_y=None,
stride_y=None,
padding_y=None,
trans=trans) trans=trans)
return conv return conv
def generator(noise): def generator(noise):
""" """
generator generates a sample given noise generator generates a sample given noise
""" """
param_attr = ParamAttr(is_static=is_discriminator_training, param_attr = ParamAttr(
initial_mean=0.0, is_static=is_discriminator_training, initial_mean=0.0, initial_std=0.02)
initial_std=0.02) bias_attr = ParamAttr(
bias_attr = ParamAttr(is_static=is_discriminator_training, is_static=is_discriminator_training, initial_mean=0.0, initial_std=0.0)
initial_mean=0.0,
initial_std=0.0) param_attr_bn = ParamAttr(
is_static=is_discriminator_training, initial_mean=1.0, initial_std=0.02)
param_attr_bn=ParamAttr(is_static=is_discriminator_training,
initial_mean=1.0, h1 = fc_layer(
initial_std=0.02) input=noise,
h1 = fc_layer(input=noise,
name="gen_layer_h1", name="gen_layer_h1",
size=s8 * s8 * gf_dim * 4, size=s8 * s8 * gf_dim * 4,
bias_attr=bias_attr, bias_attr=bias_attr,
param_attr=param_attr, param_attr=param_attr,
act=LinearActivation()) act=LinearActivation())
h1_bn = batch_norm_layer(h1, h1_bn = batch_norm_layer(
h1,
act=ReluActivation(), act=ReluActivation(),
name="gen_layer_h1_bn", name="gen_layer_h1_bn",
bias_attr=bias_attr, bias_attr=bias_attr,
param_attr=param_attr_bn, param_attr=param_attr_bn,
use_global_stats=False) use_global_stats=False)
h2_bn = conv_bn(h1_bn, h2_bn = conv_bn(
h1_bn,
channels=gf_dim * 4, channels=gf_dim * 4,
output_x=s8, output_x=s8,
num_filters=gf_dim * 2, num_filters=gf_dim * 2,
@ -154,7 +184,8 @@ def generator(noise):
bn=True, bn=True,
trans=True) trans=True)
h3_bn = conv_bn(h2_bn, h3_bn = conv_bn(
h2_bn,
channels=gf_dim * 2, channels=gf_dim * 2,
output_x=s4, output_x=s4,
num_filters=gf_dim, num_filters=gf_dim,
@ -167,8 +198,8 @@ def generator(noise):
bn=True, bn=True,
trans=True) trans=True)
return conv_bn(
return conv_bn(h3_bn, h3_bn,
channels=gf_dim, channels=gf_dim,
output_x=s2, output_x=s2,
num_filters=c_dim, num_filters=c_dim,
@ -191,18 +222,16 @@ def discriminator(sample):
of the sample is from generator and dimension 1 is the probabblity of the sample is from generator and dimension 1 is the probabblity
of the sample is from real data. of the sample is from real data.
""" """
param_attr = ParamAttr(is_static=is_generator_training, param_attr = ParamAttr(
initial_mean=0.0, is_static=is_generator_training, initial_mean=0.0, initial_std=0.02)
initial_std=0.02) bias_attr = ParamAttr(
bias_attr = ParamAttr(is_static=is_generator_training, is_static=is_generator_training, initial_mean=0.0, initial_std=0.0)
initial_mean=0.0,
initial_std=0.0) param_attr_bn = ParamAttr(
is_static=is_generator_training, initial_mean=1.0, initial_std=0.02)
param_attr_bn=ParamAttr(is_static=is_generator_training,
initial_mean=1.0, h0 = conv_bn(
initial_std=0.02) sample,
h0 = conv_bn(sample,
channels=c_dim, channels=c_dim,
imgSize=sample_dim, imgSize=sample_dim,
num_filters=df_dim, num_filters=df_dim,
@ -214,7 +243,8 @@ def discriminator(sample):
param_attr_bn=param_attr_bn, param_attr_bn=param_attr_bn,
bn=False) bn=False)
h1_bn = conv_bn(h0, h1_bn = conv_bn(
h0,
channels=df_dim, channels=df_dim,
imgSize=s2, imgSize=s2,
num_filters=df_dim * 2, num_filters=df_dim * 2,
@ -226,7 +256,8 @@ def discriminator(sample):
param_attr_bn=param_attr_bn, param_attr_bn=param_attr_bn,
bn=True) bn=True)
h2_bn = conv_bn(h1_bn, h2_bn = conv_bn(
h1_bn,
channels=df_dim * 2, channels=df_dim * 2,
imgSize=s4, imgSize=s4,
num_filters=df_dim * 4, num_filters=df_dim * 4,
@ -238,13 +269,15 @@ def discriminator(sample):
param_attr_bn=param_attr_bn, param_attr_bn=param_attr_bn,
bn=True) bn=True)
return fc_layer(input=h2_bn, name="dis_prob", size=2, return fc_layer(
input=h2_bn,
name="dis_prob",
size=2,
bias_attr=bias_attr, bias_attr=bias_attr,
param_attr=param_attr, param_attr=param_attr,
act=SoftmaxActivation()) act=SoftmaxActivation())
if is_generator_training: if is_generator_training:
noise = data_layer(name="noise", size=noise_dim) noise = data_layer(name="noise", size=noise_dim)
sample = generator(noise) sample = generator(noise)
@ -256,7 +289,8 @@ if is_generator_training or is_discriminator_training:
label = data_layer(name="label", size=1) label = data_layer(name="label", size=1)
prob = discriminator(sample) prob = discriminator(sample)
cost = cross_entropy(input=prob, label=label) cost = cross_entropy(input=prob, label=label)
classification_error_evaluator(input=prob, label=label, name=mode+'_error') classification_error_evaluator(
input=prob, label=label, name=mode + '_error')
outputs(cost) outputs(cost)
if is_generator: if is_generator:

@ -1,4 +1,4 @@
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved # Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@ -24,6 +24,7 @@ from paddle.trainer.config_parser import logger
import py_paddle.swig_paddle as api import py_paddle.swig_paddle as api
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
def plot2DScatter(data, outputfile): def plot2DScatter(data, outputfile):
''' '''
Plot the data as a 2D scatter plot and save to outputfile Plot the data as a 2D scatter plot and save to outputfile
@ -41,9 +42,11 @@ def plot2DScatter(data, outputfile):
plt.scatter(x, y) plt.scatter(x, y)
plt.savefig(outputfile, bbox_inches='tight') plt.savefig(outputfile, bbox_inches='tight')
def CHECK_EQ(a, b): def CHECK_EQ(a, b):
assert a == b, "a=%s, b=%s" % (a, b) assert a == b, "a=%s, b=%s" % (a, b)
def copy_shared_parameters(src, dst): def copy_shared_parameters(src, dst):
''' '''
copy the parameters from src to dst copy the parameters from src to dst
@ -52,11 +55,9 @@ def copy_shared_parameters(src, dst):
:param dst: the destination of the parameters :param dst: the destination of the parameters
:type dst: GradientMachine :type dst: GradientMachine
''' '''
src_params = [src.getParameter(i) src_params = [src.getParameter(i) for i in xrange(src.getParameterSize())]
for i in xrange(src.getParameterSize())]
src_params = dict([(p.getName(), p) for p in src_params]) src_params = dict([(p.getName(), p) for p in src_params])
for i in xrange(dst.getParameterSize()): for i in xrange(dst.getParameterSize()):
dst_param = dst.getParameter(i) dst_param = dst.getParameter(i)
src_param = src_params.get(dst_param.getName(), None) src_param = src_params.get(dst_param.getName(), None)
@ -68,14 +69,16 @@ def copy_shared_parameters(src, dst):
dst_value.copyFrom(src_value) dst_value.copyFrom(src_value)
dst_param.setValueUpdated() dst_param.setValueUpdated()
def print_parameters(src): def print_parameters(src):
src_params = [src.getParameter(i) src_params = [src.getParameter(i) for i in xrange(src.getParameterSize())]
for i in xrange(src.getParameterSize())]
print "***************" print "***************"
for p in src_params: for p in src_params:
print "Name is %s" % p.getName() print "Name is %s" % p.getName()
print "value is %s \n" % p.getBuf(api.PARAMETER_VALUE).copyToNumpyArray() print "value is %s \n" % p.getBuf(api.PARAMETER_VALUE).copyToNumpyArray(
)
def load_mnist_data(imageFile): def load_mnist_data(imageFile):
f = open(imageFile, "rb") f = open(imageFile, "rb")
@ -93,6 +96,7 @@ def load_mnist_data(imageFile):
f.close() f.close()
return data.astype('float32') return data.astype('float32')
def load_cifar_data(cifar_path): def load_cifar_data(cifar_path):
batch_size = 10000 batch_size = 10000
data = numpy.zeros((5 * batch_size, 32 * 32 * 3), dtype="float32") data = numpy.zeros((5 * batch_size, 32 * 32 * 3), dtype="float32")
@ -106,11 +110,13 @@ def load_cifar_data(cifar_path):
data = data / 255.0 * 2.0 - 1.0 data = data / 255.0 * 2.0 - 1.0
return data return data
# synthesize 2-D uniform data # synthesize 2-D uniform data
def load_uniform_data(): def load_uniform_data():
data = numpy.random.rand(1000000, 2).astype('float32') data = numpy.random.rand(1000000, 2).astype('float32')
return data return data
def merge(images, size): def merge(images, size):
if images.shape[1] == 28 * 28: if images.shape[1] == 28 * 28:
h, w, c = 28, 28, 1 h, w, c = 28, 28, 1
@ -124,6 +130,7 @@ def merge(images, size):
((images[idx, :].reshape((h, w, c), order="F").transpose(1, 0, 2) + 1.0) / 2.0 * 255.0) ((images[idx, :].reshape((h, w, c), order="F").transpose(1, 0, 2) + 1.0) / 2.0 * 255.0)
return img.astype('uint8') return img.astype('uint8')
def save_images(images, path): def save_images(images, path):
merged_img = merge(images, [8, 8]) merged_img = merge(images, [8, 8])
if merged_img.shape[2] == 1: if merged_img.shape[2] == 1:
@ -132,13 +139,16 @@ def save_images(images, path):
im = Image.fromarray(merged_img, mode="RGB") im = Image.fromarray(merged_img, mode="RGB")
im.save(path) im.save(path)
def get_real_samples(batch_size, data_np): def get_real_samples(batch_size, data_np):
return data_np[numpy.random.choice(data_np.shape[0], batch_size, return data_np[numpy.random.choice(
replace=False),:] data_np.shape[0], batch_size, replace=False), :]
def get_noise(batch_size, noise_dim): def get_noise(batch_size, noise_dim):
return numpy.random.normal(size=(batch_size, noise_dim)).astype('float32') return numpy.random.normal(size=(batch_size, noise_dim)).astype('float32')
def get_fake_samples(generator_machine, batch_size, noise): def get_fake_samples(generator_machine, batch_size, noise):
gen_inputs = api.Arguments.createArguments(1) gen_inputs = api.Arguments.createArguments(1)
gen_inputs.setSlotValue(0, api.Matrix.createDenseFromNumpy(noise)) gen_inputs.setSlotValue(0, api.Matrix.createDenseFromNumpy(noise))
@ -147,12 +157,14 @@ def get_fake_samples(generator_machine, batch_size, noise):
fake_samples = gen_outputs.getSlotValue(0).copyToNumpyMat() fake_samples = gen_outputs.getSlotValue(0).copyToNumpyMat()
return fake_samples return fake_samples
def get_training_loss(training_machine, inputs): def get_training_loss(training_machine, inputs):
outputs = api.Arguments.createArguments(0) outputs = api.Arguments.createArguments(0)
training_machine.forward(inputs, outputs, api.PASS_TEST) training_machine.forward(inputs, outputs, api.PASS_TEST)
loss = outputs.getSlotValue(0).copyToNumpyMat() loss = outputs.getSlotValue(0).copyToNumpyMat()
return numpy.mean(loss) return numpy.mean(loss)
def prepare_discriminator_data_batch_pos(batch_size, data_np): def prepare_discriminator_data_batch_pos(batch_size, data_np):
real_samples = get_real_samples(batch_size, data_np) real_samples = get_real_samples(batch_size, data_np)
labels = numpy.ones(batch_size, dtype='int32') labels = numpy.ones(batch_size, dtype='int32')
@ -161,6 +173,7 @@ def prepare_discriminator_data_batch_pos(batch_size, data_np):
inputs.setSlotIds(1, api.IVector.createVectorFromNumpy(labels)) inputs.setSlotIds(1, api.IVector.createVectorFromNumpy(labels))
return inputs return inputs
def prepare_discriminator_data_batch_neg(generator_machine, batch_size, noise): def prepare_discriminator_data_batch_neg(generator_machine, batch_size, noise):
fake_samples = get_fake_samples(generator_machine, batch_size, noise) fake_samples = get_fake_samples(generator_machine, batch_size, noise)
labels = numpy.zeros(batch_size, dtype='int32') labels = numpy.zeros(batch_size, dtype='int32')
@ -169,6 +182,7 @@ def prepare_discriminator_data_batch_neg(generator_machine, batch_size, noise):
inputs.setSlotIds(1, api.IVector.createVectorFromNumpy(labels)) inputs.setSlotIds(1, api.IVector.createVectorFromNumpy(labels))
return inputs return inputs
def prepare_generator_data_batch(batch_size, noise): def prepare_generator_data_batch(batch_size, noise):
label = numpy.ones(batch_size, dtype='int32') label = numpy.ones(batch_size, dtype='int32')
inputs = api.Arguments.createArguments(2) inputs = api.Arguments.createArguments(2)
@ -193,10 +207,9 @@ def get_layer_size(model_conf, layer_name):
def main(): def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-d", "--data_source", help="mnist or cifar or uniform") parser.add_argument("-d", "--data_source", help="mnist or cifar or uniform")
parser.add_argument("--use_gpu", default="1", parser.add_argument(
help="1 means use gpu for training") "--use_gpu", default="1", help="1 means use gpu for training")
parser.add_argument("--gpu_id", default="0", parser.add_argument("--gpu_id", default="0", help="the gpu_id parameter")
help="the gpu_id parameter")
args = parser.parse_args() args = parser.parse_args()
data_source = args.data_source data_source = args.data_source
use_gpu = args.use_gpu use_gpu = args.use_gpu
@ -209,8 +222,9 @@ def main():
if not os.path.exists("./%s_params/" % data_source): if not os.path.exists("./%s_params/" % data_source):
os.makedirs("./%s_params/" % data_source) os.makedirs("./%s_params/" % data_source)
api.initPaddle('--use_gpu=' + use_gpu, '--dot_period=10', '--log_period=100', api.initPaddle('--use_gpu=' + use_gpu, '--dot_period=10',
'--gpu_id=' + args.gpu_id, '--save_dir=' + "./%s_params/" % data_source) '--log_period=100', '--gpu_id=' + args.gpu_id,
'--save_dir=' + "./%s_params/" % data_source)
if data_source == "uniform": if data_source == "uniform":
conf = "gan_conf.py" conf = "gan_conf.py"
@ -220,7 +234,8 @@ def main():
num_iter = 1000 num_iter = 1000
gen_conf = parse_config(conf, "mode=generator_training,data=" + data_source) gen_conf = parse_config(conf, "mode=generator_training,data=" + data_source)
dis_conf = parse_config(conf, "mode=discriminator_training,data=" + data_source) dis_conf = parse_config(conf,
"mode=discriminator_training,data=" + data_source)
generator_conf = parse_config(conf, "mode=generator,data=" + data_source) generator_conf = parse_config(conf, "mode=generator,data=" + data_source)
batch_size = dis_conf.opt_config.batch_size batch_size = dis_conf.opt_config.batch_size
noise_dim = get_layer_size(gen_conf.model_config, "noise") noise_dim = get_layer_size(gen_conf.model_config, "noise")
@ -245,11 +260,9 @@ def main():
generator_machine = api.GradientMachine.createFromConfigProto( generator_machine = api.GradientMachine.createFromConfigProto(
generator_conf.model_config) generator_conf.model_config)
dis_trainer = api.Trainer.create( dis_trainer = api.Trainer.create(dis_conf, dis_training_machine)
dis_conf, dis_training_machine)
gen_trainer = api.Trainer.create( gen_trainer = api.Trainer.create(gen_conf, gen_training_machine)
gen_conf, gen_training_machine)
dis_trainer.startTrain() dis_trainer.startTrain()
gen_trainer.startTrain() gen_trainer.startTrain()
@ -272,21 +285,23 @@ def main():
noise = get_noise(batch_size, noise_dim) noise = get_noise(batch_size, noise_dim)
data_batch_dis_pos = prepare_discriminator_data_batch_pos( data_batch_dis_pos = prepare_discriminator_data_batch_pos(
batch_size, data_np) batch_size, data_np)
dis_loss_pos = get_training_loss(dis_training_machine, data_batch_dis_pos) dis_loss_pos = get_training_loss(dis_training_machine,
data_batch_dis_pos)
data_batch_dis_neg = prepare_discriminator_data_batch_neg( data_batch_dis_neg = prepare_discriminator_data_batch_neg(
generator_machine, batch_size, noise) generator_machine, batch_size, noise)
dis_loss_neg = get_training_loss(dis_training_machine, data_batch_dis_neg) dis_loss_neg = get_training_loss(dis_training_machine,
data_batch_dis_neg)
dis_loss = (dis_loss_pos + dis_loss_neg) / 2.0 dis_loss = (dis_loss_pos + dis_loss_neg) / 2.0
# Do forward pass in generator to get the gen_loss # Do forward pass in generator to get the gen_loss
data_batch_gen = prepare_generator_data_batch( data_batch_gen = prepare_generator_data_batch(batch_size, noise)
batch_size, noise)
gen_loss = get_training_loss(gen_training_machine, data_batch_gen) gen_loss = get_training_loss(gen_training_machine, data_batch_gen)
if i % 100 == 0: if i % 100 == 0:
print "d_pos_loss is %s d_neg_loss is %s" % (dis_loss_pos, dis_loss_neg) print "d_pos_loss is %s d_neg_loss is %s" % (dis_loss_pos,
dis_loss_neg)
print "d_loss is %s g_loss is %s" % (dis_loss, gen_loss) print "d_loss is %s g_loss is %s" % (dis_loss, gen_loss)
# Decide which network to train based on the training history # Decide which network to train based on the training history
@ -300,7 +315,8 @@ def main():
curr_strike = 1 curr_strike = 1
dis_trainer.trainOneDataBatch(batch_size, data_batch_dis_neg) dis_trainer.trainOneDataBatch(batch_size, data_batch_dis_neg)
dis_trainer.trainOneDataBatch(batch_size, data_batch_dis_pos) dis_trainer.trainOneDataBatch(batch_size, data_batch_dis_pos)
copy_shared_parameters(dis_training_machine, gen_training_machine) copy_shared_parameters(dis_training_machine,
gen_training_machine)
else: else:
if curr_train == "gen": if curr_train == "gen":
@ -311,7 +327,8 @@ def main():
gen_trainer.trainOneDataBatch(batch_size, data_batch_gen) gen_trainer.trainOneDataBatch(batch_size, data_batch_gen)
# TODO: add API for paddle to allow true parameter sharing between different GradientMachines # TODO: add API for paddle to allow true parameter sharing between different GradientMachines
# so that we do not need to copy shared parameters. # so that we do not need to copy shared parameters.
copy_shared_parameters(gen_training_machine, dis_training_machine) copy_shared_parameters(gen_training_machine,
dis_training_machine)
copy_shared_parameters(gen_training_machine, generator_machine) copy_shared_parameters(gen_training_machine, generator_machine)
dis_trainer.finishTrainPass() dis_trainer.finishTrainPass()
@ -319,11 +336,14 @@ def main():
# At the end of each pass, save the generated samples/images # At the end of each pass, save the generated samples/images
fake_samples = get_fake_samples(generator_machine, batch_size, noise) fake_samples = get_fake_samples(generator_machine, batch_size, noise)
if data_source == "uniform": if data_source == "uniform":
plot2DScatter(fake_samples, "./%s_samples/train_pass%s.png" % (data_source, train_pass)) plot2DScatter(fake_samples, "./%s_samples/train_pass%s.png" %
(data_source, train_pass))
else: else:
save_images(fake_samples, "./%s_samples/train_pass%s.png" % (data_source, train_pass)) save_images(fake_samples, "./%s_samples/train_pass%s.png" %
(data_source, train_pass))
dis_trainer.finishTrain() dis_trainer.finishTrain()
gen_trainer.finishTrain() gen_trainer.finishTrain()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

@ -1,4 +1,4 @@
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved # Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.

@ -1,4 +1,4 @@
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved # Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.

@ -1,4 +1,4 @@
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved # Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.

@ -1,4 +1,4 @@
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved # Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved # Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.

@ -1,4 +1,4 @@
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved # Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.

@ -1,4 +1,4 @@
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved # Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved # Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved # Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.

@ -1,4 +1,4 @@
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved # Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.

@ -1,4 +1,4 @@
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved # Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.

@ -1,7 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved # Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save