def train(sel): print('start training...\n') import lc_CNN_backward_FC as backward bw=backward.CNN_backward( MODEL_SAVE_PATH=os.path.join(sel.model_path,sel.save_model_name), BATCH_SIZE = 10, LEARNING_RATE_BASE = 0.0001, LEARNING_RATE_DECAY = 0.99, REGULARIZER = pow(10,-4) , STEPS = 3000, MOVING_AVERAGE_DECAY=0.99,) Loss_train,Loss_test=bw.main_train_and_test(sel.x_train,sel.y_train,sel.x_test, sel.y_test) return Loss_train,Loss_test
) import time import tensorflow as tf import lc_CNN_forward_FC import numpy as np import pandas as pd import os from sklearn.metrics import classification_report from sklearn.metrics import accuracy_score from sklearn.metrics import roc_auc_score import matplotlib.pyplot as plt # 导入backfoward import lc_CNN_backward_FC as backward bw = backward.CNN_backward() validation_INTERVAL_SECS = 5 def validation(x_validation, y_validation, which_model='last', just_fetch=True): """复现已有的计算图(后续程序会自动读取已经训练过的模型,即使没有也没有关系,程序会从头训练)""" with tf.Graph().as_default() as g: x = tf.placeholder(tf.float32, [ None, lc_CNN_forward_FC.MAT_SIZE_ROW, lc_CNN_forward_FC.MAT_SIZE_COL, lc_CNN_forward_FC.NUM_CHANNELS ]) y_ = tf.placeholder(tf.float32, [None, lc_CNN_forward_FC.OUTPUT_NODE])
def train(sel): import lc_CNN_backward_FC as backward bw = backward.CNN_backward() bw.main_train_and_test(sel.x_train, sel.y_train, sel.x_test, sel.y_test)
def eval_prformance(sel, y_test_one_hot, y_pred, y_pred_prob): y_test_1d = np.argmax(y_test_one_hot, 1) accuracy = float('%.4f' % (accuracy_score(y_test_1d, y_pred))) report = classification_report(y_test_1d, y_pred) report = report.split('\n') specificity = report[2].strip().split(' ') sensitivity = report[3].strip().split(' ') specificity = float([spe for spe in specificity if spe != ''][2]) sensitivity = float([sen for sen in sensitivity if sen != ''][2]) auc_score = float('%.4f' % roc_auc_score(y_test_one_hot, y_pred_prob)) return accuracy, sensitivity, specificity, auc_score def main_train_and_test(sel, x_train, y_train): import time s = time.time() loss_train = sel.backward(x_train, y_train) e = time.time() print('Running time:{}\n'.format(e - s)) return loss_train if __name__ == '__main__': import lc_CNN_backward_FC as backward sel = backward.CNN_backward()