Exemple #1
0
def calculateError_validation(path_Folder, mode, WV, dim,  W1 , W2, OutputFile, WSat, WNu, iteration, activationFunc):
    posFileList_test = os.listdir(path_Folder + "pos/")
    negFileList_test = os.listdir(path_Folder + "neg/")
    numberofSamples_test = min(len(posFileList_test), len(negFileList_test))
    sumErr=0.0

    for k in range(0, numberofSamples_test):
        path_File_test = path_Folder + "pos/" + posFileList_test[k]
        EDUs = readTree_att_NSWeight(path_File_test, W1, WV, dim, WSat, WNu, activationFunc)
        y = [1.0, 0]
        eduKeys = sortEduKey(EDUs.keys(), reverse=True)
        input2 = EDUs[str(eduKeys[0])].vector
        y_in = feedforward(input2, W2)
        output = feedforward_act(input2, W2, activationFunc)
        sumErr += MSE(y, output)

        path_File_test = path_Folder + "neg/" + negFileList_test[k]
        #print negFileList_test[k]
        EDUs = readTree_att_NSWeight(path_File_test, W1, WV, dim, WSat, WNu, activationFunc)
        y = [0, 1.0]
        eduKeys = sortEduKey(EDUs.keys(), reverse=True)
        input2 = EDUs[str(eduKeys[0])].vector
        y_in = feedforward(input2, W2)
        output = feedforward_act(input2, W2, activationFunc)
        sumErr += MSE(y, output)

    totall_Err = sumErr/(2*numberofSamples_test)
    return totall_Err
    print  iteration, " ", mode , " ", totall_Err
    OutputFile.write("%s,%s,%s\n" % (iteration, mode, totall_Err))
Exemple #2
0
def test_AttWeight(path_Folder, mode, WV, dim,  W1 , W2, OutputFile, WSat, WNu, iteration, activationFunc):
    posFileList_test = os.listdir(path_Folder + "pos/")
    negFileList_test = os.listdir(path_Folder + "neg/")
    numberofSamples_test = min(len(posFileList_test), len(negFileList_test))
    #numberofSamples_test=100
    tp = 0
    fp = 0
    tn = 0
    fn = 0

    for k in range(0, numberofSamples_test):
        path_File_test = path_Folder + "pos/" + posFileList_test[k]
        #print posFileList_test[k]
        EDUs = readTree_att_NSWeight(path_File_test, W1, WV, dim, WSat, WNu, activationFunc)
        y = [1.0, 0]
        eduKeys = sortEduKey(EDUs.keys(), reverse=True)
        input2 = EDUs[str(eduKeys[0])].vector
        # print "pos"
        # print input2
        y_in = feedforward(input2, W2)
        output = feedforward_act(input2, W2, activationFunc)
        #print output

        if output[0] > output[1]:
            tp += 1
        else:
            fn += 1

        path_File_test = path_Folder + "neg/" + negFileList_test[k]
        #print negFileList_test[k]
        EDUs = readTree_att_NSWeight(path_File_test, W1, WV, dim, WSat, WNu, activationFunc)
        eduKeys = sortEduKey(EDUs.keys(), reverse=True)
        input2 = EDUs[str(eduKeys[0])].vector
        # print "neg"
        # print input2
        y_in = feedforward(input2, W2)
        output = feedforward_act(input2, W2, activationFunc)

        if output[0] < output[1]:

            tn += 1
        else:
            fp += 1

    accuracy = float(tp + tn) / (tp + tn + fp + fn)
    if (tp+fp) == 0:
        precision = 0
    else:
        precision = float(tp) / (tp + fp)
    recall = float(tp) / (tp + fn)
    if (precision + recall) == 0:
        F1 =0
    else:
        F1 = 2 * (float(precision * recall)) / (precision + recall)

    print  iteration, " ", mode , " ", tp, " ", tn, " ", fp, " ", fn, " ", accuracy, " ", precision, " ", recall, " ", F1
    OutputFile.write("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n" % (iteration, mode, tp, tn, fp, fn, accuracy, precision, recall, F1))
Exemple #3
0
def train_for_each_Sample_AttWeight (EDUs, EDUs_test, y, W1, W21, W22, eta, dim, activationFunc, dropOutPercent):

    W1_copy = W1.copy()
    W21_copy = W21.copy()
    W22_copy = W22.copy()

    indexNode = dropOut(len(W1[0]), dropOutPercent)
    #W1_doc = dropcolrow(W1, indexNode, False)

    #indexNode = dropOut(len(W1_query[0]), dropOutPercent)
    W1 = dropcolrow(W1, indexNode, False)

    indexNode2 = []
    indexNode2.extend(indexNode)
    indexNode2.extend(indexNode*2)

    W21 = dropcolrow(W21, indexNode2, True)

    eduKeys_test = sortEduKey(EDUs_test.keys(), reverse=True)
    input2_test = EDUs_test[str(eduKeys_test[0])].vector

    eduKeys = sortEduKey(EDUs.keys(), reverse=True)
    input2 = EDUs[str(eduKeys[0])].vector

    input = (np.concatenate([input2, input2_test], 0))
    #y_in1 = feedforward(input, W21)
    output1 = feedforward_act(input, W21, activationFunc)
    y_in = feedforward(output1, W22)
    output = feedforward_act(output1, W22, global_outputActivation)

    error_soft = softmax_error(y, output, y_in, global_outputActivation)
    delta_W22 = calculate_deltaW(error_soft, output1)

    error_hidden = non_softmax_error(error_soft, W22, input, W21, activationFunc)
    delta_W21 = calculate_deltaW(error_hidden, input)

    delta_W1_doc = BpthroughTree(EDUs, error_hidden, W1, W21, dim, activationFunc, True)
    delta_W1_query = BpthroughTree(EDUs_test, error_hidden, W1, W21, dim, activationFunc, False)

    #print ("=============== : ", np.sum(delta_W1_doc[:, indexNode]))
    delta_W1_doc = dropcolrow(delta_W1_doc, indexNode, False)
    delta_W1_query = dropcolrow(delta_W1_query, indexNode, False)
    delta_W21 = dropcolrow(delta_W21, indexNode2, True)

    delta_W = np.divide(np.add(delta_W1_doc, delta_W1_query), 2)

    W21 = update_weight(eta, W21_copy, delta_W21)
    W22 = update_weight(eta, W22_copy, delta_W22)
    W1 = update_weight(eta, W1_copy, delta_W)


    return W1, W21, W22
Exemple #4
0
def train_for_each_Sample (EDUs, y, W1, W2, eta, activationFunc):
    eduKeys = sortEduKey(EDUs.keys(), reverse=True)
    input2 = EDUs[str(eduKeys[0])].vector
    y_in = feedforward(input2, W2)
    output = feedforward_act(input2, W2, activationFunc)
    #print output
    error_soft = softmax_error(y, output, y_in, activationFunc)
    delta_W2 = calculate_deltaW(error_soft, input2)
    delta_W1 = BpthroughTree(EDUs, error_soft, W1, W2, activationFunc)
    W2 = update_weight(eta, W2, delta_W2)
    W1 = update_weight(eta, W1, delta_W1)

    return W1, W2
Exemple #5
0
def test_AttWeight_DrHarati(path_Folder, mode, WV, dim,  W1 , W2, OutputFile, WSat, WNu, iteration, activationFunc):
    posFileList_test = os.listdir(path_Folder + "pos/")
    negFileList_test = os.listdir(path_Folder + "neg/")
    numberofSamples_test = min(len(posFileList_test), len(negFileList_test))
    #numberofSamples_test=100
    tp = 0
    fp = 0
    tn = 0
    fn = 0

    for k in range(0, numberofSamples_test):
        path_File_test = path_Folder + "pos/" + posFileList_test[k]
        EDUs = readTree_att_NSWeight(path_File_test, W1, WV, dim, WSat, WNu, activationFunc)
        y = [1.0, 0]
        eduKeys = sortEduKey(EDUs.keys(), reverse=True)
        input2 = EDUs[str(eduKeys[0])].vector
        y_in = feedforward(input2, W2)
        output = feedforward_act(input2, W2, activationFunc)

        if output[0] == output[1]:
            print "input", input2
            # print "W2", W2[0, :]
            # print "W1", W1[0, :]
            # print "WN", WNu[0, :]
            # print "WS", WSat[0, :]
            # print "pos", output

        if output[0] > output[1]:
            #print "pos ", output
            tp += 1
        else:

            fn += 1

        path_File_test = path_Folder + "neg/" + negFileList_test[k]
        EDUs = readTree_att_NSWeight(path_File_test, W1, WV, dim, WSat, WNu, activationFunc)
        eduKeys = sortEduKey(EDUs.keys(), reverse=True)
        input2 = EDUs[str(eduKeys[0])].vector
        y_in = feedforward(input2, W2)
        output = feedforward_act(input2, W2, activationFunc)

        if output[0] == output[1]:
            print "input", input2
            # print "W2", W2[0, :]
            # print "W1", W1[0,:]
            # print "WN", WNu[0,:]
            # print "WS", WSat[0,:]
            # print "neg",output

        if output[0] < output[1]:
            #print "neg ", output
            tn += 1
        else:
            fp += 1

    accuracy = float(tp + tn) / (tp + tn + fp + fn)

    precision, recall, F1 = calculate_eval_metrics(tp, tn, fp, fn)

    print  iteration, " ", mode , " ", tp, " ", tn, " ", fp, " ", fn, " ", accuracy, " ", precision, " ", recall, " ", F1
    OutputFile.write("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n" % (iteration, mode, tp, tn, fp, fn, accuracy, precision, recall, F1))