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 if len(inputData) > 0: RD.saveArray('Q_input.txt', inputData) if len(outputData) > 0: 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 DLmain.deeplearning applies it try: DLmain.deepLearning('Q_input.txt', 'Q_output.txt', None, None, None, None, 0.0, None, 'modelConfig.txt', deviceName, epoch, printed, 'deepQ_model') except: print('Q_input.txt or Q_output.txt does not exist.')
def train(num, deviceName, epoch, printed): # meta info TRI = 'input_' + str(num) + '.txt' TRO = 'output_' + str(num) + '.txt' TEI = None TEO = None TE_real = None TE_report = None VAL_rate = 0.05 VAL_report = 'report_val_' + str(num) + '.txt' modelConfig = 'model_config_' + str(num) + '.txt' # training and test # 'config.txt' is used for configuration DL.deepLearning(TRI, TRO, TEI, TEO, TE_real, TE_report, VAL_rate, VAL_report, modelConfig, deviceName, epoch, printed, 'model_' + str(num) + '_')
# main if __name__ == '__main__': # meta info TRI = 'mnist_train_input.txt' TRO = 'mnist_train_output.txt' TEI = 'mnist_test_input.txt' TEO = 'mnist_test_predict.txt' TE_real = 'mnist_test_output.txt' TE_report = 'mnist_test_report.txt' VAL_rate = 0.0 VAL_report = 'mnist_val_report.txt' modelConfig = 'mnist_model_config.txt' # preprocess data convertToNumeric() # user data deviceName = input('device name (for example, cpu:0 or gpu:0)') epoch = int(input('epoch')) printed = int(input('printed? (0 -> do not print)')) # training and test # 'config.txt' is used for configuration DL.deepLearning(TRI, TRO, TEI, TEO, TE_real, TE_report, VAL_rate, VAL_report, modelConfig, deviceName, epoch, printed, 'model')
int(math.sqrt(len(trainO_))))) if validRate == 0.0: print('\ntestI_array = stop - delta=' + str(i + 1) + ', index=' + str(j)) print( testI_.reshape(int(math.sqrt(len(testI_))), int(math.sqrt(len(testI_))))) # deep learning : test array -> training array #### validation mode #### # test input is not used -> just use training data file to make model # regardless of existance of test input file if validRate > 0: DL.deepLearning(trainIName, trainOName, None, None, None, testReport, validRate, validReport, modelConfig, deviceName, epoch, verbose, modelName) #### use_n_sub == False or use_n_sub_for_test == True #### # training input, training output and test input are all exist -> just use this data file to make model elif use_n_sub == False or use_n_sub_for_test == True: DL.deepLearning(trainIName, trainOName, testIName, testOName, None, testReport, validRate, validReport, modelConfig, deviceName, epoch, verbose, modelName) #### use_n_sub == True and use_n_sub_for_test == False #### # training input, training ouput and model exist, but test input does not exist # -> read and reshape test data first as read.makeData else: # read and reshape test data from each file
if __name__ == '__main__': # training input and test input: # NORMALIZED using avg and stddev of each training input column # meta info TRI = 'train_input.txt' TRO = 'train_output.txt' TEI = 'test_input.txt' TEO = 'test_output.txt' TE_real = None TE_report = 'report_test.txt' VAL_rate = 0.0 VAL_report = 'report_val.txt' modelConfig = 'model_config.txt' # user data deviceName = input('device name (for example, cpu:0 or gpu:0)') epoch = int(input('epoch')) printed = int(input('printed? (0 -> do not print)')) # training and test # 'config.txt' is used for configuration for i in range(64): DL.deepLearning(TRI, TRO, TEI, TEO[:-4] + '_' + str(i) + '.txt', TE_real, TE_report[:-4] + '_' + str(i) + '.txt', VAL_rate, VAL_report[:-4] + '_' + str(i) + '.txt', modelConfig, deviceName, epoch, printed, 'model_' + str(i))
normalize(testOutputs_1, trainO_1_mean, trainO_1_std, 6), 1, testNormalizedOutputFileName_1) # normalized test output (move) SF.saveAsFile( normalize(testOutputs_2, trainO_2_mean, trainO_2_std, 6), 1, testNormalizedOutputFileName_2) # normalized test output (total cost) ## 딥러닝 실시 ## 관련 학습/테스트 입출력 데이터 파일이 이미 존재하면 딥러닝에 의한 예측값만 파일로 쓰기 deviceName = input('device name (for example, cpu:0 or gpu:0)') epoch = int(input('epoch')) printed = int(input('printed? (0 -> do not print)')) # comparison print('\n ### deep learning for COMPARISON ###') DL.deepLearning(trainInputFileName, trainNormalizedOutputFileName_0, testInputFileName, testNormalizedOutputFileName_0, size, deviceName, epoch, printed, 'test0_comparison') # move print('\n\n\n ### deep learning for MOVE ###') DL.deepLearning(trainInputFileName, trainNormalizedOutputFileName_1, testInputFileName, testNormalizedOutputFileName_1, size, deviceName, epoch, printed, 'test1_move') # total cost print('\n\n\n ### deep learning for TOTAL COST ###') DL.deepLearning(trainInputFileName, trainNormalizedOutputFileName_2, testInputFileName, testNormalizedOutputFileName_2, size, deviceName, epoch, printed, 'test2_totalCost') ## 딥러닝 결과로부터 loss 계산 : mean absolute and mean square error
def AIbase_deeplearning(): # read configuration file f = open('config.txt', 'r') fl = f.readlines() f.close() for i in range(len(fl)): fl[i] = fl[i].split('\n')[0] # init values as None trainInputFile = None trainOutputFile = None testInputFile = None testOutputFile = None testOutputReal = None test_report = None valid_rate = None valid_report = None modelConfig = None normalizeName = None # extract configuration # trainInput : train input data file name # trainOutput : train output data file name # testInput : test input data file name # testOutput : test output (prediction) data file name # testOutputReal : test output (real) data file name (if not None, compare output prediction with real test output data) # test_report : test report file name # valid_rate : rate of validation data among training data (0 -> use test data) # valid_report : vaildation report file name # modelConfig : model configuration file name # normalizeName : normalize info (average and stddev) file name for i in range(len(fl)): configSplit = fl[i].split(' ') # split if configSplit[0] == 'trainInput': trainInputFile = configSplit[1] elif configSplit[0] == 'trainOutput': trainOutputFile = configSplit[1] elif configSplit[0] == 'testInput': testInputFile = configSplit[1] elif configSplit[0] == 'testOutput': testOutputFile = configSplit[1] elif configSplit[0] == 'testOutputReal': testOutputReal = configSplit[1] elif configSplit[0] == 'test_report': test_report = configSplit[1] elif configSplit[0] == 'valid_rate': valid_rate = float(configSplit[1]) elif configSplit[0] == 'valid_report': valid_report = configSplit[1] elif configSplit[0] == 'modelConfig': modelConfig = configSplit[1] elif configSplit[0] == 'normalizeName': normalizeName = configSplit[1] # convert 'None' to None if trainInputFile == 'None': trainInputFile = None if trainOutputFile == 'None': trainOutputFile = None if testInputFile == 'None': testInputFile = None if testOutputFile == 'None': testOutputFile = None if testOutputReal == 'None': testOutputReal = None if test_report == 'None': test_report = None if valid_rate == 'None': valid_rate = None if valid_report == 'None': valid_report = None if modelConfig == 'None': modelConfig = None if normalizeName == 'None': normalizeName = None # do Deep Learning # if the file for training/test data already exists, just write prediction for deep learning deviceName = input('device name (for example, cpu:0 or gpu:0)') epoch = int(input('epoch')) printed = int(input('printed? (0 -> do not print)')) print('\n ### deep learning ###') DL.deepLearning(trainInputFile, trainOutputFile, testInputFile, testOutputFile, testOutputReal, test_report, valid_rate, valid_report, modelConfig, deviceName, epoch, printed, 'model')