Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pycodestyle #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions nets.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ def cnn_net(data,
"""
# embedding layer
emb = fluid.layers.embedding(input=data, size=[dict_dim, emb_dim])

# convolution layer
conv_3 = fluid.nets.sequence_conv_pool(
input=emb,
num_filters=hid_dim,
filter_size=win_size,
act="tanh",
pool_type="max")

# full connect layer
fc_1 = fluid.layers.fc(input=[conv_3], size=hid_dim2)
# softmax layer
Expand Down Expand Up @@ -77,17 +77,17 @@ def lstm_net(data,
input=data,
size=[dict_dim, emb_dim],
param_attr=fluid.ParamAttr(learning_rate=emb_lr))

# Lstm layer
fc0 = fluid.layers.fc(input=emb, size=hid_dim * 4)

lstm_h, c = fluid.layers.dynamic_lstm(
input=fc0, size=hid_dim * 4, is_reverse=False)

# max pooling layer
lstm_max = fluid.layers.sequence_pool(input=lstm_h, pool_type='max')
lstm_max_tanh = fluid.layers.tanh(lstm_max)

# full connect layer
fc1 = fluid.layers.fc(input=lstm_max_tanh, size=hid_dim2, act='tanh')
# softmax layer
Expand Down Expand Up @@ -116,7 +116,7 @@ def bilstm_net(data,
input=data,
size=[dict_dim, emb_dim],
param_attr=fluid.ParamAttr(learning_rate=emb_lr))

# bi-lstm layer
fc0 = fluid.layers.fc(input=emb, size=hid_dim * 4)

Expand All @@ -127,17 +127,17 @@ def bilstm_net(data,

rlstm_h, c = fluid.layers.dynamic_lstm(
input=rfc0, size=hid_dim * 4, is_reverse=True)

# extract last layer
lstm_last = fluid.layers.sequence_last_step(input=lstm_h)
rlstm_last = fluid.layers.sequence_last_step(input=rlstm_h)

lstm_last_tanh = fluid.layers.tanh(lstm_last)
rlstm_last_tanh = fluid.layers.tanh(rlstm_last)

# concat layer
lstm_concat = fluid.layers.concat(input=[lstm_last, rlstm_last], axis=1)

# full connect layer
fc1 = fluid.layers.fc(input=lstm_concat, size=hid_dim2, act='tanh')
# softmax layer
Expand Down Expand Up @@ -180,4 +180,3 @@ def gru_net(data,
acc = fluid.layers.accuracy(input=prediction, label=label)

return avg_cost, acc, prediction

20 changes: 10 additions & 10 deletions sentiment_classify.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def train_net(train_reader,
elif network == "gru_net":
network = gru_net
else:
print ("unknown network type")
print("unknown network type")
return
# word seq data
data = fluid.layers.data(
Expand Down Expand Up @@ -147,8 +147,8 @@ def train_net(train_reader,
for data in train_reader():
# train a batch
avg_cost_np, avg_acc_np = train_exe.run(
feed=feeder.feed(data),
fetch_list=[cost.name, acc.name])
feed=feeder.feed(data),
fetch_list=[cost.name, acc.name])
data_size = len(data)
total_acc += data_size * np.sum(avg_acc_np)
total_cost += data_size * np.sum(avg_cost_np)
Expand All @@ -167,7 +167,7 @@ def eval_net(test_reader, use_gpu, model_path=None):
Evaluation function
"""
if model_path is None:
print (str(model_path) + "can not be found")
print(str(model_path) + "can not be found")
return
# set place, executor
place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
Expand All @@ -178,7 +178,7 @@ def eval_net(test_reader, use_gpu, model_path=None):
# load the saved model
[inference_program, feed_target_names,
fetch_targets] = fluid.io.load_inference_model(model_path, exe)

# compute 2class and 3class accuracy
class2_acc, class3_acc = 0.0, 0.0
total_count, neu_count = 0, 0
Expand Down Expand Up @@ -246,7 +246,7 @@ def main(args):
word_dict, train_reader = utils.prepare_data(
args.train_data_path, args.word_dict_path, args.batch_size,
args.mode)

train_net(
train_reader,
word_dict,
Expand All @@ -257,18 +257,18 @@ def main(args):
args.lr,
args.batch_size,
args.num_passes)

# eval mode
elif args.mode == "eval":
# prepare_data to get word_dict, test_reader
# prepare_data to get word_dict, test_reader
word_dict, test_reader = utils.prepare_data(
args.test_data_path, args.word_dict_path, args.batch_size,
args.mode)
eval_net(
test_reader,
args.use_gpu,
args.model_path)

# infer mode
elif args.mode == "infer":
# prepare_data to get word_dict, test_reader
Expand All @@ -283,4 +283,4 @@ def main(args):

if __name__ == "__main__":
args = parse_args()
main(args)
main(args)