Spaces:
Runtime error
Runtime error
add all files
Browse files- .gitattributes +3 -0
- Data.py +62 -0
- README.md +1 -1
- app.py +82 -0
- dataset/30_length/train.pickle +3 -0
- dataset/30_length/validation.pickle +3 -0
- inference.py +26 -0
- model.py +314 -0
- models/bi_directional_concat_256_batch_160_epoch_30_length_ass_eng_nmt_weights.h5 +3 -0
- tokenizer/30_tokenizer_ass.pickle +3 -0
- tokenizer/30_tokenizer_eng.pickle +3 -0
.gitattributes
CHANGED
@@ -25,3 +25,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
25 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
26 |
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
27 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
25 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
26 |
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
27 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
29 |
+
30_tokenizer_ass.pickle filter=lfs diff=lfs merge=lfs -text
|
30 |
+
30_tokenizer_eng.pickle filter=lfs diff=lfs merge=lfs -text
|
Data.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import re
|
3 |
+
import tensorflow as tf
|
4 |
+
from tensorflow.keras.layers import Embedding, LSTM, Dense,Bidirectional
|
5 |
+
from tensorflow.keras.models import Model
|
6 |
+
from tensorflow.keras.preprocessing.text import Tokenizer
|
7 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
8 |
+
import numpy as np
|
9 |
+
import string
|
10 |
+
from string import digits
|
11 |
+
from sklearn.utils import shuffle
|
12 |
+
from sklearn.model_selection import train_test_split
|
13 |
+
import nltk
|
14 |
+
from nltk.tokenize import word_tokenize
|
15 |
+
from tqdm import tqdm
|
16 |
+
|
17 |
+
class Dataset:
|
18 |
+
def __init__(self, data, tknizer_ass, tknizer_eng, max_len):
|
19 |
+
self.encoder_inps = data['ass'].values
|
20 |
+
self.decoder_inps = data['eng_inp'].values
|
21 |
+
self.decoder_outs = data['eng_out'].values
|
22 |
+
self.tknizer_eng = tknizer_eng
|
23 |
+
self.tknizer_ass = tknizer_ass
|
24 |
+
self.max_len = max_len
|
25 |
+
|
26 |
+
def __getitem__(self, i):
|
27 |
+
self.encoder_seq = self.tknizer_ass.texts_to_sequences([self.encoder_inps[i]]) # need to pass list of values
|
28 |
+
self.decoder_inp_seq = self.tknizer_eng.texts_to_sequences([self.decoder_inps[i]])
|
29 |
+
self.decoder_out_seq = self.tknizer_eng.texts_to_sequences([self.decoder_outs[i]])
|
30 |
+
|
31 |
+
self.encoder_seq = pad_sequences(self.encoder_seq, maxlen=self.max_len, dtype='int32', padding='post')
|
32 |
+
self.decoder_inp_seq = pad_sequences(self.decoder_inp_seq, maxlen=self.max_len, dtype='int32', padding='post')
|
33 |
+
self.decoder_out_seq = pad_sequences(self.decoder_out_seq, maxlen=self.max_len, dtype='int32', padding='post')
|
34 |
+
return self.encoder_seq, self.decoder_inp_seq, self.decoder_out_seq
|
35 |
+
|
36 |
+
def __len__(self): # your model.fit_gen requires this function
|
37 |
+
return len(self.encoder_inps)
|
38 |
+
|
39 |
+
|
40 |
+
class Dataloder(tf.keras.utils.Sequence):
|
41 |
+
def __init__(self, dataset, batch_size=1):
|
42 |
+
self.dataset = dataset
|
43 |
+
self.batch_size = batch_size
|
44 |
+
self.indexes = np.arange(len(self.dataset.encoder_inps))
|
45 |
+
|
46 |
+
|
47 |
+
def __getitem__(self, i):
|
48 |
+
start = i * self.batch_size
|
49 |
+
stop = (i + 1) * self.batch_size
|
50 |
+
data = []
|
51 |
+
for j in range(start, stop):
|
52 |
+
data.append(self.dataset[j])
|
53 |
+
|
54 |
+
batch = [np.squeeze(np.stack(samples, axis=1), axis=0) for samples in zip(*data)]
|
55 |
+
# we are creating data like ([italian, english_inp], english_out) these are already converted into seq
|
56 |
+
return tuple([[batch[0],batch[1]],batch[2]])
|
57 |
+
|
58 |
+
def __len__(self): # your model.fit_gen requires this function
|
59 |
+
return len(self.indexes) // self.batch_size
|
60 |
+
|
61 |
+
def on_epoch_end(self):
|
62 |
+
self.indexes = np.random.permutation(self.indexes)
|
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
title: Attention_based_Assamese_English_NMT_BiLSTM
|
3 |
-
emoji:
|
4 |
colorFrom: gray
|
5 |
colorTo: indigo
|
6 |
sdk: streamlit
|
|
|
1 |
---
|
2 |
title: Attention_based_Assamese_English_NMT_BiLSTM
|
3 |
+
emoji: 📚
|
4 |
colorFrom: gray
|
5 |
colorTo: indigo
|
6 |
sdk: streamlit
|
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import datetime
|
4 |
+
import numpy as np
|
5 |
+
import datetime
|
6 |
+
import model
|
7 |
+
import inference
|
8 |
+
|
9 |
+
|
10 |
+
# Global params
|
11 |
+
if 'model' not in st.session_state:
|
12 |
+
loaded_model,tokenizer_eng,tokenizer_ass,in_input_length = model.main()
|
13 |
+
st.session_state['model'] = loaded_model
|
14 |
+
st.session_state['tokenizer_eng'] = tokenizer_eng
|
15 |
+
st.session_state['tokenizer_ass'] = tokenizer_ass
|
16 |
+
st.session_state['in_input_length'] = in_input_length
|
17 |
+
|
18 |
+
# st.write(st.session_state)
|
19 |
+
|
20 |
+
# def model_loading():
|
21 |
+
# return model.main()
|
22 |
+
|
23 |
+
def show_information():
|
24 |
+
# Show Information about the selected Stock
|
25 |
+
st.header('Now translate everything into English!')
|
26 |
+
# st.caption("Analyzing data from 2015 to 2021")
|
27 |
+
# st.text("1) There is a 60% chance of gap up opening in any random trade in Reliance 😮 ")
|
28 |
+
# st.text("2) 1% of the gap up is more than Rs:15.00 i.e more quantity == more profit😇")
|
29 |
+
# st.text("3) Median, Q3 or 75th percentile have increased from 2015(1.8) to 2021(11.55)💰")
|
30 |
+
|
31 |
+
def select_text():
|
32 |
+
# Select the Suggested Assamese Text
|
33 |
+
option = st.selectbox(
|
34 |
+
'Select these suggested Assamese Sentences',
|
35 |
+
('সমগ্ৰ দেশজুৰি ব্যাপক চৰ্চা হৈছিল উক্ত ঘটনাৰ ',
|
36 |
+
'দৃষ্টান্ত ব্যৱহাৰ কৰাৰ সম্পৰ্কে আমি যীচুৰ পৰা কি শিকিব পাৰোঁ ',
|
37 |
+
'তেওঁ যি ইচ্ছা তাকে কৰিব নোৱাৰে '))
|
38 |
+
|
39 |
+
st.write('You have selected suggested text')
|
40 |
+
|
41 |
+
title = st.text_input('Assamese Text Input', option)
|
42 |
+
# st.write('Your Assamese Text', title)
|
43 |
+
|
44 |
+
return title
|
45 |
+
# return selected_date
|
46 |
+
|
47 |
+
# @st.cache
|
48 |
+
# def prepare_data_for_selected_date():
|
49 |
+
# df = pd.read_csv("dataset/reliance_30min.csv")
|
50 |
+
# df = helper.format_date(df)
|
51 |
+
# df = helper.replace_vol(df)
|
52 |
+
# df = helper.feature_main(df)
|
53 |
+
# df.to_csv('dataset/processed_reliance30m.csv')
|
54 |
+
|
55 |
+
# return df
|
56 |
+
|
57 |
+
# @st.cache
|
58 |
+
# def show_result(sentence):
|
59 |
+
# pass
|
60 |
+
|
61 |
+
|
62 |
+
# def show_prediction_result(prepared_data):
|
63 |
+
# model = all_model.load_model()
|
64 |
+
# result = all_model.prediction(model,prepared_data)
|
65 |
+
|
66 |
+
# return result
|
67 |
+
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
def main():
|
72 |
+
st.title('📚Assamese to English Translator🤓')
|
73 |
+
show_information()
|
74 |
+
text = select_text()
|
75 |
+
if st.button('Translate'):
|
76 |
+
result = inference.infer(st.session_state['model'],text,st.session_state['tokenizer_ass'],
|
77 |
+
st.session_state['tokenizer_eng'],st.session_state['in_input_length'])
|
78 |
+
st.caption('Your Assamese translated text')
|
79 |
+
st.text(result[:-6])
|
80 |
+
|
81 |
+
if __name__ == "__main__":
|
82 |
+
main()
|
dataset/30_length/train.pickle
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2cfb065d12104363eb94913a59d8610568e64ee3c1c9a77a14bf898900e0b756
|
3 |
+
size 29548368
|
dataset/30_length/validation.pickle
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f3662a9bcb3bc6d91999c035944c0e913c8bdb2edd47b4757a1a69f18cb2b630
|
3 |
+
size 7384761
|
inference.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from tensorflow import argmax,expand_dims,convert_to_tensor
|
2 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
3 |
+
# BRUTE FORCE
|
4 |
+
def infer(model,sentence,tokenizer_ass,tokenizer_eng,in_input_length):
|
5 |
+
encoder_seq = tokenizer_ass.texts_to_sequences([sentence]) # need to pass list of values
|
6 |
+
encoder_seq = pad_sequences(encoder_seq, maxlen=in_input_length, dtype='int32', padding='post')
|
7 |
+
encoder_seq = convert_to_tensor(encoder_seq)
|
8 |
+
initial_state = model.layers[0].initialize_states_bidirectional(batch_size=1)
|
9 |
+
encoder_outputs, f_encoder_hidden, f_encoder_cell,b_encoder_hidden, b_encoder_cell = model.layers[0](encoder_seq,initial_state)
|
10 |
+
dec_input = expand_dims([tokenizer_eng.word_index['<start>']],0)
|
11 |
+
|
12 |
+
result = ''
|
13 |
+
for t in range(30):
|
14 |
+
Output, dec_h,dec_c,attention_w,context_vec = model.layers[1].onestep_decoder(dec_input,encoder_outputs,f_encoder_hidden, f_encoder_cell,b_encoder_hidden, b_encoder_cell)
|
15 |
+
# result_beam_list = beam_search(Output,k=1)
|
16 |
+
# result_beam = result_beam_list[0][0]
|
17 |
+
# attention_weights = tf.reshape(attention_w,(-1,))
|
18 |
+
predict_id = argmax(Output[0]).numpy()
|
19 |
+
result += tokenizer_eng.index_word[predict_id]+' '
|
20 |
+
if tokenizer_eng.index_word[predict_id] == '<end>':
|
21 |
+
break
|
22 |
+
dec_input = expand_dims([predict_id],0)
|
23 |
+
|
24 |
+
|
25 |
+
print(result)
|
26 |
+
return result
|
model.py
ADDED
@@ -0,0 +1,314 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import matplotlib.pyplot as plt
|
2 |
+
# %matplotlib inline
|
3 |
+
# import seaborn as sns
|
4 |
+
import pickle
|
5 |
+
import pandas as pd
|
6 |
+
import re
|
7 |
+
import os
|
8 |
+
os.add_dll_directory("C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.2/bin")
|
9 |
+
import tensorflow as tf
|
10 |
+
from tensorflow.keras.layers import Embedding, LSTM, Dense,Bidirectional
|
11 |
+
from tensorflow.keras.models import Model
|
12 |
+
from tensorflow.keras.preprocessing.text import Tokenizer
|
13 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
14 |
+
from tensorflow.keras import backend as K
|
15 |
+
import numpy as np
|
16 |
+
import string
|
17 |
+
from string import digits
|
18 |
+
from sklearn.utils import shuffle
|
19 |
+
from sklearn.model_selection import train_test_split
|
20 |
+
import nltk
|
21 |
+
from nltk.tokenize import word_tokenize
|
22 |
+
from tqdm import tqdm
|
23 |
+
from Data import Dataset,Dataloder
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
"""########################################------MODEL------########################################
|
28 |
+
"""
|
29 |
+
|
30 |
+
########################################------Encoder model------########################################
|
31 |
+
class Encoder(tf.keras.Model):
|
32 |
+
|
33 |
+
|
34 |
+
def __init__(self,inp_vocab_size,embedding_size,lstm_size,input_length):
|
35 |
+
super().__init__()
|
36 |
+
|
37 |
+
self.inp_vocab_size = inp_vocab_size
|
38 |
+
self.embedding_size = embedding_size
|
39 |
+
self.lstm_size = lstm_size
|
40 |
+
self.input_length = input_length
|
41 |
+
#Initialize Embedding layer
|
42 |
+
|
43 |
+
def build(self,input_shape):
|
44 |
+
self.embedding = Embedding(input_dim=self.inp_vocab_size, output_dim=self.embedding_size,
|
45 |
+
input_length=self.input_length,trainable=True,name="encoder_embed")
|
46 |
+
#Intialize Encoder LSTM layer
|
47 |
+
self.bilstm = tf.keras.layers.Bidirectional(LSTM(units = self.lstm_size,return_sequences=True,return_state=True),merge_mode='sum')
|
48 |
+
|
49 |
+
def call(self,input_sequence,initial_state):
|
50 |
+
'''
|
51 |
+
Input:Input_sequence[batch_size,input_length]
|
52 |
+
Initial_state 4x[batch_size,encoder_units]
|
53 |
+
|
54 |
+
Output: lstm_enc_output [batch_size,input_length,encoder_units]
|
55 |
+
forward_h/c & backward_h/c [batch_size,encoder_units]
|
56 |
+
'''
|
57 |
+
# print("initial_state",len(initial_state))
|
58 |
+
input_embd = self.embedding(input_sequence)
|
59 |
+
lstm_enc_output, forward_h, forward_c, backward_h, backward_c = self.bilstm(input_embd,initial_state)
|
60 |
+
return lstm_enc_output, forward_h, forward_c, backward_h, backward_c
|
61 |
+
# return lstm_enc_output, forward_h, forward_c
|
62 |
+
|
63 |
+
|
64 |
+
def initialize_states(self,batch_size):
|
65 |
+
'''
|
66 |
+
Given a batch size it will return intial hidden state and intial cell state.
|
67 |
+
If batch size is 32- Hidden state is zeros of size [32,lstm_units], cell state zeros is of size [32,lstm_units]
|
68 |
+
'''
|
69 |
+
self.lstm_state_h = tf.random.uniform(shape=[batch_size,self.lstm_size],dtype=tf.float32)
|
70 |
+
self.lstm_state_c = tf.random.uniform(shape=[batch_size,self.lstm_size],dtype=tf.float32)
|
71 |
+
return self.lstm_state_h,self.lstm_state_c
|
72 |
+
|
73 |
+
def initialize_states_bidirectional(self,batch_size):
|
74 |
+
states = [tf.zeros((batch_size, self.lstm_size)) for i in range(4)]
|
75 |
+
return states
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
########################################------Attention model------########################################
|
80 |
+
class Attention(tf.keras.layers.Layer):
|
81 |
+
def __init__(self,scoring_function, att_units):
|
82 |
+
super().__init__()
|
83 |
+
self.att_units = att_units
|
84 |
+
self.scoring_function = scoring_function
|
85 |
+
# self.batch_size = batch_size
|
86 |
+
# Please go through the reference notebook and research paper to complete the scoring functions
|
87 |
+
|
88 |
+
if self.scoring_function=='dot':
|
89 |
+
pass
|
90 |
+
|
91 |
+
elif scoring_function == 'general':
|
92 |
+
self.dense = Dense(self.att_units)
|
93 |
+
|
94 |
+
elif scoring_function == 'concat':
|
95 |
+
self.dense = tf.keras.layers.Dense(att_units, activation='tanh')
|
96 |
+
self.dense1 = tf.keras.layers.Dense(1)
|
97 |
+
|
98 |
+
|
99 |
+
def call(self,decoder_hidden_state,encoder_output):
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
+
if self.scoring_function == 'dot':
|
104 |
+
decoder_hidden_state = tf.expand_dims(decoder_hidden_state,axis=2)
|
105 |
+
similarity = tf.matmul(encoder_output,decoder_hidden_state)
|
106 |
+
weights = tf.nn.softmax(similarity,axis=1)
|
107 |
+
context_vector = tf.matmul(weights,encoder_output,transpose_a=True)
|
108 |
+
context_vector = tf.squeeze(context_vector, axis=1)
|
109 |
+
return context_vector,weights
|
110 |
+
|
111 |
+
elif self.scoring_function == 'general':
|
112 |
+
decoder_hidden_state=tf.expand_dims(decoder_hidden_state, 1)
|
113 |
+
score = tf.matmul(decoder_hidden_state, self.dense(
|
114 |
+
encoder_output), transpose_b=True)
|
115 |
+
attention_weights = tf.keras.activations.softmax(score, axis=-1)
|
116 |
+
context_vector = tf.matmul(attention_weights, encoder_output)
|
117 |
+
context_vector=tf.reduce_sum(context_vector, axis=1)
|
118 |
+
attention_weights=tf.reduce_sum(attention_weights, axis=1)
|
119 |
+
attention_weights=tf.expand_dims(attention_weights, 2)
|
120 |
+
return context_vector,attention_weights
|
121 |
+
|
122 |
+
elif self.scoring_function == 'concat':
|
123 |
+
decoder_hidden_state=tf.expand_dims(decoder_hidden_state, 1)
|
124 |
+
decoder_hidden_state = tf.tile(
|
125 |
+
decoder_hidden_state, [1,30, 1])
|
126 |
+
score = self.dense1(
|
127 |
+
self.dense(tf.concat((decoder_hidden_state, encoder_output), axis=-1)))
|
128 |
+
score = tf.transpose(score, [0, 2, 1])
|
129 |
+
attention_weights = tf.keras.activations.softmax(score, axis=-1)
|
130 |
+
context_vector = tf.matmul(attention_weights, encoder_output)
|
131 |
+
context_vector=tf.reduce_sum(context_vector, axis=1)
|
132 |
+
attention_weights=tf.reduce_sum(attention_weights, axis=1)
|
133 |
+
attention_weights=tf.expand_dims(attention_weights, 2)
|
134 |
+
|
135 |
+
return context_vector,attention_weights
|
136 |
+
|
137 |
+
|
138 |
+
########################################------OneStepDecoder model------########################################
|
139 |
+
class OneStepDecoder(tf.keras.Model):
|
140 |
+
def __init__(self,tar_vocab_size, embedding_dim, input_length, dec_units ,score_fun ,att_units):
|
141 |
+
|
142 |
+
# Initialize decoder embedding layer, LSTM and any other objects needed
|
143 |
+
super().__init__()
|
144 |
+
self.tar_vocab_size = tar_vocab_size
|
145 |
+
self.embedding_dim = embedding_dim
|
146 |
+
self.input_length = input_length
|
147 |
+
self.dec_units = dec_units
|
148 |
+
self.score_fun = score_fun
|
149 |
+
self.att_units = att_units
|
150 |
+
|
151 |
+
def build(self,input_shape):
|
152 |
+
self.attention = Attention('concat', self.att_units)
|
153 |
+
self.embedding = Embedding(input_dim=self.tar_vocab_size,output_dim=self.embedding_dim,
|
154 |
+
input_length=self.input_length,mask_zero=True,trainable=True,name="Decoder_Embed")
|
155 |
+
self.bilstm = tf.keras.layers.Bidirectional(LSTM(units = self.dec_units,return_sequences=True,return_state=True),merge_mode='sum')
|
156 |
+
self.dense = Dense(self.tar_vocab_size)
|
157 |
+
|
158 |
+
|
159 |
+
|
160 |
+
def call(self,input_to_decoder, encoder_output, f_state_h,f_state_c,b_state_h,b_state_c):
|
161 |
+
dec_embd = self.embedding(input_to_decoder)
|
162 |
+
context_vectors,attention_weights = self.attention(f_state_h,encoder_output)
|
163 |
+
context_vectors_ = tf.expand_dims(context_vectors,axis=1)
|
164 |
+
concat_vector = tf.concat([dec_embd,context_vectors_],axis=2)
|
165 |
+
states = [f_state_h,f_state_c,b_state_h,b_state_c]
|
166 |
+
decoder_outputs,dec_f_state_h,dec_f_state_c,dec_b_state_h,dec_b_state_c = self.bilstm(concat_vector,states)
|
167 |
+
decoder_outputs = tf.squeeze(decoder_outputs,axis=1)
|
168 |
+
dense_output = self.dense(decoder_outputs)
|
169 |
+
|
170 |
+
return dense_output,dec_f_state_h,dec_f_state_c,attention_weights,context_vectors
|
171 |
+
|
172 |
+
|
173 |
+
########################################------Decoder model------########################################
|
174 |
+
class Decoder(tf.keras.Model):
|
175 |
+
def __init__(self,out_vocab_size, embedding_dim, input_length, dec_units ,score_fun ,att_units):
|
176 |
+
#Intialize necessary variables and create an object from the class onestepdecoder
|
177 |
+
super().__init__()
|
178 |
+
self.out_vocab_size = out_vocab_size
|
179 |
+
self.embedding_dim = embedding_dim
|
180 |
+
self.input_length = input_length
|
181 |
+
self.dec_units = dec_units
|
182 |
+
self.score_fun = score_fun
|
183 |
+
self.att_units = att_units
|
184 |
+
|
185 |
+
def build(self,input_shape):
|
186 |
+
self.onestep_decoder = OneStepDecoder(self.out_vocab_size, self.embedding_dim, self.input_length, self.dec_units ,self.score_fun ,
|
187 |
+
self.att_units)
|
188 |
+
|
189 |
+
def call(self, input_to_decoder,encoder_output,f_decoder_hidden_state,f_decoder_cell_state,b_decoder_hidden_state,b_decoder_cell_state ):
|
190 |
+
|
191 |
+
all_outputs = tf.TensorArray(tf.float32, size=self.input_length,name="output_array")
|
192 |
+
|
193 |
+
for timestep in range(self.input_length):
|
194 |
+
output,state_h,state_c,attention_weights,context_vector = self.onestep_decoder(input_to_decoder[:,timestep:timestep+1],encoder_output,
|
195 |
+
f_decoder_hidden_state,f_decoder_cell_state,b_decoder_hidden_state,b_decoder_cell_state)
|
196 |
+
all_outputs = all_outputs.write(timestep,output)
|
197 |
+
|
198 |
+
all_outputs = tf.transpose(all_outputs.stack(),[1,0,2])
|
199 |
+
|
200 |
+
return all_outputs
|
201 |
+
|
202 |
+
########################################------encoder_decoder model------########################################
|
203 |
+
class encoder_decoder(tf.keras.Model):
|
204 |
+
def __init__(self,out_vocab_size,inp_vocab_size,embedding_dim,embedding_size,in_input_length,tar_input_length,dec_units,lstm_size,att_units,batch_size):
|
205 |
+
super().__init__()
|
206 |
+
#Intialize objects from encoder decoder
|
207 |
+
self.out_vocab_size = out_vocab_size
|
208 |
+
self.inp_vocab_size = inp_vocab_size
|
209 |
+
|
210 |
+
self.embedding_dim_target = embedding_dim
|
211 |
+
self.embedding_dim_input = embedding_size
|
212 |
+
self.in_input_length = in_input_length
|
213 |
+
self.tar_input_length = tar_input_length
|
214 |
+
|
215 |
+
self.dec_lstm_size = dec_units
|
216 |
+
self.enc_lstm_size = lstm_size
|
217 |
+
|
218 |
+
self.att_units = att_units
|
219 |
+
self.batch_size = batch_size
|
220 |
+
|
221 |
+
def build(self,input_shape):
|
222 |
+
self.encoder = Encoder(self.inp_vocab_size,self.embedding_dim_input,self.enc_lstm_size,self.in_input_length)
|
223 |
+
self.decoder = Decoder(self.out_vocab_size,self.embedding_dim_target, self.tar_input_length, self.dec_lstm_size ,'general' ,self.att_units)
|
224 |
+
|
225 |
+
def call(self,data):
|
226 |
+
input_sequence, target_sequence = data[0],data[1]
|
227 |
+
# print(input_sequence.shape)
|
228 |
+
encoder_initial_state = self.encoder.initialize_states_bidirectional(self.batch_size)
|
229 |
+
# print(len(encoder_initial_state))
|
230 |
+
encoder_output,f_encoder_state_h,f_encoder_state_c,b_encoder_state_h,b_encoder_state_c = self.encoder(input_sequence,encoder_initial_state)
|
231 |
+
decoder_output = self.decoder(target_sequence,encoder_output,f_encoder_state_h,f_encoder_state_c,b_encoder_state_h,b_encoder_state_c)
|
232 |
+
return decoder_output
|
233 |
+
|
234 |
+
|
235 |
+
def loss_function(real, pred):
|
236 |
+
loss_object = tf.keras.losses.SparseCategoricalCrossentropy(
|
237 |
+
from_logits=True)
|
238 |
+
mask = tf.math.logical_not(tf.math.equal(real, 0))
|
239 |
+
loss_ = loss_object(real, pred)
|
240 |
+
mask = tf.cast(mask, dtype=loss_.dtype)
|
241 |
+
loss_ *= mask
|
242 |
+
|
243 |
+
return tf.reduce_mean(loss_)
|
244 |
+
|
245 |
+
def accuracy(real,pred):
|
246 |
+
pred_val = K.cast(K.argmax(pred,axis=-1),dtype='float32')
|
247 |
+
real_val = K.cast(K.equal(real,pred_val),dtype='float32')
|
248 |
+
|
249 |
+
mask = K.cast(K.greater(real,0),dtype='float32')
|
250 |
+
n_correct = K.sum(mask*real_val)
|
251 |
+
n_total = K.sum(mask)
|
252 |
+
|
253 |
+
return n_correct/n_total
|
254 |
+
|
255 |
+
def load_weights():
|
256 |
+
"""======================================================LOADING======================================================"""
|
257 |
+
# Dataset
|
258 |
+
with open('dataset/30_length/train.pickle', 'rb') as handle:
|
259 |
+
train = pickle.load(handle)
|
260 |
+
|
261 |
+
with open('dataset/30_length/validation.pickle', 'rb') as handle:
|
262 |
+
validation = pickle.load(handle)
|
263 |
+
|
264 |
+
# Tokenizer
|
265 |
+
with open('tokenizer/30_tokenizer_eng.pickle', 'rb') as handle:
|
266 |
+
tokenizer_eng = pickle.load(handle)
|
267 |
+
|
268 |
+
with open('tokenizer/30_tokenizer_ass.pickle', 'rb') as handle:
|
269 |
+
tokenizer_ass = pickle.load(handle)
|
270 |
+
|
271 |
+
# Vocab Size
|
272 |
+
vocab_size_ass = len(tokenizer_ass.word_index.keys())
|
273 |
+
vocab_size_eng = len(tokenizer_eng.word_index.keys())
|
274 |
+
|
275 |
+
return train,validation,tokenizer_eng,tokenizer_ass,vocab_size_ass,vocab_size_eng
|
276 |
+
|
277 |
+
def main():
|
278 |
+
train,validation,tokenizer_eng,tokenizer_ass,vocab_size_ass,vocab_size_eng = load_weights()
|
279 |
+
in_input_length = 30
|
280 |
+
tar_input_length = 30
|
281 |
+
inp_vocab_size = vocab_size_ass
|
282 |
+
out_vocab_size = vocab_size_eng
|
283 |
+
|
284 |
+
dec_units = 128
|
285 |
+
lstm_size = 128
|
286 |
+
att_units = 256
|
287 |
+
batch_size = 32
|
288 |
+
embedding_dim = 300
|
289 |
+
embedding_size = 300
|
290 |
+
|
291 |
+
train_dataset = Dataset(train, tokenizer_ass, tokenizer_eng, in_input_length)
|
292 |
+
test_dataset = Dataset(validation, tokenizer_ass, tokenizer_eng, in_input_length)
|
293 |
+
|
294 |
+
train_dataloader = Dataloder(train_dataset, batch_size)
|
295 |
+
test_dataloader = Dataloder(test_dataset, batch_size)
|
296 |
+
|
297 |
+
|
298 |
+
print(train_dataloader[0][0][0].shape, train_dataloader[0][0][1].shape, train_dataloader[0][1].shape)
|
299 |
+
|
300 |
+
model = encoder_decoder(out_vocab_size,inp_vocab_size,embedding_dim,embedding_size,in_input_length,tar_input_length,dec_units,lstm_size,att_units,batch_size)
|
301 |
+
optimizer = tf.keras.optimizers.Adam()
|
302 |
+
model.compile(optimizer=optimizer,loss=loss_function,metrics=[accuracy])
|
303 |
+
|
304 |
+
# train_steps=train.shape[0]//32
|
305 |
+
# valid_steps=validation.shape[0]//32
|
306 |
+
model.fit(train_dataloader, steps_per_epoch=10, epochs=1,verbose=1, validation_data=train_dataloader, validation_steps=1)
|
307 |
+
|
308 |
+
model.load_weights('models/bi_directional_concat_256_batch_160_epoch_30_length_ass_eng_nmt_weights.h5')
|
309 |
+
model.fit(train_dataloader, steps_per_epoch=10, epochs=1,verbose=1, validation_data=train_dataloader, validation_steps=1)
|
310 |
+
model.summary()
|
311 |
+
|
312 |
+
return model,tokenizer_eng,tokenizer_ass,in_input_length
|
313 |
+
# if __name__=="__main__":
|
314 |
+
# main()
|
models/bi_directional_concat_256_batch_160_epoch_30_length_ass_eng_nmt_weights.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:87b2c0bc456cb3feb5577eea4d62bdba08db30086c5a342491030569b0a700c4
|
3 |
+
size 130891904
|
tokenizer/30_tokenizer_ass.pickle
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:73a1b81f54315f32ca37eebbee823eec031dbe364aa2c3088e9bb8a8cbdda90d
|
3 |
+
size 3461824
|
tokenizer/30_tokenizer_eng.pickle
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:768f98cc461fe1f96b868c92d4bbcdaef5bbbe05daf85ad9e922125aa640b4a3
|
3 |
+
size 1209912
|