Last active
June 13, 2018 05:39
-
-
Save chatrapathik/71c4dcef6cec417393f13ee4e117f9c5 to your computer and use it in GitHub Desktop.
SimpleRNN model
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
import sys | |
import numpy as np | |
from keras.models import Sequential, Model | |
from keras.layers import Dense, SimpleRNN, Input, LSTM | |
from keras.utils import to_categorical | |
VOCABS = None | |
UNITS = 100 | |
TEXT = ['India', 'StackOverflow', 'Keras', 'SimpleRNN', 'Sequential', 'to_categorical'] | |
def char_to_int(chars): | |
return dict((c, i+1) for i, c in enumerate(chars)) | |
def int_to_char(chars): | |
return dict((i+1, c) for i, c in enumerate(chars)) | |
def get_unique_chars(text): | |
chars = sorted(list(set(text))) | |
return chars | |
def get_sequences(char_to_int): | |
training_data = [] | |
data = TEXT | |
for word in data: | |
word = '\t' + word.strip() + '\n' | |
seq_data = np.zeros(UNITS) | |
for i, char in enumerate(word): | |
seq_data[i] = char_to_int[char] | |
seq_data.tolist() | |
training_data.append(seq_data) | |
training_data = np.array(training_data) | |
target_data = [] | |
for x in training_data: | |
tar_seq = [] | |
for i in x[1:]: | |
tar_seq.append(i) | |
tar_seq.append(0) | |
target_data.append(tar_seq) | |
target_data = np.array(target_data) | |
return training_data, target_data | |
def _get_one_hot_vector(sequence): | |
one_hot_vector = [] | |
for num in sequence: | |
data = np.zeros(VOCABS) | |
if int(num) >= 1: | |
data[int(num)] = 1 | |
one_hot_vector.append(data) | |
return one_hot_vector | |
def get_one_hot_vectors(training_data, target_data): | |
train_vectors = [] | |
for x in training_data: | |
data = _get_one_hot_vector(x) | |
train_vectors.append(data) | |
training_data = np.array(train_vectors) | |
target_vectors = [] | |
for x in target_data: | |
data = _get_one_hot_vector(x) | |
target_vectors.append(data) | |
target_data = np.array(target_vectors) | |
return training_data, target_data | |
def create_model(data): | |
model = Sequential() | |
model.add(SimpleRNN(256, input_shape=(data.shape[1], data.shape[2]))) | |
model.add(Dense(VOCABS, activation='softmax')) | |
return model | |
def compile_model(model): | |
model.compile(optimizer='adam', | |
loss='categorical_crossentropy', | |
metrics=['mae']) | |
return model | |
def train_model(model, trainx, testx): | |
model.fit(trainx, testx, epochs=100, batch_size=32) | |
return model | |
train_text = ''.join(TEXT) + '\t' + '\n' | |
unique_chars = get_unique_chars(train_text) | |
VOCABS = len(unique_chars) + 1 | |
chars_to_int = char_to_int(unique_chars) | |
training_data, target_data = get_sequences(chars_to_int) | |
training_data, target_data = get_one_hot_vectors(training_data, target_data) | |
model = create_model(training_data) | |
model = compile_model(model) | |
model = train_model(model, training_data, target_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment