Skip to content
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
104 changes: 104 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
.static_storage/
.media/
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
92 changes: 46 additions & 46 deletions program/BasicModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from Encoding import encoding
import argparse
from keras.preprocessing import sequence
from keras.models import Sequential, Graph, Model
from keras.layers import Input, merge, Merge, Dense, TimeDistributedDense, Dropout, Activation, RepeatVector, Permute, Reshape, RepeatVector, Flatten
from keras.models import Sequential, Model
from keras.layers import Input, merge, Dense, Dropout, Activation, RepeatVector, Permute, Reshape, RepeatVector, Flatten, concatenate
from keras.layers.convolutional import Convolution1D, MaxPooling1D, AveragePooling1D
from keras.layers.embeddings import Embedding
from keras.layers.recurrent import SimpleRNN, GRU, LSTM
Expand Down Expand Up @@ -176,18 +176,18 @@ def build( self ):
forward = GRU(self.hidden_size, return_sequences=True, init=self.init_type, activation=self.activation)(current)
backward = GRU(self.hidden_size, return_sequences=True, init=self.init_type, activation=self.activation, go_backwards=True)(current)
elif 'lstm' in self.arch:
forward = LSTM(self.hidden_size, return_sequences=True, init=self.init_type, activation=self.activation)(current)
backward = LSTM(self.hidden_size, return_sequences=True, init=self.init_type, activation=self.activation, go_backwards=True)(current)
forward = LSTM(self.hidden_size, return_sequences=True, kernel_initializer=self.init_type, activation=self.activation)(current)
backward = LSTM(self.hidden_size, return_sequences=True, kernel_initializer=self.init_type, activation=self.activation, go_backwards=True)(current)
if 'b' in self.arch:
tagger = merge([forward, backward], mode='concat')
tagger = concatenate([forward, backward])
else:
tagger = forward
if self.dropout:
tagger = Dropout(self.dropout_ratio)(tagger)
prediction = TimeDistributed(Dense(self.output_vocab_size, activation='softmax'))(tagger)
prediction = TimeDistributed(Dense(self.output_vocab_size, activation='softmax'))(tagger)

self.model = Model(input=raw_current, output=prediction)
self.model.compile(loss='categorical_crossentropy', optimizer=opt_func)
self.model = Model(input=raw_current, output=prediction)
self.model.compile(loss='categorical_crossentropy', optimizer=opt_func)

# 2-Stacked Layered RNN (LSTM, SimpleRNN, GRU)
elif self.arch == '2lstm' or self.arch == '2rnn' or self.arch == '2gru':
Expand Down Expand Up @@ -242,14 +242,14 @@ def build( self ):
encoder = fencoder
labeling = flabeling
#intent = Dense(self.output_vocab_size, activation='softmax')(encoder)
encoder = RepeatVector(self.time_length)(encoder)
tagger = merge([encoder, labeling], mode='concat', concat_axis=-1)
encoder = RepeatVector(self.time_length)(encoder)
tagger = merge([encoder, labeling], mode='concat', concat_axis=-1)
if self.dropout:
tagger = Dropout(self.dropout_ratio)(tagger)
prediction = TimeDistributed(Dense(self.output_vocab_size, activation='softmax'))(tagger)
prediction = TimeDistributed(Dense(self.output_vocab_size, activation='softmax'))(tagger)

self.model = Model(input=raw_current, output=prediction)
self.model.compile(loss='categorical_crossentropy', optimizer=opt_func)
self.model = Model(input=raw_current, output=prediction)
self.model.compile(loss='categorical_crossentropy', optimizer=opt_func)

# Encode intent information by feeding all words and then start tagging
elif self.arch == 'i-c-rnn' or self.arch == 'i-c-gru' or self.arch == 'i-c-lstm' or self.arch == 'i-c-brnn' or self.arch == 'i-c-bgru' or self.arch == 'i-c-blstm':
Expand All @@ -275,14 +275,14 @@ def build( self ):
else:
labeling = forward
#intent = Dense(self.output_vocab_size, activation='softmax')(encoder)
encoder = RepeatVector(self.time_length)(encoder)
tagger = merge([encoder, labeling], mode='concat', concat_axis=-1)
encoder = RepeatVector(self.time_length)(encoder)
tagger = merge([encoder, labeling], mode='concat', concat_axis=-1)
if self.dropout:
tagger = Dropout(self.dropout_ratio)(tagger)
prediction = TimeDistributed(Dense(self.output_vocab_size, activation='softmax'))(tagger)
prediction = TimeDistributed(Dense(self.output_vocab_size, activation='softmax'))(tagger)

self.model = Model(input=raw_current, output=prediction)
self.model.compile(loss='categorical_crossentropy', optimizer=opt_func)
self.model = Model(input=raw_current, output=prediction)
self.model.compile(loss='categorical_crossentropy', optimizer=opt_func)


# Encode all history and the current utterance first and then start tagging
Expand Down Expand Up @@ -317,14 +317,14 @@ def build( self ):
encoder = fencoder
labeling = flabeling
#intent = Dense(self.output_vocab_size, activation='softmax')(encoder)
encoder = RepeatVector(self.time_length)(encoder)
tagger = merge([encoder, labeling], mode='concat', concat_axis=-1)
encoder = RepeatVector(self.time_length)(encoder)
tagger = merge([encoder, labeling], mode='concat', concat_axis=-1)
if self.dropout:
tagger = Dropout(self.dropout_ratio)(tagger)
prediction = TimeDistributed(Dense(self.output_vocab_size, activation='softmax'))(tagger)
prediction = TimeDistributed(Dense(self.output_vocab_size, activation='softmax'))(tagger)

self.model = Model(input=[raw_his, raw_cur], output=prediction)
self.model.compile(loss='categorical_crossentropy', optimizer=opt_func)
self.model = Model(input=[raw_his, raw_cur], output=prediction)
self.model.compile(loss='categorical_crossentropy', optimizer=opt_func)

# Encode all history and the current utterance first and then start tagging
elif self.arch == 'hi-c-rnn' or self.arch == 'hi-c-gru' or self.arch == 'hi-c-lstm' or self.arch == 'hi-c-brnn' or self.arch == 'hi-c-bgru' or self.arch == 'hi-c-blstm':
Expand Down Expand Up @@ -353,14 +353,14 @@ def build( self ):
encoder = MaxPooling1D(self.time_length)(encoder)
encoder = Flatten()(encoder)
#intent = Dense(self.output_vocab_size, activation='softmax')(encoder)
encoder = RepeatVector(self.time_length)(encoder)
tagger = merge([encoder, labeling], mode='concat', concat_axis=-1)
encoder = RepeatVector(self.time_length)(encoder)
tagger = merge([encoder, labeling], mode='concat', concat_axis=-1)
if self.dropout:
tagger = Dropout(self.dropout_ratio)(tagger)
prediction = TimeDistributed(Dense(self.output_vocab_size, activation='softmax'))(tagger)
prediction = TimeDistributed(Dense(self.output_vocab_size, activation='softmax'))(tagger)

self.model = Model(input=[raw_his, raw_cur], output=prediction)
self.model.compile(loss='categorical_crossentropy', optimizer=opt_func)
self.model = Model(input=[raw_his, raw_cur], output=prediction)
self.model.compile(loss='categorical_crossentropy', optimizer=opt_func)

elif 'amemn2n' in self.arch:
# current: (, time_length, embedding_size)
Expand Down Expand Up @@ -434,7 +434,7 @@ def build( self ):
else:
raw_input_memory = Input(shape=(self.his_length * self.time_length, self.embedding_size), name='input_memory')
input_memory = Reshape((self.his_length, self.time_length, self.embedding_size))(raw_input_memory)
mem_vec = TimeDistributed(sent_model)(input_memory)
mem_vec = TimeDistributed(sent_model)(input_memory)

# compute the similarity between sentence embeddings for attention
match = merge([mem_vec, cur_vec], mode='dot', dot_axes=[2, 1])
Expand All @@ -452,18 +452,18 @@ def build( self ):
backward = LSTM(self.hidden_size, return_sequences=False, init=self.init_type, activation=self.activation, go_backwards=True)(current)
labeling = merge([forward, backward], mode='concat', concat_axis=-1)
elif 'rnn' in self.arch:
labeling = SimpleRNN(self.hidden_size, return_sequences=False, init=self.init_type, activation=self.activation)(current)
labeling = SimpleRNN(self.hidden_size, return_sequences=False, init=self.init_type, activation=self.activation)(current)
elif 'gru' in self.arch:
labeling = GRU(self.hidden_size, return_sequences=False, init=self.init_type, activation=self.activation)(current)
labeling = GRU(self.hidden_size, return_sequences=False, init=self.init_type, activation=self.activation)(current)
elif 'lstm' in self.arch:
labeling = LSTM(self.hidden_size, return_sequences=False, init=self.init_type, activation=self.activation)(current)
tagger = merge([encoder, labeling], mode='concat', concat_axis=-1)
labeling = LSTM(self.hidden_size, return_sequences=False, init=self.init_type, activation=self.activation)(current)
tagger = merge([encoder, labeling], mode='concat', concat_axis=-1)
if self.dropout:
tagger = Dropout(self.dropout_ratio)(tagger)
prediction = Dense(self.output_vocab_size, activation='softmax')(tagger)
prediction = Dense(self.output_vocab_size, activation='softmax')(tagger)

self.model = Model(input=[raw_input_memory, raw_current], output=prediction)
self.model.compile(loss='categorical_crossentropy', optimizer=opt_func)
self.model = Model(input=[raw_input_memory, raw_current], output=prediction)
self.model.compile(loss='categorical_crossentropy', optimizer=opt_func)

elif 'memn2n' in self.arch:
# current: (, time_length, embedding_size)
Expand Down Expand Up @@ -537,7 +537,7 @@ def build( self ):
else:
raw_input_memory = Input(shape=(self.his_length * self.time_length, self.embedding_size), name='input_memory')
input_memory = Reshape((self.his_length, self.time_length, self.embedding_size))(raw_input_memory)
mem_vec = TimeDistributed(sent_model)(input_memory)
mem_vec = TimeDistributed(sent_model)(input_memory)

# compute the similarity between sentence embeddings for attention
match = merge([mem_vec, cur_vec], mode='dot', dot_axes=[2, 1])
Expand All @@ -548,31 +548,31 @@ def build( self ):
his_vec = merge([mem_vec, match], mode='dot', dot_axes=[1, 1])
encoder = merge([his_vec, cur_vec], mode='sum')
encoder = Dense(self.embedding_size)(encoder)
encoder = RepeatVector(self.time_length)(encoder)
encoder = RepeatVector(self.time_length)(encoder)

# tagging the words in the current sentence
if 'blstm' in self.arch:
forward = LSTM(self.hidden_size, return_sequences=True, init=self.init_type, activation=self.activation)(current)
backward = LSTM(self.hidden_size, return_sequences=True, init=self.init_type, activation=self.activation, go_backwards=True)(current)
labeling = merge([forward, backward], mode='concat', concat_axis=-1)
elif 'rnn' in self.arch:
labeling = SimpleRNN(self.hidden_size, return_sequences=True, init=self.init_type, activation=self.activation)(current)
labeling = SimpleRNN(self.hidden_size, return_sequences=True, init=self.init_type, activation=self.activation)(current)
elif 'gru' in self.arch:
labeling = GRU(self.hidden_size, return_sequences=True, init=self.init_type, activation=self.activation)(current)
labeling = GRU(self.hidden_size, return_sequences=True, init=self.init_type, activation=self.activation)(current)
elif 'lstm' in self.arch:
labeling = LSTM(self.hidden_size, return_sequences=True, init=self.init_type, activation=self.activation)(current)
tagger = merge([encoder, labeling], mode='concat', concat_axis=-1)
labeling = LSTM(self.hidden_size, return_sequences=True, init=self.init_type, activation=self.activation)(current)
tagger = merge([encoder, labeling], mode='concat', concat_axis=-1)
if self.dropout:
tagger = Dropout(self.dropout_ratio)(tagger)
prediction = TimeDistributed(Dense(self.output_vocab_size, activation='softmax'))(tagger)
prediction = TimeDistributed(Dense(self.output_vocab_size, activation='softmax'))(tagger)

self.model = Model(input=[raw_input_memory, raw_current], output=prediction)
self.model.compile(loss='categorical_crossentropy', optimizer=opt_func)
self.model = Model(input=[raw_input_memory, raw_current], output=prediction)
self.model.compile(loss='categorical_crossentropy', optimizer=opt_func)


def train(self, H_train, X_train, y_train, H_dev, X_dev, y_dev, val_ratio=0.0):
# load saved model weights
if self.load_weight is not None:
if self.load_weight is not None:
sys.stderr.write("Load the pretrained weights for the model.\n")
self.model.load_weights(self.load_weight)
else:
Expand Down
Binary file removed program/BasicModel.pyc
Binary file not shown.
Binary file removed program/History.pyc
Binary file not shown.
Binary file removed program/PredefinedEmbedding.pyc
Binary file not shown.
Binary file removed program/wordSlotDataSet.pyc
Binary file not shown.