提交 27bbbda2 编写于 作者: Q QI JUN 提交者: GitHub

Merge pull request #282 from QiJune/feature/refine_codes

refine some codes
...@@ -45,56 +45,60 @@ def convolutional_neural_network(img): ...@@ -45,56 +45,60 @@ def convolutional_neural_network(img):
return predict return predict
paddle.init(use_gpu=False, trainer_count=1) def main():
paddle.init(use_gpu=False, trainer_count=1)
# define network topology
images = paddle.layer.data( # define network topology
name='pixel', type=paddle.data_type.dense_vector(784)) images = paddle.layer.data(
label = paddle.layer.data(name='label', type=paddle.data_type.integer_value(10)) name='pixel', type=paddle.data_type.dense_vector(784))
label = paddle.layer.data(
# Here we can build the prediction network in different ways. Please name='label', type=paddle.data_type.integer_value(10))
# choose one by uncomment corresponding line.
# predict = softmax_regression(images) # Here we can build the prediction network in different ways. Please
# predict = multilayer_perceptron(images) # choose one by uncomment corresponding line.
predict = convolutional_neural_network(images) # predict = softmax_regression(images)
# predict = multilayer_perceptron(images)
cost = paddle.layer.classification_cost(input=predict, label=label) predict = convolutional_neural_network(images)
parameters = paddle.parameters.create(cost) cost = paddle.layer.classification_cost(input=predict, label=label)
optimizer = paddle.optimizer.Momentum( parameters = paddle.parameters.create(cost)
learning_rate=0.1 / 128.0,
momentum=0.9, optimizer = paddle.optimizer.Momentum(
regularization=paddle.optimizer.L2Regularization(rate=0.0005 * 128)) learning_rate=0.1 / 128.0,
momentum=0.9,
trainer = paddle.trainer.SGD( regularization=paddle.optimizer.L2Regularization(rate=0.0005 * 128))
cost=cost, parameters=parameters, update_equation=optimizer)
trainer = paddle.trainer.SGD(
lists = [] cost=cost, parameters=parameters, update_equation=optimizer)
lists = []
def event_handler(event):
if isinstance(event, paddle.event.EndIteration): def event_handler(event):
if event.batch_id % 100 == 0: if isinstance(event, paddle.event.EndIteration):
print "Pass %d, Batch %d, Cost %f, %s" % ( if event.batch_id % 100 == 0:
event.pass_id, event.batch_id, event.cost, event.metrics) print "Pass %d, Batch %d, Cost %f, %s" % (
if isinstance(event, paddle.event.EndPass): event.pass_id, event.batch_id, event.cost, event.metrics)
result = trainer.test(reader=paddle.batch( if isinstance(event, paddle.event.EndPass):
paddle.dataset.mnist.test(), batch_size=128)) result = trainer.test(reader=paddle.batch(
print "Test with Pass %d, Cost %f, %s\n" % (event.pass_id, result.cost, paddle.dataset.mnist.test(), batch_size=128))
result.metrics) print "Test with Pass %d, Cost %f, %s\n" % (
lists.append((event.pass_id, result.cost, event.pass_id, result.cost, result.metrics)
result.metrics['classification_error_evaluator'])) lists.append((event.pass_id, result.cost,
result.metrics['classification_error_evaluator']))
trainer.train( trainer.train(
reader=paddle.batch( reader=paddle.batch(
paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=8192), paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=8192),
batch_size=128), batch_size=128),
event_handler=event_handler, event_handler=event_handler,
num_passes=100) num_passes=100)
# find the best pass # find the best pass
best = sorted(lists, key=lambda list: float(list[1]))[0] best = sorted(lists, key=lambda list: float(list[1]))[0]
print 'Best pass is %s, testing Avgcost is %s' % (best[0], best[1]) print 'Best pass is %s, testing Avgcost is %s' % (best[0], best[1])
print 'The classification accuracy is %.2f%%' % (100 - float(best[2]) * 100) print 'The classification accuracy is %.2f%%' % (100 - float(best[2]) * 100)
if __name__ == '__main__':
main()
...@@ -3,9 +3,7 @@ import cPickle ...@@ -3,9 +3,7 @@ import cPickle
import copy import copy
def main(): def get_usr_combined_features():
paddle.init(use_gpu=False)
movie_title_dict = paddle.dataset.movielens.get_movie_title_dict()
uid = paddle.layer.data( uid = paddle.layer.data(
name='user_id', name='user_id',
type=paddle.data_type.integer_value( type=paddle.data_type.integer_value(
...@@ -36,7 +34,11 @@ def main(): ...@@ -36,7 +34,11 @@ def main():
input=[usr_fc, usr_gender_fc, usr_age_fc, usr_job_fc], input=[usr_fc, usr_gender_fc, usr_age_fc, usr_job_fc],
size=200, size=200,
act=paddle.activation.Tanh()) act=paddle.activation.Tanh())
return usr_combined_features
def get_mov_combined_features():
movie_title_dict = paddle.dataset.movielens.get_movie_title_dict()
mov_id = paddle.layer.data( mov_id = paddle.layer.data(
name='movie_id', name='movie_id',
type=paddle.data_type.integer_value( type=paddle.data_type.integer_value(
...@@ -61,7 +63,13 @@ def main(): ...@@ -61,7 +63,13 @@ def main():
input=[mov_fc, mov_categories_hidden, mov_title_conv], input=[mov_fc, mov_categories_hidden, mov_title_conv],
size=200, size=200,
act=paddle.activation.Tanh()) act=paddle.activation.Tanh())
return mov_combined_features
def main():
paddle.init(use_gpu=False)
usr_combined_features = get_usr_combined_features()
mov_combined_features = get_mov_combined_features()
inference = paddle.layer.cos_sim( inference = paddle.layer.cos_sim(
a=usr_combined_features, b=mov_combined_features, size=1, scale=5) a=usr_combined_features, b=mov_combined_features, size=1, scale=5)
cost = paddle.layer.mse_cost( cost = paddle.layer.mse_cost(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册