Skip to content

Instantly share code, notes, and snippets.

@dipanjanS
Created March 13, 2018 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dipanjanS/dbff31351127c893a4579c9be11e113d to your computer and use it in GitHub Desktop.
Save dipanjanS/dbff31351127c893a4579c9be11e113d to your computer and use it in GitHub Desktop.
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'))
@Gli7ch
Copy link

Gli7ch commented Sep 27, 2018

Could you translate this to the Keras Functional API?

@Sinchit
Copy link

Sinchit commented Apr 14, 2021

I am getting error on this statement:
from keras.layers import Merge

Error is: can't import keras.layers.Merge
can you please help

@dipanjanS
Copy link
Author

@Sinchit
Copy link

Sinchit commented Apr 14, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment