how do i save my trained seq2seq model. i understand that the weights do not change in this model, and ive tried multiple functions i found online but none of them seem to work well. let me know if you need any info about the model
i tried checkpoints but it did not exactly work. it seems to have 'jumbled' the words.
here is a copy of the relevant part of my code:
def encode_sequences(tokenizer,length,lines):
seq=tokenizer.texts_to_sequences(lines)
seq=pad_sequences(seq,maxlen=length,padding='post')
return seq
from sklearn.model_selection import train_test_split
train,test=train_test_split(fra_eng,test_size=0.2,random_state=12)
trainX= encode_sequences(fra_tokenizer,fra_length,train[:,1])
trainY= encode_sequences(eng_tokenizer,eng_length,train[:,0])
testX= encode_sequences(fra_tokenizer, fra_length,test[:,1])
testY=encode_sequences(eng_tokenizer,eng_length,test[:,0])
def struct_model(in_vocab,out_vocab,a,b,neuron):
model = Sequential()
model.add(Embedding(in_vocab,neuron,input_length=a,mask_zero=True))
model.add(LSTM(neuron))
model.add(RepeatVector(8))
model.add(LSTM(neuron,return_sequences=True))
model.add(Dense(out_vocab,activation='softmax'))
return model
model=struct_model(fra_vocab_size,eng_vocab_size,fra_length,eng_length,512)
rms=optimizers.RMSprop(learning_rate=0.001)
model.compile(optimizer=rms,loss='sparse_categorical_crossentropy')
**checkpoint_path = "training_1/cp.h5"
checkpoint_dir = os.path.dirname(checkpoint_path)
cp_callback = ModelCheckpoint(filepath=checkpoint_path,
save_best_only=True,
save_weights_only=False,
verbose=1)**
history=model.fit(trainX , trainY.reshape(trainY.shape[0],trainY.shape[1],1),
epochs=50,batch_size=500,validation_split=0.1,callbacks=[cp_callback])
**# After training is complete
from keras.models import load_model
# Load the saved model
model = load_model("training_1/cp.h5")**
# You can then make predictions with the loaded model or continue training
import numpy as np
pred_probabilities = model.predict(testX.reshape((testX.shape[0], testY.shape[1])))
pred = np.argmax(pred_probabilities, axis=-1)
def gettingword(p,tokenizer):
for word,index in tokenizer.word_index.items():
if index ==p:
return word
preds_text = []
for i in pred:
temp = []
for j in range(len(i)):
t = gettingword(i[j], eng_tokenizer)
if j > 0:
if t and (t != gettingword(i[j-1], eng_tokenizer)):
temp.append(t)
else:
if t:
temp.append(t)
preds_text.append(' '.join(temp))
**output=pd.DataFrame({'french':test[:,1],'actual':test[:,0],'predicted' : preds_text})
output.sample(15)**