-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* compositional models working for DistMult and ModelE
* supported composition functions: 'LSTM', 'GRU', 'RNN', 'BoW', 'BiLSTM', 'BiGRU', 'BiRNN'
- Loading branch information
1 parent
2e305aa
commit 4cc1faf
Showing
6 changed files
with
559 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import tensorflow as tf | ||
from models import * | ||
from comp_models import * | ||
from data.load_fb15k237 import split_relations | ||
|
||
def default_init(): | ||
return tf.random_normal_initializer(0.0, 0.1) | ||
|
||
|
||
def create_model(kb, size, batch_size, is_train=True, num_neg=200, learning_rate=1e-2, | ||
l2_lambda=0.0, is_batch_training=False, type="DistMult", | ||
observed_sets=["train_text"], composition=None, num_buckets= 10): | ||
''' | ||
Factory Method for all models | ||
:param type: any or combination of "ModelF", "DistMult", "ModelE", "ModelO", "ModelN" | ||
:param composition: "Tanh", "LSTM", "GRU", "BiTanh", "BiLSTM", "BiGRU", "BoW" or None | ||
:return: Model(s) of type "type" | ||
''' | ||
if not isinstance(type, list): | ||
if composition == "Tanh": | ||
composition = TanhRNNCompModel(kb, size, num_buckets, split_relations, batch_size/(num_neg+1), learning_rate) | ||
elif composition == "LSTM": | ||
composition = LSTMCompModel(kb, size, num_buckets, split_relations, batch_size/(num_neg+1), learning_rate) | ||
elif composition == "GRU": | ||
composition = GRUCompModel(kb, size, num_buckets, split_relations, batch_size/(num_neg+1), learning_rate) | ||
elif composition == "BiTanh": | ||
composition = BiTanhRNNCompModel(kb, size, num_buckets, split_relations, batch_size/(num_neg+1), learning_rate) | ||
elif composition == "BiLSTM": | ||
composition = BiLSTMCompModel(kb, size, num_buckets, split_relations, batch_size/(num_neg+1), learning_rate) | ||
elif composition == "BiGRU": | ||
composition = BiGRUCompModel(kb, size, num_buckets, split_relations, batch_size/(num_neg+1), learning_rate) | ||
elif composition == "BoW": | ||
composition = CompositionModel(kb, size, num_buckets, split_relations, batch_size/(num_neg+1), learning_rate) | ||
else: | ||
composition = None | ||
|
||
if type == "ModelF": | ||
return ModelF(kb, size, batch_size, is_train, num_neg, learning_rate, l2_lambda, is_batch_training) | ||
elif type == "DistMult": | ||
if composition: | ||
return CompDistMult(kb, size, batch_size, composition, is_train, num_neg, learning_rate) | ||
else: | ||
return DistMult(kb, size, batch_size, is_train, num_neg, learning_rate, l2_lambda, is_batch_training) | ||
elif type == "ModelE": | ||
if composition: | ||
return CompModelE(kb, size, batch_size, composition, is_train, num_neg, learning_rate) | ||
else: | ||
return ModelE(kb, size, batch_size, is_train, num_neg, learning_rate, l2_lambda, is_batch_training) | ||
elif type == "ModelO": | ||
return ModelO(kb, size, batch_size, is_train, num_neg, learning_rate, l2_lambda, is_batch_training, observed_sets) | ||
elif type == "ModelN": | ||
return ModelN(kb, size, batch_size, is_train, num_neg, learning_rate, l2_lambda, is_batch_training, observed_sets) | ||
else: | ||
raise NameError("There is no model with type %s. " | ||
"Possible values are 'ModelF', 'DistMult', 'ModelE', 'ModelO', 'ModelN'." % type) | ||
else: | ||
return CombinedModel(type, kb, size, batch_size, is_train, num_neg, | ||
learning_rate, l2_lambda, is_batch_training, composition) |
Oops, something went wrong.