Skip to content

Instantly share code, notes, and snippets.

@brpy
Last active March 22, 2021 18:32
Show Gist options
  • Save brpy/e231edef9b8413e3c584e5121685619c to your computer and use it in GitHub Desktop.
Save brpy/e231edef9b8413e3c584e5121685619c to your computer and use it in GitHub Desktop.

By using rules method we can define custom behaviour and add necessary rules within it. Like symbols and abbreviations.

We have to provide the rules data and update the vocabulary accordingly.

The rule_data is a dictionary like {"dollar" : '$', "C M" : "CM"} etc.

We can also include regex or functions as dictionary elements. Like lambda x:x*3 for triple.

class Speech2text(tf.keras.Model):
def __init__(self, vocab_size, lstm_units, rules):
super().__init__()
self.vocab_size = vocab_size
self.lstm_units= lstm_units
self.rule_data = rules # a dictionary
self.dense = Dense(vocab_size, activation="softmax")
def build(self, input_shape):
self.lstm = LSTM(self.lstm_units, return_state=True, return_sequences=True)
def rules(word):
# can add any extra functionality through this method; like symbols, abbreviation etc.
if word in self.rule_data:
return self.rule_data[word]
else:
return word
def call(self, audio, states):
lstm_output, lstm_state_h, lstm_state_c = self.lstm(audio)
id = dense(self.lstm_output)
word = tokenizer.id_to_txt(id)
word = self.rules(word)
return lstm_output, lstm_state_h, lstm_state_c, word
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment