diff --git a/nets.py b/nets.py index 652ea65..8703af2 100644 --- a/nets.py +++ b/nets.py @@ -41,7 +41,7 @@ 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, @@ -49,7 +49,7 @@ def cnn_net(data, 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 @@ -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 @@ -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) @@ -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 @@ -180,4 +180,3 @@ def gru_net(data, acc = fluid.layers.accuracy(input=prediction, label=label) return avg_cost, acc, prediction - diff --git a/sentiment_classify.py b/sentiment_classify.py index 524a6fe..8bd2e82 100644 --- a/sentiment_classify.py +++ b/sentiment_classify.py @@ -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( @@ -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) @@ -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() @@ -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 @@ -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, @@ -257,10 +257,10 @@ 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) @@ -268,7 +268,7 @@ def main(args): test_reader, args.use_gpu, args.model_path) - + # infer mode elif args.mode == "infer": # prepare_data to get word_dict, test_reader @@ -283,4 +283,4 @@ def main(args): if __name__ == "__main__": args = parse_args() - main(args) + main(args)