Skip to content

Instantly share code, notes, and snippets.

View jainanchit51's full-sized avatar

Anchit Jain jainanchit51

View GitHub Profile
import numpy as np
import tensorflow as tf
from keras import backend as K
q1 = input("Type Question 1 here -->")
q2 = input("Type Question 2 here -->")
q1 = np.array([[q1],[q1]])
q2 = np.array([[q2],[q2]])
# Using the same tensorflow session for embedding the test string
from keras.callbacks import ModelCheckpoint
# Creating the tensorflow session to train the model and save checkpoint after every epoch.
with tf.Session() as session:
K.set_session(session)
session.run(tf.global_variables_initializer())
session.run(tf.tables_initializer())
filepath="drive/My Drive/Colab Notebooks/Northout/model-{epoch:02d}-{val_acc:.2f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=True, mode='auto', period=1)
import tensorflow as tf
import tensorflow_hub as hub
# enabling the pretrained model for trainig our custom model using tensorflow hub
module_url = "https://tfhub.dev/google/universal-sentence-encoder-large/3"
embed = hub.Module(module_url)
# creating a method for embedding and will using method for every input layer
def UniversalEmbedding(x):
return embed(tf.squeeze(tf.cast(x, tf.string)), signature="default", as_dict=True)["default"]
# function that returns the 30 most similar movies based on the cosine similarity score.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def main():
title = request.args.get('movie')
idx = indices[title]
print("Index",idx)
similar_scores = list(enumerate(cosine_sim[idx]))
# Cosine similarity
from sklearn.metrics.pairwise import linear_kernel
cosine_sim = linear_kernel(tfidf_matrix, tfidf_matrix)
smd = smd.reset_index()
titles = smd['title']
# finding indices of every title
indices = pd.Series(smd.index, index=titles)
from sklearn.feature_extraction.text import TfidfVectorizer
tf = TfidfVectorizer(analyzer='word',ngram_range=(1, 3),min_df=0, stop_words='english')
tfidf_matrix = tf.fit_transform(smd['description'])
# mount your drive
from google.colab import drive
drive.mount('/content/drive')
# read the CSV file
md = pd. read_csv('drive/My Drive/Colab Notebooks/Movie_recommendation/movie_dataset/movies_metadata.csv')
# droping rows by index
md = md.drop([19730, 29503, 35587])
#performing look up opertion on all movies that are present in links_small dataset
new_text = ["We are looking for Social media professional"]
new_text = np.array(new_text, dtype=object)[:, np.newaxis]
with tf.Session() as session:
K.set_session(session)
session.run(tf.global_variables_initializer())
session.run(tf.tables_initializer())
model.load_weights('./model.h5')
predicts = model.predict(new_text, batch_size=32)
def create_conv_model():
model_conv = Sequential()
model_conv.add(Embedding(vocabulary_size, 100, input_length=50))
model_conv.add(Dropout(0.2))
model_conv.add(Conv1D(64, 5, activation='relu'))
model_conv.add(MaxPooling1D(pool_size=4))
model_conv.add(LSTM(100))
model_conv.add(Dense(1, activation='sigmoid'))
model_conv.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
return model_conv
input_text = layers.Input(shape=(1,), dtype=tf.string)
embedding = layers.Lambda(UniversalEmbedding, output_shape=(embed_size,))(input_text)
dense = layers.Dense(256, activation='relu')(embedding)
pred = layers.Dense(category_counts, activation='softmax')(dense)
model = Model(inputs=[input_text], outputs=pred)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()