-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #130 from Yancey1989/demo_fit_a_line
Add demo: fit a line
- Loading branch information
Showing
18 changed files
with
137 additions
and
52 deletions.
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,49 @@ | ||
import paddle.v2 as paddle | ||
import pcloud.dataset.uci_housing as uci_housing | ||
|
||
def main(): | ||
# init | ||
paddle.init() | ||
|
||
# network config | ||
x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13)) | ||
y_predict = paddle.layer.fc(input=x, size=1, act=paddle.activation.Linear()) | ||
y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1)) | ||
cost = paddle.layer.mse_cost(input=y_predict, label=y) | ||
|
||
# create parameters | ||
parameters = paddle.parameters.create(cost) | ||
|
||
# create optimizer | ||
optimizer = paddle.optimizer.Momentum(momentum=0) | ||
|
||
trainer = paddle.trainer.SGD( | ||
cost=cost, parameters=parameters, update_equation=optimizer) | ||
|
||
feeding = {'x': 0, 'y': 1} | ||
|
||
# event_handler to print training and testing info | ||
def event_handler(event): | ||
if isinstance(event, paddle.event.EndIteration): | ||
if event.batch_id % 100 == 0: | ||
print "Pass %d, Batch %d, Cost %f" % ( | ||
event.pass_id, event.batch_id, event.cost) | ||
|
||
if isinstance(event, paddle.event.EndPass): | ||
result = trainer.test( | ||
reader=paddle.batch(uci_housing.test(), batch_size=2), | ||
feeding=feeding) | ||
print "Test %d, Cost %f" % (event.pass_id, result.cost) | ||
|
||
# training | ||
trainer.train( | ||
reader=paddle.batch( | ||
paddle.reader.shuffle(uci_housing.train(), buf_size=500), | ||
batch_size=2), | ||
feeding=feeding, | ||
event_handler=event_handler, | ||
num_passes=30) | ||
|
||
|
||
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,3 @@ | ||
Dockerfile | ||
*.pyc | ||
pcloud.egg-info |
File renamed without changes.
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
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
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,3 @@ | ||
build | ||
dist | ||
paddlecloud.egg-info |
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 @@ | ||
__all__ = ["dataset"] |
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,3 @@ | ||
import uci_housing | ||
import common | ||
__all__ = ["uci_housing", "common"] |
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,11 @@ | ||
import uci_housing | ||
import paddle | ||
import importlib | ||
def fetch_all(): | ||
for module_name in filter(lambda x: not x.startswith("__"), | ||
dir(paddle.cloud.dataset)): | ||
if "fetch" in dir( | ||
importlib.import_module("pcloud.dataset.%s" % module_name)): | ||
getattr( | ||
importlib.import_module("pcloud.dataset.%s" % module_name), | ||
"fetch")() |
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,31 @@ | ||
import paddle.v2.dataset.uci_housing as uci_housing | ||
import paddle.v2.dataset.common as common | ||
import os | ||
|
||
__all__=["train", "test", "fetch"] | ||
|
||
dc = os.getenv("PADDLE_CLOUD_CURRENT_DATACENTER") | ||
|
||
#The default public directory on PaddleCloud is /pfs/${DATACENTER}/public/ | ||
common.DATA_HOME = "/pfs/%s/public/dataset" % dc | ||
|
||
TRAIN_FILES_PATTERN = os.path.join(common.DATA_HOME, | ||
"uci_housing/train-*.pickle") | ||
TRAIN_FILES_SUFFIX = os.path.join(common.DATA_HOME, | ||
"uci_housing/train-%05d.pickle") | ||
|
||
|
||
def train(): | ||
return common.cluster_files_reader( | ||
TRAIN_FILES_PATTERN, | ||
trainer_count = int(os.getenv("PADDLE_INIT_NUM_GRADIENT_SERVERS", "1")), | ||
trainer_id = int(os.getenv("PADDLE_INIT_TRAINER_ID", "0"))) | ||
|
||
def test(): | ||
return uci_housing.test() | ||
|
||
def fetch(): | ||
print "fetch cluster files: %s" % TRAIN_FILES_SUFFIX | ||
common.split(uci_housing.train(), | ||
line_count = 500, | ||
suffix=TRAIN_FILES_SUFFIX) |
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,11 @@ | ||
from setuptools import setup | ||
|
||
packages=[ | ||
'pcloud', | ||
'pcloud.dataset'] | ||
|
||
setup(name='pcloud', | ||
version='0.1.1', | ||
description="PaddlePaddle Cloud", | ||
packages=packages | ||
) |
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
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
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