Python keras.layers.Merge() Examples

The following are 30 code examples of keras.layers.Merge(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module keras.layers , or try the search function .
Example #1
Source Project: Sarcasm-Detection   Author: MirunaPislar   File: emoji2vec.py    License: MIT License 6 votes vote down vote up
def emoji2vec_model(embedding_matrix, emoji_vocab_size, word_vocab_size):
    emoji_model = Sequential()
    emoji_model.add(Embedding(emoji_vocab_size + 1, embedding_dim, input_length=1, trainable=True))
    emoji_model.add(Reshape((embedding_dim,)))
    word_model = Sequential()
    word_model.add(Embedding(word_vocab_size + 1, embedding_dim, weights=[embedding_matrix], input_length=maximum_length, trainable=False))
    word_model.add(Bidirectional(LSTM(embedding_dim, dropout=0.5), merge_mode='sum'))
    model = Sequential()
    model.add(Merge([emoji_model, word_model], mode='concat'))
    model.add(Dense(embedding_dim * 2, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(2, activation='softmax'))
    return emoji_model, word_model, model


# Solely based on emoji descriptions, obtain the emoji2vec representations for all possible emojis 
Example #2
Source Project: grammar-activity-prediction   Author: SiyuanQi   File: vgg_fine_tune.py    License: MIT License 6 votes vote down vote up
def sequential_model(input_dim_x1=1596, input_dim_x2=10, weights_path=None):
    left_branch = Sequential()
    left_branch.add(Dense(4096, activation='relu', init=my_init, input_dim=input_dim_x1))
    left_branch.add(Dropout(0.5))
    left_branch.add(Dense(2048, activation='relu', init=my_init))
    left_branch.add(Dropout(0.5))
    left_branch.add(Dense(512, activation='relu', init=my_init))
    right_branch = Sequential()
    right_branch.add(Dense(512, activation='relu', init=my_init, input_dim=input_dim_x2))
    merged = Merge([left_branch, right_branch], mode='concat')
    final_model = Sequential()
    final_model.add(merged)
    final_model.add(Dense(12))
    final_model.add(Activation('softmax'))
    if weights_path:
        final_model.load_weights(weights_path)
    return final_model 
Example #3
Source Project: Image-Captioning   Author: Shobhit20   File: SceneDesc.py    License: MIT License 6 votes vote down vote up
def create_model(self, ret_model = False):
	       
		image_model = Sequential()
		image_model.add(Dense(EMBEDDING_DIM, input_dim = 4096, activation='relu'))
		image_model.add(RepeatVector(self.max_length))

		lang_model = Sequential()
		lang_model.add(Embedding(self.vocab_size, 256, input_length=self.max_length))
		lang_model.add(LSTM(256,return_sequences=True))
		lang_model.add(TimeDistributed(Dense(EMBEDDING_DIM)))

		model = Sequential()
		model.add(Merge([image_model, lang_model], mode='concat'))
		model.add(LSTM(1000,return_sequences=False))
		model.add(Dense(self.vocab_size))
		model.add(Activation('softmax'))

		print ("Model created!")

		if(ret_model==True):
		    return model

		model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
		return model 
Example #4
Source Project: gandlf   Author: codekansas   File: wrappers.py    License: MIT License 5 votes vote down vote up
def call(self, x, mask=None):
        layer_output = self.layer.call(x, mask)
        if isinstance(self.merge_mode, str):
            self.merge_mode = Merge(mode=self.merge_mode)
        output = self.merge_mode([x, layer_output])
        return output 
Example #5
Source Project: gandlf   Author: codekansas   File: wrappers.py    License: MIT License 5 votes vote down vote up
def get_config(self):
        config = {'merge_mode': {'class_name': 'Merge',
                                 'config': self.merge_mode.get_config()}}
        base_config = super(Residual, self).get_config()
        return dict(list(base_config.items()) + list(config.items())) 
Example #6
Source Project: DeepLearn   Author: GauravBh1010tt   File: eval_fnc.py    License: MIT License 5 votes vote down vote up
def prepare_model(ninputs=9600, n_feats=45,nclass=4,n_tfidf=10001):
    inp1 = Input(shape=(ninputs,))
    inp2 = Input(shape=(n_feats,))
    inp3 = Input(shape=(n_tfidf,))
    reg = 0.00005
    out_neurons1 = 500
    #out_neurons2 = 20
    #out_neurons2 = 10
    m1 = Dense(input_dim=ninputs, output_dim=out_neurons1,activation='sigmoid'\
                      ,kernel_regularizer=regularizers.l2(0.00000001))(inp1)
    m1 = Dropout(0.2)(m1)
    m1 = Dense(100,activation='sigmoid')(m1)
    #m1 = Dropout(0.2)(m1)
    #m1 = Dense(4, activation='sigmoid')(m1)
    
    #m2 = Dense(input_dim=n_feats, output_dim=n_feats,activation='relu')(inp2)
    m2 = Dense(50,activation='relu')(inp2)
    #m2=Dense(4,activation='relu')(m2)
    
    m3 = Dense(500, input_dim=n_tfidf, activation='relu',\
                    kernel_regularizer=regularizers.l2(reg))(inp3)
    
    m3 = Dropout(0.4)(m3)
    m3 = Dense(50, activation='relu')(m3)
    #m3 = Dropout(0.4)(m3)
    #m3 = Dense(4, activation='softmax')(m3)
    
    
    #m1 = Dense(input_dim=ninputs, output_dim=out_neurons2,activation='sigmoid')(m1)
    #m2 = Dense(input_dim=ninputs, output_dim=out_neurons2,activation='softmax')(m2)
    
    m = Merge(mode='concat')([m1,m2,m3])
    
    #mul = Multiply()([m1,m2])
    #add = Abs()([m1,m2])
    #m = Merge(mode='concat')([mul,add])
    
    score = Dense(output_dim=nclass,activation='softmax')(m)
    model = Model([inp1,inp2,inp3],score)
    model.compile(loss='categorical_crossentropy', optimizer='adam')
    return model 
Example #7
Source Project: caption_generator   Author: anuragmishracse   File: caption_generator.py    License: MIT License 5 votes vote down vote up
def create_model(self, ret_model = False):
        #base_model = VGG16(weights='imagenet', include_top=False, input_shape = (224, 224, 3))
        #base_model.trainable=False
        image_model = Sequential()
        #image_model.add(base_model)
        #image_model.add(Flatten())
        image_model.add(Dense(EMBEDDING_DIM, input_dim = 4096, activation='relu'))

        image_model.add(RepeatVector(self.max_cap_len))

        lang_model = Sequential()
        lang_model.add(Embedding(self.vocab_size, 256, input_length=self.max_cap_len))
        lang_model.add(LSTM(256,return_sequences=True))
        lang_model.add(TimeDistributed(Dense(EMBEDDING_DIM)))

        model = Sequential()
        model.add(Merge([image_model, lang_model], mode='concat'))
        model.add(LSTM(1000,return_sequences=False))
        model.add(Dense(self.vocab_size))
        model.add(Activation('softmax'))

        print "Model created!"

        if(ret_model==True):
            return model

        model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
        return model 
Example #8
Source Project: VQA-Keras-Visual-Question-Answering   Author: anantzoid   File: models.py    License: MIT License 5 votes vote down vote up
def vqa_model(embedding_matrix, num_words, embedding_dim, seq_length, dropout_rate, num_classes):
    vgg_model = img_model(dropout_rate)
    lstm_model = Word2VecModel(embedding_matrix, num_words, embedding_dim, seq_length, dropout_rate)
    print "Merging final model..."
    fc_model = Sequential()
    fc_model.add(Merge([vgg_model, lstm_model], mode='mul'))
    fc_model.add(Dropout(dropout_rate))
    fc_model.add(Dense(1000, activation='tanh'))
    fc_model.add(Dropout(dropout_rate))
    fc_model.add(Dense(num_classes, activation='softmax'))
    fc_model.compile(optimizer='rmsprop', loss='categorical_crossentropy',
        metrics=['accuracy'])
    return fc_model 
Example #9
Source Project: Audio-Vision   Author: channelCS   File: my_models.py    License: MIT License 5 votes vote down vote up
def basic_mlp(img_vec_dim, vocabulary_size, word_emb_dim,
              max_ques_length, num_hidden_units_lstm, 
              num_hidden_layers_mlp, num_hidden_units_mlp,
              dropout, nb_classes, class_activation):
    # Image model
    model_image = Sequential()
    model_image.add(Reshape((img_vec_dim,), input_shape=(img_vec_dim,)))

    # Language Model
    model_language = Sequential()
    model_language.add(Embedding(vocabulary_size, word_emb_dim, input_length=max_ques_length))
    model_language.add(LSTM(num_hidden_units_lstm, return_sequences=True, input_shape=(max_ques_length, word_emb_dim)))
    model_language.add(LSTM(num_hidden_units_lstm, return_sequences=True))
    model_language.add(LSTM(num_hidden_units_lstm, return_sequences=False))

    # combined model
    model = Sequential()
    model.add(Merge([model_language, model_image], mode='concat', concat_axis=1))


    for i in xrange(num_hidden_layers_mlp):
        model.add(Dense(num_hidden_units_mlp))
        model.add(Dropout(dropout))

    model.add(Dense(nb_classes))
    model.add(Activation(class_activation))

    return model 
Example #10
Source Project: Audio-Vision   Author: channelCS   File: my_models.py    License: MIT License 5 votes vote down vote up
def deeper_lstm(img_vec_dim, activation_1,activation_2, dropout, vocabulary_size,
                num_hidden_units_lstm, max_ques_length,
                word_emb_dim, num_hidden_layers_mlp,
                num_hidden_units_mlp, nb_classes, class_activation,embedding_matrix):
    
    # Make image model
    inpx1=Input(shape=(img_vec_dim,))
    x1=Dense(1024, activation=activation_1)(inpx1)
    x1=Dropout(dropout)(x1)
    image_model = Model([inpx1],x1)
    image_model.summary()
    
    # Make language Model
    inpx0=Input(shape=(max_ques_length,))
    x0=Embedding(vocabulary_size, word_emb_dim, weights=[embedding_matrix], trainable=False)(inpx0)
    x1=LSTM(num_hidden_units_lstm, return_sequences=True)(x0)
    x1=LSTM(num_hidden_units_lstm, return_sequences=True)(x1)
    x2=LSTM(num_hidden_units_lstm, return_sequences=False)(x1)
    x2=Dense(1024,activation=activation_2)(x2)
    x2=Dropout(dropout)(x2)

    # Make embedding_model
    embedding_model = Model([inpx0],x2)
    embedding_model.summary()
    
    # Make combined model
    model = Sequential()
    model.add(Merge([image_model,embedding_model],mode = 'mul'))
    for i in xrange(num_hidden_layers_mlp):
        model.add(Dense(num_hidden_units_mlp))
        model.add(Activation(activation_1))
        model.add(Dropout(dropout))
    model.summary()
    model.add(Dense(nb_classes))
    model.add(Activation(class_activation))

    return model 
Example #11
Source Project: Audio-Vision   Author: channelCS   File: my_models.py    License: MIT License 5 votes vote down vote up
def visual_lstm(img_vec_dim, activation_1,activation_2, dropout, vocabulary_size,
                num_hidden_units_lstm, max_ques_length,
                word_emb_dim, num_hidden_layers_mlp,
                num_hidden_units_mlp, nb_classes, class_activation,embedding_matrix):
    
    # Make image model
    inpx1=Input(shape=(img_vec_dim,))
    x1=Dense(embedding_matrix.shape[1], activation='tanh')(inpx1)
    x1=Reshape((1,embedding_matrix.shape[1]))(x1)
    image_model = Model([inpx1],x1)
    image_model.summary()
    
    # Make language Model
    inpx0=Input(shape=(max_ques_length,))
    x0=Embedding(vocabulary_size, word_emb_dim, weights=[embedding_matrix], trainable=False)(inpx0)
    x2=Dense(embedding_matrix.shape[1],activation='tanh')(x0)
    x2=Dropout(dropout)(x2)

    # Make embedding_model
    embedding_model = Model([inpx0],x2)
    embedding_model.summary()
    
    # Make combined model
    model = Sequential()
    model.add(Merge([image_model,embedding_model],mode = 'concat', concat_axis=1))
    model.add(LSTM(num_hidden_units_lstm, return_sequences=False, go_backwards=True))
    model.add(Dense(num_hidden_units_mlp))
    model.add(Activation('relu'))
    model.add(Dropout(dropout))
    
    model.summary()
    model.add(Dense(nb_classes))
    model.add(Activation(class_activation))

    return model 
Example #12
Source Project: Audio-Vision   Author: channelCS   File: my_models.py    License: MIT License 5 votes vote down vote up
def visual_lstm2(img_vec_dim, activation_1,activation_2, dropout, vocabulary_size,
                num_hidden_units_lstm, max_ques_length,
                word_emb_dim, num_hidden_layers_mlp,
                num_hidden_units_mlp, nb_classes, class_activation,embedding_matrix):
    
    # Make image model
    inpx1=Input(shape=(img_vec_dim,))
    x1=Dense(embedding_matrix.shape[1], activation=activation_1)(inpx1)
    x1=Reshape((1,embedding_matrix.shape[1]))(x1)
    image_model = Model([inpx1],x1)
    image_model.summary()
    
    # Make language Model
    inpx0=Input(shape=(max_ques_length,))
    x0=Embedding(vocabulary_size, word_emb_dim, weights=[embedding_matrix], trainable=False)(inpx0)
    x2=Dense(embedding_matrix.shape[1],activation=activation_2)(x0)
    x2=Dropout(dropout)(x2)

    # Make embedding_model
    embedding_model = Model([inpx0],x2)
    embedding_model.summary()
    
    inpx2=Input(shape=(img_vec_dim,))
    x1=Dense(embedding_matrix.shape[1], activation=activation_1)(inpx1)
    x3=Reshape((1,embedding_matrix.shape[1]))(x1)
    image_model2 = Model([inpx2],x3)
    image_model2.summary()
    
    # Make combined model
    model = Sequential()
    model.add(Merge([image_model,embedding_model, image_model2],mode = 'concat', concat_axis=1))
    model.add(Bidirectional(LSTM(num_hidden_units_lstm, return_sequences=False)))
    model.add(Dense(num_hidden_units_mlp))
    model.add(Activation(activation_1))
    model.add(Dropout(dropout))
    
    model.summary()
    model.add(Dense(nb_classes))
    model.add(Activation(class_activation))

    return model 
Example #13
Source Project: DeepCCA   Author: VahidooX   File: models.py    License: MIT License 5 votes vote down vote up
def create_model(layer_sizes1, layer_sizes2, input_size1, input_size2,
                    learning_rate, reg_par, outdim_size, use_all_singular_values):
    """
    builds the whole model
    the structure of each sub-network is defined in build_mlp_net,
    and it can easily get substituted with a more efficient and powerful network like CNN
    """
    view1_model = build_mlp_net(layer_sizes1, input_size1, reg_par)
    view2_model = build_mlp_net(layer_sizes2, input_size2, reg_par)

    model = Sequential()
    model.add(Merge([view1_model, view2_model], mode='concat'))

    model_optimizer = RMSprop(lr=learning_rate)
    model.compile(loss=cca_loss(outdim_size, use_all_singular_values), optimizer=model_optimizer)

    return model 
Example #14
Source Project: keras-wide-n-deep   Author: jorahn   File: wide_deep_keras.py    License: MIT License 5 votes vote down vote up
def main():
    df_train = load('adult.data')
    df_test = load('adult.test')
    df = pd.concat([df_train, df_test])
    train_len = len(df_train)
    
    X, y = preprocess(df)
    X_train = X[:train_len]
    y_train = y[:train_len]
    X_test = X[train_len:]
    y_test = y[train_len:]
    
    wide = Sequential()
    wide.add(Dense(1, input_dim=X_train.shape[1]))
    
    deep = Sequential()
    # TODO: add embedding
    deep.add(Dense(input_dim=X_train.shape[1], output_dim=100, activation='relu'))
    deep.add(Dense(100, activation='relu'))
    deep.add(Dense(50, activation='relu'))
    deep.add(Dense(1, activation='sigmoid'))
    
    model = Sequential()
    model.add(Merge([wide, deep], mode='concat', concat_axis=1))
    model.add(Dense(1, activation='sigmoid'))
    
    model.compile(
        optimizer='rmsprop',
        loss='binary_crossentropy',
        metrics=['accuracy']
    )
    
    model.fit([X_train, X_train], y_train, nb_epoch=10, batch_size=32)
    
    loss, accuracy = model.evaluate([X_test, X_test], y_test)
    print('\n', 'test accuracy:', accuracy) 
Example #15
Source Project: grammar-activity-prediction   Author: SiyuanQi   File: affordance_lstm.py    License: MIT License 5 votes vote down vote up
def train(x_1_train, x_2_train, y_train, x_1_test, x_2_test, y_test, model_path):
    batch_size = 16
    model_name = 'layer_3_with_dropout_0.5_dropoutWU_0.4_maxlen_200_epoch_300_feature_completion.h5'
    left_branch = Sequential()
    left_branch.add(Dense(32, input_dim=x_1_train.shape[1]))
    # left_branch.add(Flatten())
    right_branch = Sequential()
    right_branch.add(LSTM(128, return_sequences=True, dropout_U=0.4, dropout_W=0.4, input_shape=x_2_train.shape[1:3]))
    right_branch.add(Dropout(0.5))
    right_branch.add(LSTM(64, dropout_U=0.4, dropout_W=0.4))
    right_branch.add(Dropout(0.5))
    right_branch.add(Dense(32))
    merged = Merge([left_branch, right_branch], mode='concat')
    final_model = Sequential()
    final_model.add(merged)
    final_model.add(Dense(12))
    final_model.add(Activation('softmax'))
    final_model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
    plot(final_model, to_file=model_path + model_name[:-3] + '.png', show_shapes=True)
    plot(right_branch, to_file=model_path + model_name[:-3] + '_right_branch.png', show_shapes=True)
    plot(left_branch, to_file=model_path + model_name[:-3] + '_left_branch.png', show_shapes=True)
    final_model.fit([x_1_train, x_2_train], y_train, batch_size=batch_size, nb_epoch=300, validation_data=([x_1_test, x_2_test], y_test))
    final_model.save(model_path + model_name)
    score, acc = final_model.evaluate([x_1_test, x_2_test], y_test, batch_size=batch_size)
    print('Test Score', score)
    print('Test Accuracy', acc)
    return model_path + model_name 
Example #16
Source Project: semeval2017-scienceie   Author: UKPLab   File: convNet.py    License: Apache License 2.0 5 votes vote down vote up
def build_cnn_char_complex(input_dim, output_dim,nb_filter):
    randomEmbeddingLayer = Embedding(input_dim,32, input_length=maxlen,dropout=0.1)
    poolingLayer = Lambda(max_1d, output_shape=(nb_filter,))
    conv_filters = []
    for n_gram in range(2,4):
        ngramModel = Sequential()
        ngramModel.add(randomEmbeddingLayer)
        ngramModel.add(Convolution1D(nb_filter=nb_filter,
                                     filter_length=n_gram,
                                     border_mode="valid",
                                     activation="relu",
                                     subsample_length=1))
        ngramModel.add(poolingLayer)
        conv_filters.append(ngramModel)
    
    clf = Sequential()
    clf.add(Merge(conv_filters,mode="concat"))
    clf.add(Activation("relu"))
    clf.add(Dense(100))
    clf.add(Dropout(0.1))
    clf.add(Activation("tanh"))
    clf.add(Dense(output_dim=output_dim, activation='softmax'))

    clf.compile(optimizer='adagrad',
                     loss='categorical_crossentropy',
                     metrics=['accuracy'])
    return clf 
Example #17
Source Project: cv   Author: vsmolyakov   File: caption_generator.py    License: MIT License 5 votes vote down vote up
def create_model(self, ret_model = False):

        #image branch
        image_model = Sequential()
        image_model.add(Dense(EMBEDDING_DIM, input_dim = 2048, activation='relu'))
        image_model.add(RepeatVector(self.max_cap_len))

        #text branch
        lang_model = Sequential()
        lang_model.add(Embedding(self.vocab_size, 256, input_length=self.max_cap_len))
        lang_model.add(LSTM(256,return_sequences=True))
        lang_model.add(TimeDistributed(Dense(EMBEDDING_DIM)))

        #concatenated
        model = Sequential()
        model.add(Merge([image_model, lang_model], mode='concat'))
        model.add(LSTM(1024, dropout=0.2, recurrent_dropout=0.2, return_sequences=False))
        model.add(Dense(self.vocab_size))
        model.add(Activation('softmax'))

        print "Model created!"

        if(ret_model==True):
            return model

        adam = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
        model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
        return model 
Example #18
Source Project: keras-resnet   Author: codekansas   File: resnet.py    License: MIT License 5 votes vote down vote up
def call(self, x, mask=None):
        layer_output = self.layer.call(x, mask)
        if isinstance(self.merge_mode, str):
            self.merge_mode = Merge(mode=self.merge_mode)
        output = self.merge_mode([x, layer_output])
        return output 
Example #19
Source Project: keras-resnet   Author: codekansas   File: resnet.py    License: MIT License 5 votes vote down vote up
def get_config(self):
        config = {"merge_mode": {'class_name': 'Merge',
                                 'config': self.merge_mode.get_config()}}
        base_config = super(Residual, self).get_config()
        return dict(list(base_config.items()) + list(config.items())) 
Example #20
Source Project: tartarus   Author: sergiooramas   File: models.py    License: MIT License 4 votes vote down vote up
def get_model_4(params):
    embedding_weights = pickle.load(open(common.TRAINDATA_DIR+"/embedding_weights_w2v_%s.pk" % params['embeddings_suffix'],"rb"))
    graph_in = Input(shape=(params['sequence_length'], params['embedding_dim']))
    convs = []
    for fsz in params['filter_sizes']:
        conv = Convolution1D(nb_filter=params['num_filters'],
                             filter_length=fsz,
                             border_mode='valid',
                             activation='relu',
                             subsample_length=1)
        x = conv(graph_in)
        logging.debug("Filter size: %s" % fsz)
        logging.debug("Output CNN: %s" % str(conv.output_shape))

        pool = GlobalMaxPooling1D()
        x = pool(x)
        logging.debug("Output Pooling: %s" % str(pool.output_shape))
        convs.append(x)

    if len(params['filter_sizes'])>1:
        merge = Merge(mode='concat')
        out = merge(convs)
        logging.debug("Merge: %s" % str(merge.output_shape))
    else:
        out = convs[0]

    graph = Model(input=graph_in, output=out)

    # main sequential model
    model = Sequential()
    if not params['model_variation']=='CNN-static':
        model.add(Embedding(len(embedding_weights[0]), params['embedding_dim'], input_length=params['sequence_length'],
                            weights=embedding_weights))
    model.add(Dropout(params['dropout_prob'][0], input_shape=(params['sequence_length'], params['embedding_dim'])))
    model.add(graph)
    model.add(Dense(params['n_dense']))
    model.add(Dropout(params['dropout_prob'][1]))
    model.add(Activation('relu'))

    model.add(Dense(output_dim=params["n_out"], init="uniform"))
    model.add(Activation(params['final_activation']))
    logging.debug("Output CNN: %s" % str(model.output_shape))

    if params['final_activation'] == 'linear':
        model.add(Lambda(lambda x :K.l2_normalize(x, axis=1)))

    return model

# word2vec ARCH with LSTM 
Example #21
Source Project: DeepLearn   Author: GauravBh1010tt   File: DeepLearn_cornet.py    License: MIT License 4 votes vote down vote up
def buildModel(loss_type,lamda):

    inpx = Input(shape=(dimx,))
    inpy = Input(shape=(dimy,))

    hx = Dense(hdim_deep,activation='sigmoid')(inpx)
    hx = Dense(hdim_deep2, activation='sigmoid',name='hid_l1')(hx)
    hx = Dense(hdim, activation='sigmoid',name='hid_l')(hx)
    
    hy = Dense(hdim_deep,activation='sigmoid')(inpy)
    hy = Dense(hdim_deep2, activation='sigmoid',name='hid_r1')(hy)
    hy = Dense(hdim, activation='sigmoid',name='hid_r')(hy)

    #h = Activation("sigmoid")( Merge(mode="sum")([hx,hy]) )
    h =  Merge(mode="sum")([hx,hy]) 
    
    #recx = Dense(hdim_deep,activation='sigmoid')(h)
    recx = Dense(dimx)(h)
    #recy = Dense(hdim_deep,activation='sigmoid')(h)
    recy = Dense(dimy)(h)
    
    branchModel = Model( [inpx,inpy],[recx,recy,h])

    #inpx = Input(shape=(dimx,))
    #inpy = Input(shape=(dimy,))

    [recx1,recy1,h1] = branchModel( [inpx, ZeroPadding()(inpy)])
    [recx2,recy2,h2] = branchModel( [ZeroPadding()(inpx), inpy ])

    #you may probably add a reconstruction from combined
    [recx3,recy3,h] = branchModel([inpx, inpy])

    corr=CorrnetCost(-lamda)([h1,h2])
    
    if loss_type == 1:
        model = Model( [inpx,inpy],[recy1,recx2,recx3,recx1,recy2,recy3,corr])
        model.compile( loss=["mse","mse","mse","mse","mse","mse",corr_loss],optimizer="rmsprop")
    elif loss_type == 2:
        model = Model( [inpx,inpy],[recy1,recx2,recx1,recy2,corr])
        model.compile( loss=["mse","mse","mse","mse",corr_loss],optimizer="rmsprop")
    elif loss_type == 3:
        model = Model( [inpx,inpy],[recy1,recx2,recx3,recx1,recy2,recy3])
        model.compile( loss=["mse","mse","mse","mse","mse","mse"],optimizer="rmsprop")
    elif loss_type == 4:
        model = Model( [inpx,inpy],[recy1,recx2,recx1,recy2])
        model.compile( loss=["mse","mse","mse","mse"],optimizer="rmsprop")

    return model, branchModel 
Example #22
Source Project: DeepLearn   Author: GauravBh1010tt   File: model.py    License: MIT License 4 votes vote down vote up
def cnn(embedding_matrix, dimx=50, dimy=50, nb_filter = 120, 
        embedding_dim = 50,filter_length = (50,4), vocab_size = 8000, depth = 1):

    print 'Model Uses Basic CNN......'
    
    inpx = Input(shape=(dimx,),dtype='int32',name='inpx')   
    inpy = Input(shape=(dimy,),dtype='int32',name='inpy')
    
    x = word2vec_embedding_layer(embedding_matrix,train=False)(inpx)
    y = word2vec_embedding_layer(embedding_matrix,train=False)(inpy)
    
    x = Permute((2,1))(x)
    y = Permute((2,1))(y)

    conv1 = Reshape((embedding_dim,dimx,1))(x)
    conv2 = Reshape((embedding_dim,dimy,1))(y)   
       
    channel_1, channel_2 = [], []
    
    for dep in range(depth):
        
        #conv1 = ZeroPadding2D((filter_width - 1, 0))(conv1)
        #conv2 = ZeroPadding2D((filter_width - 1, 0))(conv2)
        

        ques = Conv2D(nb_filter=nb_filter, kernel_size = filter_length, activation='relu',
                data_format = 'channels_last',border_mode="valid")(conv1)
        ans = Conv2D(nb_filter, kernel_size = filter_length, activation='relu',
                data_format="channels_last",border_mode="valid")(conv2)
                    
            
        #conv1 = GlobalMaxPooling2D()(ques)
        #conv2 = GlobalMaxPooling2D()(ans)
        #conv1 = MaxPooling2D()(ques)
        #conv2 = MaxPooling2D()(ans)
        
        channel_1.append(GlobalMaxPooling2D()(ques))
        channel_2.append(GlobalMaxPooling2D()(ans))
        
        #channel_1.append(GlobalAveragePooling2D()(ques))
        #channel_2.append(GlobalAveragePooling2D()(ans))
    
    h1 = channel_1.pop(-1)
    if channel_1:
        h1 = merge([h1] + channel_1, mode="concat")

    h2 = channel_2.pop(-1)
    if channel_2:
        h2 = merge([h2] + channel_2, mode="concat")
    
    h =  Merge(mode="concat",name='h')([h1, h2])
    #h = Dropout(0.2)(h)
    #h = Dense(50, kernel_regularizer=regularizers.l2(reg2),activation='relu')(h)
    #wrap = Dropout(0.5)(h)
    #wrap = Dense(64, activation='tanh')(h)   
    
    score = Dense(2,activation='softmax',name='score')(h)
    model = Model([inpx, inpy],[score])
    model.compile( loss='categorical_crossentropy',optimizer='adam')
    
    return model 
Example #23
Source Project: DeepLearn   Author: GauravBh1010tt   File: p3_cnn.py    License: MIT License 4 votes vote down vote up
def trainCNN(obj, dataset_headLines, dataset_body):
    embedding_dim = 300
    LSTM_neurons = 50
    dense_neuron = 16
    dimx = 100
    dimy = 200
    lamda = 0.0
    nb_filter = 100
    filter_length = 4
    vocab_size = 10000
    batch_size = 50
    epochs = 5
    ntn_out = 16
    ntn_in = nb_filter 
    state = False
    
    
    train_head,train_body,embedding_matrix = obj.process_data(sent_Q=dataset_headLines,
                                                     sent_A=dataset_body,dimx=dimx,dimy=dimy,
                                                     wordVec_model = wordVec_model)    
    inpx = Input(shape=(dimx,),dtype='int32',name='inpx')
    #x = Embedding(output_dim=embedding_dim, input_dim=vocab_size, input_length=dimx)(inpx)
    x = word2vec_embedding_layer(embedding_matrix)(inpx)  
    inpy = Input(shape=(dimy,),dtype='int32',name='inpy')
    #y = Embedding(output_dim=embedding_dim, input_dim=vocab_size, input_length=dimy)(inpy)
    y = word2vec_embedding_layer(embedding_matrix)(inpy)
    ques = Convolution1D(nb_filter=nb_filter, filter_length=filter_length,
                         border_mode='valid', activation='relu',
                         subsample_length=1)(x)
                            
    ans = Convolution1D(nb_filter=nb_filter, filter_length=filter_length,
                        border_mode='valid', activation='relu',
                        subsample_length=1)(y)
            
    #hx = Lambda(max_1d, output_shape=(nb_filter,))(ques)
    #hy = Lambda(max_1d, output_shape=(nb_filter,))(ans)
    hx = GlobalMaxPooling1D()(ques)
    hy = GlobalMaxPooling1D()(ans)
    #wordVec_model = []
    #h =  Merge(mode="concat",name='h')([hx,hy])
    
    h1 = Multiply()([hx,hy])
    h2 = Abs()([hx,hy])

    h =  Merge(mode="concat",name='h')([h1,h2])
    #h = NeuralTensorLayer(output_dim=1,input_dim=ntn_in)([hx,hy])
    #h = ntn_layer(ntn_in,ntn_out,activation=None)([hx,hy])
    #score = h
    wrap = Dense(dense_neuron, activation='relu',name='wrap')(h)
    #score = Dense(1,activation='sigmoid',name='score')(h)
    #wrap = Dense(dense_neuron,activation='relu',name='wrap')(h)
    score = Dense(4,activation='softmax',name='score')(wrap)
    
    #score=K.clip(score,1e-7,1.0-1e-7)
    #corr = CorrelationRegularization(-lamda)([hx,hy])
    #model = Model( [inpx,inpy],[score,corr])
    model = Model( [inpx,inpy],score)
    model.compile( loss='categorical_crossentropy',optimizer="adadelta",metrics=['accuracy'])    
    return model,train_head,train_body 
Example #24
Source Project: DeepLearn   Author: GauravBh1010tt   File: eval_sick.py    License: MIT License 4 votes vote down vote up
def prepare_model(ninputs=9600,n_feats=47, nclass=5):
    """
    Set up and compile the model architecture (Logistic regression)
    """
    inp1 = Input(shape=(ninputs,))
    inp2 = Input(shape=(n_feats,))
    out_neurons1 = 50
    out_neurons2 = 20
    out_neurons2 = 10
    m1 = Dense(input_dim=ninputs, output_dim=out_neurons1,activation='sigmoid')(inp1)
    m2 = Dense(input_dim=ninputs, output_dim=out_neurons1,activation='softmax')(inp2)
    
    m1 = Dense(input_dim=ninputs, output_dim=out_neurons2,activation='sigmoid')(m1)
    m2 = Dense(input_dim=ninputs, output_dim=out_neurons2,activation='softmax')(m2)
    
    #m1 = Dense(input_dim=ninputs, output_dim=out_neurons2,activation='sigmoid')(m1)
    #m2 = Dense(input_dim=ninputs, output_dim=out_neurons2,activation='softmax')(m2)
    
    m = Merge(mode='concat')([m1,m2])
    
    #mul = Multiply()([m1,m2])
    #add = Abs()([m1,m2])
    #m = Merge(mode='concat')([mul,add])
    
    score = Dense(output_dim=nclass,activation='softmax')(m)
    model = Model([inp1,inp2],score)
    model.compile(loss='categorical_crossentropy', optimizer='adam')
    return model
    '''
    lrmodel = Sequential()
    lrmodel.add(Dense(input_dim=ninputs, output_dim=nclass))
    #lrmodel.add(Activation('softmax'))
    #lrmodel.compile(loss='categorical_crossentropy', optimizer='adam')
    
    #return lrmodel
    
    model_feat = Sequential()
    model_feat.add(Dense(input_dim=27, output_dim=nclass))
    merge_model = Sequential()
    merge_model.add(Merge([lrmodel, model_feat], mode='concat'))
    merge_model.add(Dense(output_dim=nclass))
    merge_model.add(Activation('softmax'))
    merge_model.compile(loss='categorical_crossentropy', optimizer='adam')
    return merge_model'''
    
    '''lrmodel.add(Dense(input_dim=ninputs, output_dim=1000,activation = 'relu'))
    lrmodel.add(Dropout(0.5))
    lrmodel.add(Dense(output_dim=500,activation = 'relu'))
    lrmodel.add(Dropout(0.5))
    lrmodel.add(Dense(output_dim=nclass))'''
    #return merge_model 
Example #25
Source Project: DeepLearn   Author: GauravBh1010tt   File: eval_fnc.py    License: MIT License 4 votes vote down vote up
def prepare_model1(ninputs=9600, n_feats=45,nclass=4,n_tfidf=10001):
    inp1 = Input(shape=(ninputs,))
    inp2 = Input(shape=(n_feats,))
    inp3 = Input(shape=(n_tfidf,))
    reg = 0.00005
    out_neurons1 = 500
    #out_neurons2 = 20
    #out_neurons2 = 10
    m1 = Dense(input_dim=ninputs, output_dim=out_neurons1,activation='sigmoid'\
                      ,kernel_regularizer=regularizers.l2(0.00000001))(inp1)
    m1 = Dropout(0.5)(m1)
    m1 = Dense(100,activation='sigmoid')(m1)
    m1 = Dropout(0.5)(m1)
    
    m2 = Dense(input_dim=n_feats, output_dim=n_feats,activation='relu')(inp2)
    m2 = Dense(30,activation='relu')(m2)
    
    
    m3 = Dense(500, input_dim=n_tfidf, activation='relu',\
                    kernel_regularizer=regularizers.l2(reg))(inp3)
    
    m3 = Dropout(0.6)(m3)
    m3 = Dense(100, activation='relu')(m3)
    m3 = Dropout(0.4)(m3)
    m3 = Dense(4, activation='softmax')(m3)
    
    
    #m1 = Dense(input_dim=ninputs, output_dim=out_neurons2,activation='sigmoid')(m1)
    #m2 = Dense(input_dim=ninputs, output_dim=out_neurons2,activation='softmax')(m2)
    
    m = Merge(mode='concat')([m1,m2,m3])
    
    #mul = Multiply()([m1,m2])
    #add = Abs()([m1,m2])
    #m = Merge(mode='concat')([mul,add])
    
    score = Dense(output_dim=nclass,activation='softmax')(m)
    model = Model([inp1,inp2,inp3],score)
    model.compile(loss='categorical_crossentropy', optimizer='adam')
    return model
    
    """
    Set up and compile the model architecture (Logistic regression)
    
    print 'changed'
    out_neurons1 = 500
    lrmodel = Sequential()
    lrmodel.add(Dense(input_dim=ninputs, output_dim=out_neurons1,activation='sigmoid'\
                      ,kernel_regularizer=regularizers.l2(0.00000001)))
    lrmodel.add(Dropout(0.5))
    #lrmodel.add(Dense(out_neurons2))
    #lrmodel.add(Dropout(0.5))
    lrmodel.add(Dense(output_dim=nclass))
    
    #lrmodel.add(Dense(input_dim=ninputs, output_dim=nclass))
    #lrmodel.add(Dropout(0.3))
    lrmodel.add(Activation('softmax'))
    lrmodel.compile(loss='categorical_crossentropy', optimizer='adam')
    return lrmodel
    """ 
Example #26
Source Project: DeepLearn   Author: GauravBh1010tt   File: CorrMCNN_Arch2.py    License: MIT License 4 votes vote down vote up
def buildModel(loss_type,lamda):
  
    inpx = Input(shape=(dimx,))
    inpy = Input(shape=(dimy,))

    hx = Reshape((28,14,1))(inpx)
    hx = Conv2D(128, (3, 3), activation='relu', padding='same')(hx)
    hx = MaxPooling2D((2, 2), padding='same')(hx)
    hx = Conv2D(64, (3, 3), activation='relu', padding='same')(hx)
    hx = MaxPooling2D((2, 2), padding='same')(hx)
    hx = Conv2D(49, (3, 3), activation='relu', padding='same')(hx)
    hx = MaxPooling2D((2, 2), padding='same')(hx)
    hx = Flatten()(hx)
    hx1 = Dense(hdim_deep,activation='sigmoid')(hx)
    hx2 = Dense(hdim_deep2, activation='sigmoid',name='hid_l1')(hx1)
    hx = Dense(hdim, activation='sigmoid',name='hid_l')(hx2)

    hy = Reshape((28,14,1))(inpy)
    hy = Conv2D(128, (3, 3), activation='relu', padding='same')(hy)
    hy = MaxPooling2D((2, 2), padding='same')(hy)
    hy = Conv2D(64, (3, 3), activation='relu', padding='same')(hy)
    hy = MaxPooling2D((2, 2), padding='same')(hy)
    hy = Conv2D(49, (3, 3), activation='relu', padding='same')(hy)
    hy = MaxPooling2D((2, 2), padding='same')(hy)
    hy = Flatten()(hy)
    hy1 = Dense(hdim_deep,activation='sigmoid')(hy)
    hy2 = Dense(hdim_deep2, activation='sigmoid',name='hid_r1')(hy1)
    hy = Dense(hdim, activation='sigmoid',name='hid_r')(hy2)

    h =  Merge(mode="sum")([hx,hy]) 
    
    recx = Dense(dimx)(h)
    recy = Dense(dimy)(h)
    
    branchModel = Model( [inpx,inpy],[recx,recy,h,hx1,hy1,hx2,hy2])

    [recx1,recy1,h1,_,_,_,_] = branchModel( [inpx, ZeroPadding()(inpy)])
    [recx2,recy2,h2,_,_,_,_] = branchModel( [ZeroPadding()(inpx), inpy ])

    #you may probably add a reconstruction from combined
    [recx3,recy3,h3,hx_1,hy_1,hx_2,hy_2] = branchModel([inpx, inpy])
    
    lamda2,lamda3 = 0.001,0.05
    
    corr1=CorrnetCost(-lamda)([h1,h2])
    corr2=CorrnetCost(-lamda2)([hx_1,hy_1])
    corr3=CorrnetCost(-lamda3)([hx_2,hy_2])
    
    model = Model( [inpx,inpy],[recy1,recx2,recx1,recy2,corr1,corr2,corr3])
    model.compile( loss=["mse","mse","mse","mse",corr_loss,corr_loss,corr_loss],optimizer="rmsprop")

    return model, branchModel 
Example #27
Source Project: DeepLearn   Author: GauravBh1010tt   File: model.py    License: MIT License 4 votes vote down vote up
def cnn(embedding_matrix, dimx=50, dimy=50, nb_filter = 120, 
        embedding_dim = 50,filter_length = (50,4), vocab_size = 8000, depth = 1):

    print 'Model Uses Basic CNN......'
    
    inpx = Input(shape=(dimx,),dtype='int32',name='inpx')   
    inpy = Input(shape=(dimy,),dtype='int32',name='inpy')
    
    x = word2vec_embedding_layer(embedding_matrix,train=False)(inpx)
    y = word2vec_embedding_layer(embedding_matrix,train=False)(inpy)
    
    x = Permute((2,1))(x)
    y = Permute((2,1))(y)

    conv1 = Reshape((embedding_dim,dimx,1))(x)
    conv2 = Reshape((embedding_dim,dimy,1))(y)   
       
    channel_1, channel_2 = [], []
    
    for dep in range(depth):
        
        #conv1 = ZeroPadding2D((filter_width - 1, 0))(conv1)
        #conv2 = ZeroPadding2D((filter_width - 1, 0))(conv2)
        

        ques = Conv2D(nb_filter=nb_filter, kernel_size = filter_length, activation='relu',
                data_format = 'channels_last',border_mode="valid")(conv1)
        ans = Conv2D(nb_filter, kernel_size = filter_length, activation='relu',
                data_format="channels_last",border_mode="valid")(conv2)
                    
            
        #conv1 = GlobalMaxPooling2D()(ques)
        #conv2 = GlobalMaxPooling2D()(ans)
        #conv1 = MaxPooling2D()(ques)
        #conv2 = MaxPooling2D()(ans)
        
        channel_1.append(GlobalMaxPooling2D()(ques))
        channel_2.append(GlobalMaxPooling2D()(ans))
        
        #channel_1.append(GlobalAveragePooling2D()(ques))
        #channel_2.append(GlobalAveragePooling2D()(ans))
    
    h1 = channel_1.pop(-1)
    if channel_1:
        h1 = merge([h1] + channel_1, mode="concat")

    h2 = channel_2.pop(-1)
    if channel_2:
        h2 = merge([h2] + channel_2, mode="concat")
    
    h =  Merge(mode="concat",name='h')([h1, h2])
    #h = Dropout(0.2)(h)
    #h = Dense(50, kernel_regularizer=regularizers.l2(reg2),activation='relu')(h)
    #wrap = Dropout(0.5)(h)
    #wrap = Dense(64, activation='tanh')(h)   
    
    score = Dense(2,activation='softmax',name='score')(h)
    model = Model([inpx, inpy],[score])
    model.compile( loss='categorical_crossentropy',optimizer='adam')
    
    return model 
Example #28
Source Project: MovieTaster-Open   Author: lujiaying   File: keras_item2vec.py    License: MIT License 4 votes vote down vote up
def skipgram_model(vocab_size, embedding_dim=100, paradigm='Functional'):
    # Sequential paradigm
    if paradigm == 'Sequential':
        target = Sequential()
        target.add(Embedding(vocab_size, embedding_dim, input_length=1))
        context = Sequential()
        context.add(Embedding(vocab_size, embedding_dim, input_length=1))

        # merge the pivot and context models
        model = Sequential()
        model.add(Merge([target, context], mode='dot'))
        model.add(Reshape((1,), input_shape=(1,1)))
        model.add(Activation('sigmoid'))
        model.compile(optimizer='adam', loss='binary_crossentropy')
        return model

    # Functional paradigm
    elif paradigm == 'Functional':
        target = Input(shape=(1,), name='target')
        context = Input(shape=(1,), name='context')
        #print target.shape, context.shape
        shared_embedding = Embedding(vocab_size, embedding_dim, input_length=1, name='shared_embedding')
        embedding_target = shared_embedding(target)
        embedding_context = shared_embedding(context)
        #print embedding_target.shape, embedding_context.shape

        merged_vector = dot([embedding_target, embedding_context], axes=-1)
        reshaped_vector = Reshape((1,), input_shape=(1,1))(merged_vector)
        #print merged_vector.shape
        prediction = Dense(1, input_shape=(1,), activation='sigmoid')(reshaped_vector)
        #print prediction.shape

        model = Model(inputs=[target, context], outputs=prediction)
        model.compile(optimizer='adam', loss='binary_crossentropy')
        return model

    else:
        print('paradigm error')
        return None 
Example #29
Source Project: Multi-level-DCNet   Author: ssrp   File: dcnet.py    License: GNU General Public License v3.0 4 votes vote down vote up
def MultiLevelDCNet(input_shape, n_class, routings):
    """
    A DCNet (1-level DCNet) on MNIST.

    :param input_shape: data shape, 3d, [width, height, channels]
    :param n_class: number of classes
    :param routings: number of routing iterations
    :return: Two Keras Models, the first one used for training, and the second one for evaluation.
    """
    
    x = layers.Input(shape=input_shape)
    concat_axis = 1 if K.image_data_format() == 'channels_first' else -1

    ########################### Primary Capsules ###########################
    # Incorporating DenseNets - Creating a dense block with 8 layers having 32 filters and 32 growth rate.
    conv, nb_filter = densenet.DenseBlock(x, growth_rate=32, nb_layers=8, nb_filter=32)
    # Batch Normalization
    DenseBlockOutput = BatchNormalization(axis=concat_axis, epsilon=1.1e-5)(conv)

    # Creating Primary Capsules
    # Here PrimaryCapsConv2D is the Conv2D output which is used as the primary capsules by reshaping and squashing (squash activation).
    # primarycaps_1 (size: [None, num_capsule, dim_capsule]) is the "reshaped and sqashed output" which will be further passed to the dynamic routing protocol.
    primarycaps, PrimaryCapsConv2D = PrimaryCap(DenseBlockOutput, dim_capsule=8, n_channels=32, kernel_size=9, strides=2, padding='valid')

    ########################### DigitCaps Output ###########################
    digitcaps = CapsuleLayer(num_capsule=n_class, dim_capsule=16, routings=routings,
                             name='digitcaps0')(primarycaps)
    out_caps = Length(name='capsnet')(digitcaps)


    # Reconstruction (decoder) network
    y = layers.Input(shape=(n_class,))
    masked_by_y = Mask()([digitcaps, y])  # The true label is used to mask the output of capsule layer. For training
    masked = Mask()(digitcaps)  # Mask using the capsule with maximal length. For prediction

    # Shared Decoder model in training and prediction
    decoder = models.Sequential(name='decoder')
    decoder.add(layers.Dense(512, activation='relu', input_dim=int(digitcaps.shape[2]*n_class), name='zero_layer'))
    decoder.add(layers.Dense(512, activation='relu', name='one_layer'))
    decoderFinal = models.Sequential(name='decoderFinal')
    # Concatenating two layers
    decoderFinal.add(layers.Merge([decoder.get_layer('zero_layer'), decoder.get_layer('one_layer')], mode='concat'))
    decoderFinal.add(layers.Dense(1024, activation='relu'))
    decoderFinal.add(layers.Dense(np.prod(input_shape), activation='sigmoid'))
    decoderFinal.add(layers.Reshape(input_shape, name='out_recon'))

    # Model for training
    train_model = models.Model([x, y], [out_caps, decoderFinal(masked_by_y)])

    # Model for evaluation (prediction)
    eval_model = models.Model(x, [out_caps, decoderFinal(masked)])

    return train_model, eval_model 
Example #30
Source Project: deep_learning   Author: jarvisqi   File: recommend_dnn.py    License: MIT License 4 votes vote down vote up
def test_dnn():
    k=128
    rating_header = ['user_id', 'movie_id', 'rating', 'timestamp']
    ratings = pd.read_csv('./data/ml-1m/ratings.dat',sep='::', names=rating_header, engine = 'python')[:300000]
    #获取最大ID
    n_users = np.max(ratings['user_id'])
    n_movies = np.max(ratings['movie_id'])
    #获取用户和电影列表
    users = ratings['user_id'].values
    movies = ratings['movie_id'].values

    #训练数据集
    X_train = [users, movies]
    #标签
    y_train = ratings['rating'].values

    #先构建2个小的神经网络
    model1 = Sequential()
    model1.add(Embedding(n_users + 1, k, input_length = 1))
    model1.add(Reshape((k,)))

    model2 = Sequential()
    model2.add(Embedding(n_movies + 1, k, input_length = 1))
    model2.add(Reshape((k,)))

    model = Sequential()
    model.add(Merge([model1, model2], mode = 'concat'))
    model.add(Dropout(0.2))
    model.add(Dense(k, activation = 'relu'))
    model.add(Dropout(0.5))
    model.add(Dense(int(k/4), activation = 'relu'))
    model.add(Dropout(0.5))
    model.add(Dense(int(k/16), activation = 'relu'))
    model.add(Dropout(0.5))
    model.add(Dense(1, activation = 'linear'))
    model.compile(loss='mse', optimizer='adam', metrics=['accuracy'])
    model.fit(X_train, y_train, epochs=64, batch_size=512,validation_split=0.2)

    sumt = 0
    for i in range(ratings.shape[0]):
        sumt += (ratings['rating'][i] - model.predict([np.array([ratings['user_id'][i]]), np.array([ratings['movie_id'][i]])])) ** 2
    mse = math.sqrt(sumt/ratings.shape[0])
    print(mse)