Skip to content

Instantly share code, notes, and snippets.

@ymnliu
ymnliu / run_cnn.sh
Last active October 24, 2019 21:47
# activate the conda environment to use
conda activate tutorial
# go to the work dir, or do "git clone" to download the dir from scratch.
cd AMIA19_W22_large_scale_nlp/scripts/
# run script
python cnn_separate_sense.py
########################################
MS
########################################
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_1 (Embedding) (None, 100, 50) 268350
_________________________________________________________________
dropout_1 (Dropout) (None, 100, 50) 0
_________________________________________________________________
########################################
MS
########################################
/Users/m142167/anaconda3/lib/python3.7/site-packages/sklearn/metrics/classification.py:1437: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples.
'precision', 'predicted', average, warn_for)
precision recall f1-score support
UNSURED SENSE 0.00 0.00 0.00 1
morphine sulfate 0.97 0.97 0.97 30
multiple sclerosis 0.85 0.94 0.89 18
musculoskeletal 0.00 0.00 0.00 1
@ymnliu
ymnliu / AttentionWithContext.py
Created October 11, 2017 16:01 — forked from cbaziotis/AttentionWithContext.py
Keras Layer that implements an Attention mechanism, with a context/query vector, for temporal data. Supports Masking. Follows the work of Yang et al. [https://www.cs.cmu.edu/~diyiy/docs/naacl16.pdf] "Hierarchical Attention Networks for Document Classification"
def dot_product(x, kernel):
"""
Wrapper for dot product operation, in order to be compatible with both
Theano and Tensorflow
Args:
x (): input
kernel (): weights
Returns:
"""
if K.backend() == 'tensorflow':
@ymnliu
ymnliu / keras_bidirectional_tagger.py
Created August 3, 2017 16:39 — forked from dirko/keras_bidirectional_tagger.py
Keras bidirectional LSTM NER tagger
# Keras==1.0.6
from keras.models import Sequential
import numpy as np
from keras.layers.recurrent import LSTM
from keras.layers.core import TimeDistributedDense, Activation
from keras.preprocessing.sequence import pad_sequences
from keras.layers.embeddings import Embedding
from sklearn.cross_validation import train_test_split
from keras.layers import Merge
from keras.backend import tf
@ymnliu
ymnliu / lambdawithmask.py
Created June 21, 2017 14:56 — forked from braingineer/lambdawithmask.py
keras lambda layer supporting masking
class Lambda(Layer):
'''Used for evaluating an arbitrary Theano / TensorFlow expression
on the output of the previous layer.
# Examples
```python
# add a x -> x^2 layer
model.add(Lambda(lambda x: x ** 2))
```
# Keras==2.0.2
import numpy as np
from keras.models import Sequential
from keras.layers.recurrent import LSTM
from keras.layers.core import Activation, Dense
from keras.layers.wrappers import TimeDistributed
from keras.preprocessing.sequence import pad_sequences
from keras.layers.embeddings import Embedding
from sklearn.cross_validation import train_test_split
from sklearn.metrics import confusion_matrix, accuracy_score, precision_recall_fscore_support