Skip to content

Instantly share code, notes, and snippets.

@ameasure
Created March 1, 2016 13:36
Show Gist options
  • Save ameasure/25403119908e52869716 to your computer and use it in GitHub Desktop.
Save ameasure/25403119908e52869716 to your computer and use it in GitHub Desktop.
import datetime
import pandas as pd
import numpy as np
np.random.seed(42)
from sklearn.preprocessing import LabelEncoder
import theano
import keras
from keras.models import Sequential, Graph
from keras.layers.convolutional import Convolution1D, MaxPooling1D, Convolution2D, MaxPooling2D
from keras.layers.core import Dense, Dropout, Activation, Flatten, Reshape, Merge
from keras.layers.recurrent import LSTM
from keras.layers.normalization import BatchNormalization
from keras.layers.embeddings import Embedding
from keras.optimizers import SGD
from keras.utils import np_utils
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.regularizers import l2
from embed_preprocessing import get_initial_embeddings
from msha_extractor import get_data
max_len = 100
embedding_size = 300
batch_size = 128
code_type = 'ACTIVITY_CD'
raw_train, raw_valid, raw_test = get_data(n_train=50000, n_valid=0, n_test=10000)
raw_train_labels = raw_train[code_type]
raw_valid_labels = raw_valid[code_type]
raw_test_labels = raw_test[code_type]
labeler = LabelEncoder()
labels = set(raw_train[code_type].tolist() + raw_test[code_type].tolist())
labeler.fit(list(labels))
nb_classes = len(set(labels))
print('nb_classes = %s' % nb_classes)
y_train = labeler.transform(raw_train_labels)
Y_train = np_utils.to_categorical(y_train, nb_classes)
y_valid = labeler.transform(raw_valid_labels)
Y_valid = np_utils.to_categorical(y_valid, nb_classes)
y_test = labeler.transform(raw_test_labels)
Y_test = np_utils.to_categorical(y_test, nb_classes)
print 'Tokenizing X_train'
tokenizer = Tokenizer()
tokenizer.fit_on_texts(raw_train['NARRATIVE'])
tokenizer.word_index['BLANK_EMBEDDING'] = 0
X_train = tokenizer.texts_to_sequences(raw_train['NARRATIVE'])
X_valid = tokenizer.texts_to_sequences(raw_valid['NARRATIVE'])
X_test = tokenizer.texts_to_sequences(raw_test['NARRATIVE'])
X_train = pad_sequences(X_train, maxlen=max_len)
X_valid = pad_sequences(X_valid, maxlen=max_len)
X_test = pad_sequences(X_test, maxlen=max_len)
print('X_train shape:', X_train.shape)
print('X_valid shape:', X_valid.shape)
print('X_test shape:', X_test.shape)
initial_embeddings = get_initial_embeddings(tokenizer.word_index)
vocab_size = len(tokenizer.word_index)
filter_length = 3
nb_filter = 300
model = Sequential()
model.add(Embedding(vocab_size, embedding_size, input_length=max_len, weights=[initial_embeddings]))
#model.add(Convolution1D(nb_filter=nb_filter,
# filter_length=filter_length,
# border_mode='valid',
# activation='relu',
# subsample_length=1))
model.add(LSTM(150, dropout_W=0.5, dropout_U=0.5, return_sequences=True))
model.add(MaxPooling1D(pool_length=3))
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(output_dim=nb_classes))
model.add(Activation('sigmoid'))
model.compile(loss='categorical_crossentropy',
optimizer='adam')
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=5,
verbose=True, show_accuracy=True, validation_split=.05)
score = model.evaluate(X_test, Y_test, batch_size=batch_size, show_accuracy=True)
print('Test score:', score[0])
print('Test accuracy:', score[1])
@ameasure
Copy link
Author

ameasure commented Mar 1, 2016

The error message, edited so it fits in here is:

Using gpu device 0: GeForce GTX 660 (CNMeM is disabled, CuDNN not available)
Using Theano backend.
nb_classes = 96
Tokenizing X_train
('X_train shape:', (50000L, 100L))
('X_valid shape:', (0L, 100L))
('X_test shape:', (10000L, 100L))
4543 words received random embeddings because previously unseen

Thousands of lines of the same C code repeated again and again

mod.cu
mod.cu(9723) : fatal error C1061: compiler limit : blocks nested too deeply

['nvcc', '-shared', '-O3', '-use_fast_math', '-arch=sm_30', '-Xlinker', '/DEBUG', '-D HAVE_ROUND', '-m64', '-Xcompiler', '-DCUDA_NDARRAY_CUH=18715462c72ed6afcd7ca5d52813ce90,-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION,/Zi,/MD', '-IC:\Users\ameasure\AppData\Local\Theano\compiledir_Windows-7-6.1.7601-SP1-Intel64_Family_6_Model_58_Stepping_9_GenuineIntel-2.7.9-64\cuda_ndarray', '-IC:\Users\ameasure\Anaconda\lib\site-packages\numpy\core\include', '-IC:\Users\ameasure\Anaconda\include', '-IC:\Users\ameasure\Anaconda\lib\site-packages\theano\gof', '-IC:\Users\ameasure\Anaconda\lib\site-packages\theano\sandbox\cuda', '-o', 'C:\Users\ameasure\AppData\Local\Theano\compiledir_Windows-7-6.1.7601-SP1-Intel64_Family_6_Model_58_Stepping_9_GenuineIntel-2.7.9-64\tmpdhx2d1\3f2660fbe493f47f2f69a326682253d6.pyd', 'mod.cu', '-LC:\Users\ameasure\AppData\Local\Theano\compiledir_Windows-7-6.1.7601-SP1-Intel64_Family_6_Model_58_Stepping_9_GenuineIntel-2.7.9-64\cuda_ndarray', '-LC:\Users\ameasure\Anaconda\libs', '-LC:\Users\ameasure\Anaconda', '-lcudart', '-lcublas', '-lcuda_ndarray', '-lpython27']

Thousands more lines of the same C code repeated again and again

Traceback (most recent call last):

File "", line 1, in
runfile('C:/Users/ameasure/Desktop/Programming Projects/cnn/cnn_lstm.py', wdir='C:/Users/ameasure/Desktop/Programming Projects/cnn')

File "C:\Users\ameasure\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
execfile(filename, namespace)

File "C:\Users\ameasure\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)

File "C:/Users/ameasure/Desktop/Programming Projects/cnn/cnn_lstm.py", line 93, in
optimizer='adam')

File "C:\Users\ameasure\Anaconda\lib\site-packages\keras\models.py", line 547, in compile
self._train = K.function(train_ins, [train_loss], updates=updates)

File "C:\Users\ameasure\Anaconda\lib\site-packages\keras\backend\theano_backend.py", line 452, in function
return Function(inputs, outputs, updates=updates)

File "C:\Users\ameasure\Anaconda\lib\site-packages\keras\backend\theano_backend.py", line 444, in init
allow_input_downcast=True, **kwargs)

File "C:\Users\ameasure\Anaconda\lib\site-packages\theano\compile\function.py", line 320, in function
output_keys=output_keys)

File "C:\Users\ameasure\Anaconda\lib\site-packages\theano\compile\pfunc.py", line 479, in pfunc
output_keys=output_keys)

File "C:\Users\ameasure\Anaconda\lib\site-packages\theano\compile\function_module.py", line 1777, in orig_function
defaults)

File "C:\Users\ameasure\Anaconda\lib\site-packages\theano\compile\function_module.py", line 1641, in create
input_storage=input_storage_lists, storage_map=storage_map)

File "C:\Users\ameasure\Anaconda\lib\site-packages\theano\gof\link.py", line 690, in make_thunk
storage_map=storage_map)[:3]

File "C:\Users\ameasure\Anaconda\lib\site-packages\theano\gof\vm.py", line 1003, in make_all
no_recycling))

File "C:\Users\ameasure\Anaconda\lib\site-packages\theano\sandbox\cuda__init__.py", line 257, in make_thunk
compute_map, no_recycling)

File "C:\Users\ameasure\Anaconda\lib\site-packages\theano\gof\op.py", line 970, in make_thunk
no_recycling)

File "C:\Users\ameasure\Anaconda\lib\site-packages\theano\gof\op.py", line 879, in make_c_thunk
output_storage=node_output_storage)

File "C:\Users\ameasure\Anaconda\lib\site-packages\theano\gof\cc.py", line 1200, in make_thunk
keep_lock=keep_lock)

File "C:\Users\ameasure\Anaconda\lib\site-packages\theano\gof\cc.py", line 1143, in compile
keep_lock=keep_lock)

File "C:\Users\ameasure\Anaconda\lib\site-packages\theano\gof\cc.py", line 1595, in cthunk_factory
key=key, lnk=self, keep_lock=keep_lock)

File "C:\Users\ameasure\Anaconda\lib\site-packages\theano\gof\cmodule.py", line 1142, in module_from_key
module = lnk.compile_cmodule(location)

File "C:\Users\ameasure\Anaconda\lib\site-packages\theano\gof\cc.py", line 1506, in compile_cmodule
preargs=preargs)

File "C:\Users\ameasure\Anaconda\lib\site-packages\theano\sandbox\cuda\nvcc_compiler.py", line 396, in compile_str
'for cmd', ' '.join(cmd))

Exception: ('The following error happened while compiling the node', GpuJoin(TensorConstant{1}, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0, GpuAlloc.0), '\n', 'nvcc return status', 2, 'for cmd', 'nvcc -shared -O3 -use_fast_math -arch=sm_30 -Xlinker /DEBUG -D HAVE_ROUND -m64 -Xcompiler -DCUDA_NDARRAY_CUH=18715462c72ed6afcd7ca5d52813ce90,-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION,/Zi,/MD -IC:\Users\ameasure\AppData\Local\Theano\compiledir_Windows-7-6.1.7601-SP1-Intel64_Family_6_Model_58_Stepping_9_GenuineIntel-2.7.9-64\cuda_ndarray -IC:\Users\ameasure\Anaconda\lib\site-packages\numpy\core\include -IC:\Users\ameasure\Anaconda\include -IC:\Users\ameasure\Anaconda\lib\site-packages\theano\gof -IC:\Users\ameasure\Anaconda\lib\site-packages\theano\sandbox\cuda -o C:\Users\ameasure\AppData\Local\Theano\compiledir_Windows-7-6.1.7601-SP1-Intel64_Family_6_Model_58_Stepping_9_GenuineIntel-2.7.9-64\tmpdhx2d1\3f2660fbe493f47f2f69a326682253d6.pyd mod.cu -LC:\Users\ameasure\AppData\Local\Theano\compiledir_Windows-7-6.1.7601-SP1-Intel64_Family_6_Model_58_Stepping_9_GenuineIntel-2.7.9-64\cuda_ndarray -LC:\Users\ameasure\Anaconda\libs -LC:\Users\ameasure\Anaconda -lcudart -lcublas -lcuda_ndarray -lpython27', '[GpuJoin(TensorConstant{1}, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>, <CudaNdarrayType(float32, col)>)]')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment