def train_model(lrmodel, X, Y, devX, devY, devscores, feat_train, feat_dev, train_tfidf, test_tfidf):
    """
    Train model, using pearsonr on dev for early stopping
    """
    done = False
    best = -1.0
    #r = np.arange(1,5)
    
    while not done:
        # Every 100 epochs, check Pearson on development set
        lrmodel.fit([X,feat_train,train_tfidf], Y, verbose=2, shuffle=False, nb_epoch = 3, validation_data=([devX,feat_dev,test_tfidf], devY))
        #yhat = np.dot(lrmodel.predict(devX, verbose=2), r)
        yhat = lrmodel.predict([devX,feat_dev,test_tfidf], verbose=2)
        yhat = [i.argmax()for i in yhat]
        
        string_predicted,test_stances = [],[]
    
        for i,j in zip(yhat,devscores):
            if i == 3:
                string_predicted.append('unrelated')
            if i == 0:
                string_predicted.append('agree')
            if i == 2:
                string_predicted.append('discuss')
            if i == 1:
                string_predicted.append('disagree')
            if j == 3:
                test_stances.append('unrelated')
            if j == 0:
                test_stances.append('agree')
            if j == 2:
                test_stances.append('discuss')
            if j == 1:
                test_stances.append('disagree')
        print 'using new limit value....'
        #score = accuracy_score(devscores, yhat)
        score = report_score(test_stances,string_predicted,val=True)
        #return lrmodel
    
        if score > best:
            print score
            best = score
            bestlrmodel = prepare_model(ninputs=X.shape[1],n_feats=feat_train.shape[1],n_tfidf=train_tfidf.shape[1])
            bestlrmodel.set_weights(lrmodel.get_weights())
        else:
            done = True
            print '***** best model obtained with score',best,'******'

    yhat = bestlrmodel.predict([devX, feat_dev, test_tfidf], verbose=2)
    yhat = [i.argmax()for i in yhat]
    string_predicted,test_stances = [],[]
    
    for i,j in zip(yhat,devscores):
        if i == 3:
            string_predicted.append('unrelated')
        if i == 0:
            string_predicted.append('agree')
        if i == 2:
            string_predicted.append('discuss')
        if i == 1:
            string_predicted.append('disagree')
        if j == 3:
            test_stances.append('unrelated')
        if j == 0:
            test_stances.append('agree')
        if j == 2:
            test_stances.append('discuss')
        if j == 1:
            test_stances.append('disagree')
            
    report_score(test_stances,string_predicted)
    return bestlrmodel

    Train model, using pearsonr on dev for early stopping
    
fake news challenge (FNC-1)/eval_fnc.py