Beispiel #1
0
def deepLearningQ_training(Q, deviceName, epoch, printed):

    # Q Table           = [[[s0], [q00, q01, ...]], [[s1], [q10, q11, ...]], ...]
    # convert to input  = converted version of [[s0], [s1], ...]
    #            output = original  version of [[q00, q01, ...], [q10, q11, ...], ...]

    # input array (need to convert original array [s0])
    inputData = []
    for i in range(len(Q)):
        inputData.append(stateTo1dArray(Q[i][0]))

    # output array (as original)
    outputData = []
    for i in range(len(Q)):
        outputData.append(Q[i][1])

    # save input and output array as file
    RD.saveArray('Q_input.txt', inputData)
    RD.saveArray('Q_output.txt', outputData)

    # train using deep learning and save the model (testInputFile and testOutputFile is None)
    # need: modelConfig.txt
    # DON'T NEED TO APPLY SIGMOID to training output data, because DL.deeplearning applies it
    DL.deepLearning('Q_input.txt', 'Q_output.txt', None, None, None, None, 0.0,
                    None, 'modelConfig.txt', deviceName, epoch, printed,
                    'deepQ_model')
Beispiel #2
0
def train(imgArray, labels, modelName, drop, lr, epoch, height, width,
          deviceName):

    # Neural Network
    NN = [
        tf.keras.layers.Reshape((height, width, 1),
                                input_shape=(
                                    height,
                                    width,
                                )),
        keras.layers.Conv2D(32,
                            kernel_size=(3, 3),
                            input_shape=(height, width, 1),
                            activation='relu'),
        keras.layers.MaxPooling2D(pool_size=2),
        keras.layers.Dropout(drop),
        keras.layers.Conv2D(32, (3, 3), activation='relu'),
        keras.layers.Dropout(drop),
        keras.layers.Conv2D(32, (3, 3), activation='relu'),
        keras.layers.Flatten(),
        keras.layers.Dropout(drop),
        keras.layers.Dense(40, activation='relu'),
        keras.layers.Dense(40, activation='relu'),
        keras.layers.Dense(10, activation='sigmoid')
    ]

    # Optimizer
    op = tf.keras.optimizers.Adam(lr)

    # learning
    DL.deepLearning(NN, op, 'mean_squared_error', imgArray, labels, modelName,
                    epoch, False, True, deviceName)
def trainAndTest(lr, dropout):
    global ALLTEXT

    # training and test inputs and outputs
    trainI = []
    trainO = []
    testI = []
    testO = []

    for i in range(len(inputs)):
        if random.random() < 0.2:
            testI.append(inputs[i])
            testO.append(outputs[i])
        else:
            trainI.append(inputs[i])
            trainO.append(outputs[i])

    # model design
    NN = [
        tf.keras.layers.Flatten(input_shape=(len(inputs[0]), )),
        keras.layers.Dense(8, activation='relu'),
        keras.layers.Dropout(dropout),
        keras.layers.Dense(8, activation='relu'),
        keras.layers.Dropout(dropout),
        keras.layers.Dense(8, activation='relu'),
        keras.layers.Dense(len(outputs[0]), activation='sigmoid')
    ]
    op = tf.keras.optimizers.Adam(lr)

    # learning
    deepLearning_GPU.deepLearning(NN, op, 'mean_squared_error', trainI, trainO,
                                  'test', 50, False, True, deviceName)

    # test
    newModel = deepLearning_GPU.deepLearningModel('test', True)
    testOutput = deepLearning_GPU.modelOutput(newModel, testI)

    # estimate
    outputLayer = testOutput[len(testOutput) - 1]
    avgSquareError = 0.0

    for i in range(len(testO)):
        op = outputLayer[i]  # output layer output
        to = testO[i]  # test output
        squareError = pow(op[0] - to[0], 2) + pow(op[1] - to[1], 2)
        avgSquareError += squareError
    avgSquareError /= len(testO)

    toAppend = str(round(lr, 8)) + ' ' + str(round(dropout, 8)) + ' ' + str(
        round(avgSquareError, 8))
    ALLTEXT += toAppend + '\n'
    print(toAppend)

    return avgSquareError
Beispiel #4
0
def deepLearning(inputFileName, outputFileName, testFileName,
                 testOutputFileName, testOutputReal, test_report, validRate,
                 valid_report, modelConfig, deviceName, epoch, printed,
                 modelName):

    # You can do only 'training' or 'testing' by setting some arguments as None.
    # inputFileName == None and outputFileName == None -> testing only
    # testFileName == None                             -> training only
    # for validation, you can set testFileName == None <- validation uses training data only

    ##############################
    ##                          ##
    ##       0. READ DATA       ##
    ##                          ##
    ##############################

    # read files
    print('[00] reading train input / train output / test input files...')

    trainI = None
    trainO = None
    testI = None

    # input train data
    if inputFileName != None: trainI = helper.getDataFromFile(inputFileName)

    #  output train data (Sigmoid applied)
    if outputFileName != None: trainO = helper.getDataFromFile(outputFileName)

    # test input data (set nullValue to 0)
    # set testI (array) as testFileName, if testFileName is an array
    if isinstance(testFileName, list):
        testI = testFileName

    # set testI (array) as test data from the file named as testFileName
    else:
        if testFileName != None: testI = helper.getDataFromFile(testFileName)

    # read configuration file (to get normalization info)
    print('[01] reading configuration files...')
    f = open('config.txt', 'r')
    fl = f.readlines()
    f.close()
    for i in range(len(fl)):
        fl[i] = fl[i].split('\n')[0]

    normalizeName = None
    validInterval = 1
    testSizeOnce = 0  # max test data size at once (for both testing and validation)

    # extract configuration
    # trainInput     : train input data file name
    # trainOutput    : train output data file name
    # testInput      : test input data file name
    for i in range(len(fl)):
        configSplit = fl[i].split('\n')[0].split(' ')  # split

        # normalize info file name
        if configSplit[0] == 'normalizeName':
            normalizeName = configSplit[1]
            if normalizeName == 'None': normalizeName = None

        # validation interval
        elif configSplit[0] == 'validInterval':
            validInterval = int(configSplit[1])

        # test input size at once
        elif configSplit[0] == 'testSize':
            testSizeOnce = int(configSplit[1])

    # read normalization info file
    if normalizeName != None and trainO != None:
        print('[02] calculating and writing average and stddev...')

        trainOutputAvg = np.mean(trainO,
                                 axis=0)  # average of train output value
        trainOutputStddev = np.std(trainO,
                                   axis=0)  # stddev of train output value

        # normalize training output data and write avg and stddev
        writeNormalizeInfo(trainO, normalizeName)
    else:
        print('[03] Reading average and stddev failed.')
        trainOutputAvg = None
        trainOutputStddev = None

    # apply sigmoid to train output data
    if trainO != None:
        print('[04] applying sigmoid to train output data...')
        for i in range(len(trainO)):
            for j in range(len(trainO[0])):
                trainO[i][j] = helper.sigmoid(trainO[i][j])

    # print input, output, and test data
    if printed != 0:
        if trainI != None:
            print('\n ---- original input data (' + str(len(trainI)) +
                  ') ----\n')
            for i in range(len(trainI)):
                print(helper.roundedArray(trainI[i], 6))

        if trainO != None:
            print('\n ---- original output data (' + str(len(trainO)) +
                  ') ----\n')
            for i in range(len(trainO)):
                print(helper.roundedArray(trainO[i], 6))

        if testI != None:
            print('\n ---- original test data (' + str(len(testI)) +
                  ') ----\n')
            for i in range(len(testI)):
                print(helper.roundedArray(testI[i], 6))

    ##############################
    ##                          ##
    ##   1. READ MODEL CONFIG   ##
    ##                          ##
    ##############################

    # model design using model configuration file
    # activation function of final layer is always 'sigmoid'
    print('[10] reading model configuration...')
    f = open(modelConfig, 'r')
    modelInfo = f.readlines()
    f.close()

    ##############################
    ##                          ##
    ##   2A. TRAINING / TEST    ##
    ##                          ##
    ##############################

    # if the model already exists, input the test input to the NN and get the result
    # if the model does not exist, newly train NN using training input and output data and then do testing procedure
    if validRate == 0:

        # NN and optimizer
        print('[11] obtaining neural network and optimizer info...')

        if trainI != None and trainO != None:
            NN = helper.getNN(modelInfo, trainI, trainO)  # Neural Network
            op = helper.getOptimizer(modelInfo)  # optimizer
            loss = helper.getLoss(modelInfo)  # loss

        try:  # try reading test.h5 and test.json
            print('[20] reading model [ ' + modelName + ' ]...')
            newModel = deepLearning_GPU.deepLearningModel(
                modelName, op, loss, True)
            testO = getTestResult(newModel, testI, testSizeOnce)

        except:  # do learning if test.h5 and test.json does not exist
            print('[21] learning...')

            # False, True는 각각 dataPrint(학습데이터 출력 여부), modelPrint(model의 summary 출력 여부)
            print(trainO[0])
            deepLearning_GPU.deepLearning(NN, op, 'mean_squared_error', trainI,
                                          trainO, modelName, epoch, False,
                                          True, deviceName)

            print('[22] reading learned model [ ' + modelName + ' ]...')
            newModel = deepLearning_GPU.deepLearningModel(
                modelName, op, loss, True)

            # get test output if testI is not None
            if testI == None:
                print('test input file name (testInput) is None.')
                return
            else:
                testO = getTestResult(newModel, testI, testSizeOnce)

        # test
        print('[23] testing...')

        # estimate

        # inverse sigmoid
        for i in range(len(testO)):  # for each output data
            for j in range(len(testO[0])):  # for each value of output data
                testO[i][j] = helper.invSigmoid(testO[i][j])

        # check if test output exists, before writing test output file
        try:
            test = open(testOutputFileName, 'r')
            test.close()
            print(' **** Delete test output file (' + testOutputFileName +
                  ') first. ****')
            return
        except:
            pass

        # write to file
        print('[24] writing test result to file [ ' + testOutputFileName +
              ' ]...')

        # open file
        f = open(testOutputFileName, 'a')

        result = ''
        for i in range(len(testO)):  # for each output data
            if i % 1000 == 0: print(str(i) + ' / ' + str(len(testO)))

            for j in range(len(testO[0])):  # for each value of output data
                result += str(testO[i][j]) + '\t'
            result += '\n'

            # flush every 10,000 steps
            if i % 10000 == 0:
                f.write(result)
                result = ''

        # final append
        f.write(result)
        f.close()

        ##############################
        ##                          ##
        ##  2A+. WRITE TEST REPORT  ##
        ##                          ##
        ##############################

        # compare prediction output data with real output data and write report
        if testOutputReal != None:
            try:
                writeTestResult(test_report, testOutputFileName,
                                testOutputReal, normalizeName, trainOutputAvg,
                                trainOutputStddev)
            except:
                pass

    ##############################
    ##                          ##
    ##      2B. VALIDATION      ##
    ##                          ##
    ##############################

    # validation (if validation rate > 0)
    else:

        ##############################
        ##                          ##
        ##   2B-0. DATA TO VALID    ##
        ##                          ##
        ##############################

        # make index-list of validation data
        print('[28] deciding data to validate...')
        inputSize = len(trainI)
        validSize = int(inputSize * validRate)
        trainSize = inputSize - validSize

        validArray = []
        for i in range(inputSize):
            validArray.append(0)
        while sum(validArray) < validSize:

            # start index for validation
            validStartIndex = int(
                random.randint(0, inputSize - 1) /
                validInterval) * validInterval

            # set data[validStartIndex : validStartIndex + validInterval] as validation data
            for i in range(validStartIndex, validStartIndex + validInterval):
                validArray[i] = 1

        # make train and validation data
        # _TrainO, _ValidO : sigmoid((originalOutput - meanOriginalOutput)/stdOriginalOutput)
        _TrainI = []  # training input
        _TrainO = []  # training output
        _ValidI = []  # valid input
        _ValidO = []  # valid output

        for i in range(inputSize):
            if validArray[i] == 0:  # training data
                _TrainI.append(trainI[i])
                _TrainO.append(trainO[i])
            else:  # validation data
                _ValidI.append(trainI[i])
                _ValidO.append(trainO[i])

        ##############################
        ##                          ##
        ## 2B-1. TRAIN (MAKE MODEL) ##
        ##                          ##
        ##############################

        # model name for validation
        newModelName = modelName + 'Valid'
        print('[29] training [ ' + newModelName + ' ]...')

        # NN and optimizer
        NN = helper.getNN(modelInfo, _TrainI, _TrainO)  # Neural Network
        op = helper.getOptimizer(modelInfo)  # optimizer
        loss = helper.getLoss(modelInfo)  # loss

        # output for validation
        try:  # try reading the validation model
            validModel = deepLearning_GPU.deepLearningModel(
                newModelName, op, loss, True)
            _predValidO = getTestResult(validModel, _ValidI, testSizeOnce)
        except:  # do learning if the validation model does not exist
            deepLearning_GPU.deepLearning(NN, op, loss, _TrainI, _TrainO,
                                          newModelName, epoch, False, True,
                                          deviceName)
            validModel = deepLearning_GPU.deepLearningModel(
                newModelName, op, loss, True)
            _predValidO = getTestResult(validModel, _ValidI, testSizeOnce)

        ##############################
        ##                          ##
        ##     2B-2. VALIDATION     ##
        ##                          ##
        ##############################
        print('[30] validating and writing result [ ' + valid_report + ' ]...')

        MAE = 0  # mean absolute error
        MSE = 0  # mean square error
        accuracy = 0  # accuracy

        # inverse sigmoid for PREDICTED validation output
        for i in range(len(_predValidO)):  # for each output data
            for j in range(len(
                    _predValidO[0])):  # for each value of output data
                _predValidO[i][j] = helper.invSigmoid(_predValidO[i][j])

        # inverse sigmoid for REAL validation output
        for i in range(len(_ValidO)):  # for each output data
            for j in range(len(_ValidO[0])):  # for each value of output data
                _ValidO[i][j] = helper.invSigmoid(_ValidO[i][j])

        # denormalize if normalized info is available (denormalize whole trainO)
        denormalize(normalizeName, len(_predValidO), len(_predValidO[0]),
                    _predValidO, trainOutputAvg, trainOutputStddev)
        denormalize(normalizeName, len(_ValidO), len(_ValidO[0]), _ValidO,
                    trainOutputAvg, trainOutputStddev)

        # compute error
        validCount = 0
        resultToWrite = ''
        outputCols = len(_ValidO[0])

        # for each data

        # set edgeitems and linewidth as infinite
        np.set_printoptions(edgeitems=10000, linewidth=1000000)

        for i in range(inputSize):
            if i % 1000 == 0: print(str(i) + ' / ' + str(inputSize))

            # validation for data whose value of valid array is 1
            if validArray[i] == 1:

                # compute MAE and MSE
                for j in range(outputCols):
                    MAE += abs(_ValidO[validCount][0] -
                               _predValidO[validCount][0])
                    MSE += pow(
                        _ValidO[validCount][0] - _predValidO[validCount][0], 2)

                # compute accuracy
                if helper.argmax(_ValidO[validCount]) == helper.argmax(
                        _predValidO[validCount]):
                    accuracy += 1

                # print and write result
                newResultToWrite = (
                    '[' + str(i) + '] pred = ' +
                    str(np.round_(_predValidO[validCount], 6)) + ', real = ' +
                    str(np.round_(_ValidO[validCount], 6)))
                resultToWrite += newResultToWrite + '\n'

                validCount += 1

        # recover edgeitems and linewidth
        np.set_printoptions(edgeitems=10000, linewidth=1000000)

        # get the average of MAE, MSE and accuracy
        MAE /= (validSize * outputCols)
        MSE /= (validSize * outputCols)
        accuracy /= validSize

        # print evaluation result
        resultSummary = '----------------\n'
        resultSummary += 'input size : ' + str(inputSize) + '\n'
        resultSummary += 'train size : ' + str(trainSize) + '\n'
        resultSummary += 'valid size : ' + str(validSize) + '\n'
        resultSummary += 'MAE        : ' + str(round(MAE, 6)) + '\n'
        resultSummary += 'MSE        : ' + str(round(MSE, 6)) + '\n'
        resultSummary += 'accuracy   : ' + str(round(accuracy, 6)) + '\n'
        resultSummary += 'pred avg   : ' + str(np.average(_predValidO,
                                                          axis=0)) + '\n'
        resultSummary += 'real avg   : ' + str(np.average(_ValidO,
                                                          axis=0)) + '\n'
        print(resultSummary)
        resultToWrite += resultSummary

        # write result file
        fvalid = open(valid_report, 'w')
        fvalid.write(resultToWrite)
        fvalid.close()

        # return final result
        return (MAE, MSE, accuracy, np.average(_predValidO, axis=0),
                np.average(_ValidO, axis=0))
Beispiel #5
0
            # learning
            print('\n <<< LEARNING >>>\n')

            NN = [
                tf.keras.layers.Flatten(input_shape=(lenInput, )),
                keras.layers.Dense(32, activation='relu'),
                keras.layers.Dense(32, activation='relu'),
                keras.layers.Dense(32, activation='relu'),
                keras.layers.Dense(32, activation='relu'),
                keras.layers.Dense(1, activation='sigmoid')
            ]
            op = tf.keras.optimizers.Adam(0.0005)  # learning rate = 0.0005

            deepLearning_GPU.deepLearning(NN, op, 'mean_squared_error',
                                          trainInputs, trainOutputs,
                                          'mainCOVID19_' + str(epoch), epoch,
                                          False, True, deviceName)

            # test
            print('\n <<< TEST >>>\n')
            newModel = deepLearning_GPU.deepLearningModel(
                'mainCOVID19_' + str(epoch), True)
            testOutputs = deepLearning_GPU.modelOutput(newModel, testInputs)

            # print output layer of test result
            print('\n <<< test result >>>\n')
            difSum = 0  # sum of difference (absolute)
            valOutputSum = 0  # sum of validation output (absolute)

            result = testOutputs[len(testOutputs) - 1]
Beispiel #6
0
def deepLearning(inputFileName, outputFileName, testFileName,
                 testOutputFileName, imgHeight, deviceName, epoch, printed,
                 modelName):

    # read files
    trainI = helper.getDataFromFile(inputFileName,
                                    imgHeight)  # input train data
    trainO = helper.getDataFromFile(
        outputFileName, None)  # output train data (Sigmoid applied)
    testI = helper.getDataFromFile(
        testFileName, imgHeight)  # test input data (set nullValue to 0)

    # apply sigmoid to train output data
    for i in range(len(trainO)):
        for j in range(len(trainO[0])):
            trainO[i][j] = helper.sigmoid(trainO[i][j])

    # flatten trainI: (N, size, size) -> (N, size*size)
    for i in range(len(trainI)):
        trainI[i] = helper.flatten(trainI[i])

    print('')
    print(' ---- number of rows ----')
    print('input  size: ' + str(len(trainI)))
    print('output size: ' + str(len(trainO)))
    print('test   size: ' + str(len(testI)))
    print('')

    # print input, output, and test data
    if printed != 0:
        print('\n ---- original input data ----\n')
        for i in range(len(trainI)):
            print(helper.roundedArray(trainI[i], 6))

        print('\n ---- original output data ----\n')
        for i in range(len(trainO)):
            print(helper.roundedArray(trainO[i], 6))

        print('\n ---- original test data ----\n')
        for i in range(len(testI)):
            print(helper.roundedArray(testI[i], 6))

    # model design using deepLearning_model.txt, in the form of
    # activation function of final layer is always 'sigmoid'
    f = open('deepLearning_model.txt', 'r')
    modelInfo = f.readlines()
    f.close()

    # NN and optimizer
    NN = helper.getNN(modelInfo, trainI, trainO)  # Neural Network
    op = helper.getOptimizer(modelInfo)  # optimizer

    try:  # try reading test.h5 and test.json
        newModel = deepLearning_GPU.deepLearningModel(modelName, True)
        testOutput = deepLearning_GPU.modelOutput(newModel, testI)
    except:  # do learning if test.h5 and test.json does not exist
        print('\n <<<< LEARNING >>>>\n')

        # False, True는 각각 dataPrint(학습데이터 출력 여부), modelPrint(model의 summary 출력 여부)
        print(trainO[0])
        deepLearning_GPU.deepLearning(NN, op, 'mean_squared_error', trainI,
                                      trainO, modelName, epoch, False, True,
                                      deviceName)

        newModel = deepLearning_GPU.deepLearningModel(modelName, True)
        testOutput = deepLearning_GPU.modelOutput(newModel, testI)

    # test
    print('\n <<<< TEST >>>>\n')

    # estimate
    outputLayer = testOutput[len(testOutput) - 1]

    # inverse sigmoid
    for i in range(len(outputLayer)):  # for each output data
        for j in range(len(outputLayer[0])):  # for each value of output data
            outputLayer[i][j] = helper.invSigmoid(outputLayer[i][j])

    # write to file
    result = ''
    print('\n<<<< output layer >>>>')
    for i in range(len(outputLayer)):  # for each output data
        for j in range(len(outputLayer[0])):  # for each value of output data
            result += str(outputLayer[i][j]) + '\t'
        result += '\n'
    print(result)

    f = open(testOutputFileName.split('.')[0] + '_prediction.txt', 'w')
    f.write(result)
    f.close()
Beispiel #7
0
def trainAndTest(numOfTrain, numOfTest, virusProb, boardSize, maxDist, deviceName): # 학습 데이터를 읽어서 배열에 저장 및 학습

    # 학습 데이터 (각 x열에 해당하는 8개의 배열, y열에 해당하는 배열)
    # 0 ~ 3 : hop1Noise_, hop2AvgNoise_, hop1NoiseZ_, hop2AvgNoiseZ_,
    # 4 ~ 7 : hop1Max_, hop2AvgMax_, hop1MaxZ_, hop2AvgMaxZ_,
    #   8   : isMal_
    mainData_train = [[], [], [], [], [], [], [], [], []] # 학습 데이터
    mainData_test = [[], [], [], [], [], [], [], [], []] # 테스트 데이터

    # 각 데이터를 랜덤하게 학습데이터(0) 또는 테스트데이터(1) 로 지정
    dataInfo = []
    dataSize = numOfTrain + numOfTest # 전체 데이터의 개수
    for i in range(dataSize): dataInfo.append(1)

    trainCount = 0
    
    while trainCount < numOfTrain: # 학습데이터(0) 지정
        index = math.floor(random.random() * dataSize)
        if dataInfo[index] != 0: # 기존에 학습데이터로 지정되지 않음
            trainCount += 1
            dataInfo[index] = 0

    # 학습 및 테스트 데이터 파일 존재 여부와 무관하게 실행
    try:
        
        # 학습 데이터를 mainFunc을 이용하여 생성
        mF = mainFunc(numOfTrain, numOfTest, virusProb, boardSize, maxDist)
        
        mainData = mF[0] # 학습 및 테스트 데이터의 각 line
        countList = mF[1] # 파일의 각 line에 대한 데이터 번호 (dataNo)
        
        # 숫자 데이터를 float으로 변환
        for i in range(len(mainData)):
            for j in range(8): mainData[j][i] = float(mainData[j][i])

        # numOfTrain_NB, numOfTrain_SVM, numOfTest에 추가
        for i in range(len(mainData[0])):
            dataNoOfThisLine = countList[i] # 데이터 파일의 해당 line의 dataNo 값
            
            if dataInfo[dataNoOfThisLine] == 0: # 0 -> 학습 데이터
                for j in range(9): mainData_train[j].append(mainData[j][i])
            elif dataInfo[dataNoOfThisLine] == 1: # 1 -> 테스트 데이터
                for j in range(9): mainData_test[j].append(mainData[j][i])

    except Exception as e:
        print('\nerror: ' + str(e) + '\n')

    # 학습 데이터, 테스트 데이터의 실제 line 개수 (각 데이터마다 node 개수만큼의 line이 있음)
    lenTrain = len(mainData_train[0])
    lenTest = len(mainData_test[0])

    print('\n################################')
    print(lenTrain, lenTest, virusProb, 'boardSize:', boardSize, 'maxDist:', maxDist)
    print('dataInfo: ' + str(dataInfo))
    print('################################\n')

    # isMal_의 'True', 'False'를 숫자 0(False), 1(True)로 변환
    for i in range(lenTrain):
        if mainData_train[8][i] == True: mainData_train[8][i] = 1
        else: mainData_train[8][i] = 0
    for i in range(lenTest):
        if mainData_test[8][i] == True: mainData_test[8][i] = 1
        else: mainData_test[8][i] = 0

    NN = [tf.keras.layers.Flatten(input_shape=(4,)),
            keras.layers.Dense(32, activation='relu'),
            keras.layers.Dense(32, activation='relu'),
            keras.layers.Dense(32, activation='relu'),
            keras.layers.Dense(32, activation='relu'),
            keras.layers.Dense(1, activation='sigmoid')]
    op = tf.keras.optimizers.Adam(0.001)

    # 학습 데이터의 입력, 출력 데이터 생성
    # 입력 데이터: hop1NoiseZ_(2), hop2AvgNoiseZ_(3), hop1MaxZ_(6), hop2AvgMaxZ_(7) / 출력 데이터: isMal_(8)
    inputs = []
    outputs = []
    for i in range(lenTrain):
        inputs.append([mainData_train[2][i], mainData_train[3][i], mainData_train[6][i], mainData_train[7][i]])
        outputs.append([mainData_train[8][i]])

    # 테스트 데이터의 입력 데이터 생성
    testInputs = [] # 테스트 입력
    testValOutputs = [] # 테스트에 대한 validation data (실제 결과값)
    for i in range(lenTest):
        testInputs.append([mainData_test[2][i], mainData_test[3][i], mainData_test[6][i], mainData_test[7][i]])
        testValOutputs.append(mainData_test[8][i])

    # 학습 실시
    print(inputs[0:5])
    deepLearning_GPU.deepLearning(NN, op, 'mean_squared_error', inputs, outputs, '200407_DTN', 2500, False, True, deviceName)

    # 테스트 실시
    testModel = deepLearning_GPU.deepLearningModel('200407_DTN', True)
    testOutputAllLayers = deepLearning_GPU.modelOutput(testModel, testInputs)
    testOutput = testOutputAllLayers[len(testOutputAllLayers)-1] # 모델에 testInputs를 입력했을 때의 결과값

    # 오차 제곱의 평균 계산
    difAvg = 0
    for i in range(lenTest): difAvg += pow(testOutput[i][0] - testValOutputs[i], 2)
    difAvg /= lenTest
    print('mean square error: ' + str(difAvg))
    print('')

    # TP, TN, FP, FN 계산
    TP = 0 # True Positive
    TN = 0 # True Negative
    FP = 0 # False Positive
    FN = 0 # False Negative

    for i in range(lenTest):
        if testOutput[i][0] >= 0.5 and testValOutputs[i] >= 0.5: TP += 1
        elif testOutput[i][0] < 0.5 and testValOutputs[i] < 0.5: TN += 1
        elif testOutput[i][0] >= 0.5 and testValOutputs[i] < 0.5: FP += 1
        elif testOutput[i][0] < 0.5 and testValOutputs[i] >= 0.5: FN += 1

    ALL = TP + TN + FP + FN
    RealP = TP + FN
    RealN = TN + FP
    correct = TP + FP

    # 테스트 결과 출력
    print('True Positive    : ' + str(TP) + ' / ' + str(round(100 * TP / ALL, 2)) + '%')
    print('True Negative    : ' + str(TN) + ' / ' + str(round(100 * TN / ALL, 2)) + '%')
    print('False Positive   : ' + str(FP) + ' / ' + str(round(100 * FP / ALL, 2)) + '%')
    print('False Negative   : ' + str(FN) + ' / ' + str(round(100 * FN / ALL, 2)) + '%')
    print('')
    print('Real Positive    : ' + str(RealP) + ' / ' + str(round(100 * RealP / ALL, 2)) + '%')
    print('Real Negative    : ' + str(RealN) + ' / ' + str(round(100 * RealN / ALL, 2)) + '%')
    print('')
    print('correct          : ' + str(correct) + ' / ' + str(round(100 * correct / ALL, 2)) + '%')
    print('')
def trainAndTest(numOfTrain, numOfTest, virusProb, boardSize, maxDist,
                 deviceName):  # 학습 데이터를 읽어서 배열에 저장 및 학습

    # 학습 및 테스트 데이터 파일 존재 여부와 무관하게 실행
    # 학습 데이터를 mainFunc을 이용하여 생성
    mF = mainFunc(numOfTrain, numOfTest, virusProb, boardSize, maxDist)

    mainData = mF[0]  # 학습 및 테스트 데이터의 각 line
    countList = mF[1]  # 파일의 각 line에 대한 데이터 번호 (dataNo)

    # 학습 실시
    #print(inputs[0:5])
    epochList = input('epochs:')
    testResult = ''
    testTable = 'epoch\ttrain\ttest\terror\tTP\tTPrate\tTN\tTNrate\tFP\tFPrate\tFN\tFNrate\treal P\trP rate\treal N\trN rate\tcorrect\tco rate\n'

    for epoch in range(len(epochList.split(' '))):

        # 학습 데이터 (각 x열에 해당하는 8개의 배열, y열에 해당하는 배열)
        # 0 ~ 5 : hop1Noise_, hop2AvgNoise_, hop2AvgNoise_, hop2AvgMax_, hop1Neis_, hop2AvgNeis_
        # 6 ~11 : hop1NoiseZ_, hop1MaxZ_, hop2AvgNoiseZ_, hop2AvgMaxZ_, hop1NeisZ_, hop2AvgNeisZ_,
        #  12   : isMal_
        mainData_train = [[], [], [], [], [], [], [], [], [], [], [], [],
                          []]  # 학습 데이터
        mainData_test = [[], [], [], [], [], [], [], [], [], [], [], [],
                         []]  # 테스트 데이터

        # 각 데이터를 랜덤하게 학습데이터(0) 또는 테스트데이터(1) 로 지정
        dataInfo = []
        dataSize = numOfTrain + numOfTest  # 전체 데이터의 개수
        for i in range(dataSize):
            dataInfo.append(1)

        trainCount = 0

        while trainCount < numOfTrain:  # 학습데이터(0) 지정
            index = math.floor(random.random() * dataSize)
            if dataInfo[index] != 0:  # 기존에 학습데이터로 지정되지 않음
                trainCount += 1
                dataInfo[index] = 0

        # 숫자 데이터를 float으로 변환
        for i in range(len(mainData)):
            for j in range(12):
                mainData[j][i] = float(mainData[j][i])

        # numOfTrain_NB, numOfTrain_SVM, numOfTest에 추가
        for i in range(len(mainData[0])):
            dataNoOfThisLine = countList[i]  # 데이터 파일의 해당 line의 dataNo 값

            if dataInfo[dataNoOfThisLine] == 0:  # 0 -> 학습 데이터
                for j in range(13):
                    mainData_train[j].append(mainData[j][i])
            elif dataInfo[dataNoOfThisLine] == 1:  # 1 -> 테스트 데이터
                for j in range(13):
                    mainData_test[j].append(mainData[j][i])

        # 학습 데이터, 테스트 데이터의 실제 line 개수 (각 데이터마다 node 개수만큼의 line이 있음)
        lenTrain = len(mainData_train[0])
        lenTest = len(mainData_test[0])
        print('length:', lenTrain, lenTest)

        print('\n################################')
        print(lenTrain, lenTest, virusProb, 'boardSize:', boardSize,
              'maxDist:', maxDist)
        print('dataInfo: ' + str(dataInfo))
        print('################################\n')

        # isMal_의 'True', 'False'를 숫자 0(False), 1(True)로 변환
        for i in range(lenTrain):
            if mainData_train[12][i] == True: mainData_train[8][i] = 1
            else: mainData_train[12][i] = 0
        for i in range(lenTest):
            if mainData_test[12][i] == True: mainData_test[8][i] = 1
            else: mainData_test[12][i] = 0

        # 신경망 생성
        NN = [
            tf.keras.layers.Flatten(input_shape=(4, )),
            keras.layers.Dense(32, activation='relu'),
            keras.layers.Dense(32, activation='relu'),
            keras.layers.Dense(32, activation='relu'),
            keras.layers.Dense(32, activation='relu'),
            keras.layers.Dense(1, activation='sigmoid')
        ]
        op = tf.keras.optimizers.Adam(0.001)

        # 학습 데이터의 입력, 출력 데이터 생성
        # 입력 데이터: hop1NoiseZ_, hop1MaxZ_, hop2AvgNoiseZ_, hop2AvgMaxZ_(6~9) / 출력 데이터: isMal_(12)
        inputs = []
        outputs = []
        for i in range(lenTrain):
            inputs.append([
                mainData_train[6][i], mainData_train[7][i],
                mainData_train[8][i], mainData_train[9][i]
            ])
            outputs.append([mainData_train[12][i]])

        # 테스트 데이터의 입력 데이터 생성
        testInputs = []  # 테스트 입력
        testValOutputs = []  # 테스트에 대한 validation data (실제 결과값)
        for i in range(lenTest):
            testInputs.append([
                mainData_test[6][i], mainData_test[7][i], mainData_test[8][i],
                mainData_test[9][i]
            ])
            testValOutputs.append(mainData_test[12][i])

        # 학습 실시
        ep = int(epochList.split(' ')[epoch])
        deepLearning_GPU.deepLearning(NN, op, 'mean_squared_error', inputs,
                                      outputs, '200407_DTN', ep, False, True,
                                      deviceName)

        # 테스트 실시
        testModel = deepLearning_GPU.deepLearningModel('200407_DTN', True)
        testOutputAllLayers = deepLearning_GPU.modelOutput(
            testModel, testInputs)
        testOutput = testOutputAllLayers[len(testOutputAllLayers) -
                                         1]  # 모델에 testInputs를 입력했을 때의 결과값

        # 오차 제곱의 평균 계산
        difAvg = 0
        for i in range(lenTest):
            difAvg += pow(testOutput[i][0] - testValOutputs[i], 2)
        difAvg /= lenTest
        print('mean square error: ' + str(difAvg))
        print('')

        # TP, TN, FP, FN 계산
        TP = 0  # True Positive
        TN = 0  # True Negative
        FP = 0  # False Positive
        FN = 0  # False Negative

        for i in range(lenTest):
            if testOutput[i][0] >= 0.5 and testValOutputs[i] >= 0.5: TP += 1
            elif testOutput[i][0] < 0.5 and testValOutputs[i] < 0.5: TN += 1
            elif testOutput[i][0] >= 0.5 and testValOutputs[i] < 0.5: FP += 1
            elif testOutput[i][0] < 0.5 and testValOutputs[i] >= 0.5: FN += 1

        ALL = TP + TN + FP + FN
        RealP = TP + FN
        RealN = TN + FP
        correct = TP + TN

        # 테스트 결과 저장
        testResult += ' <<< epoch ' + str(ep) + ' >>>\n'
        testResult += 'mean square error: ' + str(difAvg) + ' (train: ' + str(
            len(inputs)) + ' test:' + str(ALL) + ')\n\n'

        testResult += 'True Positive    : ' + str(TP) + ' / ' + str(
            round(100 * TP / ALL, 2)) + '%\n'
        testResult += 'True Negative    : ' + str(TN) + ' / ' + str(
            round(100 * TN / ALL, 2)) + '%\n'
        testResult += 'False Positive   : ' + str(FP) + ' / ' + str(
            round(100 * FP / ALL, 2)) + '%\n'
        testResult += 'False Negative   : ' + str(FN) + ' / ' + str(
            round(100 * FN / ALL, 2)) + '%\n\n'

        testResult += 'Real Positive    : ' + str(RealP) + ' / ' + str(
            round(100 * RealP / ALL, 2)) + '%\n'
        testResult += 'Real Negative    : ' + str(RealN) + ' / ' + str(
            round(100 * RealN / ALL, 2)) + '%\n'
        testResult += 'correct          : ' + str(correct) + ' / ' + str(
            round(100 * correct / ALL, 2)) + '%\n\n'

        testTable += str(ep) + '\t' + str(
            len(inputs)) + '\t' + str(ALL) + '\t' + str(round(
                difAvg, 4)) + '\t' + str(TP) + '\t' + str(
                    round(100 * TP / ALL, 2)) + '%\t'
        testTable += str(TN) + '\t' + str(round(100 * TN / ALL, 2)) + '%\t'
        testTable += str(FP) + '\t' + str(round(100 * FP / ALL, 2)) + '%\t'
        testTable += str(FN) + '\t' + str(round(100 * FN / ALL, 2)) + '%\t'
        testTable += str(RealP) + '\t' + str(round(100 * RealP / ALL,
                                                   2)) + '%\t'
        testTable += str(RealN) + '\t' + str(round(100 * RealN / ALL,
                                                   2)) + '%\t'
        testTable += str(correct) + '\t' + str(round(100 * correct / ALL,
                                                     2)) + '%\n'

    # 파일에 쓰기
    tR = open('DTN_testResult.txt', 'w')
    tR.write(testResult)
    tR.close()

    tT = open('DTN_testTable.txt', 'w')
    tT.write(testTable)
    tT.close()
Beispiel #9
0
    NN = [
        tf.keras.layers.Flatten(input_shape=(2, )),
        keras.layers.Dense(16, activation='relu'),
        keras.layers.Dense(16, activation='relu'),
        keras.layers.Dense(2, activation='sigmoid')
    ]
    op = tf.keras.optimizers.Adam(0.001)

    inputs = [[2, 1], [3, 2], [5, 3], [8, 5], [13, 8], [21, 13], [34, 21]]
    outputs = [[0.2, 0.5], [0.4, 0.6], [0.5, 0.7], [0.7, 0.75], [0.8, 0.77],
               [0.85, 0.8], [0.9, 0.85]]

    # learning
    print('\n ' + ('#===' * 16) + ' <<<< LEARNING >>>> ' + ('===#' * 16) +
          '\n')
    deepLearning_GPU.deepLearning(NN, op, 'mean_squared_error', inputs,
                                  outputs, 'test', 500, True, True, deviceName)

    # test
    print('\n ' + ('#===' * 16) + ' <<<< TEST >>>> ' + ('===#' * 16) + '\n')

    print('\n << test output (첫번째 Neural Network) >>\n')
    newModel = deepLearning_GPU.deepLearningModel('test', True)
    testOutput = deepLearning_GPU.modelOutput(newModel,
                                              [[4, 2.5], [6, 3.5], [7, 4.5]])
    print('\n[[4, 2.5], [6, 3.5], [7, 4.5]]에 대한 학습 결과:\n')

    for i in range(len(testOutput)):
        print(' << layer No: ' + str(i) + ' >>')
        print(str(testOutput[i]))
    # model design
    NN = [
        tf.keras.layers.Flatten(input_shape=(len(inputs[0]), )),
        keras.layers.Dense(16, activation='relu'),
        keras.layers.Dense(16, activation='relu'),
        keras.layers.Dense(16, activation='relu'),
        keras.layers.Dense(16, activation='relu'),
        keras.layers.Dense(len(outputs[0]), activation='sigmoid')
    ]
    op = tf.keras.optimizers.Adam(0.001)

    # learning
    print('\n ' + ('#===' * 16) + ' <<<< LEARNING >>>> ' + ('===#' * 16) +
          '\n')
    deepLearning_GPU.deepLearning(NN, op, 'mean_squared_error', trainI, trainO,
                                  'test', 500, True, True, deviceName)

    # test
    print('\n ' + ('#===' * 16) + ' <<<< TEST >>>> ' + ('===#' * 16) + '\n')

    print('\n << test output (첫번째 Neural Network) >>\n')
    newModel = deepLearning_GPU.deepLearningModel('test', True)
    testOutput = deepLearning_GPU.modelOutput(newModel, testI)
    print('\ntest output에 대한 테스트 결과:\n')

    # estimate
    outputLayer = testOutput[len(testOutput) - 1]
    print('')
    print('layers in the network: ' + str(len(testOutput)))
    print('')
    difAbs = 0.0
Beispiel #11
0
def deepLearning(inputFileName, outputFileName, testFileName,
                 testOutputFileName, valid, deviceName, epoch, printed,
                 modelName, normalizeTarget):

    # read files
    # trainO : (originalOutput - meanOriginalOutput)/stdOriginalOutput
    trainI = helper.getDataFromFile(inputFileName, None)  # input train data
    trainO = helper.getDataFromFile(
        outputFileName, None)  # output train data (Sigmoid applied)
    testI = helper.getDataFromFile(
        testFileName, None)  # test input data (set nullValue to 0)

    # apply sigmoid to train output data
    # trainO :   sigmoid(normalize(originalOutput))
    #          = sigmoid((originalOutput - meanOriginalOutput)/stdOriginalOutput)
    for i in range(len(trainO)):
        for j in range(len(trainO[0])):
            trainO[i][j] = helper.sigmoid(trainO[i][j])

    # for i in range(15): print(trainO[i])

    print('')
    print(' ---- number of rows ----')
    print('input  size: ' + str(len(trainI)))
    print('output size: ' + str(len(trainO)))
    print('test   size: ' + str(len(testI)))
    print('')

    # print input, output, and test data
    if printed != 0:
        print('\n ---- original input data ----\n')
        for i in range(len(trainI)):
            print(helper.roundedArray(trainI[i], 6))

        print('\n ---- original output data ----\n')
        for i in range(len(trainO)):
            print(helper.roundedArray(trainO[i], 6))

        print('\n ---- original test data ----\n')
        for i in range(len(testI)):
            print(helper.roundedArray(testI[i], 6))

    # model design using deepLearning_model.txt, in the form of
    # activation function of final layer is always 'sigmoid'
    f = open('deepLearning_model.txt', 'r')
    modelInfo = f.readlines()
    f.close()

    # read normalization info
    if normalizeTarget == True:
        fnorm = open('data_normalizeInfo.txt', 'r')
        fnormInfo = fnorm.readlines()
        fnormMean = float(fnormInfo[0].split(' ')[0])  # mean of training data
        fnormStd = float(fnormInfo[0].split(' ')[1])  # stddev of training data

    #### TEST when the value of valid is 0 ####
    if valid == 0:

        # NN and optimizer
        NN = helper.getNN(modelInfo, trainI, trainO)  # Neural Network
        op = helper.getOptimizer(modelInfo)  # optimizer

        #print(trainI[:5])
        #print(trainO[:5])

        try:  # try reading test.h5 and test.json
            newModel = deepLearning_GPU.deepLearningModel(modelName, True)
            testOutput = deepLearning_GPU.modelOutput(newModel, testI)
        except:  # do learning if test.h5 and test.json does not exist
            print('\n <<<< LEARNING >>>>\n')

            # False, True는 각각 dataPrint(학습데이터 출력 여부), modelPrint(model의 summary 출력 여부)
            deepLearning_GPU.deepLearning(NN, op, 'mean_squared_error', trainI,
                                          trainO, modelName, epoch, False,
                                          True, deviceName)

            newModel = deepLearning_GPU.deepLearningModel(modelName, True)
            testOutput = deepLearning_GPU.modelOutput(newModel, testI)

        # test
        print('\n <<<< TEST >>>>\n')

        # estimate
        outputLayer = testOutput[len(testOutput) - 1]

        # inverse sigmoid
        # output:   denormalize(invSigmoid(sigmoid(normalize(originalOutput))))
        #         = denormalize((originalOutput - meanOriginalOutput)/stdOriginalOutput)
        #         = originalOutput
        for i in range(len(outputLayer)):  # for each output data
            for j in range(len(
                    outputLayer[0])):  # for each value of output data
                outputLayer[i][j] = helper.invSigmoid(outputLayer[i][j])
                if normalizeTarget == True:
                    outputLayer[i][
                        j] = outputLayer[i][j] * fnormStd + fnormMean

        # write to file
        result = ''
        print('\n<<<< output layer >>>>')
        for i in range(len(outputLayer)):  # for each output data
            for j in range(len(
                    outputLayer[0])):  # for each value of output data
                result += str(outputLayer[i][j]) + '\t'
            result += '\n'

        f = open(testOutputFileName.split('.')[0] + '_prediction.txt', 'w')
        f.write(result)
        f.close()

        # return final result
        finalResult = []
        for i in range(len(outputLayer)):  # for each output data
            finalResult.append(outputLayer[i][0])

        return finalResult

    #### VALIDATION when the value of valid is >0 ####
    else:

        # make index-list of validation data
        inputSize = len(trainI)
        validSize = int(inputSize * valid)
        trainSize = inputSize - validSize

        validArray = []
        for i in range(inputSize):
            validArray.append(0)
        while sum(validArray) < validSize:
            validArray[random.randint(0, inputSize - 1)] = 1

        # make train and validation data
        # _TrainO, _ValidO : sigmoid((originalOutput - meanOriginalOutput)/stdOriginalOutput)
        _TrainI = []  # training input
        _TrainO = []  # training output
        _ValidI = []  # valid input
        _ValidO = []  # valid output

        for i in range(inputSize):
            if validArray[i] == 0:  # training data
                _TrainI.append(trainI[i])
                _TrainO.append(trainO[i])
            else:  # validation data
                _ValidI.append(trainI[i])
                _ValidO.append(trainO[i])

        # model name for validation
        newModelName = modelName + 'Valid'

        # NN and optimizer
        NN = helper.getNN(modelInfo, _TrainI, _TrainO)  # Neural Network
        op = helper.getOptimizer(modelInfo)  # optimizer

        # output for validation
        try:  # try reading testValid.h5 and test.json
            validModel = deepLearning_GPU.deepLearningModel(newModelName, True)
            predictedValidO = deepLearning_GPU.modelOutput(validModel, _ValidI)
        except:  # do learning if testValid.h5 and test.json does not exist
            print('\n <<<< LEARNING >>>>\n')

            # False, True는 각각 dataPrint(학습데이터 출력 여부), modelPrint(model의 summary 출력 여부)
            # _TrainO : sigmoid((originalOutput - meanOriginalOutput)/stdOriginalOutput)
            deepLearning_GPU.deepLearning(NN, op, 'mean_squared_error',
                                          _TrainI, _TrainO, newModelName,
                                          epoch, False, True, deviceName)

            validModel = deepLearning_GPU.deepLearningModel(newModelName, True)
            predictedValidO = deepLearning_GPU.modelOutput(validModel, _ValidI)

        # evaluation
        print('\n <<<< VALID >>>>\n')

        MAE = 0  # mean absolute error
        MSE = 0  # mean square error
        accuracy = 0  # accuracy

        # predicted validation output
        outputLayer = predictedValidO[len(predictedValidO) - 1]

        # inverse sigmoid
        # output :   invSigmoid(sigmoid(normalize(originalOutput)))
        #          = (originalOutput - meanOriginalOutput)/stdOriginalOutput
        for i in range(len(outputLayer)):  # for each output data
            for j in range(len(
                    outputLayer[0])):  # for each value of output data
                outputLayer[i][j] = helper.invSigmoid(outputLayer[i][j])

        # compute error
        # output  : denormalize((originalOutput - meanOriginalOutput)/stdOriginalOutput)
        #           = originalOutput
        # _Valid0 : denormalize(invSigmoid(sigmoid((originalOutput - meanOriginalOutput)/stdOriginalOutput)))
        #           = denormalize((originalOutput - meanOriginalOutput)/stdOriginalOutput)
        #           = originalOutput
        for i in range(len(outputLayer)):  # for each output data
            for j in range(len(
                    outputLayer[0])):  # for each value of output data
                _ValidO[i][j] = helper.invSigmoid(_ValidO[i][j])
                if normalizeTarget == True:
                    _ValidO[i][j] = _ValidO[i][j] * fnormStd + fnormMean
                    outputLayer[i][
                        j] = outputLayer[i][j] * fnormStd + fnormMean

        # compute error
        validCount = 0
        resultToWrite = ''
        for i in range(inputSize):
            if validArray[i] == 1:

                # compute errors and accuracy
                thisAE = abs(_ValidO[validCount][0] -
                             outputLayer[validCount][0])
                thisSE = pow(
                    _ValidO[validCount][0] - outputLayer[validCount][0], 2)
                MAE += thisAE
                MSE += thisSE
                if thisSE <= 0.5: accuracy += 1

                # print and write result
                newResultToWrite = ('[' + str(i) + '] pred = ' +
                                    str(int(outputLayer[validCount][0])) +
                                    ', real = ' +
                                    str(int(_ValidO[validCount][0])) +
                                    ', AE = ' + str(int(thisAE)) + ', SE = ' +
                                    str(int(thisSE)))
                resultToWrite += newResultToWrite + '\n'
                print(newResultToWrite)

                validCount += 1

        MAE /= validSize
        MSE /= validSize
        accuracy /= validSize

        # print evaluation result
        resultSummary = ''
        resultSummary += 'input size : ' + str(inputSize) + '\n'
        resultSummary += 'train size : ' + str(trainSize) + '\n'
        resultSummary += 'valid size : ' + str(validSize) + '\n'
        resultSummary += 'MAE        : ' + str(round(MAE, 6)) + '\n'
        resultSummary += 'MSE        : ' + str(round(MSE, 6)) + '\n'
        resultSummary += 'accuracy   : ' + str(round(accuracy, 6)) + '\n'
        resultSummary += 'pred avg   : ' + str(np.average(outputLayer,
                                                          axis=0)) + '\n'
        resultSummary += 'real avg   : ' + str(np.average(_ValidO,
                                                          axis=0)) + '\n'
        print(resultSummary)
        resultToWrite += resultSummary

        # write result file
        fvalid = open('data_valid_result.txt', 'w')
        fvalid.write(resultToWrite)
        fvalid.close()
def run(size, numTrain, numTest, numWD, deviceName, doTrainAndTest):

    inputs = []  # 딥러닝 입력값 저장
    outputs = []  # 딥러닝 출력값 저장

    isFile = False  # valueMaze_X.json 학습 모델 파일이 있는가?

    # 입력 정보
    originalScreen = []  # 각 맵의 스크린
    wdList = [
    ]  # 각 맵에서 wireless device의 위치를 저장한 배열, [[wd0Y, wd0X], [wd1Y, wd1X], ..., [wd(n-1)Y, wd(n-1)X]]

    # 출력 정보
    outputScreen = [
    ]  # 각 맵에서의 각 좌표에서의 throughput을 나타낸 맵, [[[thrput(0,0), thrput(0,1), ...], [thrput(1,0), ...], ...], (so on)]

    # 평가 결과 저장
    RL = []

    # 1. 트레이닝 및 테스트용 맵 생성
    # 맵의 구성: .(공간), H(HAP), W(wireless device)
    if readOrWrite == 1:  # 맵 파일 쓰고 그 맵으로 트레이닝, 테스트
        print('generating maps...')
        for i in range(numTrain + numTest):
            initScreen_ = WPCN_helper_REAL.initScreen(size, False, numWD)

            originalScreen.append(initScreen_[0])
            wdList.append(initScreen_[2])

            # 맵 파일 작성
            f = open(
                'originalMaps_' + str(size) + '_' + str(numWD) + '/DL_WPCN_' +
                ('0' if i < 1000 else '') + ('0' if i < 100 else '') +
                ('0' if i < 10 else '') + str(i) + '.txt', 'w')
            for j in range(size):
                for k in range(size):
                    if originalScreen[i][j][k] == -1:
                        f.write('W')  # wireless device
                    elif originalScreen[i][j][k] == 0:
                        f.write('.')  # space
                    elif originalScreen[i][j][k] == 1:
                        f.write('H')  # HAP (not exist in the original map)
                f.write('\n')
            f.close()

    else:  # 기존 맵 파일 읽어서 기존 맵으로 트레이닝, 테스트
        print('reading maps...')
        for i in range(numTrain + numTest):
            f = open(
                'originalMaps_' + str(size) + '_' + str(numWD) + '/DL_WPCN_' +
                ('0' if i < 1000 else '') + ('0' if i < 100 else '') +
                ('0' if i < 10 else '') + str(i) + '.txt', 'r')
            map_ = f.readlines()  # 맵을 나타낸 배열
            f.close()
            for j in range(len(map_)):
                map_[j] = map_[j].replace('\n', '')  # 각 줄마다 개행문자 제거

            # originalScreen과 wdList 읽어오기
            wdListTemp = []
            thisScreen = [[0] * size
                          for j in range(size)]  # originalScreen에 추가할 맵(스크린)
            for j in range(size):
                for k in range(size):
                    if map_[j][k] == 'W':
                        thisScreen[j][k] = -1  # wireless device
                        wdListTemp.append([j, k])  # wdListTemp에 추가
                    elif map_[j][k] == '.':
                        thisScreen[j][k] = 0  # space
                    elif map_[j][k] == 'H':
                        thisScreen[j][
                            k] = 1  # HAP (not exist in the original map)

            originalScreen.append(thisScreen)
            wdList.append(wdListTemp)

    # print(wdList)

    # 2. 트레이닝 및 테스트용 맵에서 각 x, y (HAP의 좌표)에 대해 최적의 할당 시간(HAPtime)을 찾아서,
    # HAP의 각 좌표에서의 throughput에 대한 맵 만들기 (optiInfoForMap_X.txt where X = 0 or 1)
    # y좌표, x좌표는 1 간격

    # 형식:
    # * (맵 번호) (HAPtime) (최적y) (최적x) (최대thrput)
    # (thrput at y=0,x=0) (thrput at y=0,x=1) ... (thrput at y=0,x=n)
    # (thrput at y=1,x=0) (thrput at y=1,x=1) ... (thrput at y=1,x=n)
    # ...                 ...                     ...
    # (thrput at y=n,x=0) (thrput at y=n,x=1) ... (thrput at y=n,x=n)
    toSave = ''

    lines = 0  # optiInfoForMap_X.txt 파일의 라인 개수 (X = 0 or 1)

    try:  # optiInfoForMap 파일이 있으면 읽기
        f = open(
            'optiInfoForMap/optiInfoForMap_' + str(problemNo) + '_forPaper_' +
            str(size) + '_' + str(numWD) + '.txt', 'r')
        optiInformation = f.readlines()
        f.close()

        lines = len(optiInformation)  # 데이터 개수는 lines / (1 + size)

        if lines / (1 + size) < numTrain + numTest:  # 완전한 정보가 없으면
            for i in range(lines):
                toSave += optiInformation[i]  # toSave에 기존 저장된 정보 추가
            raiseError = 1 / 0  # 오류 발생시키기

        # 출력값 배열에 기존 파일에 있는 추가
        temp = []  # 출력값 배열에 추가할 데이터
        for i in range(lines):

            # '*' 기호 (새로운 맵)를 만나면 기존 temp 배열의 데이터를 저장하고 temp는 초기화
            if optiInformation[i][0] == '*':
                if len(temp) > 0: outputScreen.append(temp)
                temp = []
            # 그렇지 않으면 temp에 해당 행의 데이터를 추가
            elif len(optiInformation[i]) >= 3:  # 공백 또는 개행만 있는 줄 제외
                temp.append(optiInformation[i].split(' '))
                for j in range(len(temp[len(temp) - 1])):
                    temp[len(temp) - 1][j] = float(temp[len(temp) - 1][j])

    except Exception as e:  # optiInfoForMap 파일이 없으면 새로 생성하기
        print(e)
        print("can't read optiInfoForMap_" + str(problemNo) + '_forPaper_' +
              str(size) + '_' + str(numWD) + '.txt')

        for i in range(int(lines / (1 + size)), numTrain + numTest):
            print('finding max throughput for map ' + str(i) + '...')

            # HAP의 위치(각 좌표)에 따른 최적의 할당 시간을 찾아서 출력값에 추가
            temp = [
                [0] * size for _ in range(size)
            ]  # 출력값 배열에 추가할 데이터 (해당 x, y 좌표에서의 HAPtime에 따른 최대 throughput)

            optiY_ = 0  # throughput (sum 또는 common)이 최대가 되는 HAP의 y좌표
            optiX_ = 0  # throughput (sum 또는 common)이 최대가 되는 HAP의 x좌표
            maxThroughput_ = 0.0  # throughput (sum 또는 common)의 최댓값
            maxHAPtime_ = 0.0  # throughput (sum 또는 common)이 최대가 되기 위한 HAP에 할당되는 시간

            for y in range(size):
                for x in range(size):
                    (throughput, HAPtime) = WPCN_helper_REAL.getThroughput(
                        wdList[i], [y, x], size, problemNo)
                    temp[y][x] = throughput

                    # throughput 최고기록을 갱신한 경우 업데이트
                    if throughput > maxThroughput_:
                        optiY_ = y
                        optiX_ = x
                        maxThroughput_ = throughput
                        maxHAPtime_ = HAPtime

            # toSave에 추가
            # * (맵 번호) (HAPtime) (최적y) (최적x) (최대thrput)
            # (thrput at y=0,x=0) (thrput at y=0,x=1) ... (thrput at y=0,x=n)
            # (thrput at y=1,x=0) (thrput at y=1,x=1) ... (thrput at y=1,x=n)
            # ...                 ...                     ...
            # (thrput at y=n,x=0) (thrput at y=n,x=1) ... (thrput at y=n,x=n)
            toSave += '* ' + str(i) + ' ' + str(maxHAPtime_) + ' ' + str(
                optiY_) + ' ' + str(optiX_) + ' ' + str(maxThroughput_) + '\n'
            for j in range(size):
                for k in range(size - 1):
                    toSave += str(temp[j][k]) + ' '
                toSave += str(temp[j][size - 1]) + '\n'

            # 출력 및 toSave에 추가
            print('max throughput: ' + str(maxThroughput_) + ' axis: ' +
                  str(optiY_) + ' ' + str(optiX_) + ' HAPtime: ' +
                  str(maxHAPtime_))

            # 출력값 배열에 추가
            if i < numTrain: outputScreen.append(temp)

            # 파일로 저장
            optiInfo = open(
                'optiInfoForMap/optiInfoForMap_' + str(problemNo) +
                '_forPaper_' + str(size) + '_' + str(numWD) + '.txt', 'w')
            optiInfo.write(toSave)
            optiInfo.close()

    # 3. 트레이닝용 맵의 정보를 입력값과 출력값 배열에 넣기
    print('make input and output data...')

    for i in range(numTrain):
        originalScreen_ = deepLearning_GPU_helper.arrayCopy(
            originalScreen[i])  # copy array from originalScreen
        originalScreen_ = deepLearning_GPU_helper.arrayCopyFlatten(
            originalScreen_, 0, size, 0, size, None)  # flatten
        inputs.append(originalScreen_)

        # find max value in outputScreen[i]
        maxVal = 0.0
        for j in range(size):
            for k in range(size):
                if outputScreen[i][j][k] > maxVal:
                    maxVal = outputScreen[i][j][k]
        for j in range(size):
            for k in range(size):
                outputScreen[i][j][
                    k] /= maxVal  # uniform [0, maxVal] -> uniform [0, 1]
                outputScreen[i][j][k] = outputScreen[i][j][
                    k] * 2.0 - 1.0  # uniform [0, 1] -> uniform [-1, 1]

        # 테스트 시 역sigmoid를 적용할 것이므로 먼저 outputScreen의 모든 원소에 sigmoid를 적용 (outputScreen의 값이 1 이상이 될수 있으므로)
        for j in range(size):
            for k in range(size):
                outputScreen[i][j][k] = deepLearning_GPU_helper.sigmoid(
                    outputScreen[i][j][k])

        outputs.append([outputScreen[i]])

    # 트레이닝, 테스트를 하지 않고 종료
    if doTrainAndTest == False: return

    # 4~7. 학습 및 테스트는 주어진 입출력 데이터를 이용하여 계속 반복
    while True:
        epoch = input('epoch').split(
            ' '
        )  # 입력형식: epoch0 epoch1 epoch2 ... epochN (ex: 5 15 30 100 200 300 500 1000)
        dropout = input('dropout').split(
            ' '
        )  # 입력형식: dropout0, dropout1, dropout2, ..., dropoutN (ex: 0 0.05 0.1 0.15 0.2)

        # 딥러닝 학습시키기
        lc = 'mean_squared_error'  # loss calculator

        for epo in range(len(epoch)):  # 각 epoch에 대하여
            for dro in range(len(dropout)):  # 각 dropout에 대하여 실험

                epoc = int(epoch[epo])
                drop = float(dropout[dro])

                while True:

                    # 실험 결과
                    resultForThisMap = ''
                    WPCNdeepNN_modelName = 'WPCNdeepNN_' + str(
                        size) + '_' + str(numWD)

                    try:
                        # 4. 모델 정의
                        NN = [
                            tf.keras.layers.Reshape(
                                (size, size, 1), input_shape=(size * size, )),
                            keras.layers.Conv2D(32,
                                                kernel_size=(3, 3),
                                                input_shape=(size, size, 1),
                                                activation='relu'),
                            keras.layers.MaxPooling2D(pool_size=2),
                            keras.layers.Dropout(drop),
                            keras.layers.Conv2D(32, (3, 3), activation='relu'),
                            keras.layers.Flatten(),
                            keras.layers.Dropout(drop),
                            keras.layers.Dense(40, activation='relu'),
                            keras.layers.Dense(size * size,
                                               activation='sigmoid'),
                            keras.layers.Reshape((1, size, size),
                                                 input_shape=(size * size, ))
                        ]

                        # 5. 옵티마이저
                        if problemNo == 0:
                            op = tf.keras.optimizers.Adam(0.001)  # for Sum-
                        else:
                            op = tf.keras.optimizers.Adam(
                                0.0001)  # for Common-

                        print('[00] training...')
                        deepLearning_GPU.deepLearning(NN, op, lc, inputs,
                                                      outputs,
                                                      WPCNdeepNN_modelName,
                                                      epoc, False, True,
                                                      deviceName)

                        # 6. 테스트 데이터에 대한 출력 결과 확인하고 정답과 비교하기
                        print('[10] loading model...')
                        newModel = deepLearning_GPU.deepLearningModel(
                            WPCNdeepNN_modelName, True)

                        print('[20] init throughput values...')
                        sumTestThroughput = 0.0  # test throughput의 합계
                        sumCorrectMaxThroughput = 0.0  # 정답의 throughput의 합계 (training data를 생성할 때와 같은 방법으로 최대 throughput 확인)

                        optiInfo = open(
                            'optiInfoForMap/optiInfoForMap_' + str(problemNo) +
                            '_forPaper_' + str(size) + '_' + str(numWD) +
                            '.txt', 'r')
                        optiInformation = optiInfo.readlines()
                        optiInfo.close()

                        print('[30] testing...')

                        for i in range(numTest):
                            if i == 0: print('[31] initializing...')
                            testScreen = originalScreen[numTrain +
                                                        i]  # 테스트할 스크린
                            testOutput = deepLearning_GPU.modelOutput(
                                newModel,
                                [np.array(testScreen).reshape(size * size)
                                 ])  # 테스트 결과
                            testOutputLayer = testOutput[
                                len(testOutput) - 1]  # 테스트 결과의 output layer의 값

                            # 6-0. 테스트 결과 확인
                            # 출력값 받아오기
                            if i == 0: print('[32] getting optiScreen...')
                            optiScreen = [[0] * size for j in range(size)
                                          ]  # 테스트 결과의 출력 스크린(=맵)
                            for j in range(size):
                                for k in range(size):
                                    optiScreen[j][
                                        k] = deepLearning_GPU_helper.invSigmoid(
                                            testOutputLayer[0][0][j][k])

                            if i == 0:
                                print('[33] finding optimal X and Y values...')
                            # 출력값에서 최적의 HAP의 x, y좌표 찾기
                            optiY_test = 0  # 출력 map에서의 최적의 HAP의 y좌표
                            optiX_test = 0  # 출력 map에서의 최적의 HAP의 x좌표
                            optiThroughput = 0  # 출력 map에서의 최적의 HAP의 x, y좌표에서의 throughput
                            for j in range(size):
                                for k in range(size):
                                    if optiScreen[j][k] > optiThroughput:
                                        optiY_test = j
                                        optiX_test = k
                                        optiThroughput = optiScreen[j][k]

                            # 상하/좌우의 throughput의 값을 이용하여 좌표 보정
                            if i == 0:
                                print('[34] modifying X and Y values...')
                            optiY = optiY_test
                            optiX = optiX_test

                            # y좌표를 (y-1), y, (y+1) 좌표의 throughput 값을 이용하여 보정
                            if optiY > 0 and optiY < size - 1:

                                up = optiScreen[optiY - 1][optiX]
                                this = optiScreen[optiY][optiX]
                                down = optiScreen[optiY + 1][optiX]
                                minVal = min(up, this, down)

                                up -= minVal
                                this -= minVal
                                down -= minVal

                                optiY_test += (up * (-1) +
                                               down * 1) / (up + this + down)

                            # x좌표를 (x-1), x, (x+1) 좌표의 throughput 값을 이용하여 보정
                            if optiX > 0 and optiX < size - 1:

                                left = optiScreen[optiY][optiX - 1]
                                this = optiScreen[optiY][optiX]
                                right = optiScreen[optiY][optiX + 1]
                                minVal = min(left, this, right)

                                left -= minVal
                                this -= minVal
                                right -= minVal

                                optiX_test += (left * (-1) + right * 1) / (
                                    left + this + right)

                            # 출력값 중 HAP의 좌표를 범위 내로 조정
                            if i == 0:
                                print(
                                    '[35] setting X and Y values as inside the range...'
                                )
                            if optiY_test > size - 1: optiY_test = size - 1
                            elif optiY_test < 0: optiY_test = 0

                            if optiX_test > size - 1: optiX_test = size - 1
                            elif optiX_test < 0: optiX_test = 0

                            # 테스트 결과 받아오기
                            if i == 0: print('[36] getting test results...')
                            (throughput,
                             HAPtime) = WPCN_helper_REAL.getThroughput(
                                 wdList[numTrain + i],
                                 [optiY_test, optiX_test], size, problemNo)
                            sumTestThroughput += throughput

                            # 6-1. testScreen과 optiScreen 중 일부 출력
                            if i == 0: print('[37] printing...')
                            if i < 10:  # 처음 10개의 map에 대해서만 출력
                                # testScreen 출력
                                print(' --- test input screen for map ' +
                                      str(i) + ' ---')
                                toPrint = ''
                                for j in range(size):
                                    toPrint += '['
                                    for k in range(size):
                                        if testScreen[j][k] == -1:
                                            toPrint += 'W  '
                                        else:
                                            toPrint += '.  '
                                    toPrint += ']\n'
                                print(toPrint)

                                # optiScreen 출력
                                print(' --- test output screen for map ' +
                                      str(i) + ' ---')
                                toPrint = ''
                                for j in range(size):
                                    toPrint += '['
                                    for k in range(size):
                                        toPrint += str(
                                            round(optiScreen[j][k], 3)
                                        ) + ' ' * (7 - len(
                                            str(round(optiScreen[j][k], 3))))
                                    toPrint += ']\n'
                                print(toPrint)

                            # 6-2. 정답과 비교
                            # 최적의 HAP의 위치 및 할당 시간 찾기
                            # HAP의 위치 및 할당 시간에 따른 throughput의 최댓값
                            if i == 0:
                                print('[38] comparing with correct answer...')
                            correctMaxThroughput = float(
                                optiInformation[(numTrain + i) *
                                                (size + 1)].split(' ')[5])
                            sumCorrectMaxThroughput += correctMaxThroughput

                            # 정답과 비교
                            resultForThisMap += (
                                'test throughput for map ' + str(i) + ' [Y=' +
                                str(round(optiY_test, 3)) + ' X=' +
                                str(round(optiX_test, 3)) + ' HT=' +
                                str(HAPtime) + '] : ' +
                                str(round(throughput, 6)) + ' / ' +
                                str(round(correctMaxThroughput, 6)) + ' (' +
                                str(
                                    round(
                                        100.0 * throughput /
                                        correctMaxThroughput, 6)) + ' %)\n')
                            if i < 10: print('')
                            else: print(i)

                        # 7. 평가
                        print('[40] evaluating...')
                        percentage = round(
                            100.0 * sumTestThroughput /
                            sumCorrectMaxThroughput, 6)
                        print('size:' + str(size) + ' train:' + str(numTrain) +
                              ' test:' + str(numTest) +
                              ' problem(0=Sum,1=Common):' + str(problemNo) +
                              ' epoch:' + str(epoc) + ' dropout:' +
                              str(round(drop, 3)))
                        print('total test throughput: ' +
                              str(round(sumTestThroughput, 6)))
                        print('total max  throughput: ' +
                              str(round(sumCorrectMaxThroughput, 6)))
                        print('percentage           : ' + str(percentage) +
                              ' %')

                        RL.append([
                            size, numTrain, numTest, problemNo, epoc, drop,
                            percentage
                        ])

                    # 오류 발생 처리
                    except Exception as ex:
                        print('\n################################')
                        print('error: ' + str(ex))
                        print('retry training and testing')
                        print('################################\n')

                    # 현재까지의 평가 결과 출력
                    print('\n <<< ESTIMATION RESULT >>>')
                    print(
                        'No.\tsize\ttrain\ttest\tproblem\tepoch\tdropout\tpercentage'
                    )

                    saveResult = '\n <<< ESTIMATION RESULT >>>\nNo.\tsize\ttrain\ttest\tproblem\tepoch\tdropout\tpercentage\n'

                    for i in range(len(RL)):
                        toPrint = (str(i) + '\t' + str(RL[i][0]) + '\t' +
                                   str(RL[i][1]) + '\t' + str(RL[i][2]) +
                                   '\t' + str(RL[i][3]) + '\t' +
                                   str(RL[i][4]) + '\t' +
                                   str(round(RL[i][5], 3)) + '\t' +
                                   (' ' if round(RL[i][6], 4) < 10.0 else '') +
                                   str(round(RL[i][6], 4)))
                        print(toPrint)
                        saveResult += (toPrint + '\n')
                    print('\n')

                    # 평가 결과 저장
                    fSave = open(
                        'DL_WPCN_forPaper_' + str(problemNo) + '_' +
                        str(size) + '_' + str(numWD) + '.txt', 'w')
                    fSave.write(saveResult)
                    fSave.close()

                    fSave_ = open(
                        'DL_WPCN_forPaper_' + str(problemNo) + '_' +
                        str(size) + '_' + str(numWD) + '_detail.txt', 'w')
                    fSave_.write(resultForThisMap)
                    fSave_.close()

                    # 학습 및 테스트 종료
                    break

        # 완전 종료
        break