SansPapyrus683

Neural Network

Aug 4th, 2021
653
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import tensorflow as tf
  2.  
  3. keras = tf.keras
  4.  
  5. embed_size = 50
  6.  
  7. user_inp = keras.layers.Input(name="user", shape=(1,))
  8. news_inp = keras.layers.Input(name="news", shape=(1,))
  9. user_embed = keras.layers.Embedding(name="user_embed",
  10.                                     input_dim=len(user_id_to_num),
  11.                                     output_dim=embed_size)(user_inp)
  12. news_embed = keras.layers.Embedding(name="news_embed",
  13.                                     input_dim=len(news_id_to_num),
  14.                                     output_dim=embed_size)(news_inp)
  15.  
  16. # Merge the layers with a dot product along the second axis
  17. merged = keras.layers.Dot(name="merged", normalize=True,
  18.                           axes=2)([user_embed, news_embed])
  19. merged = keras.layers.Reshape(target_shape=(1,))(merged)
  20. out = keras.layers.Dense(1, activation="sigmoid")(merged)
  21. model = keras.Model(inputs=[user_inp, news_inp], outputs=out)
  22. model.compile(optimizer=keras.optimizers.Adam(), loss=keras.losses.mean_squared_error,
  23.               metrics=["accuracy"])
RAW Paste Data