Skip to content

Instantly share code, notes, and snippets.

def _init_lstm(self):
for s in ['wu', 'wf', 'wo', 'wc']:
setattr(self, s, [tf.Variable(tf.random.normal(
stddev=std_dev,
shape=shape,
dtype=tf.double))
for shape, std_dev in self.w_shapes])
for s in ['bu', 'bf', 'bo', 'bc']:
def _init_gru(self):
for s in ['wr', 'wu', 'wa']:
setattr(self, s, [tf.Variable(tf.random.normal(
stddev=std_dev,
shape=shape,
dtype=tf.double))
for shape, std_dev in self.w_shapes])
for s in ['br', 'bu', 'ba']:
def _init(self,
vocabulary: list,
inter_time_step_size: int,
unit_type: str,
depth: int):
self.vocab = vocabulary
self.vocab_size = len(vocabulary)
self.inter_time_step_size = inter_time_step_size
self.combined_size = self.vocab_size + self.inter_time_step_size
class Model:
def __init__(self,
vocabulary: list,
inter_time_step_size: int,
unit_type: str,
depth: int):
# unit_type can be one of: 'gru' or 'lstm'
# depth >= 1
def sample_word(vocabulary: list, prob: np.ndarray, threshold: float) -> str:
# sample a word from the vocabulary according to 'prob'
# probability distribution (the softmax output of our model)
prob = prob.tolist()
vocab_prob = [[vocabulary[i], prob[i]] for i in range(len(prob))]
vocab_prob.sort(reverse=True, key=lambda e: e[1])
s = 0
for i in range(len(vocab_prob)):
EOS = chr(10) # End of sentence
def build_vocabulary() -> list:
# builds a vocabulary using ASCII characters
vocabulary = [chr(i) for i in range(10, 128)]
return vocabulary
def word2index(vocabulary: list, word: str) -> int:
# returns the index of 'word' in the vocabulary
return vocabulary.index(word)
def save(self, name: str) -> None:
mkdir(f'./{name}')
with open(f'./{name}/vocabulary.txt', 'w') as f:
f.write(','.join(self.vocab))
with open(f'./{name}/a_size.txt', 'w') as f:
f.write(str(self.a_size))
np.save(f'./{name}/wa.npy', self.wa.numpy())
np.save(f'./{name}/ba.npy', self.ba.numpy())
np.save(f'./{name}/wy.npy', self.wy.numpy())
np.save(f'./{name}/by.npy', self.by.numpy())
def predict_next(self, sentence: str) -> str:
# predict the next part of the sentence given as parameter
a = np.zeros((1, self.a_size))
for word in sentence.strip().split():
if word not in vocabulary:
word = UNK
x = words2onehot(self.vocab, [word])
a, y_hat = self(a, x)
s = ''
while True:
def sample(self) -> str:
# sample a new sentence from the learned model
sentence = ''
a = np.zeros((1, self.a_size))
x = np.zeros((1, self.vocab_size))
while True:
a, y_hat = self(a, x)
word = sample_word(self.vocab, tf.reshape(y_hat, (-1,)))
if word == EOS:
break
def fit(self,
sentences: list,
batch_size: int = 128,
epochs: int = 10) -> None:
n_sent = len(sentences)
num_batches = ceil(n_sent / batch_size)
for epoch in range(epochs):