未验证 提交 0ce38b77 编写于 作者: Q Qiao Longfei 提交者: GitHub

correct optimizer import (#5699)

上级 06d155b5
import numpy as np
import paddle.v2 as paddle import paddle.v2 as paddle
import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.core as core import paddle.v2.fluid.core as core
import paddle.v2.fluid.optimizer as optimizer
import paddle.v2.fluid.framework as framework import paddle.v2.fluid.framework as framework
from paddle.v2.fluid.io import save_persistables, load_persistables import paddle.v2.fluid.layers as layers
from paddle.v2.fluid.executor import Executor from paddle.v2.fluid.executor import Executor
from paddle.v2.fluid.io import save_persistables, load_persistables
from paddle.v2.fluid.optimizer import SGDOptimizer
import numpy as np x = layers.data(name='x', shape=[13], data_type='float32')
x = layers.data(
name='x',
shape=[13],
data_type='float32')
y_predict = layers.fc(input=x, y_predict = layers.fc(input=x, size=1, act=None)
size=1,
act=None)
y = layers.data( y = layers.data(name='y', shape=[1], data_type='float32')
name='y',
shape=[1],
data_type='float32')
cost = layers.square_error_cost( cost = layers.square_error_cost(input=y_predict, label=y)
input=y_predict,
label=y)
avg_cost = layers.mean(x=cost) avg_cost = layers.mean(x=cost)
sgd_optimizer = optimizer.SGDOptimizer(learning_rate=0.001) sgd_optimizer = SGDOptimizer(learning_rate=0.001)
opts = sgd_optimizer.minimize(avg_cost) opts = sgd_optimizer.minimize(avg_cost)
BATCH_SIZE = 20 BATCH_SIZE = 20
......
import numpy as np import numpy as np
import paddle.v2 as paddle import paddle.v2 as paddle
import paddle.v2.fluid.core as core import paddle.v2.fluid.core as core
import paddle.v2.fluid.framework as framework
import paddle.v2.fluid.layers as layers import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.nets as nets import paddle.v2.fluid.nets as nets
import paddle.v2.fluid.optimizer as optimizer
from paddle.v2.fluid.executor import Executor from paddle.v2.fluid.executor import Executor
import paddle.v2.fluid.framework as framework
from paddle.v2.fluid.initializer import XavierInitializer from paddle.v2.fluid.initializer import XavierInitializer
from paddle.v2.fluid.optimizer import AdamOptimizer
def resnet_cifar10(input, depth=32): def resnet_cifar10(input, depth=32):
def conv_bn_layer(input, def conv_bn_layer(input, ch_out, filter_size, stride, padding, act='relu'):
ch_out,
filter_size,
stride,
padding,
act='relu'):
tmp = layers.conv2d( tmp = layers.conv2d(
input=input, input=input,
filter_size=filter_size, filter_size=filter_size,
...@@ -24,9 +19,7 @@ def resnet_cifar10(input, depth=32): ...@@ -24,9 +19,7 @@ def resnet_cifar10(input, depth=32):
padding=padding, padding=padding,
act=None, act=None,
bias_attr=False) bias_attr=False)
return layers.batch_norm( return layers.batch_norm(input=tmp, act=act)
input=tmp,
act=act)
def shortcut(input, ch_in, ch_out, stride, program, init_program): def shortcut(input, ch_in, ch_out, stride, program, init_program):
if ch_in != ch_out: if ch_in != ch_out:
...@@ -35,28 +28,11 @@ def resnet_cifar10(input, depth=32): ...@@ -35,28 +28,11 @@ def resnet_cifar10(input, depth=32):
else: else:
return input return input
def basicblock(input, def basicblock(input, ch_in, ch_out, stride):
ch_in, tmp = conv_bn_layer(input, ch_out, 3, stride, 1)
ch_out, tmp = conv_bn_layer(tmp, ch_out, 3, 1, 1, act=None)
stride):
tmp = conv_bn_layer(
input,
ch_out,
3,
stride,
1)
tmp = conv_bn_layer(
tmp,
ch_out,
3,
1,
1,
act=None)
short = shortcut(input, ch_in, ch_out, stride) short = shortcut(input, ch_in, ch_out, stride)
return layers.elementwise_add( return layers.elementwise_add(x=tmp, y=short, act='relu')
x=tmp,
y=short,
act='relu')
def layer_warp(block_func, input, ch_in, ch_out, count, stride): def layer_warp(block_func, input, ch_in, ch_out, count, stride):
tmp = block_func(input, ch_in, ch_out, stride) tmp = block_func(input, ch_in, ch_out, stride)
...@@ -67,45 +43,17 @@ def resnet_cifar10(input, depth=32): ...@@ -67,45 +43,17 @@ def resnet_cifar10(input, depth=32):
assert (depth - 2) % 6 == 0 assert (depth - 2) % 6 == 0
n = (depth - 2) / 6 n = (depth - 2) / 6
conv1 = conv_bn_layer( conv1 = conv_bn_layer(
input=input, input=input, ch_out=16, filter_size=3, stride=1, padding=1)
ch_out=16, res1 = layer_warp(basicblock, conv1, 16, 16, n, 1)
filter_size=3, res2 = layer_warp(basicblock, res1, 16, 32, n, 2)
stride=1, res3 = layer_warp(basicblock, res2, 32, 64, n, 2)
padding=1)
res1 = layer_warp(
basicblock,
conv1,
16,
16,
n,
1)
res2 = layer_warp(
basicblock,
res1,
16,
32,
n,
2)
res3 = layer_warp(
basicblock,
res2,
32,
64,
n,
2)
pool = layers.pool2d( pool = layers.pool2d(
input=res3, input=res3, pool_size=8, pool_type='avg', pool_stride=1)
pool_size=8,
pool_type='avg',
pool_stride=1)
return pool return pool
def vgg16_bn_drop(input): def vgg16_bn_drop(input):
def conv_block(input, def conv_block(input, num_filter, groups, dropouts):
num_filter,
groups,
dropouts):
return nets.img_conv_group( return nets.img_conv_group(
input=input, input=input,
pool_size=2, pool_size=2,
...@@ -123,22 +71,14 @@ def vgg16_bn_drop(input): ...@@ -123,22 +71,14 @@ def vgg16_bn_drop(input):
conv4 = conv_block(conv3, 512, 3, [0.4, 0.4, 0]) conv4 = conv_block(conv3, 512, 3, [0.4, 0.4, 0])
conv5 = conv_block(conv4, 512, 3, [0.4, 0.4, 0]) conv5 = conv_block(conv4, 512, 3, [0.4, 0.4, 0])
drop = layers.dropout( drop = layers.dropout(x=conv5, dropout_prob=0.5)
x=conv5,
dropout_prob=0.5)
fc1 = layers.fc(input=drop, fc1 = layers.fc(input=drop,
size=512, size=512,
act=None, act=None,
param_attr={"initializer": XavierInitializer()}) param_attr={"initializer": XavierInitializer()})
reshape1 = layers.reshape( reshape1 = layers.reshape(x=fc1, shape=list(fc1.shape + (1, 1)))
x=fc1, bn = layers.batch_norm(input=reshape1, act='relu')
shape=list(fc1.shape + (1, 1))) drop2 = layers.dropout(x=bn, dropout_prob=0.5)
bn = layers.batch_norm(
input=reshape1,
act='relu')
drop2 = layers.dropout(
x=bn,
dropout_prob=0.5)
fc2 = layers.fc(input=drop2, fc2 = layers.fc(input=drop2,
size=512, size=512,
act=None, act=None,
...@@ -165,8 +105,8 @@ cost = layers.cross_entropy(input=predict, label=label) ...@@ -165,8 +105,8 @@ cost = layers.cross_entropy(input=predict, label=label)
avg_cost = layers.mean(x=cost) avg_cost = layers.mean(x=cost)
accuracy = layers.accuracy(input=predict, label=label) accuracy = layers.accuracy(input=predict, label=label)
# optimizer = optimizer.SGDOptimizer(learning_rate=0.001) # optimizer = SGDOptimizer(learning_rate=0.001)
optimizer = optimizer.AdamOptimizer(learning_rate=0.001) optimizer = AdamOptimizer(learning_rate=0.001)
opts = optimizer.minimize(avg_cost) opts = optimizer.minimize(avg_cost)
BATCH_SIZE = 128 BATCH_SIZE = 128
......
import numpy as np
import paddle.v2 as paddle import paddle.v2 as paddle
import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.nets as nets
import paddle.v2.fluid.core as core import paddle.v2.fluid.core as core
import paddle.v2.fluid.optimizer as optimizer
import paddle.v2.fluid.evaluator as evaluator import paddle.v2.fluid.evaluator as evaluator
import paddle.v2.fluid.framework as framework import paddle.v2.fluid.framework as framework
import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.nets as nets
from paddle.v2.fluid.executor import Executor from paddle.v2.fluid.executor import Executor
from paddle.v2.fluid.optimizer import AdamOptimizer
import numpy as np images = layers.data(name='pixel', shape=[1, 28, 28], data_type='float32')
label = layers.data(name='label', shape=[1], data_type='int64')
images = layers.data(
name='pixel',
shape=[1, 28, 28],
data_type='float32')
label = layers.data(
name='label',
shape=[1],
data_type='int64')
conv_pool_1 = nets.simple_img_conv_pool( conv_pool_1 = nets.simple_img_conv_pool(
input=images, input=images,
filter_size=5, filter_size=5,
...@@ -32,17 +25,13 @@ conv_pool_2 = nets.simple_img_conv_pool( ...@@ -32,17 +25,13 @@ conv_pool_2 = nets.simple_img_conv_pool(
pool_stride=2, pool_stride=2,
act="relu") act="relu")
predict = layers.fc(input=conv_pool_2, predict = layers.fc(input=conv_pool_2, size=10, act="softmax")
size=10,
act="softmax")
cost = layers.cross_entropy(input=predict, label=label) cost = layers.cross_entropy(input=predict, label=label)
avg_cost = layers.mean(x=cost) avg_cost = layers.mean(x=cost)
optimizer = optimizer.AdamOptimizer(learning_rate=0.01, beta1=0.9, beta2=0.999) optimizer = AdamOptimizer(learning_rate=0.01, beta1=0.9, beta2=0.999)
opts = optimizer.minimize(avg_cost) opts = optimizer.minimize(avg_cost)
accuracy, acc_out = evaluator.accuracy( accuracy, acc_out = evaluator.accuracy(input=predict, label=label)
input=predict,
label=label)
BATCH_SIZE = 50 BATCH_SIZE = 50
PASS_NUM = 3 PASS_NUM = 3
......
import numpy as np
import paddle.v2 as paddle import paddle.v2 as paddle
import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.core as core import paddle.v2.fluid.core as core
import paddle.v2.fluid.optimizer as optimizer
import paddle.v2.fluid.framework as framework import paddle.v2.fluid.framework as framework
import paddle.v2.fluid.layers as layers
from paddle.v2.fluid.executor import Executor from paddle.v2.fluid.executor import Executor
from paddle.v2.fluid.regularizer import L2DecayRegularizer
from paddle.v2.fluid.initializer import UniformInitializer from paddle.v2.fluid.initializer import UniformInitializer
from paddle.v2.fluid.optimizer import MomentumOptimizer
import numpy as np from paddle.v2.fluid.regularizer import L2DecayRegularizer
BATCH_SIZE = 128 BATCH_SIZE = 128
image = layers.data( image = layers.data(name='x', shape=[784], data_type='float32')
name='x',
shape=[784],
data_type='float32')
param_attr = { param_attr = {
'name': None, 'name': None,
...@@ -22,32 +18,21 @@ param_attr = { ...@@ -22,32 +18,21 @@ param_attr = {
'regularization': L2DecayRegularizer(0.0005 * BATCH_SIZE) 'regularization': L2DecayRegularizer(0.0005 * BATCH_SIZE)
} }
hidden1 = layers.fc(input=image, hidden1 = layers.fc(input=image, size=128, act='relu', param_attr=param_attr)
size=128, hidden2 = layers.fc(input=hidden1, size=64, act='relu', param_attr=param_attr)
act='relu',
param_attr=param_attr)
hidden2 = layers.fc(input=hidden1,
size=64,
act='relu',
param_attr=param_attr)
predict = layers.fc(input=hidden2, predict = layers.fc(input=hidden2,
size=10, size=10,
act='softmax', act='softmax',
param_attr=param_attr) param_attr=param_attr)
label = layers.data( label = layers.data(name='y', shape=[1], data_type='int64')
name='y',
shape=[1],
data_type='int64')
cost = layers.cross_entropy(input=predict, label=label) cost = layers.cross_entropy(input=predict, label=label)
avg_cost = layers.mean(x=cost) avg_cost = layers.mean(x=cost)
accuracy = layers.accuracy( accuracy = layers.accuracy(input=predict, label=label)
input=predict,
label=label)
optimizer = optimizer.MomentumOptimizer(learning_rate=0.001, momentum=0.9) optimizer = MomentumOptimizer(learning_rate=0.001, momentum=0.9)
opts = optimizer.minimize(avg_cost) opts = optimizer.minimize(avg_cost)
train_reader = paddle.batch( train_reader = paddle.batch(
......
import numpy as np
import paddle.v2 as paddle import paddle.v2 as paddle
import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.nets as nets
import paddle.v2.fluid.core as core import paddle.v2.fluid.core as core
import paddle.v2.fluid.optimizer as optimizer
import paddle.v2.fluid.framework as framework import paddle.v2.fluid.framework as framework
import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.nets as nets
from paddle.v2.fluid.executor import Executor from paddle.v2.fluid.executor import Executor
from paddle.v2.fluid.optimizer import SGDOptimizer
import numpy as np
IS_SPARSE = True IS_SPARSE = True
USE_GPU = False USE_GPU = False
...@@ -19,10 +18,7 @@ def get_usr_combined_features(): ...@@ -19,10 +18,7 @@ def get_usr_combined_features():
USR_DICT_SIZE = paddle.dataset.movielens.max_user_id() + 1 USR_DICT_SIZE = paddle.dataset.movielens.max_user_id() + 1
uid = layers.data( uid = layers.data(name='user_id', shape=[1], data_type='int64')
name='user_id',
shape=[1],
data_type='int64')
usr_emb = layers.embedding( usr_emb = layers.embedding(
input=uid, input=uid,
...@@ -31,15 +27,11 @@ def get_usr_combined_features(): ...@@ -31,15 +27,11 @@ def get_usr_combined_features():
param_attr={'name': 'user_table'}, param_attr={'name': 'user_table'},
is_sparse=IS_SPARSE) is_sparse=IS_SPARSE)
usr_fc = layers.fc(input=usr_emb, usr_fc = layers.fc(input=usr_emb, size=32)
size=32)
USR_GENDER_DICT_SIZE = 2 USR_GENDER_DICT_SIZE = 2
usr_gender_id = layers.data( usr_gender_id = layers.data(name='gender_id', shape=[1], data_type='int64')
name='gender_id',
shape=[1],
data_type='int64')
usr_gender_emb = layers.embedding( usr_gender_emb = layers.embedding(
input=usr_gender_id, input=usr_gender_id,
...@@ -47,14 +39,10 @@ def get_usr_combined_features(): ...@@ -47,14 +39,10 @@ def get_usr_combined_features():
param_attr={'name': 'gender_table'}, param_attr={'name': 'gender_table'},
is_sparse=IS_SPARSE) is_sparse=IS_SPARSE)
usr_gender_fc = layers.fc(input=usr_gender_emb, usr_gender_fc = layers.fc(input=usr_gender_emb, size=16)
size=16)
USR_AGE_DICT_SIZE = len(paddle.dataset.movielens.age_table) USR_AGE_DICT_SIZE = len(paddle.dataset.movielens.age_table)
usr_age_id = layers.data( usr_age_id = layers.data(name='age_id', shape=[1], data_type="int64")
name='age_id',
shape=[1],
data_type="int64")
usr_age_emb = layers.embedding( usr_age_emb = layers.embedding(
input=usr_age_id, input=usr_age_id,
...@@ -62,14 +50,10 @@ def get_usr_combined_features(): ...@@ -62,14 +50,10 @@ def get_usr_combined_features():
is_sparse=IS_SPARSE, is_sparse=IS_SPARSE,
param_attr={'name': 'age_table'}) param_attr={'name': 'age_table'})
usr_age_fc = layers.fc(input=usr_age_emb, usr_age_fc = layers.fc(input=usr_age_emb, size=16)
size=16)
USR_JOB_DICT_SIZE = paddle.dataset.movielens.max_job_id() + 1 USR_JOB_DICT_SIZE = paddle.dataset.movielens.max_job_id() + 1
usr_job_id = layers.data( usr_job_id = layers.data(name='job_id', shape=[1], data_type="int64")
name='job_id',
shape=[1],
data_type="int64")
usr_job_emb = layers.embedding( usr_job_emb = layers.embedding(
input=usr_job_id, input=usr_job_id,
...@@ -77,16 +61,12 @@ def get_usr_combined_features(): ...@@ -77,16 +61,12 @@ def get_usr_combined_features():
param_attr={'name': 'job_table'}, param_attr={'name': 'job_table'},
is_sparse=IS_SPARSE) is_sparse=IS_SPARSE)
usr_job_fc = layers.fc(input=usr_job_emb, usr_job_fc = layers.fc(input=usr_job_emb, size=16)
size=16)
concat_embed = layers.concat( concat_embed = layers.concat(
input=[usr_fc, usr_gender_fc, usr_age_fc, usr_job_fc], input=[usr_fc, usr_gender_fc, usr_age_fc, usr_job_fc], axis=1)
axis=1)
usr_combined_features = layers.fc(input=concat_embed, usr_combined_features = layers.fc(input=concat_embed, size=200, act="tanh")
size=200,
act="tanh")
return usr_combined_features return usr_combined_features
...@@ -95,10 +75,7 @@ def get_mov_combined_features(): ...@@ -95,10 +75,7 @@ def get_mov_combined_features():
MOV_DICT_SIZE = paddle.dataset.movielens.max_movie_id() + 1 MOV_DICT_SIZE = paddle.dataset.movielens.max_movie_id() + 1
mov_id = layers.data( mov_id = layers.data(name='movie_id', shape=[1], data_type='int64')
name='movie_id',
shape=[1],
data_type='int64')
mov_emb = layers.embedding( mov_emb = layers.embedding(
input=mov_id, input=mov_id,
...@@ -107,36 +84,24 @@ def get_mov_combined_features(): ...@@ -107,36 +84,24 @@ def get_mov_combined_features():
param_attr={'name': 'movie_table'}, param_attr={'name': 'movie_table'},
is_sparse=IS_SPARSE) is_sparse=IS_SPARSE)
mov_fc = layers.fc(input=mov_emb, mov_fc = layers.fc(input=mov_emb, size=32)
size=32)
CATEGORY_DICT_SIZE = len(paddle.dataset.movielens.movie_categories()) CATEGORY_DICT_SIZE = len(paddle.dataset.movielens.movie_categories())
category_id = layers.data( category_id = layers.data(name='category_id', shape=[1], data_type='int64')
name='category_id',
shape=[1],
data_type='int64')
mov_categories_emb = layers.embedding( mov_categories_emb = layers.embedding(
input=category_id, input=category_id, size=[CATEGORY_DICT_SIZE, 32], is_sparse=IS_SPARSE)
size=[CATEGORY_DICT_SIZE, 32],
is_sparse=IS_SPARSE)
mov_categories_hidden = layers.sequence_pool( mov_categories_hidden = layers.sequence_pool(
input=mov_categories_emb, input=mov_categories_emb, pool_type="sum")
pool_type="sum")
MOV_TITLE_DICT_SIZE = len(paddle.dataset.movielens.get_movie_title_dict()) MOV_TITLE_DICT_SIZE = len(paddle.dataset.movielens.get_movie_title_dict())
mov_title_id = layers.data( mov_title_id = layers.data(name='movie_title', shape=[1], data_type='int64')
name='movie_title',
shape=[1],
data_type='int64')
mov_title_emb = layers.embedding( mov_title_emb = layers.embedding(
input=mov_title_id, input=mov_title_id, size=[MOV_TITLE_DICT_SIZE, 32], is_sparse=IS_SPARSE)
size=[MOV_TITLE_DICT_SIZE, 32],
is_sparse=IS_SPARSE)
mov_title_conv = nets.sequence_conv_pool( mov_title_conv = nets.sequence_conv_pool(
input=mov_title_emb, input=mov_title_emb,
...@@ -146,13 +111,10 @@ def get_mov_combined_features(): ...@@ -146,13 +111,10 @@ def get_mov_combined_features():
pool_type="sum") pool_type="sum")
concat_embed = layers.concat( concat_embed = layers.concat(
input=[mov_fc, mov_categories_hidden, mov_title_conv], input=[mov_fc, mov_categories_hidden, mov_title_conv], axis=1)
axis=1)
# FIXME(dzh) : need tanh operator # FIXME(dzh) : need tanh operator
mov_combined_features = layers.fc(input=concat_embed, mov_combined_features = layers.fc(input=concat_embed, size=200, act="tanh")
size=200,
act="tanh")
return mov_combined_features return mov_combined_features
...@@ -162,18 +124,11 @@ def model(): ...@@ -162,18 +124,11 @@ def model():
mov_combined_features = get_mov_combined_features() mov_combined_features = get_mov_combined_features()
# need cos sim # need cos sim
inference = layers.cos_sim( inference = layers.cos_sim(X=usr_combined_features, Y=mov_combined_features)
X=usr_combined_features,
Y=mov_combined_features)
label = layers.data( label = layers.data(name='score', shape=[1], data_type='float32')
name='score',
shape=[1],
data_type='float32')
square_cost = layers.square_error_cost( square_cost = layers.square_error_cost(input=inference, label=label)
input=inference,
label=label)
avg_cost = layers.mean(x=square_cost) avg_cost = layers.mean(x=square_cost)
...@@ -182,7 +137,7 @@ def model(): ...@@ -182,7 +137,7 @@ def model():
def main(): def main():
cost = model() cost = model()
sgd_optimizer = optimizer.SGDOptimizer(learning_rate=0.2) sgd_optimizer = SGDOptimizer(learning_rate=0.2)
opts = sgd_optimizer.minimize(cost) opts = sgd_optimizer.minimize(cost)
if USE_GPU: if USE_GPU:
......
import numpy as np
import paddle.v2 as paddle import paddle.v2 as paddle
import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.nets as nets
import paddle.v2.fluid.core as core import paddle.v2.fluid.core as core
import paddle.v2.fluid.optimizer as optimizer
import paddle.v2.fluid.framework as framework import paddle.v2.fluid.framework as framework
import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.nets as nets
from paddle.v2.fluid.executor import Executor from paddle.v2.fluid.executor import Executor
from paddle.v2.fluid.optimizer import AdamOptimizer
import numpy as np
def convolution_net(input_dim, class_dim=2, emb_dim=32, hid_dim=32): def convolution_net(input_dim, class_dim=2, emb_dim=32, hid_dim=32):
...@@ -31,7 +30,7 @@ def convolution_net(input_dim, class_dim=2, emb_dim=32, hid_dim=32): ...@@ -31,7 +30,7 @@ def convolution_net(input_dim, class_dim=2, emb_dim=32, hid_dim=32):
act="softmax") act="softmax")
cost = layers.cross_entropy(input=prediction, label=label) cost = layers.cross_entropy(input=prediction, label=label)
avg_cost = layers.mean(x=cost) avg_cost = layers.mean(x=cost)
adam_optimizer = optimizer.AdamOptimizer(learning_rate=0.002) adam_optimizer = AdamOptimizer(learning_rate=0.002)
opts = adam_optimizer.minimize(avg_cost) opts = adam_optimizer.minimize(avg_cost)
acc = layers.accuracy(input=prediction, label=label) acc = layers.accuracy(input=prediction, label=label)
return avg_cost, acc return avg_cost, acc
......
import numpy as np
import paddle.v2 as paddle import paddle.v2 as paddle
import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.nets as nets
import paddle.v2.fluid.core as core import paddle.v2.fluid.core as core
import paddle.v2.fluid.optimizer as optimizer
import paddle.v2.fluid.framework as framework import paddle.v2.fluid.framework as framework
import paddle.v2.fluid.layers as layers
from paddle.v2.fluid.executor import Executor from paddle.v2.fluid.executor import Executor
from paddle.v2.fluid.optimizer import AdamOptimizer
import numpy as np
def stacked_lstm_net(input_dim, def stacked_lstm_net(input_dim,
...@@ -41,7 +39,7 @@ def stacked_lstm_net(input_dim, ...@@ -41,7 +39,7 @@ def stacked_lstm_net(input_dim,
act='softmax') act='softmax')
cost = layers.cross_entropy(input=prediction, label=label) cost = layers.cross_entropy(input=prediction, label=label)
avg_cost = layers.mean(x=cost) avg_cost = layers.mean(x=cost)
adam_optimizer = optimizer.AdamOptimizer(learning_rate=0.002) adam_optimizer = AdamOptimizer(learning_rate=0.002)
opts = adam_optimizer.minimize(avg_cost) opts = adam_optimizer.minimize(avg_cost)
acc = layers.accuracy(input=prediction, label=label) acc = layers.accuracy(input=prediction, label=label)
return avg_cost, acc return avg_cost, acc
......
import numpy as np
import paddle.v2 as paddle import paddle.v2 as paddle
import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.core as core import paddle.v2.fluid.core as core
import paddle.v2.fluid.optimizer as optimizer
import paddle.v2.fluid.framework as framework import paddle.v2.fluid.framework as framework
import paddle.v2.fluid.layers as layers
from paddle.v2.fluid.executor import Executor from paddle.v2.fluid.executor import Executor
from paddle.v2.fluid.optimizer import AdamOptimizer
import numpy as np
def lstm_net(dict_dim, class_dim=2, emb_dim=32, seq_len=80, batch_size=50): def lstm_net(dict_dim, class_dim=2, emb_dim=32, seq_len=80, batch_size=50):
...@@ -33,7 +32,7 @@ def lstm_net(dict_dim, class_dim=2, emb_dim=32, seq_len=80, batch_size=50): ...@@ -33,7 +32,7 @@ def lstm_net(dict_dim, class_dim=2, emb_dim=32, seq_len=80, batch_size=50):
cost = layers.cross_entropy(input=prediction, label=label) cost = layers.cross_entropy(input=prediction, label=label)
avg_cost = layers.mean(x=cost) avg_cost = layers.mean(x=cost)
adam_optimizer = optimizer.AdamOptimizer(learning_rate=0.002) adam_optimizer = AdamOptimizer(learning_rate=0.002)
opts = adam_optimizer.minimize(avg_cost) opts = adam_optimizer.minimize(avg_cost)
acc = layers.accuracy(input=prediction, label=label) acc = layers.accuracy(input=prediction, label=label)
......
import numpy as np
import paddle.v2 as paddle import paddle.v2 as paddle
import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.core as core import paddle.v2.fluid.core as core
import paddle.v2.fluid.optimizer as optimizer
import paddle.v2.fluid.framework as framework import paddle.v2.fluid.framework as framework
import paddle.v2.fluid.layers as layers
from paddle.v2.fluid.executor import Executor from paddle.v2.fluid.executor import Executor
from paddle.v2.fluid.optimizer import SGDOptimizer
import numpy as np
PASS_NUM = 100 PASS_NUM = 100
EMBED_SIZE = 32 EMBED_SIZE = 32
...@@ -17,26 +16,11 @@ IS_SPARSE = True ...@@ -17,26 +16,11 @@ IS_SPARSE = True
word_dict = paddle.dataset.imikolov.build_dict() word_dict = paddle.dataset.imikolov.build_dict()
dict_size = len(word_dict) dict_size = len(word_dict)
first_word = layers.data( first_word = layers.data(name='firstw', shape=[1], data_type='int64')
name='firstw', second_word = layers.data(name='secondw', shape=[1], data_type='int64')
shape=[1], third_word = layers.data(name='thirdw', shape=[1], data_type='int64')
data_type='int64') forth_word = layers.data(name='forthw', shape=[1], data_type='int64')
second_word = layers.data( next_word = layers.data(name='nextw', shape=[1], data_type='int64')
name='secondw',
shape=[1],
data_type='int64')
third_word = layers.data(
name='thirdw',
shape=[1],
data_type='int64')
forth_word = layers.data(
name='forthw',
shape=[1],
data_type='int64')
next_word = layers.data(
name='nextw',
shape=[1],
data_type='int64')
embed_first = layers.embedding( embed_first = layers.embedding(
input=first_word, input=first_word,
...@@ -64,19 +48,12 @@ embed_forth = layers.embedding( ...@@ -64,19 +48,12 @@ embed_forth = layers.embedding(
param_attr={'name': 'shared_w'}) param_attr={'name': 'shared_w'})
concat_embed = layers.concat( concat_embed = layers.concat(
input=[embed_first, embed_second, embed_third, embed_forth], input=[embed_first, embed_second, embed_third, embed_forth], axis=1)
axis=1) hidden1 = layers.fc(input=concat_embed, size=HIDDEN_SIZE, act='sigmoid')
hidden1 = layers.fc(input=concat_embed, predict_word = layers.fc(input=hidden1, size=dict_size, act='softmax')
size=HIDDEN_SIZE, cost = layers.cross_entropy(input=predict_word, label=next_word)
act='sigmoid')
predict_word = layers.fc(input=hidden1,
size=dict_size,
act='softmax')
cost = layers.cross_entropy(
input=predict_word,
label=next_word)
avg_cost = layers.mean(x=cost) avg_cost = layers.mean(x=cost)
sgd_optimizer = optimizer.SGDOptimizer(learning_rate=0.001) sgd_optimizer = SGDOptimizer(learning_rate=0.001)
opts = sgd_optimizer.minimize(avg_cost) opts = sgd_optimizer.minimize(avg_cost)
train_reader = paddle.batch( train_reader = paddle.batch(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册