コード例 #1
0
ファイル: main.py プロジェクト: PierreGe/neural-network
def exo67():
    print("\n\n>>EXERCICE 6 et 7 : Calcul matriciel")
    print(" --- K=1 ---")
    #Xtrain, ytrain, Xvalid, yvalid, Xtest, ytest = utils.readMoonFile()
    Xtrain = [[30, 20, 40, 50], [25, 15, 35, 45]]
    ytrain = [0,0]
    default_h = 2
    nn = NeuralNetwork(len(Xtrain[0]), default_h, utils.getClassCount(ytrain), K=1, wd=0)
    nne = NeuralNetworkEfficient(len(Xtrain[0]), default_h, utils.getClassCount(ytrain), K=1, wd=0)
    nne._w1 = nn._w1 # trick pour que l'aleatoire soit egale
    nne._w2 = nn._w2
    nn.train(Xtrain,ytrain,1)
    nne.train(Xtrain,ytrain,1)
    utils.compareNN(nn,nne)
    print(" --- K=10 ---")
    Xtrain = [[30, 20, 40, 50], [25, 15, 35, 45],[30, 76, 45, 44],[89, 27, 42, 52],[30, 24, 44, 53],[89, 25, 45, 50],[30, 20, 40, 50],[30, 65, 47, 50],[30, 34, 40, 50],[39, 20, 29, 58]]
    ytrain = [0,0,0,0,0,0,0,0,0,0]
    default_h = 2
    nn = NeuralNetwork(len(Xtrain[0]), default_h, utils.getClassCount(ytrain), K=10, wd=0)
    nne = NeuralNetworkEfficient(len(Xtrain[0]), default_h, utils.getClassCount(ytrain), K=10, wd=0)
    nne._w1 = nn._w1 # trick pour que l'aleatoire soit egale
    nne._w2 = nn._w2
    nn.train(Xtrain,ytrain,1)
    nne.train(Xtrain,ytrain,1)
    utils.compareNN(nn,nne,10)
コード例 #2
0
ファイル: main.py プロジェクト: PierreGe/neural-network
def test():

    Xtrain, ytrain, Xvalid, yvalid, Xtest, ytest = utils.readMoonFile()
    h = 200
    wd = 0.0001
    K = 200
    maxIter = 500

    # neuralNetwork = NeuralNetwork(len(Xtrain[0]), h, utils.getClassCount(ytrain), K=10, wd=wd)
    neuralNetworkEfficient = NeuralNetworkEfficient(len(Xtrain[0]), h, utils.getClassCount(ytrain), K, wd=wd)
    # neuralNetworkEfficient._w1 = neuralNetwork._w1
    # neuralNetworkEfficient._w2 = neuralNetwork._w2
    # neuralNetwork.train(Xtrain, ytrain, maxIter)
    # predTrain = neuralNetwork.computePredictions(Xtrain)
    # predValid = neuralNetwork.computePredictions(Xvalid)
    # predTest = neuralNetwork.computePredictions(Xtest)
    # trainEfficiency = utils.calculatePredictionsEfficiency(predTrain, ytrain)
    # validEfficiency = utils.calculatePredictionsEfficiency(predValid, yvalid)
    # testEfficiency = utils.calculatePredictionsEfficiency(predTest, ytest)
    # print( "Train Err: " + "{:.2f}".format(100 - trainEfficiency) + "%" \
    #         + " / Valid Err: " + "{:.2f}".format(100 - validEfficiency) + "%" \
    #         + " / Test Err: " + "{:.2f}".format(100 - testEfficiency) + "%")


    neuralNetworkEfficient.train(Xtrain, ytrain, maxIter)
    predTrain = neuralNetworkEfficient.computePredictions(Xtrain)
    predValid = neuralNetworkEfficient.computePredictions(Xvalid)
    predTest = neuralNetworkEfficient.computePredictions(Xtest)
    trainEfficiency = utils.calculatePredictionsEfficiency(predTrain, ytrain)
    validEfficiency = utils.calculatePredictionsEfficiency(predValid, yvalid)
    testEfficiency = utils.calculatePredictionsEfficiency(predTest, ytest)
    print( "Train Err: " + "{:.2f}".format(100 - trainEfficiency) + "%" \
            + " / Valid Err: " + "{:.2f}".format(100 - validEfficiency) + "%" \
            + " / Test Err: " + "{:.2f}".format(100 - testEfficiency) + "%")
コード例 #3
0
ファイル: main.py プロジェクト: PierreGe/neural-network
def exo9_10():
    print("\n\n>>EXERCICE 9-10")
    print("Train Err;Valid Err;Test Err;Avg Cost Train;Avg Cost Valid;Avg Cost Test")

    h = 300
    wd = 0.0001
    maxIter = 500
    K = 50

    Xtrain, ytrain, Xvalid, yvalid, Xtest, ytest = utils.readMNISTfile()
    neuralNetwork = NeuralNetworkEfficient(len(Xtrain[0]), h, utils.getClassCount(ytrain), K, wd)
    neuralNetwork.setDataSets(Xtrain, Xvalid, Xtest, ytrain, yvalid, ytest)
    neuralNetwork.train(Xtrain, ytrain, maxIter)

    epochsData = ""
    for d in neuralNetwork.epochData:
        epochsData += d+"\n"

    f = open('no9.txt', 'w')
    f.write(epochsData)
    f.close()

    x = range(1, maxIter+1, 10)
    title = "Taux d'erreur - "+str(maxIter)+" epoques"
    name = str(h) + "_" + str(wd)+ "_" + str(h)+ "_" + "taux_erreur"
    utils.plotCourbeApprentissage(neuralNetwork.trainError, neuralNetwork.validError, neuralNetwork.testError, x, title, name)

    title = "Cout moyen - "+str(maxIter)+" epoques"
    name = str(h) + "_" + str(wd)+ "_" + str(h)+ "_" +"cout_moyen"
    utils.plotCourbeApprentissage(neuralNetwork.trainSumL, neuralNetwork.validSumL, neuralNetwork.testSumL, x, title, name)
コード例 #4
0
ファイル: main.py プロジェクト: PierreGe/neural-network
def trainAndPrint(Xtrain, ytrain, Xvalid, yvalid, Xtest, ytest, h, wd, maxIter):
    neuralNetwork = NeuralNetworkEfficient(len(Xtrain[0]), h, utils.getClassCount(ytrain), K=50, wd=wd)
    neuralNetwork.train(Xtrain, ytrain, maxIter)
    predTrain = neuralNetwork.computePredictions(Xtrain)
    predValid = neuralNetwork.computePredictions(Xvalid)
    predTest = neuralNetwork.computePredictions(Xtest)
    trainEfficiency = utils.calculatePredictionsEfficiency(predTrain, ytrain)
    validEfficiency = utils.calculatePredictionsEfficiency(predValid, yvalid)
    testEfficiency = utils.calculatePredictionsEfficiency(predTest, ytest)
    hparams = "[h: " + str(h) + " / wd: " + str(wd) + " / " + str(maxIter) + " epoques]"
    title = "Train Err: " + "{:.2f}".format(100 - trainEfficiency) + "%" \
            + " / Valid Err: " + "{:.2f}".format(100 - validEfficiency) + "%" \
            + " / Test Err: " + "{:.2f}".format(100 - testEfficiency) + "%"
    name = "regions_decision" + str(h) + "_" + str(wd) + "_" + str(maxIter)
    utils.plotRegionsDescision(Xtrain, ytrain, Xvalid, yvalid, Xtest, ytest, neuralNetwork, title, name,hparams)
コード例 #5
0
ファイル: main.py プロジェクト: PierreGe/neural-network
def exo8():
    print("\n\n>>EXERCICE 8 MNIST")
    Xtrain, ytrain, Xvalid, yvalid, Xtest, ytest = utils.readMNISTfile()
    default_h = 30
    maxIter = 1
    neuralNetwork = NeuralNetwork(len(Xtrain[0]), default_h, utils.getClassCount(ytrain),K=100)
    neuralNetworkEfficient = NeuralNetworkEfficient(len(Xtrain[0]), default_h, utils.getClassCount(ytrain),K=100)
    neuralNetworkEfficient._w1 = neuralNetwork._w1
    neuralNetworkEfficient._w2 = neuralNetwork._w2
    print("--- Reseau de depart ---")
    t1 = datetime.now()
    neuralNetwork.train(Xtrain, ytrain, maxIter)
    t2 = datetime.now()
    delta = t2 - t1
    print("Cela a mis : " + str(delta.total_seconds()) + " secondes")
    print("--- Reseau optimise ---")
    t1 = datetime.now()
    neuralNetworkEfficient.train(Xtrain, ytrain, maxIter)
    t2 = datetime.now()
    delta = t2 - t1
    print("Cela a mis : " + str(delta.total_seconds()) + " secondes")
コード例 #6
0
ファイル: main.py プロジェクト: PierreGe/neural-network
def test():

    Xtrain, ytrain, Xvalid, yvalid, Xtest, ytest = utils.readMoonFile()
    h = 200
    wd = 0.0001
    K = 200
    maxIter = 500

    # neuralNetwork = NeuralNetwork(len(Xtrain[0]), h, utils.getClassCount(ytrain), K=10, wd=wd)
    neuralNetworkEfficient = NeuralNetworkEfficient(
        len(Xtrain[0]), h, utils.getClassCount(ytrain), K, wd=wd)
    # neuralNetworkEfficient._w1 = neuralNetwork._w1
    # neuralNetworkEfficient._w2 = neuralNetwork._w2
    # neuralNetwork.train(Xtrain, ytrain, maxIter)
    # predTrain = neuralNetwork.computePredictions(Xtrain)
    # predValid = neuralNetwork.computePredictions(Xvalid)
    # predTest = neuralNetwork.computePredictions(Xtest)
    # trainEfficiency = utils.calculatePredictionsEfficiency(predTrain, ytrain)
    # validEfficiency = utils.calculatePredictionsEfficiency(predValid, yvalid)
    # testEfficiency = utils.calculatePredictionsEfficiency(predTest, ytest)
    # print( "Train Err: " + "{:.2f}".format(100 - trainEfficiency) + "%" \
    #         + " / Valid Err: " + "{:.2f}".format(100 - validEfficiency) + "%" \
    #         + " / Test Err: " + "{:.2f}".format(100 - testEfficiency) + "%")

    neuralNetworkEfficient.train(Xtrain, ytrain, maxIter)
    predTrain = neuralNetworkEfficient.computePredictions(Xtrain)
    predValid = neuralNetworkEfficient.computePredictions(Xvalid)
    predTest = neuralNetworkEfficient.computePredictions(Xtest)
    trainEfficiency = utils.calculatePredictionsEfficiency(predTrain, ytrain)
    validEfficiency = utils.calculatePredictionsEfficiency(predValid, yvalid)
    testEfficiency = utils.calculatePredictionsEfficiency(predTest, ytest)
    print( "Train Err: " + "{:.2f}".format(100 - trainEfficiency) + "%" \
            + " / Valid Err: " + "{:.2f}".format(100 - validEfficiency) + "%" \
            + " / Test Err: " + "{:.2f}".format(100 - testEfficiency) + "%")
コード例 #7
0
ファイル: main.py プロジェクト: PierreGe/neural-network
def exo8():
    print("\n\n>>EXERCICE 8 MNIST")
    Xtrain, ytrain, Xvalid, yvalid, Xtest, ytest = utils.readMNISTfile()
    default_h = 30
    maxIter = 1
    neuralNetwork = NeuralNetwork(len(Xtrain[0]),
                                  default_h,
                                  utils.getClassCount(ytrain),
                                  K=100)
    neuralNetworkEfficient = NeuralNetworkEfficient(
        len(Xtrain[0]), default_h, utils.getClassCount(ytrain), K=100)
    neuralNetworkEfficient._w1 = neuralNetwork._w1
    neuralNetworkEfficient._w2 = neuralNetwork._w2
    print("--- Reseau de depart ---")
    t1 = datetime.now()
    neuralNetwork.train(Xtrain, ytrain, maxIter)
    t2 = datetime.now()
    delta = t2 - t1
    print("Cela a mis : " + str(delta.total_seconds()) + " secondes")
    print("--- Reseau optimise ---")
    t1 = datetime.now()
    neuralNetworkEfficient.train(Xtrain, ytrain, maxIter)
    t2 = datetime.now()
    delta = t2 - t1
    print("Cela a mis : " + str(delta.total_seconds()) + " secondes")
コード例 #8
0
ファイル: main.py プロジェクト: PierreGe/neural-network
def trainAndPrint(Xtrain, ytrain, Xvalid, yvalid, Xtest, ytest, h, wd,
                  maxIter):
    neuralNetwork = NeuralNetworkEfficient(len(Xtrain[0]),
                                           h,
                                           utils.getClassCount(ytrain),
                                           K=50,
                                           wd=wd)
    neuralNetwork.train(Xtrain, ytrain, maxIter)
    predTrain = neuralNetwork.computePredictions(Xtrain)
    predValid = neuralNetwork.computePredictions(Xvalid)
    predTest = neuralNetwork.computePredictions(Xtest)
    trainEfficiency = utils.calculatePredictionsEfficiency(predTrain, ytrain)
    validEfficiency = utils.calculatePredictionsEfficiency(predValid, yvalid)
    testEfficiency = utils.calculatePredictionsEfficiency(predTest, ytest)
    hparams = "[h: " + str(h) + " / wd: " + str(wd) + " / " + str(
        maxIter) + " epoques]"
    title = "Train Err: " + "{:.2f}".format(100 - trainEfficiency) + "%" \
            + " / Valid Err: " + "{:.2f}".format(100 - validEfficiency) + "%" \
            + " / Test Err: " + "{:.2f}".format(100 - testEfficiency) + "%"
    name = "regions_decision" + str(h) + "_" + str(wd) + "_" + str(maxIter)
    utils.plotRegionsDescision(Xtrain, ytrain, Xvalid, yvalid, Xtest, ytest,
                               neuralNetwork, title, name, hparams)
コード例 #9
0
ファイル: main.py プロジェクト: PierreGe/neural-network
def exo9_10():
    print("\n\n>>EXERCICE 9-10")
    print(
        "Train Err;Valid Err;Test Err;Avg Cost Train;Avg Cost Valid;Avg Cost Test"
    )

    h = 300
    wd = 0.0001
    maxIter = 500
    K = 50

    Xtrain, ytrain, Xvalid, yvalid, Xtest, ytest = utils.readMNISTfile()
    neuralNetwork = NeuralNetworkEfficient(len(Xtrain[0]), h,
                                           utils.getClassCount(ytrain), K, wd)
    neuralNetwork.setDataSets(Xtrain, Xvalid, Xtest, ytrain, yvalid, ytest)
    neuralNetwork.train(Xtrain, ytrain, maxIter)

    epochsData = ""
    for d in neuralNetwork.epochData:
        epochsData += d + "\n"

    f = open('no9.txt', 'w')
    f.write(epochsData)
    f.close()

    x = range(1, maxIter + 1, 10)
    title = "Taux d'erreur - " + str(maxIter) + " epoques"
    name = str(h) + "_" + str(wd) + "_" + str(h) + "_" + "taux_erreur"
    utils.plotCourbeApprentissage(neuralNetwork.trainError,
                                  neuralNetwork.validError,
                                  neuralNetwork.testError, x, title, name)

    title = "Cout moyen - " + str(maxIter) + " epoques"
    name = str(h) + "_" + str(wd) + "_" + str(h) + "_" + "cout_moyen"
    utils.plotCourbeApprentissage(neuralNetwork.trainSumL,
                                  neuralNetwork.validSumL,
                                  neuralNetwork.testSumL, x, title, name)
コード例 #10
0
ファイル: main.py プロジェクト: PierreGe/neural-network
def exo67():
    print("\n\n>>EXERCICE 6 et 7 : Calcul matriciel")
    print(" --- K=1 ---")
    #Xtrain, ytrain, Xvalid, yvalid, Xtest, ytest = utils.readMoonFile()
    Xtrain = [[30, 20, 40, 50], [25, 15, 35, 45]]
    ytrain = [0, 0]
    default_h = 2
    nn = NeuralNetwork(len(Xtrain[0]),
                       default_h,
                       utils.getClassCount(ytrain),
                       K=1,
                       wd=0)
    nne = NeuralNetworkEfficient(len(Xtrain[0]),
                                 default_h,
                                 utils.getClassCount(ytrain),
                                 K=1,
                                 wd=0)
    nne._w1 = nn._w1  # trick pour que l'aleatoire soit egale
    nne._w2 = nn._w2
    nn.train(Xtrain, ytrain, 1)
    nne.train(Xtrain, ytrain, 1)
    utils.compareNN(nn, nne)
    print(" --- K=10 ---")
    Xtrain = [[30, 20, 40, 50], [25, 15, 35, 45], [30, 76, 45, 44],
              [89, 27, 42, 52], [30, 24, 44, 53], [89, 25, 45, 50],
              [30, 20, 40, 50], [30, 65, 47, 50], [30, 34, 40, 50],
              [39, 20, 29, 58]]
    ytrain = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    default_h = 2
    nn = NeuralNetwork(len(Xtrain[0]),
                       default_h,
                       utils.getClassCount(ytrain),
                       K=10,
                       wd=0)
    nne = NeuralNetworkEfficient(len(Xtrain[0]),
                                 default_h,
                                 utils.getClassCount(ytrain),
                                 K=10,
                                 wd=0)
    nne._w1 = nn._w1  # trick pour que l'aleatoire soit egale
    nne._w2 = nn._w2
    nn.train(Xtrain, ytrain, 1)
    nne.train(Xtrain, ytrain, 1)
    utils.compareNN(nn, nne, 10)