merge function is not working for Skipgram code
NK10 opened this issue · 3 comments
NK10 commented
in the below code with keras latest version, its not working.
from keras.layers import Merge
from keras.layers.core import Dense, Reshape
from keras.layers.embeddings import Embedding
from keras.models import Sequential
# build skip-gram architecture
word_model = Sequential()
word_model.add(Embedding(vocab_size, embed_size,
embeddings_initializer="glorot_uniform",
input_length=1))
word_model.add(Reshape((embed_size, )))
context_model = Sequential()
context_model.add(Embedding(vocab_size, embed_size,
embeddings_initializer="glorot_uniform",
input_length=1))
context_model.add(Reshape((embed_size,)))
model = Sequential()
model.add(Merge([word_model, context_model], mode="dot"))
model.add(Dense(1, kernel_initializer="glorot_uniform", activation="sigmoid"))
model.compile(loss="mean_squared_error", optimizer="rmsprop")
# view model summary
print(model.summary())
# visualize model structure
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
SVG(model_to_dot(model, show_shapes=True, show_layer_names=False,
rankdir='TB').create(prog='dot', format='svg'))
I have modified the code from sequential api to functional API. please find below the code:
from keras.layers import dot
from keras.layers import Dense, Input
from keras.layers.core import Reshape
from keras.layers import Embedding
from keras.models import Model
from keras.backend import reshape
# build skip-gram architecture
inp = Input(shape=(1,),name = "first_input")
word_model22 = Embedding(input_dim=vocab_size, output_dim=embed_size,
embeddings_initializer="glorot_uniform",
input_length=1)
emb = word_model22(inp)
word_model22 = Reshape(target_shape= (embed_size,))(emb)
inp1 = Input(shape=(1,),name = "2nd_input")
context_model = Embedding(input_dim=vocab_size, output_dim=embed_size,
embeddings_initializer="glorot_uniform",
input_length=1)
emb1 = context_model(inp1)
context_model = Reshape(target_shape= (embed_size,))(emb1)
mo = (dot([word_model22, context_model],axes=-1))
mo = (Dense(1, kernel_initializer="glorot_uniform", activation="sigmoid"))(mo)
model = Model(inputs = (inp, inp1), outputs =mo)
model.compile(loss="mean_squared_error", optimizer="rmsprop")
# view model summary
print(model.summary())
# visualize model structure
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
SVG(model_to_dot(model, show_shapes=True, show_layer_names=False,
rankdir='TB').create(prog='dot', format='svg'))
NK10 commented
sorry, didn't mention earlier, it was the merge part that is not working.
dipanjanS commented
Thanks, yes we are aware of this happening due to the deprecation of the Merge
API, I will be pushing this change soon in my 2nd edition of the text analytics book and once that is out, will be replacing the code here also!
amraneabdeslam commented
Hi, you can replace Merge by Concatenate
model.add(Merge([word_model, context_model], mode="dot"))
-->
model.add(Concatenate([word_model, context_model]))