Skip to content

Commit

Permalink
kerem commit 2
Browse files Browse the repository at this point in the history
  • Loading branch information
arqcenick committed Dec 22, 2017
1 parent eec47bb commit 27a5b85
Show file tree
Hide file tree
Showing 13 changed files with 55,658 additions and 45,555 deletions.
555 changes: 555 additions & 0 deletions FikraRegresser.ipynb

Large diffs are not rendered by default.

615 changes: 615 additions & 0 deletions GenerateWordEmbeddings-Freq.ipynb

Large diffs are not rendered by default.

521 changes: 521 additions & 0 deletions GenerateWordEmbeddings.ipynb

Large diffs are not rendered by default.

747 changes: 747 additions & 0 deletions NaiveRegress.ipynb

Large diffs are not rendered by default.

45,087 changes: 3,019 additions & 42,068 deletions data/train.txt

Large diffs are not rendered by default.

4,133 changes: 763 additions & 3,370 deletions data/valid.txt

Large diffs are not rendered by default.

3,761 changes: 3,761 additions & 0 deletions data_bak/data/test.txt

Large diffs are not rendered by default.

42,068 changes: 42,068 additions & 0 deletions data_bak/data/train.txt

Large diffs are not rendered by default.

3,370 changes: 3,370 additions & 0 deletions data_bak/data/valid.txt

Large diffs are not rendered by default.

245 changes: 128 additions & 117 deletions generate.py
Original file line number Diff line number Diff line change
@@ -1,117 +1,128 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import time
import numpy as np
import tensorflow as tf

import model
from data_reader import load_data, DataReader


flags = tf.flags

# data
flags.DEFINE_string('load_model', None, 'filename of the model to load')
# we need data only to compute vocabulary
flags.DEFINE_string('data_dir', 'data', 'data directory')
flags.DEFINE_integer('num_samples', 300, 'how many words to generate')
flags.DEFINE_float('temperature', 1.0, 'sampling temperature')

# model params
flags.DEFINE_integer('rnn_size', 650, 'size of LSTM internal state')
flags.DEFINE_integer('highway_layers', 2, 'number of highway layers')
flags.DEFINE_integer('char_embed_size', 15, 'dimensionality of character embeddings')
flags.DEFINE_string ('kernels', '[1,2,3,4,5,6,7]', 'CNN kernel widths')
flags.DEFINE_string ('kernel_features', '[50,100,150,200,200,200,200]', 'number of features in the CNN kernel')
flags.DEFINE_integer('rnn_layers', 2, 'number of layers in the LSTM')
flags.DEFINE_float ('dropout', 0.5, 'dropout. 0 = no dropout')

# optimization
flags.DEFINE_integer('max_word_length', 65, 'maximum word length')

# bookkeeping
flags.DEFINE_integer('seed', 3435, 'random number generator seed')
flags.DEFINE_string ('EOS', '+', '<EOS> symbol. should be a single unused character (like +) for PTB and blank for others')

FLAGS = flags.FLAGS


def main(_):
''' Loads trained model and evaluates it on test split '''

if FLAGS.load_model is None:
print('Please specify checkpoint file to load model from')
return -1

if not os.path.exists(FLAGS.load_model + '.meta'):
print('Checkpoint file not found', FLAGS.load_model)
return -1

word_vocab, char_vocab, word_tensors, char_tensors, max_word_length = \
load_data(FLAGS.data_dir, FLAGS.max_word_length, eos=FLAGS.EOS)

print('initialized test dataset reader')

with tf.Graph().as_default(), tf.Session() as session:

# tensorflow seed must be inside graph
tf.set_random_seed(FLAGS.seed)
np.random.seed(seed=FLAGS.seed)

''' build inference graph '''
with tf.variable_scope("Model"):
m = model.inference_graph(
char_vocab_size=char_vocab.size,
word_vocab_size=word_vocab.size,
char_embed_size=FLAGS.char_embed_size,
batch_size=1,
num_highway_layers=FLAGS.highway_layers,
num_rnn_layers=FLAGS.rnn_layers,
rnn_size=FLAGS.rnn_size,
max_word_length=max_word_length,
kernels=eval(FLAGS.kernels),
kernel_features=eval(FLAGS.kernel_features),
num_unroll_steps=1,
dropout=0)

# we need global step only because we want to read it from the model
global_step = tf.Variable(0, dtype=tf.int32, name='global_step')

saver = tf.train.Saver()
saver.restore(session, FLAGS.load_model)
print('Loaded model from', FLAGS.load_model, 'saved at global step', global_step.eval())

''' training starts here '''
rnn_state = session.run(m.initial_rnn_state)
logits = np.ones((word_vocab.size,))
rnn_state = session.run(m.initial_rnn_state)
for i in range(FLAGS.num_samples):
logits = logits / FLAGS.temperature
prob = np.exp(logits)
prob /= np.sum(prob)
prob = prob.ravel()
ix = np.random.choice(range(len(prob)), p=prob)

word = word_vocab.token(ix)
if word == '|': # EOS
print('<unk>', end=' ')
elif word == '+':
print('\n')
else:
print(word, end=' ')

char_input = np.zeros((1, 1, max_word_length))
for i,c in enumerate('{' + word + '}'):
char_input[0,0,i] = char_vocab[c]

logits, rnn_state = session.run([m.logits, m.final_rnn_state],
{m.input: char_input,
m.initial_rnn_state: rnn_state})
logits = np.array(logits)


if __name__ == "__main__":
tf.app.run()
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import time
import numpy as np
import tensorflow as tf

import model
from data_reader import load_data, DataReader

os.environ["CUDA_VISIBLE_DEVICES"]="0"


flags = tf.flags

# data
flags.DEFINE_string('load_model', None, 'filename of the model to load')
# we need data only to compute vocabulary
flags.DEFINE_string('data_dir', 'data', 'data directory')
flags.DEFINE_integer('num_samples', 300, 'how many words to generate')
flags.DEFINE_float('temperature', 1.0, 'sampling temperature')

# model params
flags.DEFINE_integer('rnn_size', 650, 'size of LSTM internal state')
flags.DEFINE_integer('highway_layers', 2, 'number of highway layers')
flags.DEFINE_integer('char_embed_size', 15, 'dimensionality of character embeddings')
flags.DEFINE_string ('kernels', '[1,2,3,4,5,6,7]', 'CNN kernel widths')
flags.DEFINE_string ('kernel_features', '[50,100,150,200,200,200,200]', 'number of features in the CNN kernel')
flags.DEFINE_integer('rnn_layers', 2, 'number of layers in the LSTM')
flags.DEFINE_float ('dropout', 0.5, 'dropout. 0 = no dropout')

# optimization
flags.DEFINE_integer('max_word_length', 65, 'maximum word length')

# bookkeeping
flags.DEFINE_integer('seed', 3435, 'random number generator seed')
flags.DEFINE_string ('EOS', '+', '<EOS> symbol. should be a single unused character (like +) for PTB and blank for others')

FLAGS = flags.FLAGS


def main(_):
''' Loads trained model and evaluates it on test split '''

if FLAGS.load_model is None:
print('Please specify checkpoint file to load model from')
return -1

if not os.path.exists(FLAGS.load_model + '.meta'):
print('Checkpoint file not found', FLAGS.load_model)
return -1

word_vocab, char_vocab, word_tensors, char_tensors, max_word_length = \
load_data(FLAGS.data_dir, FLAGS.max_word_length, eos=FLAGS.EOS)

print('initialized test dataset reader')

with tf.Graph().as_default(), tf.Session() as session:

# tensorflow seed must be inside graph
tf.set_random_seed(FLAGS.seed)
np.random.seed(seed=FLAGS.seed)

''' build inference graph '''
with tf.variable_scope("Model"):
m = model.inference_graph(
char_vocab_size=char_vocab.size,
word_vocab_size=word_vocab.size,
char_embed_size=FLAGS.char_embed_size,
batch_size=1,
num_highway_layers=FLAGS.highway_layers,
num_rnn_layers=FLAGS.rnn_layers,
rnn_size=FLAGS.rnn_size,
max_word_length=max_word_length,
kernels=eval(FLAGS.kernels),
kernel_features=eval(FLAGS.kernel_features),
num_unroll_steps=1,
dropout=0)

# we need global step only because we want to read it from the model
global_step = tf.Variable(0, dtype=tf.int32, name='global_step')

saver = tf.train.Saver()
saver.restore(session, FLAGS.load_model)
print('Loaded model from', FLAGS.load_model, 'saved at global step', global_step.eval())

''' training starts here '''
rnn_state = session.run(m.initial_rnn_state)
logits = np.ones((word_vocab.size,))
rnn_state = session.run(m.initial_rnn_state)
for i in range(FLAGS.num_samples):
if i > 0:
logits = logits / FLAGS.temperature
prob = np.exp(logits)
prob /= np.sum(prob)
prob = prob.ravel()

ix = np.random.choice(range(len(prob)), p=prob)
word = word_vocab.token(ix)
else:
word_input = input("Enter the first word:")
word = word_vocab.token(word_vocab.get(word_input))


if word == '|': # EOS
print('<unk>', end=' ')
elif word == '+':
print('\n')
elif word == '#':
print('\nFıkra is generated')
break
else:
print(word, end=' ')

char_input = np.zeros((1, 1, max_word_length))
for i,c in enumerate('{' + word + '}'):
char_input[0,0,i] = char_vocab[c]

logits, rnn_state = session.run([m.logits, m.final_rnn_state],
{m.input: char_input,
m.initial_rnn_state: rnn_state})
logits = np.array(logits)


if __name__ == "__main__":
tf.app.run()
110 changes: 110 additions & 0 deletions generate_wordembeddings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import time
import numpy as np
import tensorflow as tf

import model
from data_reader import load_data, DataReader

os.environ["CUDA_VISIBLE_DEVICES"]="0"


flags = tf.flags

# data
flags.DEFINE_string('load_model', None, 'filename of the model to load')
# we need data only to compute vocabulary
flags.DEFINE_string('data_dir', 'data', 'data directory')
flags.DEFINE_integer('num_samples', 300, 'how many words to generate')
flags.DEFINE_float('temperature', 1.0, 'sampling temperature')

# model params
flags.DEFINE_integer('rnn_size', 650, 'size of LSTM internal state')
flags.DEFINE_integer('highway_layers', 2, 'number of highway layers')
flags.DEFINE_integer('char_embed_size', 15, 'dimensionality of character embeddings')
flags.DEFINE_string ('kernels', '[1,2,3,4,5,6,7]', 'CNN kernel widths')
flags.DEFINE_string ('kernel_features', '[50,100,150,200,200,200,200]', 'number of features in the CNN kernel')
flags.DEFINE_integer('rnn_layers', 2, 'number of layers in the LSTM')
flags.DEFINE_float ('dropout', 0.5, 'dropout. 0 = no dropout')

# optimization
flags.DEFINE_integer('max_word_length', 65, 'maximum word length')

# bookkeeping
flags.DEFINE_integer('seed', 3435, 'random number generator seed')
flags.DEFINE_string ('EOS', '+', '<EOS> symbol. should be a single unused character (like +) for PTB and blank for others')

FLAGS = flags.FLAGS


def main(_):
''' Loads trained model and evaluates it on test split '''

if FLAGS.load_model is None:
print('Please specify checkpoint file to load model from')
return -1

if not os.path.exists(FLAGS.load_model + '.meta'):
print('Checkpoint file not found', FLAGS.load_model)
return -1

word_vocab, char_vocab, word_tensors, char_tensors, max_word_length = \
load_data(FLAGS.data_dir, FLAGS.max_word_length, eos=FLAGS.EOS)

print('initialized test dataset reader')

with tf.Graph().as_default(), tf.Session() as session:

# tensorflow seed must be inside graph
tf.set_random_seed(FLAGS.seed)
np.random.seed(seed=FLAGS.seed)

''' build inference graph '''
outputs_list = []
with tf.variable_scope("Model"):
m = model.inference_graph(
char_vocab_size=char_vocab.size,
word_vocab_size=word_vocab.size,
char_embed_size=FLAGS.char_embed_size,
batch_size=1,
num_highway_layers=FLAGS.highway_layers,
num_rnn_layers=FLAGS.rnn_layers,
rnn_size=FLAGS.rnn_size,
max_word_length=max_word_length,
kernels=eval(FLAGS.kernels),
kernel_features=eval(FLAGS.kernel_features),
num_unroll_steps=1,
dropout=0)

# we need global step only because we want to read it from the model
global_step = tf.Variable(0, dtype=tf.int32, name='global_step')

saver = tf.train.Saver()
saver.restore(session, FLAGS.load_model)
print('Loaded model from', FLAGS.load_model, 'saved at global step', global_step.eval())

''' training starts here '''
rnn_state = session.run(m.initial_rnn_state)
logits = np.ones((word_vocab.size,))
rnn_state = session.run(m.initial_rnn_state)
for i in range(5):

word = word_vocab.token(i)
char_input = np.zeros((1, 1, max_word_length))
for i,c in enumerate('{' + word + '}'):
char_input[0,0,i] = char_vocab[c]

outputs, logits = session.run([m.rnn_outputs,m.logits],
{m.input: char_input,
m.initial_rnn_state: rnn_state})
outputs_list.append(outputs)

outputs_array = np.array(outputs_list)
print(outputs_array.shape)

if __name__ == "__main__":
tf.app.run()
1 change: 1 addition & 0 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import model
from data_reader import load_data, DataReader

os.environ["CUDA_VISIBLE_DEVICES"]="0"

flags = tf.flags

Expand Down
Binary file added x_test.npz
Binary file not shown.

0 comments on commit 27a5b85

Please sign in to comment.