Skip to content
Merged
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
1 change: 1 addition & 0 deletions config.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ network:
rnn_unit: 'lstm' # Can be 'gru' or 'lstm', default: 'lstm'
hidden_layers: 0
hidden_neurons: 300
subreddit_input_neurons: 10 #Probebly not the best default value
use_concat_input: false
pre_train_subreddit: false
# Regularisation configs:
Expand Down
1 change: 1 addition & 0 deletions definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
RNN_UNIT = "rnn_unit"
HIDDEN_NEURONS = 'hidden_neurons'
HIDDEN_LAYERS = 'hidden_layers'
SUB_INPUT_NEURONS = 'subreddit_input_neurons'
USE_CONCAT_INPUT = 'use_concat_input'
BATCH_SIZE = 'batch_size'
TRAINING_EPOCHS = 'training_epochs'
Expand Down
22 changes: 13 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,20 @@ def main():
args = parser.parse_args()

for conf in args.configs:
config_file = networkconfig[conf]
with tf.Session() as sess:
builder = ModelBuilder(config_file, sess)
try:
print("Starting config ", conf)
config_file = networkconfig[conf]
with tf.Session() as sess:
builder = ModelBuilder(config_file, sess)

network_model = builder.build()
if config_file[USE_PRETRAINED_NET]:
network_model.train(USE_PRETRAINED_NET)
network_model.train()
network_model.close_writers()
tf.reset_default_graph()
network_model = builder.build()
if config_file[USE_PRETRAINED_NET]:
network_model.train(USE_PRETRAINED_NET)
network_model.train()
network_model.close_writers()
tf.reset_default_graph()
except:
print("Config ", networkconfig[conf]["name"], "failed to complete")

if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def __init__(self, config, session):
self.dropout_prob = config[DROPOUT_PROB] # Only used for train op
self.hidden_layers = config[HIDDEN_LAYERS]
self.hidden_neurons = config[HIDDEN_NEURONS]
self.subreddit_input_neurons = config[SUB_INPUT_NEURONS]
self.is_trainable_matrix = config[TRAINABLE_MATRIX]
self.use_pretrained = config[USE_PRETRAINED]
self.use_constant_limit = config[USE_CONSTANT_LIMIT]
Expand Down
28 changes: 22 additions & 6 deletions model/model_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def add_input_layer(self):
name="input")
self._model.subreddit_input = \
tf.placeholder(tf.float64,
[None, 1],
[None, self._model.subreddit_count],
name="subreddit_input")
self._model.target = \
tf.placeholder(tf.float64,
Expand Down Expand Up @@ -94,7 +94,23 @@ def add_input_layer(self):
output = outputs[-1]
if self._model.use_concat_input:
# Add subreddit to end of input
output = tf.concat([output, self._model.subreddit_input], 1)
subreddit_weights = tf.Variable(tf.random_normal(
[self._model.subreddit_count,
self._model.subreddit_input_neurons],
stddev=0.35,
dtype=tf.float64),
name="sub_input_weights")

subreddit_bias = tf.Variable(tf.random_normal(
[self._model.subreddit_input_neurons],
stddev=0.35,
dtype=tf.float64),
name="sub_input_bias")

logit_subreddit = tf.add(
tf.matmul(self._model.subreddit_input, subreddit_weights),
subreddit_bias)
output = tf.concat([output, logit_subreddit], 1)

self._model.latest_layer = output

Expand All @@ -107,7 +123,9 @@ def add_layer(self, number_of_neurons):
self.added_layers = True
weights = tf.Variable(tf.random_normal(
[self._model.rnn_neurons +
(1 if self._model.use_concat_input else 0),
(self._model.subreddit_input_neurons
if self._model.use_concat_input
else 0),
number_of_neurons],
stddev=0.35,
dtype=tf.float64),
Expand All @@ -133,9 +151,7 @@ def add_layer(self, number_of_neurons):
logits, name="hidden_layer-" + str(self.number_of_layers))

if self._model.use_l2_loss:
self._model.l2_term = tf.add(
tf.add(self._model.l2_term, tf.nn.l2_loss(weights)),
tf.nn.l2_loss(bias))
self._model.l2_term = tf.add(self._model.l2_term, tf.nn.l2_loss(weights))
if self._model.use_dropout:
self._model.latest_layer = \
tf.nn.dropout(self._model.latest_layer,
Expand Down
15 changes: 9 additions & 6 deletions model/util/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ def next_train_batch(self, batch_size=None):
self.train_present += present
self.train_absent += absent

subreddit_vec = helper.subreddit_index(subreddit,
self.subreddit_dict)
subreddit_vec = helper.label_vector(subreddit,
self.subreddit_dict,
self.subreddit_count)

label_vec = helper.label_vector(label.split(), self.users_dict,
self.user_count)
Expand Down Expand Up @@ -198,8 +199,9 @@ def next_valid_batch(self, batch_size=None):
self.valid_present += pres
self.valid_absent += absent

subreddit_vec = helper.subreddit_index(subreddit,
self.subreddit_dict)
subreddit_vec = helper.label_vector(subreddit,
self.subreddit_dict,
self.subreddit_count)

label_vec = helper.label_vector(label.split(), self.users_dict,
self.user_count)
Expand Down Expand Up @@ -238,8 +240,9 @@ def next_test_batch(self, batch_size=None):
self.word_dict,
self.max_title_length)

subreddit_vec = helper.subreddit_index(subreddit,
self.subreddit_dict)
subreddit_vec = helper.label_vector(subreddit,
self.subreddit_dict,
self.subreddit_count)

label_vec = helper.label_vector(label.split(), self.users_dict,
self.user_count)
Expand Down
2 changes: 1 addition & 1 deletion model/util/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def log_samefile(config, f1_score_valid, f1_score_train, epoch_top, prec_valid,
raise FileNotFoundError('Can not write because no directory is created')

config_headers = [NET_TYPE, NET_NAME, VOC_SIZE, USER_COUNT, LEARN_RATE, EMBEDD_SIZE, MAX_TITLE_LENGTH, RNN_NEURONS, RNN_UNIT,
HIDDEN_NEURONS, HIDDEN_LAYERS, USE_CONCAT_INPUT, BATCH_SIZE, TRAINING_EPOCHS, USE_L2_LOSS, L2_FACTOR, USE_DROPOUT,
HIDDEN_NEURONS, HIDDEN_LAYERS, SUB_INPUT_NEURONS, USE_CONCAT_INPUT, BATCH_SIZE, TRAINING_EPOCHS, USE_L2_LOSS, L2_FACTOR, USE_DROPOUT,
DROPOUT_PROB, USE_CONSTANT_LIMIT, CONSTANT_PREDICTION_LIMIT, TRAINABLE_MATRIX,
PRE_TRAINED_MATRIX, USE_PRETRAINED, VALIDATION_DATA, TRAINING_DATA, TESTING_DATA]

Expand Down