Exemplo n.º 1
0
from collections import Counter
from keras.models import model_from_json
from keras.models import Sequential
from keras.layers.embeddings import Embedding
from keras.layers.core import Dense, Activation, Dropout, TimeDistributedDense,RepeatVector,Merge
from keras.layers.recurrent import GRU,LSTM
from keras.models import model_from_json
import helper

"""
script to train a char-level LSTM or GRU to predict the next word based on 
the previous words, previous chars, and topic vector
"""

#load our ModelHelper, pickled when training
mh      = helper.loadHelper("../models/recipeRNN3noState")

temp    = mh.word_indices

maxlen      = mh.get("maxlen")
maxWord     = mh.get("maxWord")
vecSize     = mh.get("vecSize")
batchSize   = 4

names       = mh.get("recipeNames")
conVecs     = mh.get("contextVectors")

charModel = Sequential()
charModel.add(LSTM(128, return_sequences=True,input_shape=(maxlen, mh.numChars)))
charModel.add(Dropout(.2))
#charModel.add(TimeDistributedDense(128))
Exemplo n.º 2
0
#print(contextVecs[:3])
#stop=raw_input("done transforming docs")

#these will be useful for sampling randomly as a seed
mh.add("contextVectors",contextVecs)
mh.add("recipeNames",names)

mh.save("../models/recipeRNN3noState")

#define how we go from document 


#define model
if len(sys.argv) > 1:
    model   = helper.loadThatModel(sys.argv[1])
    mh      = helper.loadHelper(sys.argv[1])
else:
    charModel = Sequential()
    charModel.add(LSTM(128, return_sequences=True,input_shape=(maxlen, mh.numChars)))
    charModel.add(Dropout(.2))
    #charModel.add(TimeDistributedDense(128))
    
    wordModel = Sequential()
    wordModel.add(Embedding(mh.vocSize+1, 256, input_length=maxWord))
    wordModel.add(LSTM(512, return_sequences=True))    
    wordModel.add(Dropout(.2))
    wordModel.add(LSTM(512, return_sequences=False))
    wordModel.add(RepeatVector(maxlen))
    
    contextModel    = Sequential()
    contextModel.add(Dense(256,input_shape=(vecSize,)))
Exemplo n.º 3
0
    contextVecs = contextVecs - np.mean(contextVecs,axis=0)
    contextVecs = contextVecs / (np.std(contextVecs,axis=0)+0.00000001)
    
    
    #these will be useful for sampling randomly as a seed
    mh.add("contextVectors",contextVecs)
    mh.add("recipeNames",names)
    mh.add("recipes",recipes)
    
    mh.save("../models/recipeRNN3noState")


#define model
if len(sys.argv) > 1:
    model = helper.loadThatModel(sys.argv[1])
    mh = helper.loadHelper(sys.argv[1])
    print("model loaded")
    contextVecs = mh.get("contextVectors")
    recipes = mh.get("recipes")
else:
    print("creating model")
    charModel = Sequential()
    charModel.add(LSTM(128, return_sequences=True,input_shape=(maxlen, mh.numChars)))
    charModel.add(Dropout(.2))
    #charModel.add(TimeDistributedDense(128))
    
    wordModel = Sequential()
    wordModel.add(Embedding(mh.vocSize, 512, input_length=maxWord))
    wordModel.add(Dropout(0.2))
    wordModel.add(LSTM(1024, return_sequences=True))    
    wordModel.add(Dropout(.2))