Giter VIP home page Giter VIP logo

abcnn's Introduction

ABCNN: Attention-Based Convolutional Neural Network for Modeling Sentence Pairs

[Update]: Someone has reported me that the problem of a loss being 'nan' can be attributed to tf.sqrt function which outpus 'nan' when its input is very small or negative. Therefore, I recommend you modify tf.sqrt functions adequately if you have in the trouble.

[Warning]: Some people have reported that there are some bugs that losses go to NaN in case of ABCNN-2 and 3. (I don't know the exact condition where the bugs appear.) Unfortunately, I have no plan to revise the code around the corner. Please be careful when using the code or please send me the pull requests when your revised version of the code works properly. Thanks.

This is the implementation of ABCNN, which is proposed by Wenpeng Yin et al., on Tensorflow.
It includes all 4 models below:

  • BCNN

    MAP MRR
    BCNN(1 layer) Results 0.6660 0.6813
    Baseline 0.6629 0.6813
    BCNN(2 layer) Results 0.6762 0.6871
    Baseline 0.6593 0.6738
  • ABCNN-1

    MAP MRR
    ABCNN-1(1 layer) Results 0.6652 0.6755
    Baseline 0.6810 0.6979
    ABCNN-1(2 layer) Results 0.6702 0.6838
    Baseline 0.6855 0.7023
  • ABCNN-2

    MAP MRR
    ABCNN-2(1 layer) Results 0.6660 0.6813
    Baseline 0.6885 0.7023
    ABCNN-2(2 layer) Results ------ ------
    Baseline 0.6879 0.7068
  • ABCNN-3

    MAP MRR
    ABCNN-3(1 layer) Results 0.6612 0.6682
    Baseline 0.6914 0.7127
    ABCNN-3(2 layer) Results 0.6571 0.6722
    Baseline 0.6921 0.7105

Note:

  • Implementation is now only focusing on AS task with WikiQA corpus. (I originally tried to deal with PI task with MSRP(Microsoft Research Paraphrase) corpus but it seems that model doesn't work without external features classifier requires.)
  • My code has verified that BCNN works fine as the authors proposed. (watched even better results than the paper's.)
  • In the case of ABCNNs, results are inferior to ones in the paper but somewhat competitive. Careful hyperparameter configuration and detailed re-examination may help to achieve optimized results.
  • I doubt that there are some bugs on ABCNNs(especially ABCNN-2 which has 2 conv layers) and will keep watching codes. Please be careful when using the results.

Specification

  • preprocess.py: preprocess (training, test) data and import word2vec to use.
  • train.py: train a model with configs.
  • test.py: test the trained model.
  • ABCNN.py: Implementation of ABCNN models.
  • show.py: pyplot codes for test results.
  • utils.py: common util functions.
  • MSRP_Corpus: MSRP corpus for PI.
  • WikiQA_Corpus: WikiQA corpus for AS.
  • models: saved models available on Tensorflow.
  • experiments: test results on AS tasks.

Development Environment

  • OS: Windows 10 (64 bit)
  • Language: Python 3.5.3
  • CPU: Intel Xeon CPU E3-1231 v3 3.4 GHz
  • RAM: 16GB
  • GPU support: GTX 970
  • Libraries:
    • tensorflow 1.2.1
    • numpy 1.12.1
    • gensim 1.0.1
    • NLTK 3.2.2
    • scikit-learn 0.18.1
    • matplotlib 2.0.0

Requirements

This model is based on pre-trained Word2vec(GoogleNews-vectors-negative300.bin) by T.Mikolov et al.
You should download this file and place it in the root folder.

Execution

(training): python train.py --lr=0.08 --ws=4 --l2_reg=0.0004 --epoch=20 --batch_size=64 --model_type=BCNN --num_layers=2 --data_type=WikiQA

Paramters
--lr: learning rate
--ws: window_size
--l2_reg: l2_reg modifier
--epoch: epoch
--batch_size: batch size
--model_type: model type
--num_layers: number of convolution layers
--data_type: MSRP or WikiQA data

(test): python test.py --ws=4 --l2_reg=0.0004 --epoch=20 --max_len=40 --model_type=BCNN --num_layers=2 --data_type=WikiQA --classifier=LR

Paramters
--ws: window_size
--l2_reg: l2_reg modifier
--epoch: epoch
--max_len: max sentence length
--model_type: model type
--num_layers: number of convolution layers
--data_type: MSRP or WikiQA data
--classifier: Final layout classifier(model, LR, SVM)

MISC.

abcnn's People

Contributors

galsang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

abcnn's Issues

output module LR/SVM

Thank you for this awesome repo.
This is not actually a code issue, I'm just curious to ask. Do you have any idea why do we need an extra linear model or SVM for the prediction? I mean this module doesn't go through the backpropagation at all.
Or do you find some improvements using this LR or SVM compared with its fully connected output layer?

Thanks

cost:nan

I used my own data to train, and cost nan occured. I checked the data, clipped the gradient, and reduced the learning rate, it still occured at the same 'batch_size*batch' location. Do I have anything else to check or change to make it run normally? Thanks for your any suggestion.

the nan error looks like follows:
[batch 1044] cost: 2.06923
[batch 1045] cost: 1.79236
[batch 1046] cost: 1.9501
[batch 1047] cost: 1.86483
[batch 1048] cost: nan
[batch 1049] cost: nan

tf.train.Saver() error in test.py

python test.py --ws=4 --l2_reg=0.0004 --epoch=20 --max_len=40 --model_type=ABCNN3 --num_layers=2 --data_type=WebQA --classifier=LR

List of Variables:
CNN-1/aW:0
CNN-1/conv/weights:0
CNN-1/conv/biases:0
CNN-2/aW:0
CNN-2/conv/weights:0
CNN-2/conv/biases:0
output-layer/FC/weights:0
output-layer/FC/biases:0

==================================================
test data size: 10
2018-04-08 12:11:47.494259: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2018-04-08 12:11:47.494336: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2018-04-08 12:11:47.494347: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2018-04-08 12:11:47.494355: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2018-04-08 12:11:47.494364: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 1139, in _do_call
return fn(*args)
File "/usr/local/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 1121, in _run_fn
status, run_metadata)
File "/usr/local/lib/python3.4/contextlib.py", line 66, in exit
next(self.gen)
File "/usr/local/lib/python3.4/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [40,300] rhs shape= [96,300]
[[Node: save/Assign = Assign[T=DT_FLOAT, _class=["loc:@CNN-1/aW"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/cpu:0"](CNN-1/aW, save/RestoreV2)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "test.py", line 147, in
classifier=params["classifier"], word2vec=params["word2vec"])
File "test.py", line 39, in test
saver.restore(sess, model_path + "-" + str(e))
File "/usr/local/lib/python3.4/site-packages/tensorflow/python/training/saver.py", line 1548, in restore
{self.saver_def.filename_tensor_name: save_path})
File "/usr/local/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 789, in run
run_metadata_ptr)
File "/usr/local/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 997, in _run
feed_dict_string, options, run_metadata)
File "/usr/local/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 1132, in _do_run
target_list, options, run_metadata)
File "/usr/local/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 1152, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [40,300] rhs shape= [96,300]
[[Node: save/Assign = Assign[T=DT_FLOAT, _class=["loc:@CNN-1/aW"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/cpu:0"](CNN-1/aW, save/RestoreV2)]]

Caused by op 'save/Assign', defined at:
File "test.py", line 147, in
classifier=params["classifier"], word2vec=params["word2vec"])
File "test.py", line 38, in test
saver = tf.train.Saver()
File "/usr/local/lib/python3.4/site-packages/tensorflow/python/training/saver.py", line 1139, in init
self.build()
File "/usr/local/lib/python3.4/site-packages/tensorflow/python/training/saver.py", line 1170, in build
restore_sequentially=self._restore_sequentially)
File "/usr/local/lib/python3.4/site-packages/tensorflow/python/training/saver.py", line 691, in build
restore_sequentially, reshape)
File "/usr/local/lib/python3.4/site-packages/tensorflow/python/training/saver.py", line 419, in _AddRestoreOps
assign_ops.append(saveable.restore(tensors, shapes))
File "/usr/local/lib/python3.4/site-packages/tensorflow/python/training/saver.py", line 155, in restore
self.op.get_shape().is_fully_defined())
File "/usr/local/lib/python3.4/site-packages/tensorflow/python/ops/state_ops.py", line 271, in assign
validate_shape=validate_shape)
File "/usr/local/lib/python3.4/site-packages/tensorflow/python/ops/gen_state_ops.py", line 45, in assign
use_locking=use_locking, name=name)
File "/usr/local/lib/python3.4/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/usr/local/lib/python3.4/site-packages/tensorflow/python/framework/ops.py", line 2506, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/usr/local/lib/python3.4/site-packages/tensorflow/python/framework/ops.py", line 1269, in init
self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): Assign requires shapes of both tensors to match. lhs shape= [40,300] rhs shape= [96,300]
[[Node: save/Assign = Assign[T=DT_FLOAT, _class=["loc:@CNN-1/aW"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/cpu:0"](CNN-1/aW, save/RestoreV2)]]

MAP and MRR equals 0

Hi galsang, thank you for sharing code. I run your codes following the README. The cost tends to reduce like this:
[Epoch 4]
('[batch 100] cost:', 0.40089718)
('[batch 200] cost:', 0.27272141)
('[batch 300] cost:', 0.16169518)
('model saved as', './models/WikiQA-BCNN-2-4')
('LR saved as', './models/WikiQA-BCNN-2-4-LR.pkl')
('SVM saved as', './models/WikiQA-BCNN-2-4-SVM.pkl')

However, when I test, MAP and MRR equal 0. Moreover, the result file is empty. Do you know how to solve this problems or what need I to focus when I run the codes. Thank you very much!

('[Epoch 1] MAP:', 0, '/ MRR:', 0)
('./models/WikiQA-BCNN-2-2', 'restored.')
('./models/WikiQA-BCNN-2-2-LR.pkl', 'restored.')
('[Epoch 2] MAP:', 0, '/ MRR:', 0)
('./models/WikiQA-BCNN-2-3', 'restored.')
('./models/WikiQA-BCNN-2-3-LR.pkl', 'restored.')
('[Epoch 3] MAP:', 0, '/ MRR:', 0)
('./models/WikiQA-BCNN-2-4', 'restored.')
('./models/WikiQA-BCNN-2-4-LR.pkl', 'restored.')

Variable CNN-1/conv/weights does not exist

ValueError: Variable CNN-1/conv/weights does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope? Anyone has the same issue?

Traceback (most recent call last):
File "train.py", line 129, in
data_type=params["data_type"], word2vec=params["word2vec"])
File "train.py", line 26, in train
num_features=train_data.num_features, num_classes=num_classes, num_layers=num_layers)
File "/Users/yuanling/Downloads/ABCNN-master/ABCNN.py", line 176, in init
LI_1, LO_1, RI_1, RO_1 = CNN_layer(variable_scope="CNN-1", x1=x1_expanded, x2=x2_expanded, d=d0)
File "/Users/yuanling/Downloads/ABCNN-master/ABCNN.py", line 152, in CNN_layer
left_conv = convolution(name_scope="left", x=pad_for_wide_conv(x1), d=d)
File "/Users/yuanling/Downloads/ABCNN-master/ABCNN.py", line 64, in convolution
scope=scope
File "/Applications/anaconda/lib/python2.7/site-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 181, in func_with_args
return func(*args, **current_args)
File "/Applications/anaconda/lib/python2.7/site-packages/tensorflow/contrib/layers/python/layers/layers.py", line 918, in convolution
outputs = layer.apply(inputs)
File "/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 320, in apply
return self.call(inputs, **kwargs)
File "/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 286, in call
self.build(input_shapes[0])
File "/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/layers/convolutional.py", line 138, in build
dtype=self.dtype)
File "/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 1049, in get_variable
use_resource=use_resource, custom_getter=custom_getter)
File "/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 948, in get_variable
use_resource=use_resource, custom_getter=custom_getter)
File "/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 349, in get_variable
validate_shape=validate_shape, use_resource=use_resource)
File "/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 1389, in wrapped_custom_getter
*args, **kwargs)
File "/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 275, in variable_getter
variable_getter=functools.partial(getter, **kwargs))
File "/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 228, in _add_variable
trainable=trainable and self.trainable)
File "/Applications/anaconda/lib/python2.7/site-packages/tensorflow/contrib/layers/python/layers/layers.py", line 1334, in layer_variable_getter
return _model_variable_getter(getter, *args, **kwargs)
File "/Applications/anaconda/lib/python2.7/site-packages/tensorflow/contrib/layers/python/layers/layers.py", line 1326, in _model_variable_getter
custom_getter=getter, use_resource=use_resource)
File "/Applications/anaconda/lib/python2.7/site-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 181, in func_with_args
return func(*args, **current_args)
File "/Applications/anaconda/lib/python2.7/site-packages/tensorflow/contrib/framework/python/ops/variables.py", line 262, in model_variable
use_resource=use_resource)
File "/Applications/anaconda/lib/python2.7/site-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 181, in func_with_args
return func(*args, **current_args)
File "/Applications/anaconda/lib/python2.7/site-packages/tensorflow/contrib/framework/python/ops/variables.py", line 217, in variable
use_resource=use_resource)
File "/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 341, in _true_getter
use_resource=use_resource)
File "/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 671, in _get_single_variable
"VarScope?" % name)
ValueError: Variable CNN-1/conv/weights does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?

Micro law

0 2182281 2182333 United Nations inspectors have discovered traces of highly enriched uranium near an Iranian nuclear facility, heightening worries that the country may have a secret nuclear weapons program. NITED NATIONS, Aug. 26 International inspectors have found traces of highly enriched uranium at an Iranian facility, according to a new confidential report distributed today.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.