def __init__(self, stateSize=32, actionSize=51, load=False, epsilon=1, name=None, character="ryu", verbose=True): """Initializes the agent and the underlying neural network Parameters ---------- stateSize The number of features that will be fed into the Agent's network actionSize The size of the action space the Agent can chose actions in load A boolean flag that specifies whether to initialize the model from scratch or load in a pretrained model epsilon The initial exploration value to assume when the model is initialized. If a model is lodaed this is set to the minimum value name A string representing the name of the agent that will be used when saving the model and training logs Defaults to the class name if none is provided character String representing the name of the character this Agent plays as verbose A boolean variable representing whether or not the print statements in the class are turned on Error messages however are not turned off Returns ------- None """ self.stateSize = stateSize self.actionSize = actionSize self.gamma = DeepQAgent.DEFAULT_DISCOUNT_RATE # discount rate if load: self.epsilon = DeepQAgent.EPSILON_MIN # If the model is already trained lower the exploration rate else: self.epsilon = epsilon # If the model is not trained set a high initial exploration rate self.epsilonDecay = DeepQAgent.DEFAULT_EPSILON_DECAY # How fast the exploration rate falls as training persists self.learningRate = DeepQAgent.DEFAULT_LEARNING_RATE self.lossHistory = LossHistory() super(DeepQAgent, self).__init__(load=load, name=name, character=character, verbose=verbose)
def train_model(model, x_train, y_train, x_dev, y_dev): i = 100 dir_model = os.path.join(MODEL_DIR, MODEL) if not os.path.exists(dir_model): os.mkdir(dir_model) filepath = dir_model + "/weights-improvement-{epoch:02d}-{val_acc:.4f}.h5" checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1) print("The " + str(i) + "-th iteration.") history = LossHistory() model.fit({ 'aux_input': x_train, 'pos_input1': pos_chem_train }, {'main_output': y_train}, validation_data=([x_dev, pos_chem_dev], y_dev), epochs=i, batch_size=64, verbose=2, callbacks=[ history, checkpoint, EarlyStopping(monitor='val_loss', min_delta=0.005, patience=5, verbose=0, mode='min') ])
def DAutoEncoderLSTM(path_to_weights): import matplotlib.pyplot as plt import numpy as np import pandas as pd import seaborn as sns from keras.models import load_model from glob import glob from keras.layers import Input, Dense, Flatten, MaxPooling1D, AveragePooling1D, Convolution1D, LSTM, Reshape, Bidirectional from keras.layers.merge import concatenate from keras.models import Model, model_from_json import keras.callbacks from keras.callbacks import ModelCheckpoint from keras.utils import plot_model from IPython.display import SVG from keras.utils.vis_utils import model_to_dot from sklearn.metrics import accuracy_score, precision_recall_fscore_support from LossHistory import LossHistory from keras.layers import Dropout seq_length = 512 input_seq = Input(shape=(seq_length, 1)) conv1 = Convolution1D(filters=8, kernel_size=4, padding='same', kernel_initializer='normal', activation='linear')(input_seq) flat1 = Flatten()(conv1) dense1 = Dense(seq_length * 8, activation='relu')(flat1) dense2 = Dense(128, activation='relu')(dense1) dense3 = Dense(seq_length * 8, activation='relu')(dense2) reshaped_layer = Reshape((seq_length, -1))(dense3) conv2 = Convolution1D(filters=1, kernel_size=4, padding='same', kernel_initializer='normal', activation='linear')(reshaped_layer) bidirectional_lstm1 = Bidirectional( LSTM(128, activation='tanh', return_sequences=True))(conv2) bidirectional_lstm2 = Bidirectional(LSTM( 128, activation='tanh'))(bidirectional_lstm1) dense4 = Dense(1024, activation='relu')(bidirectional_lstm2) # output layer predictions = Dense(seq_length, activation='linear')(dense4) # create the model model = Model(inputs=input_seq, outputs=predictions) # compile the model -- define the loss and the optimizer model.compile(loss='mean_squared_error', optimizer='Adam') # record the loss history history = LossHistory() # save the weigths when the vlaidation lost decreases only checkpointer = ModelCheckpoint(filepath=path_to_weights, save_best_only=True, verbose=1) model.summary() return model, history, checkpointer
def ConvNNParallel(path_to_weights): import matplotlib.pyplot as plt import numpy as np import pandas as pd import seaborn as sns from keras.models import load_model from glob import glob from keras.layers import Input, Dense, Flatten, MaxPooling1D, AveragePooling1D, Convolution1D, LSTM, Reshape, Bidirectional from keras.layers.merge import concatenate from keras.models import Model, model_from_json import keras.callbacks from keras.callbacks import ModelCheckpoint from keras.utils import plot_model from IPython.display import SVG from keras.utils.vis_utils import model_to_dot from sklearn.metrics import accuracy_score, precision_recall_fscore_support from LossHistory import LossHistory from keras.layers import Dropout seq_length = 512 input_seq = Input(shape=(seq_length, 1)) # first convolutional layer conv1_layer = Convolution1D(filters=32, kernel_size=3, padding='same', kernel_initializer='normal', activation='relu') conv1_layer_2 = Convolution1D(filters=32, kernel_size=5, padding='same', kernel_initializer='normal', activation='relu') conv1_layer_3 = Convolution1D(filters=32, kernel_size=7, padding='same', kernel_initializer='normal', activation='relu') conv1 = conv1_layer(input_seq) conv2 = conv1_layer_2(input_seq) conv3 = conv1_layer_3(input_seq) merged = concatenate([conv1, conv2, conv3]) # flatten the weights flat = Flatten()(merged) dense1 = Dense(128, activation='relu')(flat) dense2 = Dense(128, activation='relu')(dense1) # output layer predictions = Dense(3, activation='linear')(dense2) # create the model model = Model(inputs=input_seq, outputs=predictions) # compile the model -- define the loss and the optimizer model.compile(loss='mean_squared_error', optimizer='Adam') # record the loss history history = LossHistory() # save the weigths when the vlaidation lost decreases only checkpointer = ModelCheckpoint(filepath=path_to_weights, save_best_only=True, verbose=1) model.summary() return model, history, checkpointer
def __init__(self, epsilon= 1): """Constructor""" self.name = self.__class__.__name__ self.epsilon = epsilon self.lossHistory = LossHistory() self.prepareForNextGame() if self.name != "Agent": self.initializeModel()
def __init__(self, stateSize=32, load=False, epsilon=1, name=None, moveList=Moves): """Initializes the agent and the underlying neural network Parameters ---------- stateSize The number of features that will be fed into the Agent's network load A boolean flag that specifies whether to initialize the model from scratch or load in a pretrained model epsilon The initial exploration value to assume when the model is initialized. If a model is lodaed this is set to the minimum value name A string representing the name of the agent that will be used when saving the model and training logs Defaults to the class name if none is provided moveList An enum class that contains all of the allowed moves the Agent can perform Returns ------- None """ self.stateSize = stateSize self.actionSize = len(moveList) self.gamma = DeepQAgent.DEFAULT_DISCOUNT_RATE # discount rate if load: self.epsilon = DeepQAgent.EPSILON_MIN # If the model is already trained lower the exploration rate else: self.epsilon = epsilon # If the model is not trained set a high initial exploration rate self.epsilonDecay = DeepQAgent.DEFAULT_EPSILON_DECAY # How fast the exploration rate falls as training persists self.learningRate = DeepQAgent.DEFAULT_LEARNING_RATE self.lossHistory = LossHistory() super(DeepQAgent, self).__init__(load=load, name=name, moveList=moveList)
def ConvNNSeq(best_weights_file): import matplotlib.pyplot as plt import numpy as np import pandas as pd import seaborn as sns from keras.models import load_model from glob import glob from keras.layers import Input, Dense, Flatten, MaxPooling1D, AveragePooling1D, Convolution1D, LSTM, Reshape, Bidirectional from keras.layers.merge import concatenate from keras.models import Model, model_from_json import keras.callbacks from keras.callbacks import ModelCheckpoint from keras.utils import plot_model from IPython.display import SVG from keras.utils.vis_utils import model_to_dot from sklearn.metrics import accuracy_score, precision_recall_fscore_support from LossHistory import LossHistory from keras.layers import Dropout seq_length = 512 # input sequence input_seq = Input(shape=(seq_length, 1)) # first convolutional layer conv1_layer = Convolution1D(filters=16, kernel_size=3, padding='valid', kernel_initializer='normal', activation='relu') conv1 = conv1_layer(input_seq) conv2 = Convolution1D(filters=16, kernel_size=3, padding='valid', kernel_initializer='normal', activation='relu')(conv1) # flatten the weights flat = Flatten()(conv2) # first dense layer dense1 = Dense(1024, activation='relu')(flat) # second dense layer dense2 = Dense(512, activation='relu', kernel_initializer='normal')(dense1) # output layer predictions = Dense(seq_length, activation='linear')(dense2) # create the model model = Model(inputs=input_seq, outputs=predictions) # compile the model -- define the loss and the optimizer model.compile(loss='mean_squared_error', optimizer='Adam') # record the loss history history = LossHistory() # save the weigths when the vlaidation lost decreases only checkpointer = ModelCheckpoint(filepath=best_weights_file, save_best_only=True, verbose=1) # fit the network using the generator of mini-batches. model.compile(loss='mean_squared_error', optimizer='sgd') return model, history, checkpointer
dir = './' + path + '/' + str(i) listImg = os.listdir(dir) for img in listImg: data.append([cv2.imread(dir + '/' + img, 0)]) # img = Image.open(dir + '/' + img) # img = img.resize((28,28)) # print(shape(img)) # img = np.array(img).astype('float32').reshape(28, 28, -1) # print(shape(img)) # data.append(img) labels.append(i) return data, labels # 记录训练的loss值和准确率 history = LossHistory() trainData, trainLabels = loadData('./datasets/MNIST/train') # testData, testLabels = loadData('test') trainData = np.array(trainData).astype('float32').reshape(-1, 28, 28, 1) trainData /= 255 trainLabels = np_utils.to_categorical(trainLabels, 10) # testLabels = np_utils.to_categorical(testLabels, 10) # (x_train, y_train), (x_test, y_test) = mnist.load_data() # # # x_test = x_test.reshape(x_test.shape[0], 28, 28, 1) # # # x_test = x_test.astype('float32') # # # x_test /= 255 # # # y_test = keras.utils.to_categorical(y_test)
class DeepQAgent(Agent): """An agent that implements the Deep Q Neural Network Reinforcement Algorithm to learn street fighter 2""" EPSILON_MIN = 0.1 # Minimum exploration rate for a trained model DEFAULT_EPSILON_DECAY = 0.999 # How fast the exploration rate falls as training persists DEFAULT_DISCOUNT_RATE = 0.98 # How much future rewards influence the current decision of the model DEFAULT_LEARNING_RATE = 0.0001 # Mapping between player state values and their one hot encoding index stateIndices = { 512: 0, 514: 1, 516: 2, 518: 3, 520: 4, 522: 5, 524: 6, 526: 7, 532: 8 } doneKeys = [0, 528, 530, 1024, 1026, 1028, 1030, 1032] ACTION_BUTTONS = ['X', 'Y', 'Z', 'A', 'B', 'C'] def _huber_loss(y_true, y_pred, clip_delta=1.0): """Implementation of huber loss to use as the loss function for the model""" error = y_true - y_pred cond = K.abs(error) <= clip_delta squared_loss = 0.5 * K.square(error) quadratic_loss = 0.5 * K.square(clip_delta) + clip_delta * ( K.abs(error) - clip_delta) return K.mean(tf.where(cond, squared_loss, quadratic_loss)) def __init__(self, stateSize=32, actionSize=51, load=False, epsilon=1, name=None, character="ryu", verbose=True): """Initializes the agent and the underlying neural network Parameters ---------- stateSize The number of features that will be fed into the Agent's network actionSize The size of the action space the Agent can chose actions in load A boolean flag that specifies whether to initialize the model from scratch or load in a pretrained model epsilon The initial exploration value to assume when the model is initialized. If a model is lodaed this is set to the minimum value name A string representing the name of the agent that will be used when saving the model and training logs Defaults to the class name if none is provided character String representing the name of the character this Agent plays as verbose A boolean variable representing whether or not the print statements in the class are turned on Error messages however are not turned off Returns ------- None """ self.stateSize = stateSize self.actionSize = actionSize self.gamma = DeepQAgent.DEFAULT_DISCOUNT_RATE # discount rate if load: self.epsilon = DeepQAgent.EPSILON_MIN # If the model is already trained lower the exploration rate else: self.epsilon = epsilon # If the model is not trained set a high initial exploration rate self.epsilonDecay = DeepQAgent.DEFAULT_EPSILON_DECAY # How fast the exploration rate falls as training persists self.learningRate = DeepQAgent.DEFAULT_LEARNING_RATE self.lossHistory = LossHistory() super(DeepQAgent, self).__init__(load=load, name=name, character=character, verbose=verbose) def getMove(self, obs, info): """Returns a set of button inputs generated by the Agent's network after looking at the current observation Parameters ---------- obs The observation of the current environment, 2D numpy array of pixel values info An array of information about the current environment, like player health, enemy health, matches won, and matches lost, etc. A full list of info can be found in data.json Returns ------- move An integer representing the move selected from the move list """ if numpy.random.rand() <= self.epsilon: move = self.getRandomMove() return move else: stateData = self.prepareNetworkInputs(info) predictedRewards = self.model.predict(stateData)[0] move = numpy.argmax(predictedRewards) return move def initializeNetwork(self): """Initializes a Neural Net for a Deep-Q learning Model Parameters ---------- None Returns ------- model The initialized neural network model that Agent will interface with to generate game moves """ model = Sequential() model.add(Dense(48, input_dim=self.stateSize, activation='relu')) model.add(Dense(96, activation='relu')) model.add(Dense(192, activation='relu')) model.add(Dense(96, activation='relu')) model.add(Dense(48, activation='relu')) model.add(Dense(self.actionSize, activation='linear')) model.compile(loss=DeepQAgent._huber_loss, optimizer=Adam(lr=self.learningRate)) if self.verbose: print('Successfully initialized model') return model def prepareMemoryForTraining(self, memory): """prepares the recorded fight sequences into training data Parameters ---------- memory A 2D array where each index is a recording of a state, action, new state, and reward sequence See readme for more details Returns ------- data The prepared training data in whatever from the model needs to train DeepQ needs a state, action, and reward sequence to train on The observation data is thrown out for this model for training """ data = [] for step in self.memory: data.append([ self.prepareNetworkInputs(step[Agent.STATE_INDEX]), step[Agent.ACTION_INDEX], step[Agent.REWARD_INDEX], step[Agent.DONE_INDEX], self.prepareNetworkInputs(step[Agent.NEXT_STATE_INDEX]) ]) return data def prepareNetworkInputs(self, step): """Generates a feature vector from the current game state information to feed into the network Parameters ---------- step A given set of state information from the environment Returns ------- feature vector An array extracted from the step that is the same size as the network input layer Takes the form of a 1 x 30 array. With the elements: enemy_health, enemy_x, enemy_y, 8 one hot encoded enemy state elements, 8 one hot encoded enemy character elements, player_health, player_x, player_y, and finally 8 one hot encoded player state elements. """ feature_vector = [] # Enemy Data if self.playerNumber == 0: enemyKey = 2 else: enemyKey = 1 feature_vector.append(step["player{0}_health".format(enemyKey)]) feature_vector.append(step["player{0}_x_position".format(enemyKey)]) feature_vector.append(step["player{0}_y_position".format(enemyKey)]) # one hot encode enemy state # enemy_status - 512 if standing, 514 if crouching, 516 if jumping, 518 blocking, 522 if normal attack, 524 if special attack, 526 if hit stun or dizzy, 532 if thrown oneHotEnemyState = [0] * len(DeepQAgent.stateIndices.keys()) statusKey = 'player{0}_status'.format(enemyKey) if step[statusKey] not in DeepQAgent.doneKeys: oneHotEnemyState[DeepQAgent.stateIndices[step[statusKey]]] = 1 feature_vector += oneHotEnemyState # one hot encode enemy character oneHotEnemyChar = [0] * 8 oneHotEnemyChar[step["player{0}_character".format(enemyKey)]] = 1 feature_vector += oneHotEnemyChar # Player Data feature_vector.append( step["player{0}_health".format(self.playerNumber + 1)]) feature_vector.append( step["player{0}_x_position".format(self.playerNumber + 1)]) feature_vector.append( step["player{0}_y_position".format(self.playerNumber + 1)]) # player_status - 512 if standing, 514 if crouching, 516 if jumping, 520 blocking, 522 if normal attack, 524 if special attack, 526 if hit stun or dizzy, 532 if thrown oneHotPlayerState = [0] * len(DeepQAgent.stateIndices.keys()) statusKey = 'player{0}_status'.format(self.playerNumber + 1) if step[statusKey] not in DeepQAgent.doneKeys: oneHotPlayerState[DeepQAgent.stateIndices[step[statusKey]]] = 1 feature_vector += oneHotPlayerState feature_vector = numpy.reshape(feature_vector, [1, self.stateSize]) return feature_vector def trainNetwork(self, data, model): """To be implemented in child class, Runs through a training epoch reviewing the training data Parameters ---------- data The training data for the model to train on, a 2D array of state, action, reward, sequence model The model to train and return the Agent to continue playing with Returns ------- model The input model now updated after this round of training on data """ minibatch = random.sample(data, len(data)) self.lossHistory.losses_clear() for state, action, reward, done, next_state in minibatch: modelOutput = model.predict(state)[0] if not done: reward = ( reward + self.gamma * numpy.amax(model.predict(next_state)[0])) modelOutput[action] = reward modelOutput = numpy.reshape(modelOutput, [1, self.actionSize]) model.fit(state, modelOutput, epochs=1, verbose=0, callbacks=[self.lossHistory]) if self.epsilon > DeepQAgent.EPSILON_MIN: self.epsilon *= self.epsilonDecay return model
import keras from LossHistory import LossHistory def loadData(path): data = [] labels = [] for i in range(10): dir = './' + path + '/' + str(i) listImg = os.listdir(dir) for img in listImg: data.append([cv2.imread(dir + '/' + img, 0)]) # imread里的0是变为一通道 labels.append(i) return data, labels # 记录训练的loss值和准确率 history = LossHistory() trainData, trainLabels = loadData('./datasets/MNIST/train+gen') # testData, testLabels = loadData('test') trainData = np.array(trainData).astype('float32').reshape(-1, 28, 28, 1) trainData /= 255 trainLabels = np_utils.to_categorical(trainLabels, 10) # testLabels = np_utils.to_categorical(testLabels, 10) # (x_train, y_train), (x_test, y_test) = mnist.load_data() # x_test = x_test.reshape(x_test.shape[0], 28, 28, 1) # x_test = x_test.astype('float32') # x_test /= 255 # y_test = keras.utils.to_categorical(y_test)
validation_steps=max(1, num_val // batch_size), epochs=train_epochs, initial_epoch=0, callbacks=[logs_loss]) model.save_weights(log_dir + 'trained_weights.h5') # 模型权重 ''' 因为容易出现这样的场景(常见):当你在进行keras的交叉验证时,例如你用5折, 对于fold_0,fold_1…一直到fold_4.,都应该有一个独立的模型,所以在每折的开头都需要加上clear_session(), 否则上一折的训练集成了这一折的验证集,会导致数据泄露。 同时,链接提到,不清空的话,那么graph上的node越来越多,内存问题,时间问题都会变得严峻。 ''' backend.clear_session() # 清除一个session,session就是tensorflow中我们常见的会话 logs_loss = LossHistory() # 全局对象,引用另外一个文件里的类 os.environ["CUDA_VISIBLE_DEVICES"] = '0' # use GPU with ID=0 # gpu_select(1) def _main(): backend.clear_session() log_dir = './logs/loss_weights_result/' # 模型训练后损失和权重的保存目录 annotation_path = './txt_annotations/station_train.txt' # 数据集的txt格式标注文件路径 classes_path = './model_data/detection_classes.txt' # 要检测的对象的名字 anchors_path = './model_data/yolo_anchors.txt' # 聚类结果路径 yolo_weights_path = './model_data/yolo_weights.h5' # yolo预训练模型权重路径 input_shape = (416, 416) # multiple of 32, h-w 输入图片尺寸
# 导入训练数据 x_train, y_train = loadData('./datasets/cifar-10/train') x_train = np.array(x_train).astype('float32').reshape(-1, 32, 32, 3) x_train /= 255 y_train = keras.utils.to_categorical(y_train, 10) # 导入测试数据 x_test, y_test = loadData('./datasets/cifar-10/test') x_test = np.array(x_test).astype('float32').reshape(-1, 32, 32, 3) x_test /= 255 y_test = keras.utils.to_categorical(y_test, 10) # 记录训练的loss值和准确率 history = LossHistory() #ResNet Block def resnet_block(inputs, num_filters=16, kernel_size=3, strides=1, activation='relu'): x = Conv2D(num_filters, kernel_size=kernel_size, strides=strides, padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(1e-4))(inputs) x = BatchNormalization()(x)
def train( model_name, batch_size=32, epochs=10, classes=2, n_gpu=8, validation=True ): """ """ start_time = datetime.now() paths = Paths() hdf5_path = os.path.join("..", "data", "76_79_80.hdf5") if n_gpu > 1: # create model on cpu print("Using {} gpus...".format(n_gpu)) with tf.device("/cpu:0"): model = generate_model( model_name, input_shape=(299, 299, 3), include_top=False, weights=None, classes=classes, pooling="avg" ) print("Generated model on cpu...") sys.stdout.flush() # generate mutiple models to use multi gpus model = multi_gpu_model(model, gpus=n_gpu) print("Created parallel model...") sys.stdout.flush() else: print("Using single gpu...") model = generate_model( model_name, input_shape=(299, 299, 3), include_top=False, weights=None, classes=2, pooling="avg" ) print("Generated model on cpu...") sys.stdout.flush() model.compile( optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] ) print("Model compiled...") sys.stdout.flush() timestamp = datetime.now().strftime(r"%Y%m%d_%I%M%p") tb_log_path = os.path.join( paths.logs, '{}_logs_{}'.format(model_name, timestamp)) os.makedirs(tb_log_path, exist_ok=True) os.makedirs(paths.models, exist_ok=True) epoch_loss_path = os.path.join( paths.logs, "{}_epoch_loss_{}.log".format(model_name, timestamp) ) batch_loss_path = os.path.join( paths.logs, "{}_batch_loss_{}.log".format(model_name, timestamp) ) model_hdf5_path = os.path.join( paths.models, "{}_{}.hdf5".format(model_name, timestamp) ) history = LossHistory( epoch_loss_path, batch_loss_path, model_hdf5_path ) tensorboard = TensorBoard(log_dir=tb_log_path) print("Start training...") sys.stdout.flush() data_params = { "batch_size": batch_size, "n_classes": classes, "shuffle": True } train_generator = DataGenerator(hdf5_path, "train", **data_params) valid_generator = DataGenerator(hdf5_path, "test", **data_params) if n_gpu == 1: model.fit_generator( train_generator, epochs=epochs, verbose=2, callbacks=[history, tensorboard], validation_data=valid_generator # use_multiprocessing = True, # workers = 3 # max_queue_size = 5 ) if n_gpu > 1: model.fit_generator( train_generator, epochs=epochs, verbose=2, callbacks=[history, tensorboard], validation_data=valid_generator, use_multiprocessing=True, workers=3 # max_queue_size = 5 ) if validation: pred_generator = DataGenerator(hdf5_path, "val", **data_params) preds = model.evaluate_generator( pred_generator # use_multiprocessing = False, # workers = 8 ) print("Validation loss: {}".format(preds[0])) print("Validation accuracy: {}".format(preds[1])) time_consumed = datetime.now() - start_time hours = time_consumed.seconds // 3600 minutes = time_consumed.seconds % 3600 // 60 seconds = time_consumed.seconds % 60 print("Training time: {}h{}m{}s".format(hours, minutes, seconds))
def deep_gKnock(y, X , Xk, group, lasso_init=None,coeff1=0.05,coeff2=0.05,num_epochs=500,batch_size = 50,use_bias=True,fdr=0.2,offset=0,num_layer=2,loss='MSE',verbose=False): # coeff is the l1 penalty tuning parameter in the dense layer #n=X.shape[0] p=X.shape[1] #num_epochs = 200; #filterNum = 1; #bias = True; #activation='relu'; #iterNum = 10; group_label=np.unique(group) g=len(group_label) newX=np.concatenate((X,Xk),axis=1) # n by 2p group_stru=np.array(pd.get_dummies(group)) group_size=np.sum(group_stru,0).reshape(g) group2=np.tile(group,2) # 2p group_stru2=np.array(pd.get_dummies(group2)) group_stru2.shape # 2p by g def my_init(shape,dtype): return initializers.get(glorot_normal(seed=1))(shape) use_bias=True #%% model = Sequential() model.add(Glayer(group_stru=group_stru2,input_shape=(2*p,),use_bias=True,kernel_regularizer=regularizers.l1(coeff1),name='lay1',kernel_initializer=my_init)) model.add(Glayer(group_stru=np.eye(g),kernel_regularizer=regularizers.l1(coeff2),use_bias=False,name='lay2',kernel_initializer=my_init)) # begin dense layer model.add(Dense(units=g, activation='relu',use_bias=use_bias,kernel_regularizer=regularizers.l1(coeff2),name='lay31',kernel_initializer=my_init)) if num_layer>=2: model.add(Dense(units=g, activation='relu',use_bias=use_bias,kernel_regularizer=regularizers.l1(coeff2),name='lay32',kernel_initializer=my_init)) if num_layer>=3: model.add(Dense(units=g, activation='relu',use_bias=use_bias,kernel_regularizer=regularizers.l1(coeff2),name='lay33',kernel_initializer=my_init)) if num_layer>=4: model.add(Dense(units=g, activation='relu',use_bias=use_bias,kernel_regularizer=regularizers.l1(coeff2),name='lay34',kernel_initializer=my_init)) if loss=='logit': model.add(Dense(units=1,name='lay4',activation='sigmoid',use_bias=False,kernel_initializer=my_init)) else: model.add(Dense(units=1,name='lay4',use_bias=False,kernel_initializer=my_init)) history = LossHistory() model.summary() #adam=optimizers.Adam(lr=0.01, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False) #model.compile(loss='mean_squared_error', optimizer=adam) if loss=='logit': print('loss = binary_crossentropy') model.compile(loss='binary_crossentropy', optimizer='adam') else: print('loss = mean_squared_error') model.compile(loss='mean_squared_error', optimizer='adam') if not lasso_init is None: print('set lasso_init\n') tmp=lasso_init.reshape(p*2,1,order='F') tmp2=list() tmp2.append(tmp) #tmp2.append(np.zeros((p,))) model.layers[0].set_weights(tmp2) model.fit(newX, y, epochs=num_epochs, batch_size=batch_size,callbacks=[history],verbose=verbose) loss=history.losses[-1] print('Deep_gKnock_loss:'+str(loss)+'\n') #%% lay1=model.get_layer('lay1') tmp=lay1.get_weights()[0] tmp=tmp.reshape(p,2,order='F') #z=np.dot(group_stru.T,np.abs(tmp)) #g by 2 #z=np.dot(group_stru.T,(tmp)**2) #g by 2 groupsize2=np.tile(group_size,2).reshape(g,2) #g by 2 z=np.dot(group_stru.T,np.abs(tmp))/groupsize2 #g by 2 lay2=model.get_layer('lay2') W0=lay2.get_weights()[0] W0=W0.reshape(g,1) W0.shape W0 # g by 1 lay31=model.get_layer('lay31') W31=lay31.get_weights() W31=W31[0].reshape(g,g) tmp=W31 if num_layer>=2: lay32=model.get_layer('lay32') W32=lay32.get_weights() W32=W32[0].reshape(g,g) tmp=np.matmul(tmp,W32) # g by g if num_layer>=3: lay33=model.get_layer('lay33') W33=lay33.get_weights() W33=W33[0].reshape(g,g) tmp=np.matmul(tmp,W33) # g by g if num_layer>=4: lay34=model.get_layer('lay34') W34=lay34.get_weights() W34=W34[0].reshape(g,g) tmp=np.matmul(tmp,W34) # g by g lay4=model.get_layer('lay4') W4=lay4.get_weights() W4=W4[0].reshape(g,1) W4.shape w=np.multiply(W0,np.matmul(tmp,W4)) # g by 1 Z=np.multiply(z,w) # g by 2 ko_stat=np.abs(Z[:,0])-np.abs(Z[:,1]) #ko_stat=Z[:,0]**2-Z[:,1]**2 # g by 1 t=knockoff_threshold(ko_stat,fdr=fdr, offset=offset) selected=np.sort(np.where(ko_stat >= t)[0]+1) selected = set(selected) output = dict() output['selected'] = selected output['t'] = t output['ko_stat'] = ko_stat output['fdr'] = fdr output['offset'] = offset #%% return(output)
epsilon=None, decay=0.0, amsgrad=False) # Which kind of loss to use? # We should write another metrics par_model.compile( #loss='cosine_proximity', loss='categorical_crossentropy', optimizer=opt, metrics=['categorical_crossentropy', acc]) print(model.summary()) epoch_nb = 70 batch = 512 cbk = MyCbk(model) history = LossHistory() par_model.fit(x_train, y_train, batch_size=batch, epochs=epoch_nb, verbose=1, validation_data=(x_test, y_test), callbacks=[cbk, history]) history.loss_plot() model.save("../model/tenpai.model") svc = pyserver.ServerChan() svc.output_to_weixin('Tenpai train done.')
def train_resnet( new_model=False, batch_size=32, epochs=20, validation=True): """ """ start_time = datetime.now() paths = Paths() model_name = "ResNet50" hdf5_path = os.path.join("..", "data", "76_79_80_noValidate.hdf5") if new_model: model = ResNet50() else: model = load_model_from_json() adam_opt = Adam(lr=0.0001, decay=1e-6) model.compile( optimizer=adam_opt, loss="categorical_crossentropy", metrics=["accuracy"] ) hdf5_file = tables.open_file(hdf5_path, mode='r') n_train = hdf5_file.root.train_img.shape[0] n_test = hdf5_file.root.test_img.shape[0] steps_per_epoch = int(ceil(n_train / batch_size)) validation_steps = int(ceil(n_test / batch_size)) timestamp = datetime.now().strftime(r"%Y%m%d_%I%M%S%p") tb_log_path = os.path.join( paths.logs, '{}_logs_{}'.format(model_name, timestamp)) os.makedirs(tb_log_path, exist_ok=True) os.makedirs(paths.models, exist_ok=True) epoch_loss_path = os.path.join( paths.logs, "{}_epoch_loss_{}.log".format(model_name, timestamp) ) batch_loss_path = os.path.join( paths.logs, "{}_batch_loss_{}.log".format(model_name, timestamp) ) model_hdf5_path = os.path.join( paths.models, "{}_{}.hdf5".format(model_name, timestamp) ) history = LossHistory( epoch_loss_path, batch_loss_path, model_hdf5_path ) tensorboard = TensorBoard( log_dir=tb_log_path, # write_grads=True, write_images=True # histogram_freq=5 ) try: model.fit_generator( read_hdf5( hdf5_file, batch_size=batch_size, ), steps_per_epoch=steps_per_epoch, epochs=epochs, verbose=1, callbacks=[history, tensorboard], validation_data=read_hdf5( hdf5_file, dataset="test", batch_size=batch_size, ), validation_steps=validation_steps ) if validation: eva_data = hdf5_file["val_img"][...] eva_labels = hdf5_file["val_labels"][...] print("Validation data shape: {}".format(str(eva_data.shape))) print("validation labels shape: {}".format(str(eva_labels.shape))) preds = model.evaluate(eva_data, eva_labels) print("Validation loss: {}".format(preds[0])) print("Validation accuracy: {}".format(preds[1])) hdf5_file.close() except StopIteration: hdf5_file.close() finally: hdf5_file.close() print("Training time: ", datetime.now() - start_time)
def deep_pink(y, X, Xk, lasso_init=None, coeff1=0.05, coeff2=0.01, num_epochs=500, batch_size=50, use_bias=True, fdr=0.2, offset=0, loss='MSE', verbose=False): # coeff is the l1 penalty tuning parameter in the dense layer #n=X.shape[0] p = X.shape[1] #num_epochs = 200; #batch_size = 500 #filterNum = 1; #bias = True; #activation='relu'; #iterNum = 10; newX = np.concatenate((X, Xk), axis=1) # n by 2p group = np.arange(1, p + 1) # p #group_stru=np.array(pd.get_dummies(group)) group2 = np.tile(group, 2) # 2p group_stru2 = np.array(pd.get_dummies(group2)) group_stru2.shape # 2p by g def my_init(shape, dtype): return initializers.get(glorot_normal(seed=1))(shape) #%% model = Sequential() model.add( Glayer(group_stru=group_stru2, input_shape=(2 * p, ), use_bias=True, name='lay1', kernel_regularizer=regularizers.l1(coeff1), kernel_initializer=Constant(0.1))) # (2+1)*p model.add( Glayer(group_stru=np.eye(p), use_bias=False, name='lay2', kernel_initializer=my_init)) # p model.add( Dense(units=p, activation='relu', use_bias=use_bias, kernel_regularizer=regularizers.l1(coeff2), name='lay3', kernel_initializer=my_init)) model.add( Dense(units=p, activation='relu', use_bias=use_bias, kernel_regularizer=regularizers.l1(coeff2), name='lay4', kernel_initializer=my_init)) if loss == 'logit': model.add( Dense(units=1, name='lay5', activation='sigmoid', use_bias=False, kernel_initializer=my_init)) else: model.add( Dense(units=1, name='lay5', use_bias=False, kernel_initializer=my_init)) model.summary() history = LossHistory() #adam=optimizers.Adam(lr=0.01, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False) #model.compile(loss='mean_squared_error', optimizer=adam) if loss == 'logit': print('loss = binary_crossentropy') model.compile(loss='binary_crossentropy', optimizer='adam') else: print('loss = mean_squared_error') model.compile(loss='mean_squared_error', optimizer='adam') if not lasso_init is None: print('set lasso_init\n') tmp = lasso_init.reshape(p * 2, 1, order='F') tmp2 = list() tmp2.append(tmp) tmp2.append(np.zeros((p, ))) model.layers[0].set_weights(tmp2) model.fit(newX, y, epochs=num_epochs, batch_size=batch_size, callbacks=[history], verbose=verbose) loss = history.losses[-1] print('Deep_Pink_loss:' + str(loss) + '\n') #%% lay1 = model.get_layer('lay1') tmp = lay1.get_weights()[0] z = tmp.reshape(p, 2, order='F') #p by 2 lay2 = model.get_layer('lay2') W0 = lay2.get_weights()[0] W0 = W0.reshape(p, 1) #W0.shape lay3 = model.get_layer('lay3') W1 = lay3.get_weights() W1 = W1[0].reshape(p, p) #W1.shape lay4 = model.get_layer('lay4') W2 = lay4.get_weights() W2 = W2[0].reshape(p, p) #W2.shape lay5 = model.get_layer('lay5') W3 = lay5.get_weights() W3 = W3[0].reshape(p, 1) #W3.shape tmp = np.matmul(W1, W2) # p by p w = np.multiply(W0, np.matmul(tmp, W3)) # g by 1 Z = np.multiply(z, w) # p by 2 ko_stat = np.abs(Z[:, 0]) - np.abs(Z[:, 1]) #ko_stat=Z[:,0]**2-Z[:,1]**2 # g by 1 t = knockoff_threshold(ko_stat, fdr=fdr, offset=offset) selected = set(np.where(ko_stat >= t)[0] + 1) selected = np.array(sorted(selected), dtype=int) output = dict() output['selected'] = selected output['t'] = t output['ko_stat'] = ko_stat output['fdr'] = fdr output['offset'] = offset #%% return (output)
y_train = keras.utils.to_categorical(y_train, 10) # 导入测试数据 x_test, y_test = loadData('./datasets/cifar-10/test+gen') x_test = np.array(x_test).astype('float32').reshape(-1, 32, 32, 3) x_test /= 255 y_test = keras.utils.to_categorical(y_test, 10) #用于正则化时权重降低的速度 weight_decay = 0.0005 nb_epoch = 50 batch_size = 256 dropout = 0.5 # 记录训练的loss值和准确率 history = LossHistory() # build model model = Sequential() # Block 1 model.add( Conv2D(64, (3, 3), padding='same', kernel_regularizer=keras.regularizers.l2(weight_decay), kernel_initializer=he_normal(), name='block1_conv1', input_shape=x_train.shape[1:])) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(
#generate indices for train_array an test_array with train_test_split_ratio = 0. train_test_split_ratio = 0.2 #[train_array, test_array] = split_cholec_data(image_dir, label_dir, train_test_split_ratio) [train_samples, validation_samples] = train_test_data_split(image_dir, label_dir, 0.2) print("Loading train data") # load training data train_generator = generator(train_samples, batch_size=BATCH_SIZE, frames_per_clip=frames) validation_generator = generator(validation_samples, batch_size=BATCH_SIZE, frames_per_clip=frames) lhistory = LossHistory() history = model.fit_generator( train_generator, steps_per_epoch=int(len(train_samples) / BATCH_SIZE), validation_data=validation_generator, validation_steps=int(len(validation_samples) / BATCH_SIZE), epochs=nb_epochs, verbose=1, callbacks=[lhistory]) plot_model(model, to_file='logs/model.png', show_shapes=True) logfile = open('logs/losses.txt', 'wt') logfile.write('\n'.join(str(l) for l in history.losses)) logfile.close()