def __init__(self, content, style, x): self.content_features = {} self.style_features = {} self.model = None # evaluate features of content and style images print("Pre-evaluate features...") # global msg # msg = "Pre-evaluate features..." self.gen_model(K.placeholder(input_shape)) self.f_output = K.function([self.input_tensor], list(self.outputs_dict.values())) self.writedown_content_feature(content) self.writedown_style_feature(style) # training model self.gen_model(K.variable(x)) self.optimizer = Nadam(lr=learning_rate) self.compile()
def train_model(model: Model, dataset_id, method, proto_num, dataset_prefix, nb_iterations=100000, batch_size=128, val_subset=None, cutoff=None, normalize_timeseries=False, opt='Adam', learning_rate=1e-3, early_stop=False, balance_classes=True): X_train1, X_train2, y_train, X_test1, X_test2, y_test, is_timeseries = load_dataset( dataset_id, method, proto_num, normalize_timeseries=normalize_timeseries) #calculate num of batches nb_epochs = math.ceil(nb_iterations * (batch_size / X_train1.shape[0])) if balance_classes == True: classes = np.arange(0, nb_classes(dataset_id)) le = LabelEncoder() y_ind = le.fit_transform(y_train.ravel()) recip_freq = len(y_train) / (len(le.classes_) * np.bincount(y_ind).astype(np.float64)) class_weight = recip_freq[le.transform(classes)] print("Class weights : ", class_weight) y_train = to_categorical(y_train, nb_classes(dataset_id)) y_test = to_categorical(y_test, nb_classes(dataset_id)) reduce_lr = ReduceLROnPlateau(monitor='loss', patience=math.ceil(nb_epochs / 20), mode='auto', factor=1 / 2., cooldown=0, min_lr=learning_rate / 10., verbose=2) model_checkpoint = ModelCheckpoint( "./weights/%s_%s_%s_weights.h5" % (dataset_prefix, method, str(proto_num)), verbose=2, monitor='loss', save_best_only=True, save_weights_only=True) tensorboard = TensorBoard(log_dir='./logs/%s_%s_%s' % (dataset_prefix, method, str(proto_num)), batch_size=batch_size) csv_logger = CSVLogger('./logs/%s_%s_%s.csv' % (dataset_prefix, method, str(proto_num))) if early_stop: early_stopping = EarlyStopping(monitor='loss', patience=500, mode='auto', verbose=2, restore_best_weights=True) callback_list = [ model_checkpoint, early_stopping, tensorboard, csv_logger ] else: callback_list = [model_checkpoint, tensorboard, csv_logger] if opt == 'SGD': optm = SGD(lr=learning_rate, momentum=0.9, decay=5e-4) elif opt == 'Nadam': optm = Nadam(lr=learning_rate) elif opt == 'Adam_decay': optm = Adam(lr=learning_rate, decay=9. / nb_iterations) else: optm = Adam(lr=learning_rate) model.compile(optimizer=optm, loss='categorical_crossentropy', metrics=['accuracy']) if balance_classes: model.fit([X_train1, X_train2], y_train, batch_size=batch_size, epochs=nb_epochs, callbacks=callback_list, class_weight=class_weight, verbose=2, validation_data=([X_test1, X_test2], y_test)) else: model.fit([X_train1, X_train2], y_train, batch_size=batch_size, epochs=nb_epochs, callbacks=callback_list, verbose=2, validation_data=([X_test1, X_test2], y_test))
# 2. 모델 구성 from keras.models import Sequential from keras.layers import Dense model = Sequential() model.add(Dense(5, input_dim=1, activation='relu')) model.add(Dense(3)) model.add(Dense(4)) model.add(Dense(1)) # 3. 훈련 from keras.optimizers import Adam, SGD, RMSprop, Adagrad, Adadelta, Adamax, Nadam # optimizer = Adam(lr=0.01) # lr=learningrate # optimizer = SGD(lr=0.01) # optimizer = RMSprop(lr=0.01) # optimizer = Adagrad(lr=0.009) # optimizer = Adadelta(lr=0.12) # optimizer = Adamax(lr=0.02) optimizer = Nadam(lr=0.01) model.compile(loss='mse', optimizer=optimizer, metrics=['mse']) # model.compile(loss='mse', optimizer='adam', metrics=['mse']) model.fit(x, y, epochs=100, batch_size=1) # 4. 평가 예측 mse, _ = model.evaluate(x, y, batch_size=1) print('mse : ', mse) pred1 = model.predict([1.5, 2.5, 3.5]) print(pred1)
x = BatchNormalization()(x) x = LeakyReLU()(x) prediction = Dense(1, activation='sigmoid')(x) model = Model([left_sent, right_sent], prediction) model.summary() return model print("Generate model...") model = supervised_model() # Compile Network ## Select optimizer if args.optimizer == "Nadam": optimizer = Nadam() else: optimizer = SGD() ## Compile model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['acc']) # Fit Network ## Including fooling in naming if args.fooling == 1: fooling = "fooling" else: fooling = "no-fooling" ## Implement callbacks #### CSVLogger date = str(datetime.datetime.now().year) + "-" + str( datetime.datetime.now().month) + "-" + str(datetime.datetime.now().day) csv_name = date + "_{}-{}_{}_ratio-{}_{}".format(
# model.add(ActivityRegularization(l1=0.01, l2=0.01)) model.add(Dropout(0.3)) model.add( Dense( nclasses, activation='softmax', kernel_initializer='lecun_normal', bias_initializer=init_b, # kernel_regularizer=regularizers.l2(rs) )) save_model(model, prefix + workfile + '.model') elif run_type == 1: model = load_model(prefix + workfile + '.model') opt = Nadam(lr=0.002) model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['categorical_accuracy']) #=============================================================================# # T R A I N I N G # #=============================================================================# if run_type == 0: print('Training...') reduce_lr = ReduceLROnPlateau(factor=0.05, patience=5, min_lr=0.000001, verbose=1) # checkpointer = ModelCheckpoint(filepath=(prefix+workfile+"_{epoch:02d}-{val_loss:.2f}"+'.hdf5'), verbose=0, save_best_only=True)
def fit_model(data,depth,growth_rate,nb_dense_block,nb_filter,dropout,lr,epochs,opt,reduction,bn,batch_size,fc_dropout,fc_filter,fc_layers): input_shape = (1,96,96) es_patience = 4 lr_patience = 3 #batch_size = 64 weight_file = 'keras_densenet_siamese_8Nov_2300_weights.h5' file_name = 'keras_densenet_siamese_8Nov_2300' dense_dropout = 0.5 print("Epochs ",epochs," batch_size: ",batch_size," lr: ",lr," optimizer: ",opt) print(" es_patience: ",es_patience," lr_patience: ",lr_patience) print(" batch_size: ",batch_size," fc_dropout: ",fc_dropout," fc_filter: ",fc_filter," fc_layers: ",fc_layers) base_network = create_base_network(depth,growth_rate,nb_dense_block,nb_filter,dropout,reduction,bn) input_a = Input(shape=input_shape) input_b = Input(shape=input_shape) processed_a = base_network(input_a) processed_b = base_network(input_b) combined_features = concatenate([processed_a, processed_b], name = 'merge_features') combined_features = Dense(fc_filter, kernel_initializer=keras.initializers.he_normal())(combined_features) combined_features = Activation('relu')(combined_features) combined_features = BatchNormalization()(combined_features) combined_features = Dropout(fc_dropout)(combined_features) combined_features = Dense(1, activation = 'sigmoid')(combined_features) model = Model(inputs = [input_a, input_b], outputs = [combined_features], name = 'model') model.summary() if opt=='adam': optimizer = Adam(lr=lr) # Using Adam instead of SGD to speed up training elif opt=='nadam': optimizer=Nadam(lr=lr) elif opt=='adadelta': optimizer=Adadelta(lr=lr) elif opt=='adamax': optimizer=Adamax(lr=lr) elif opt=='rmsprop': optimizer=RMSprop(lr=lr) else: optimizer=SGD(lr=lr,momentum=0.9) model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['acc']) print('Finished compiling') #model.compile(loss=binary_crossentropy, optimizer=opt, metrics=['accuracy']) es = EarlyStopping(monitor='val_acc', patience=es_patience,verbose=1) checkpointer = ModelCheckpoint(filepath=weight_file, verbose=2, save_best_only=True) lr_reducer = ReduceLROnPlateau(monitor='val_loss', factor=np.sqrt(0.1), cooldown=0, patience=lr_patience, min_lr=0.5e-6,verbose=1) model.fit([data[0], data[1]],data[2], batch_size=batch_size, epochs=epochs, validation_data=([data[3], data[4]],data[5]), callbacks=[es,lr_reducer], verbose=2) #model = load_model(weight_file) #This is the best model score, acc = model.evaluate([data[6], data[7]],data[8], verbose=0) print("Test accuracy:%0.3f"% acc) pred = model.predict([data[6], data[7]]) auc_score = roc_auc_score(data[8],pred) auc_score = np.round(auc_score,4) print("current auc_score ------------------> %0.3f"%auc_score) if(auc_score > .94): model_json = model.to_json() score = str(auc_score) with open(file_name+score+".json", "w") as json_file: json_file.write(model_json) model.save_weights(file_name+score+".h5") print("Saved model to disk") del model K.clear_session() return acc,auc_score
ReLU2 = ReLU()(dense2) dropout2 = Dropout(dropout_rate)(ReLU2) dense3 = Dense(dense_scale // 2)(dropout2) #bn3 = BatchNormalization(momentum = .95)(dense3) ReLU3 = ReLU()(dense3) dropout3 = Dropout(dropout_rate)(ReLU3) output = Dense(1, activation = "sigmoid")(dropout3) #Create the model model = Model(inputs = [Z_input, X_input, Y_input], outputs = output) #Summarize the model model.summary() #compile the model model.compile(optimizer=Nadam(lr=0.001), loss='binary_crossentropy', metrics=['acc']) #train the model Checkpoint_Loss = ModelCheckpoint('/home/admin/Desktop/aerogel_CNNs/loss_FOV100_3.h5', verbose=1, save_best_only=True, monitor='val_loss') Checkpoint_Acc = ModelCheckpoint('/home/admin/Desktop/Saved_CNNs/acc_FOV100_3.h5', verbose=1, save_best_only=True, monitor='val_acc') model.fit_generator( generator = TrainGenerator, steps_per_epoch = len(trainAnswers) // batch_size, epochs = 150, verbose = 2, validation_data = ValGenerator, validation_steps = len(valAnswers) // batch_size, callbacks = [Checkpoint_Acc, Checkpoint_Loss], class_weight = class_weights )
def __build_graph(self, enable_fine_tune): # create the base pre-trained model base_model = Xception(weights='imagenet', include_top=False) # add a global spatial max pooling layer x = base_model.output #(?,3,3,2048) x = GlobalAveragePooling2D()(x) #(?, 2048) x = Dropout(rate=self.drop_out_rate)(x) assert K.int_shape(x) == (None, 2048) ## Load pretrained deep weigths if not os.path.exists(self.model_file) and os.path.exists( os.path.join(self.deep_model_dir, MODEL_BEST_NAME)): print("@@@@Load DEEP model weights from %s" % (self.deep_model_dir)) pred = Dense(self.num_classes, activation='sigmoid')(x) model = Model(inputs=base_model.input, outputs=pred) model.load_weights( os.path.join(self.deep_model_dir, MODEL_BEST_NAME)) ## Load DONE ########################################## # RNN begins # hidden_size: 20 # embedding_size: 20 # output_size: 20 h0 = Dense(self.gru_hidden_size)(x) hr = Dense(self.gru_hidden_size)(x) assert K.int_shape(h0) == ( None, self.gru_hidden_size), 'h0.shape: {}'.format(K.int_shape(h0)) label_ids = Lambda( lambda h: K.arange(self.num_classes, dtype='int32'))(h0) def batch_labels(h, label_ids): # check: https://github.com/keras-team/keras/issues/8343 # check: https://stackoverflow.com/questions/47066635/checkpointing-keras-model-typeerror-cant-pickle-thread-lock-objects label_id_tf = K.constant(label_ids, dtype='int32') return K.ones(shape=(K.shape(h)[0], 228), dtype='int32') * label_id_tf batch_label_ids = Lambda( batch_labels, output_shape=(228, ), arguments={'label_ids': np.array([i for i in range(228)])})(h0) assert K.int_shape(batch_label_ids) == (None, 228) label_emb = Embedding( self.num_classes, self.gru_hidden_size, embeddings_initializer=self.__pretrained_label_embed, input_length=self.num_classes)(batch_label_ids) assert K.int_shape(label_emb) == (None, 228, 20) if not self.label_emb_trainable: label_emb.trainable = False # gru_fn = CuDNNGRU if self.use_cudnn else GRU gru_fn = GRU label_gru_emb = Bidirectional( gru_fn(self.gru_hidden_size, input_shape=(self.num_classes, self.gru_hidden_size), return_sequences=True))(label_emb, initial_state=[h0, hr]) assert K.int_shape(label_gru_emb) == (None, 228, 40) # Approach 1: use a giant fully connect if self.rnn_concat_all: label_gru_emb = Reshape( [self.num_classes * self.gru_hidden_size * 2])(label_gru_emb) x = Concatenate(axis=1)([label_gru_emb, x]) predictions = Dense(self.num_classes, activation='sigmoid')(x) # Approach 2: separate out prediction for each class else: # (batch_size, 228, 40) + (batch_size, 228, 2048) x = RepeatVector(self.num_classes)(x) x = Concatenate(axis=2)([label_gru_emb, x]) # (batch_size, 228, 2088) predictions = TimeDistributed(Dense(1, activations='sigmoid'), input_shape=(self.num_classes, 2088))(x) # RNN ends ########################################## # this is the model we will train model = Model(inputs=base_model.input, outputs=predictions) f_metrics = FMetrics() f_scores = f_metrics.get_fscores() if not enable_fine_tune: # first: train only the top layers (which were randomly initialized) # i.e. freeze all convolutional InceptionV3 layers for layer in base_model.layers: layer.trainable = False # compile the model (should be done *after* setting layers to non-trainable) optimizer = RMSprop(lr=5e-4, decay=0.001) model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'] + f_scores) else: print("@@@@@Fine tune enabled.@@@@@") print("Fine tune the last feature flow and the entire exit flow") for layer in model.layers: # change from 116 to 36 to ALL layer.trainable = True layer.kernel_regularizer = regularizers.l2(self.reg) # compile the model with a SGD/momentum optimizer # and a very slow learning rate. optimizer = Nadam(lr=5e-4, schedule_decay=0.001) model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'] + f_scores) print(model.summary()) return model
def train_model(FLAGS): # data flow generator train_sequence, validation_sequence = data_flow(FLAGS.data_local, FLAGS.batch_size, FLAGS.num_classes, FLAGS.input_size) # optimizer = adam(lr=FLAGS.learning_rate, clipnorm=0.001) optimizer = Nadam(lr=FLAGS.learning_rate, beta_1=0.9, beta_2=0.999, epsilon=1e-08, schedule_decay=0.004) # optimizer = SGD(lr=FLAGS.learning_rate, momentum=0.9) objective = 'categorical_crossentropy' metrics = ['accuracy'] model = model_fn(FLAGS, objective, optimizer, metrics) if FLAGS.restore_model_path != '' and os.path.exists(FLAGS.restore_model_path): if FLAGS.restore_model_path.startswith('s3://'): restore_model_name = FLAGS.restore_model_path.rsplit('/', 1)[1] shutil.copyfile(FLAGS.restore_model_path, '/cache/tmp/' + restore_model_name) model.load_weights('/cache/tmp/' + restore_model_name) os.remove('/cache/tmp/' + restore_model_name) else: model.load_weights(FLAGS.restore_model_path) print("LOAD OK!!!") if not os.path.exists(FLAGS.train_local): os.makedirs(FLAGS.train_local) log_local = '../log_file/' tensorBoard = TensorBoard(log_dir=log_local) # reduce_lr = ks.callbacks.ReduceLROnPlateau(monitor='val_acc', factor=0.5, verbose=1, patience=1, # min_lr=1e-7) # 余弦退火学习率 sample_count = len(train_sequence) * FLAGS.batch_size epochs = FLAGS.max_epochs warmup_epoch = 5 batch_size = FLAGS.batch_size learning_rate_base = FLAGS.learning_rate total_steps = int(epochs * sample_count / batch_size) warmup_steps = int(warmup_epoch * sample_count / batch_size) warm_up_lr = WarmUpCosineDecayScheduler(learning_rate_base=learning_rate_base, total_steps=total_steps, warmup_learning_rate=0, warmup_steps=warmup_steps, hold_base_rate_steps=0, ) history = LossHistory(FLAGS) model.fit_generator( train_sequence, steps_per_epoch=len(train_sequence), epochs=FLAGS.max_epochs, verbose=1, callbacks=[history, tensorBoard, warm_up_lr], validation_data=validation_sequence, max_queue_size=10, workers=int(multiprocessing.cpu_count() * 0.7), use_multiprocessing=True, shuffle=True ) print('training done!') if FLAGS.deploy_script_path != '': from save_model import save_pb_model save_pb_model(FLAGS, model) if FLAGS.test_data_url != '': print('test dataset predicting...') from eval import load_test_data img_names, test_data, test_labels = load_test_data(FLAGS) test_data = preprocess_input(test_data) predictions = model.predict(test_data, verbose=0) right_count = 0 for index, pred in enumerate(predictions): predict_label = np.argmax(pred, axis=0) test_label = test_labels[index] if predict_label == test_label: right_count += 1 accuracy = right_count / len(img_names) print('accuracy: %0.4f' % accuracy) metric_file_name = os.path.join(FLAGS.train_local, 'metric.json') metric_file_content = '{"total_metric": {"total_metric_values": {"accuracy": %0.4f}}}' % accuracy with open(metric_file_name, "w") as f: f.write(metric_file_content + '\n') print('end')
def cnn_from_cfg(cfg): saved_model_path = '../../models/' + str(cfg['first_kernel_size']) + '_' \ + str(cfg['conv_filters']) + '_' \ + str(cfg['n_conv']) + '_' \ + str(cfg['dropout']) + '_' \ + cfg['activation'] + '_' \ + str(cfg['dense_width']) + '_' \ + str(cfg['dense_length']) + '_' \ + cfg['optimizer'] + '_' \ + str(cfg['optimizer_lr']) + '_' \ + str(cfg['learning_decay_rate']) + '.hdf5' model = Sequential() model.add( Conv2D(cfg['conv_filters'], (cfg['first_kernel_size'], cfg['first_kernel_size']), input_shape=(X_train.shape[1], X_train.shape[2], 1))) model.add(Activation(cfg['activation'])) for i in range(0, cfg['n_conv'] - 1): model.add(Conv2D(cfg['conv_filters'] * 2, (3, 3))) model.add(Activation(cfg['activation'])) if i % 2 == 1: model.add(MaxPooling2D((2, 2))) model.add(Flatten()) for i in range(0, cfg['dense_length']): model.add(Dense(cfg['dense_width'])) model.add(Activation(cfg['activation'])) model.add(Dropout(cfg['dropout'])) model.add(Dense(1)) model.add(Activation(cfg['activation'])) if cfg['optimizer'] == 'adam': opt = Adam(lr=cfg['optimizer_lr'], decay=cfg['learning_decay_rate']) elif cfg['optimizer'] == 'sgd': opt = SGD(lr=cfg['optimizer_lr'], decay=cfg['learning_decay_rate']) elif cfg['optimizer'] == 'nadam': opt = Nadam(lr=cfg['optimizer_lr'], schedule_decay=cfg['learning_decay_rate']) elif cfg['optimizer'] == 'RMSprop': opt = RMSprop(lr=cfg['optimizer_lr'], decay=cfg['learning_decay_rate']) model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['mae']) model.summary() model.fit(X_train, y_train, batch_size=1024, epochs=10, validation_split=0.2, callbacks=[ ModelCheckpoint(saved_model_path, monitor='val_loss', verbose=2, save_best_only=True) ], sample_weight=weights_train) return model.evaluate(X_val, y_val)[0]
activation='relu')(conv) conv = MaxPooling2D(pool_size=(2, 2))(conv) conv = Conv2D(32, (3, 3), name='conv2', padding='same', activation='relu')(conv) conv = MaxPooling2D(pool_size=(2, 2))(conv) flatten = Flatten()(conv) flatten = Dropout(0.2)(flatten) state = Input(shape=xb[1].shape[1:]) flatten = concatenate([flatten, state]) dense = Dense(64, name='dense0', activation='relu')(flatten) dense = Dropout(0.2)(dense) dense = Dense(10, name='dense1', activation='relu')(dense) dense = Dropout(0.2)(dense) dense = Dense(1, name='output')(dense) nadam = Nadam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08) model = Model(inputs=[inp, state], outputs=dense) model.compile(optimizer=nadam, loss='mse') MODEL_DIR = "./model/" plateau_cb = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3, min_lr=0.0001, verbose=1) chk_path = MODEL_DIR + "model.{0}-{1}.h5".format('{epoch:02d}', '{val_loss:.7f}') ckpt_cb = ModelCheckpoint(chk_path, save_best_only=True, verbose=1) csv_cb = CSVLogger(MODEL_DIR + "train_log.csv") early_stop_cb = EarlyStopping(monitor='val_loss', patience=10, verbose=1) callbacks = [plateau_cb, csv_cb, ckpt_cb, early_stop_cb]
def pass_arg(Xx, nsim, tr_size, droprate): print("Tr_size:", tr_size) def fix_seeds(seed): random.seed(seed) np.random.seed(seed) tf.random.set_seed(seed) session_conf = tf.compat.v1.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1) sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph(), config=session_conf) # K.set_session(sess) tf.compat.v1.keras.backend.set_session(sess) ss = 1 fix_seeds(ss) # import pickle # def save_obj(obj, name): # with open(name, 'wb') as f: # pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL) # Compute the RMSE given the ground truth (y_true) and the predictions(y_pred) def root_mean_squared_error(y_true, y_pred): return K.sqrt(K.mean(K.square(y_pred - y_true), axis=-1)) # # Making sure dimensionless bond length is less than 1 # def bond(bl): # return bl-1.0 # Making sure dimensionless bond length is less than 1 def bond(bl): bln = -bl * (bl < 0) blp = bl * (bl >= 1.0) - 1 * (bl >= 1.0) return bln + blp # # Making sure final porosity is less than initial # def poros(poroi, porof): # # porof[porof < 0] = 1-porof[porof < 0] # porof[porof < 0] = poroi[0]-porof[porof < 0] # print(porof) # return porof-poroi # Making sure final porosity is less than initial def poros(poroi, porof): porofn = -porof * (porof < 0) porofp = porof * (porof >= poroi) - poroi * (porof >= poroi) return porofp + porofn def strength1(bl, porof, nlayer=6): sigma01, sigma02 = 6, 31 C1s = 21 sigma_long = sigma01 * (np.exp( (1.0 - porof)**(C1s * nlayer)) - porof) + sigma02 * (1.0 - porof) sigma_long_sorted = np.sort(sigma_long, axis=-1) # sorts along first axis (down) ind = np.argsort(sigma_long, axis=-1) # sorts along first axis (down) bl_sorted = np.take_along_axis(bl, ind, axis=-1) # same as np.sort(x, axis=0) corr_bl_sorted = np.sort(bl, axis=-1) # sorts along first axis (down) return corr_bl_sorted - bl_sorted def strength2(bl, porof, nlayer=6): sigma01, sigma02 = 6, 31 C1s = 21 sigma_long = sigma01 * (np.exp( (1.0 - porof)**(C1s * nlayer)) - porof) + sigma02 * (1.0 - porof) sigma_long_sorted = np.sort(sigma_long, axis=-1) # sorts along first axis (down) ind = np.argsort(sigma_long, axis=-1) # sorts along first axis (down) bl_sorted = np.take_along_axis(bl, ind, axis=-1) # same as np.sort(x, axis=0) return sum(bl_sorted[1:] - bl_sorted[:-1] < 0) / 14 def phy_loss_mean(params): # useful for cross-checking training loss1, loss2, loss3, loss4, lam1, lam2 = params x1, x2, x3 = loss1 * (loss1 > 0), loss2 * (loss2 > 0), loss3 * (loss3 > 0) if x1.any() and x1.shape[0] > 1: X_scaled1 = (x1 - np.min(x1)) / (np.max(x1) - np.min(x1)) x1 = X_scaled1 if x2.any() and x2.shape[0] > 1: X_scaled2 = (x2 - np.min(x2)) / (np.max(x2) - np.min(x2)) x2 = X_scaled2 if x3.any() and x3.shape[0] > 1: X_scaled3 = (x3 - np.min(x3)) / (np.max(x3) - np.min(x3)) x3 = X_scaled3 return (lam1 * np.mean(x1) + lam2 * np.mean(x2) + lam2 * np.mean(x3)) # return (lam1*np.mean(x1) + lam2*np.mean(x2) + lam2*np.mean(x3) + lam2*loss4) def PGNN_train_test(optimizer_name, optimizer_val, drop_rate, iteration, n_layers, n_nodes, tr_size, lamda, reg): # Hyper-parameters of the training process # batch_size = int(tr_size/2) batch_size = 1 num_epochs = 300 val_frac = 0.25 patience_val = 80 # Initializing results filename exp_name = "FeatureEng_" + optimizer_name + '_drop' + str( drop_rate) + '_nL' + str(n_layers) + '_nN' + str( n_nodes) + '_trsize' + str(tr_size) + '_iter' + str(iteration) exp_name = exp_name.replace('.', 'pt') results_dir = '../results/' model_name = results_dir + exp_name + '_NoPhyInfomodel.h5' # storing the trained model if reg: results_name = results_dir + exp_name + '_results_regularizer.dat' # storing the results of the model else: results_name = results_dir + exp_name + '_results.dat' # storing the results of the model # Load labeled data data = np.loadtxt('../data/labeled_data.dat') # x_labeled = data[:, :-5] # -2 because we do not need porosity predictions x_label = data[:, : -3] # -2 because we do not need porosity predictions x_labeled = np.hstack((x_label[:, :2], x_label[:, -2:])) y_labeled = data[:, -3:-1] # normalize dataset with MinMaxScaler scaler = preprocessing.MinMaxScaler(feature_range=(0, 1.0)) # scaler = preprocessing.StandardScaler() x_labeled = scaler.fit_transform(x_labeled) # y_labeled = scaler.fit_transform(y_labeled) # train and test data trainX, trainY = x_labeled[:tr_size, :], y_labeled[:tr_size] # testX, testY = x_labeled[tr_size:,:], y_labeled[tr_size:] # init_poro = data[tr_size:, -1] testX, testY = x_labeled[tr_size:, :], y_labeled[tr_size:] init_poro = data[tr_size:, -1] # Creating the model model = Sequential() for layer in np.arange(n_layers): if layer == 0: model.add( Dense(n_nodes, activation='relu', input_shape=(np.shape(trainX)[1], ))) else: if reg: model.add( Dense(n_nodes, activation='relu', kernel_regularizer=l1_l2(l1=.001, l2=.001))) else: model.add(Dense(n_nodes, activation='relu')) model.add(Dropout(rate=drop_rate)) model.add(Dense(2, activation='linear')) model.compile(loss='mean_squared_error', optimizer=optimizer_val, metrics=[root_mean_squared_error]) early_stopping = EarlyStopping(monitor='val_loss', patience=patience_val, verbose=1) print('Running...' + optimizer_name) history = model.fit(trainX, trainY, batch_size=batch_size, epochs=num_epochs, verbose=0, validation_split=val_frac, callbacks=[early_stopping, TerminateOnNaN()]) test_score = model.evaluate(testX, testY, verbose=1) print(test_score) # predictions = model.predict(testX) # # inv_pred = scaler.inverse_transform(predictions) # phyloss1 = bond(predictions[:,0]) # physics loss 1 # # init_poro_ndim = np.ones((init_poro.shape)) # # diff2 = poros(init_poro_ndim, predictions[:,1]) # physics loss 2 # phyloss2 = poros(init_poro, predictions[:,1]) # physics loss 2 # phyloss3 = strength1(predictions[:,0], predictions[:,1]) # phyloss4 = strength2(predictions[:,0], predictions[:,1]) # lam1, lam2 = lamda[0], lamda[1] # phyloss = phy_loss_mean([phyloss1, phyloss2, phyloss3, phyloss4, lam1, lam2]) # print('iter: ' + str(iteration) + # ' nL: ' + str(n_layers) + ' nN: ' + str(n_nodes) + # ' trsize: ' + str(tr_size) + # ' TestRMSE: ' + str(test_score[1]) + ' PhyLoss: ' + str(phyloss), "\n") # # model.save(model_name) # # save results # results = {'train_rmse':history.history['root_mean_squared_error'], # 'val_rmse':history.history['val_root_mean_squared_error'], # 'test_rmse':test_score[1], 'PhyLoss':phyloss} # save_obj(results, results_name) # return results, results_name, predictions, testY, test_score[1] # predictions = model.predict(Xx) samples = [] for i in range(int(nsim)): print("simulation num:", i) predictions = model.predict(Xx) predictions = predictions[:, 1] samples.append(predictions[:, np.newaxis]) return np.array(samples) # Main Function if __name__ == '__main__': fix_seeds(1) # List of optimizers to choose from optimizer_names = [ 'Adagrad', 'Adadelta', 'Adam', 'Nadam', 'RMSprop', 'SGD', 'NSGD' ] optimizer_vals = [ Adagrad(clipnorm=1), Adadelta(clipnorm=1), Adam(clipnorm=1), Nadam(clipnorm=1), RMSprop(clipnorm=1), SGD(clipnorm=1.), SGD(clipnorm=1, nesterov=True) ] # selecting the optimizer optimizer_num = 1 optimizer_name = optimizer_names[optimizer_num] optimizer_val = optimizer_vals[optimizer_num] # Selecting Other Hyper-parameters drop_rate = droprate # Fraction of nodes to be dropped out n_layers = 2 # Number of hidden layers n_nodes = 5 # Number of nodes per hidden layer # # Iterating over different training fractions and splitting indices for train-test splits # trsize_range = [4,6,8,10,20] # #default training size = 5000 # tr_size = trsize_range[4] tr_size = int(tr_size) # use regularizer reg = True #set lamda=0 for pgnn0 lamda = [1, 1] # Physics-based regularization constant # total number of runs iter_range = np.arange(1) testrmse = [] # iterating through all possible params for iteration in iter_range: # results, result_file, pred, obs, rmse = PGNN_train_test(optimizer_name, optimizer_val, drop_rate, # iteration, n_layers, n_nodes, tr_size, lamda, reg) # testrmse.append(rmse) pred = PGNN_train_test(optimizer_name, optimizer_val, drop_rate, iteration, n_layers, n_nodes, tr_size, lamda, reg) return np.squeeze(pred)
model.add(Conv2D(10, (5, 5), activation=activ)) model.add(Dropout(0.3)) model.add(Conv2D(5, (1, 1), activation=activ)) model.add(Dropout(0.3)) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(120, activation=activ)) model.add(Dropout(0.3)) model.add(Dense(100, activation=activ)) model.add(Dropout(0.3)) model.add(Dense(2, activation='softmax')) model.summary() # Optimizer settings optim = Nadam(lr=0.001) model.compile(loss='categorical_crossentropy', optimizer=optim, metrics=['accuracy']) ''' Fitting the model ''' print('Fitting model ...') model.fit(x_train, y_train, epochs=10, batch_size=10) print('Calculating the score...') score, acc = model.evaluate(x_test, y_test, batch_size=1) print('Test score:', score) print('Test accuracy:', acc)
min_inputs = np.array(f['min_inputs']) max_inputs = np.array(f['max_inputs']) N, numOut = Q.shape print("Setting up Model") # Asymmetric loss function lossFactor = 40.0 # Define model architecture model = Sequential() model.add(Dense(hu, activation='relu', input_dim=4)) model.add(Dense(hu, activation='relu')) model.add(Dense(hu, activation='relu')) model.add(Dense(hu, activation='relu')) model.add(Dense(hu, activation='relu')) model.add(Dense(hu, activation='relu')) model.add(Dense(numOut)) opt = Nadam(lr=0.0003) model.compile(loss=asymMSE, optimizer=opt, metrics=['accuracy']) # Train and write nnet files epoch = saveEvery while epoch <= totalEpochs: model.fit(X_train, Q, epochs=saveEvery, batch_size=2 ** 8, shuffle=True) saveFile = nnetFiles % (pra, ver, epoch) saveNNet(model, saveFile, means, ranges, min_inputs, max_inputs) epoch += saveEvery plt.scatter()
time_output = Dense(1, init='glorot_uniform', name='time_output')(l_hidden_bn_1[-1]) model = Model(input=[main_input], output=[time_output]) model.summary() # serialize model to JSON model_json = model.to_json() with open( "output_files/models/" + eventlog + "_" + str(".".join(str(s) for s in architecture)) + "_model.json", "w") as json_file: json_file.write(model_json) opt = Nadam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=1e-08, schedule_decay=0.004, clipvalue=3) #opt = Nadam(lr=LR, beta_1=0.9, beta_2=0.999, epsilon=1e-08, schedule_decay=0.004, clipvalue=3) #opt= Adagrad(lr=0.01, epsilon=1e-08, decay=0.0, clipvalue=3) model.compile(loss={'time_output': 'mae'}, optimizer=opt) early_stopping = EarlyStopping(monitor='val_loss', patience=42) model_checkpoint = ModelCheckpoint( 'output_files/models/' + eventlog + "_" + str(".".join(str(s) for s in architecture)) + '_weights_best.h5', monitor='val_loss', verbose=1, save_best_only=True, mode='auto') #model_checkpoint = ModelCheckpoint('output_files/models/200_model_{epoch:02d}-{val_loss:.2f}.h5', monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=False, mode='auto')
def start(self): # Break into X (predictors) & y (prediction) x, y = to_xy(self.df, 'outcome') # x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=6) x_test, y_test = to_xy(self.df_test, 'outcome') print('shape of x,y:', x.shape, y.shape) print('shape of x_test,y_test:', x_test.shape, y_test.shape) # 构建模型 model = Sequential() DROPOUT = 0.5 """ 1. 24-256-512-256-5 """ model.add( Dense(24, input_dim=x.shape[1], activation='relu', kernel_initializer='normal')) # model.add(Dropout(DROPOUT)) # model.add(BatchNormalization()) model.add(Dense(256, activation='relu', kernel_initializer='normal')) # model.add(Dropout(DROPOUT)) model.add(Dense(512, activation='relu', kernel_initializer='normal')) # model.add(Dropout(DROPOUT)) model.add(Dense(256, activation='relu', kernel_initializer='normal')) # model.add(Dropout(DROPOUT)) model.add(Dense(5, activation='softmax')) """ 2. 12-128-256-128-5 """ # model.add(Dense(12, input_dim=x.shape[1], activation='relu', kernel_initializer='normal')) # model.add(Dense(128, activation='relu', kernel_initializer='normal')) # model.add(Dense(256, activation='relu', kernel_initializer='normal')) # model.add(Dense(128, activation='relu', kernel_initializer='normal')) # model.add(Dense(5, activation='softmax')) """ 3. 48-512-1024-512-5 """ # model.add(Dense(48, input_dim=x.shape[1], activation='relu', kernel_initializer='normal')) # model.add(Dense(512, activation='relu', kernel_initializer='normal')) # model.add(Dense(1024, activation='relu', kernel_initializer='normal')) # model.add(Dense(512, activation='relu', kernel_initializer='normal')) # model.add(Dense(5, activation='softmax')) """ 4. 12-24-48-24-5 """ # model.add(Dense(12, input_dim=x.shape[1], activation='relu', kernel_initializer='normal')) # model.add(Dense(24, activation='relu', kernel_initializer='normal')) # model.add(Dense(48, activation='relu', kernel_initializer='normal')) # model.add(Dense(24, activation='relu', kernel_initializer='normal')) # model.add(Dense(5, activation='softmax')) """ 5. 6-12-24-12-5 """ # model.add(Dense(6, input_dim=x.shape[1], activation='relu', kernel_initializer='normal')) # model.add(Dense(12, activation='relu', kernel_initializer='normal')) # model.add(Dense(24, activation='relu', kernel_initializer='normal')) # model.add(Dense(12, activation='relu', kernel_initializer='normal')) # model.add(Dense(5, activation='softmax')) """ 6. 3-6-12-6-5 """ # model.add(Dense(3, input_dim=x.shape[1], activation='relu', kernel_initializer='normal')) # model.add(Dense(6, activation='relu', kernel_initializer='normal')) # model.add(Dense(12, activation='relu', kernel_initializer='normal')) # model.add(Dense(6, activation='relu', kernel_initializer='normal')) # model.add(Dense(5, activation='softmax')) """ 7. 24-512-256-5 """ # model.add(Dense(24, input_dim=x.shape[1], activation='relu', kernel_initializer='normal')) # model.add(Dense(512, activation='relu', kernel_initializer='normal')) # model.add(Dense(256, activation='relu', kernel_initializer='normal')) # model.add(Dense(5, activation='softmax')) """ 8. 24-256-512-256-128-5 """ # model.add(Dense(24, input_dim=x.shape[1], activation='relu', kernel_initializer='normal')) # model.add(Dense(256, activation='relu', kernel_initializer='normal')) # model.add(Dense(512, activation='relu', kernel_initializer='normal')) # model.add(Dense(256, activation='relu', kernel_initializer='normal')) # model.add(Dense(128, activation='relu', kernel_initializer='normal')) # model.add(Dense(5, activation='softmax')) """ 9. 10-50-10-5 """ # model.add(Dense(10, input_dim=x.shape[1], activation='relu', kernel_initializer='normal')) # model.add(Dense(50, activation='relu', kernel_initializer='normal')) # model.add(Dense(10, activation='relu', kernel_initializer='normal')) # model.add(Dense(5, activation='softmax')) # 编译模型 epochs_nb = 20 Nadam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=None, schedule_decay=0.004) model.compile(optimizer='nadam', loss='categorical_crossentropy', metrics=['accuracy']) # define some callback functions checkpointer = ModelCheckpoint(filepath="model.h5", verbose=0, save_best_only=True) # 将模型保存到h5文件 tensorboard = TensorBoard(log_dir='./logs', histogram_freq=0, write_graph=True, write_images=True) # 保存训练进度文件 monitor = EarlyStopping(monitor='val_acc', min_delta=1e-3, patience=5, verbose=1, mode='auto') # 训练模型,以 32 个样本为一个 batch 进行迭代 # history = model.fit(x, y, epochs=epochs_nb, batch_size=512, # validation_split=0.25, verbose=1, # callbacks=[checkpointer, tensorboard, monitor]).history history = model.fit(x, y, epochs=epochs_nb, batch_size=512, validation_data=(x_test, y_test), verbose=1, callbacks=[checkpointer, tensorboard, monitor]).history # 生成模型的结构图 plot_model(model, to_file='model.png') # 输出训练过程中训练集和验证集准确值和损失值得变化 print(history.keys()) loss_values = history['loss'] val_loss_values = history['val_loss'] epochs = range(1, len(history['acc']) + 1) plt.plot(epochs, loss_values, 'bo', label='Training loss') plt.plot(epochs, val_loss_values, 'b', label='Validation loss') plt.title('Training and validation loss') plt.xlabel('Epochs') plt.ylabel('Loss') plt.legend() plt.show() # summarize history for loss plt.clf() # 清除图片 acc_values = history['acc'] val_acc_values = history['val_acc'] plt.plot(epochs, acc_values, 'bo', label='Training acc') plt.plot(epochs, val_acc_values, 'b', label='Validation acc') plt.title('Training and validation accuracy') plt.xlabel('Epochs') plt.ylabel('Loss') plt.legend() plt.show() # 加载保存的模型 trained_model = load_model('model.h5') print("successfully load trained model: model.h5") # 打印模型的基本结构 print(trained_model.summary()) # Measure accuracy score = trained_model.evaluate(x_test, y_test, batch_size=512) print('Test score:', score[0]) print('Test accuracy:', score[1]) return history
except: opts = None args = None loaded = False for opt, arg in opts: if opt == '-p': print('Loading model {}'.format(arg)) model = load_model(arg) loaded = True if not loaded: print('Building model') if OPTIMIZER_TYPE == "nadam": optimizer = Nadam(lr=LEARN_RATE) elif OPTIMIZER_TYPE == "adam": optimizer = Adam(lr=LEARN_RATE) else: optimizer = RMSprop(lr=LEARN_RATE, clipnorm=1.) #Build model #Convolution layers #As per https://arxiv.org/pdf/1412.6806v3.pdf # CONV LEVEL 1 l_input = Input(batch_shape=X_train.shape) l_conv_1_1 = Convolution1D(N_CONV_FILTERS, FILT_LEN, init='he_normal', border_mode='same',
np.random.seed(1671) # for reproducibility # network and training epochs = st.number_input('Fill in number of epoch: ', min_value=12, format=None, step=1) batch_sz = st.number_input('Specify batch size: ', min_value=168, format=None, step=8) amount_of_output_classes = st.number_input('Specify amount of output classes', min_value=10, format=None, step=1) NB_EPOCH = epochs BATCH_SIZE = batch_sz VERBOSE = 1 NB_CLASSES = amount_of_output_classes # number of outputs = number of digits amount_of_hidden_nodes = st.number_input('Specify amount of hidden nodes', min_value=168, format=None, step=1) N_HIDDEN = amount_of_hidden_nodes st.write(""" #### Optimizer Stochastic Gradient Descent """) OPTIMIZER = Nadam() # SGD optimizer, explained later in this chapter VALIDATION_SPLIT = 0.2 # how much TRAIN is reserved for VALIDATION # data: shuffled and split between train and test sets # (X_train, y_train), (X_test, y_test) = mnist.load_data() # X_train is 60000 rows of 28x28 values --> reshaped in 60000 x 784 RESHAPED = 784 # X_train = X_train.reshape(60000, RESHAPED) X_test = X_test.reshape(10000, RESHAPED) X_train = X_train.astype('float32') X_test = X_test.astype('float32') # normalize
classifier.compile(optimizer=opt, loss='mean_squared_error', metrics=['accuracy']) return classifier # Por fim compilamos o modelo especificando um otimizador, a função de custo, e opcionalmente # métricas para serem observadas durante treinamento. #lambd = 0.002 add = 0 for optm in ["Nadam", "Adamax", "Adadelta"]: for lambd in [0.001, 0.0015, 0.0013, 0.0008]: #, 0.05, 0.1, 0.5, 1.0, 3.0]: optmizer = None if (optm == "Nadam"): optmizer = Nadam(lr=lambd) elif (optm == "Adamax"): optmizer = Adamax(lr=lambd) elif (optm == "Adadelta" and add == 0): optmizer = Adadelta() lambd = 1.0 add = 1 else: continue print("Executing " + optm + " Optimizer For Learning Rate: " + str(lambd)) classifier = create_baseline_model(lambd, optmizer) # Para treinar a rede passamos o conjunto de treinamento e especificamos o tamanho do mini-batch, # o número máximo de épocas, e opcionalmente callbacks. No seguinte exemplo utilizamos early # stopping para interromper o treinamento caso a performance não melhore em um conjunto de validação.
tr = arguments[1] ts = arguments[2] outfile = arguments[3] train = pd.read_csv(tr,header=None).values test = pd.read_csv(ts,header=None).values x_tr = train[:,1:].reshape(train.shape[0],32,32,1).astype('float32')/255 y_tr = np_utils.to_categorical(train[:,0]) x_ts = test[:,1:].reshape(test.shape[0],32,32,1).astype('float32')/255 #############OPTIMIZERS################# opt = SGD(lr=0.01) opt_adam = Adam() opt_rms = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0) opt_nadam = Nadam() ######################################## augment = ImageDataGenerator(rotation_range=10,zoom_range = 0.1,width_shift_range=0.1,height_shift_range=0.1) augment.fit(x_tr) ###############CALLBACKS################# annealer = ReduceLROnPlateau(patience=3, verbose=1, factor=0.5, min_lr=0.00001) early_stopper = EarlyStopping(patience=6, verbose=1, mode='auto') ######################################## model = lenet.build(1,32,32,46) # model.compile(loss="categorical_crossentropy", optimizer=opt,metrics=["accuracy"]) model.compile(loss="categorical_crossentropy", optimizer=opt_adam,metrics=["accuracy"]) # model.compile(loss="categorical_crossentropy", optimizer=opt_rms,metrics=["accuracy"]) # model.compile(loss="categorical_crossentropy", optimizer=opt_nadam,metrics=["accuracy"])
sss = ShuffleSplit(n_splits=10, test_size=0.2, random_state=RANDOM_STATE) train_index, test_index = sss.split(xtrain, y).next() X_train = xtrain[train_index] y_train = y[train_index] X_val = xtrain[test_index] y_val = y[test_index] num_epoch = 3000 model = nn_model() batch_size = 2**7 model.compile(loss='mse', optimizer=Nadam(lr=1e-4), metrics=[f_eval]) model.fit_generator(generator=batch_generator(X_train, y_train, batch_size, True), verbose=1, validation_data=batch_generator(X_val, y_val, batch_size, False), nb_val_samples=X_val.shape[0], nb_epoch=num_epoch, samples_per_epoch=10000 * batch_size, callbacks=[EarlyStopping(patience=10)]) # model.compile(loss='mse', # optimizer=Nadam(lr=1e-5), # metrics=[f_eval] # )
from keras import backend as K from keras.layers import ActivityRegularization, Conv2D, Dense, Dropout, Flatten, Input, Lambda, MaxPooling2D, concatenate from keras.models import Model from keras.optimizers import SGD, Adam, Nadam, RMSprop SUPPORTED_NETWORKS = [ 'inception', 'vgglike', 'vgg16', 'vgg19', 'simnet', 'simnetlike' ] SUPPORTED_OPTIMIZERS = { 'sgd': SGD(lr=0.0003, decay=1e-6, momentum=0.9, nesterov=True), 'adam': Adam(), 'nadam': Nadam(), 'rms': RMSprop() } def euclidean_distance(vects): x, y = vects return K.sqrt( K.maximum(K.sum(K.square(x - y), axis=1, keepdims=True), K.epsilon())) def eucl_dist_output_shape(shapes): shape1, shape2 = shapes return (shape1[0], 1) def create_mlp(input_shape): input = Input(shape=input_shape) x = Flatten()(input) x = Dense(128, activation='relu')(x)
m = Conv2D(256, (3, 3), activation='elu', padding='same')(m) m = MaxPooling2D()(m) m = Flatten()(m) m = Dense(512, activation='elu')(m) m = Dropout(0.5)(m) m = Dense(512, activation='elu')(m) m = Dropout(0.5)(m) o = Dense(out_dim, activation='softmax')(m) model = Model(inputs=i, outputs=o) model.summary() data_augmentation = False # causes MemoryError if not data_augmentation: model.compile(loss='categorical_crossentropy', optimizer=Nadam(lr=1e-3), metrics=['accuracy']) model.fit(x_train, y_train, epochs=4, verbose=1, validation_data=(x_val, y_val)) model.compile(loss='categorical_crossentropy', optimizer=Nadam(lr=1e-4), metrics=['accuracy']) model.fit(x_train, y_train, epochs=4, verbose=1, validation_data=(x_val, y_val)) model.compile(loss='categorical_crossentropy', optimizer=Adadelta(lr=1e-4), metrics=['accuracy']) model.fit(x_train, y_train, epochs=4, verbose=1, validation_data=(x_val, y_val)) else: datagen = ImageDataGenerator( featurewise_center=False, # set input mean to 0 over the dataset samplewise_center=False, # set each sample mean to 0 featurewise_std_normalization=False, # divide inputs by std of the dataset samplewise_std_normalization=False, # divide each input by its std zca_whitening=False, # apply ZCA whitening rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180) width_shift_range=0.1, # randomly shift images horizontally (fraction of total width) height_shift_range=0.1, # randomly shift images vertically (fraction of total height)
def _training_model(vec, ac_weights, rl_weights, output_folder, args): """Example function with types documented in the docstring. Args: param1 (int): The first parameter. param2 (str): The second parameter. Returns: bool: The return value. True for success, False otherwise. """ print('Build model...') print(args) # ============================================================================= # Input layer # ============================================================================= ac_input = Input(shape=(vec['prefixes']['activities'].shape[1], ), name='ac_input') rl_input = Input(shape=(vec['prefixes']['roles'].shape[1], ), name='rl_input') t_input = Input(shape=(vec['prefixes']['times'].shape[1], 1), name='t_input') int_input = Input(shape=(vec['prefixes']['inter_attr'].shape[1], vec['prefixes']['inter_attr'].shape[2]), name='int_input') # ============================================================================= # Embedding layer for categorical attributes # ============================================================================= ac_embedding = Embedding( ac_weights.shape[0], ac_weights.shape[1], weights=[ac_weights], input_length=vec['prefixes']['activities'].shape[1], trainable=False, name='ac_embedding')(ac_input) rl_embedding = Embedding(rl_weights.shape[0], rl_weights.shape[1], weights=[rl_weights], input_length=vec['prefixes']['roles'].shape[1], trainable=False, name='rl_embedding')(rl_input) # ============================================================================= # Concatenation layer # ============================================================================= merged1 = Concatenate(name='conc_categorical', axis=2)([ac_embedding, rl_embedding, int_input]) merged2 = Concatenate(name='conc_continuous', axis=2)([t_input, int_input]) # ============================================================================= # Layer 1 # ============================================================================= l1_c1 = LSTM(args['l_size'], kernel_initializer='glorot_uniform', return_sequences=True, dropout=0.2, implementation=args['imp'])(merged1) l1_c2 = LSTM(args['l_size'], activation=args['lstm_act'], kernel_initializer='glorot_uniform', return_sequences=True, dropout=0.2, implementation=args['imp'])(int_input) l1_c3 = LSTM(args['l_size'], activation=args['lstm_act'], kernel_initializer='glorot_uniform', return_sequences=True, dropout=0.2, implementation=args['imp'])(merged2) # ============================================================================= # Batch Normalization Layer # ============================================================================= batch1 = BatchNormalization()(l1_c1) batch2 = BatchNormalization()(l1_c2) batch3 = BatchNormalization()(l1_c3) # ============================================================================= # The layer specialized in prediction # ============================================================================= l2_c1 = LSTM(args['l_size'], kernel_initializer='glorot_uniform', return_sequences=False, dropout=0.2, implementation=args['imp'])(batch1) # The layer specialized in role prediction l2_c2 = LSTM(args['l_size'], kernel_initializer='glorot_uniform', return_sequences=False, dropout=0.2, implementation=args['imp'])(batch1) # The layer specialized in role prediction l2_c3 = LSTM(args['l_size'], kernel_initializer='glorot_uniform', return_sequences=False, dropout=0.2, implementation=args['imp'])(batch2) # The layer specialized in time prediction l2_c4 = LSTM(args['l_size'], activation=args['lstm_act'], kernel_initializer='glorot_uniform', return_sequences=False, dropout=0.2, implementation=args['imp'])(batch3) # ============================================================================= # Output Layer # ============================================================================= act_output = Dense(vec['next_evt']['activities'].shape[1], activation='softmax', kernel_initializer='glorot_uniform', name='act_output')(l2_c1) role_output = Dense(vec['next_evt']['roles'].shape[1], activation='softmax', kernel_initializer='glorot_uniform', name='role_output')(l2_c2) if ('dense_act' in args) and (args['dense_act'] is not None): inter_output = Dense(vec['next_evt']['inter_attr'].shape[1], activation=args['dense_act'], kernel_initializer='glorot_uniform', name='inter_output')(l2_c3) else: inter_output = Dense(vec['next_evt']['inter_attr'].shape[1], kernel_initializer='glorot_uniform', name='inter_output')(l2_c3) if ('dense_act' in args) and (args['dense_act'] is not None): time_output = Dense(1, activation=args['dense_act'], kernel_initializer='glorot_uniform', name='time_output')(l2_c4) else: time_output = Dense(1, kernel_initializer='glorot_uniform', name='time_output')(l2_c4) model = Model(inputs=[ac_input, rl_input, t_input, int_input], outputs=[act_output, role_output, time_output, inter_output]) if args['optim'] == 'Nadam': opt = Nadam(learning_rate=0.002, beta_1=0.9, beta_2=0.999) elif args['optim'] == 'Adam': opt = Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False) elif args['optim'] == 'SGD': opt = SGD(learning_rate=0.01, momentum=0.0, nesterov=False) elif args['optim'] == 'Adagrad': opt = Adagrad(learning_rate=0.01) model.compile(loss={ 'act_output': 'categorical_crossentropy', 'role_output': 'categorical_crossentropy', 'time_output': 'mae', 'inter_output': 'mae' }, optimizer=opt) model.summary() early_stopping = EarlyStopping(monitor='val_loss', patience=50) cb = tc.TimingCallback(output_folder) clean_models = cm.CleanSavedModelsCallback(output_folder, 2) # Output file output_file_path = os.path.join( output_folder, 'model_' + str(args['model_type']) + '_{epoch:02d}-{val_loss:.2f}.h5') # Saving model_checkpoint = ModelCheckpoint(output_file_path, monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=False, mode='auto') lr_reducer = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=10, verbose=0, mode='auto', min_delta=0.0001, cooldown=0, min_lr=0) batch_size = vec['prefixes']['activities'].shape[1] model.fit( { 'ac_input': vec['prefixes']['activities'], 'rl_input': vec['prefixes']['roles'], 't_input': vec['prefixes']['times'], 'int_input': vec['prefixes']['inter_attr'] }, { 'act_output': vec['next_evt']['activities'], 'role_output': vec['next_evt']['roles'], 'time_output': vec['next_evt']['times'], 'inter_output': vec['next_evt']['inter_attr'] }, validation_split=0.2, verbose=2, callbacks=[ early_stopping, model_checkpoint, lr_reducer, cb, clean_models ], batch_size=batch_size, epochs=1000)
def compile_model(arch='unet', input_shape=(256, 256, 13), base_depth=64, lr=0.0001, optimizer='Adam', loss_func='binary_crossentropy', additional_metrics=[], verbose=False, **model_args): if arch == 'unet': print("<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>") print("Training using valina Unet.") print("<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>") model = vanilla_unet(input_shape=input_shape, base_depth=base_depth, **model_args) elif arch == 'h_unet': print("<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>") print("Training using Han Unet.") print("<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>") model = load_model( 'cache/8_1000_Atlanta_nadir49_catid_103001000349270091-0.84_ckpt_best.hdf5', custom_objects={ 'jaccard_coef_loss': jaccard_coef_loss, 'jaccard_coef_int': jaccard_coef_int, 'precision': precision, 'recall': recall }) # origin_model = model.layers[-2] #model = h_unet(input_shape=input_shape) elif arch == 'ternausnetv1': model = ternausnetv1(input_shape=input_shape, base_depth=base_depth) else: raise ValueError("Unknown model architecture {}".format(arch)) if optimizer == 'Adam': opt_f = Adam(lr=lr) elif optimizer == 'SGD': opt_f = SGD(lr=lr) elif optimizer == 'Adagrad': opt_f = Adagrad(lr=lr) elif optimizer == 'Nadam': opt_f = Nadam(lr=lr) else: opt_f = optimizer try: parallel_model = multi_gpu_model(model, cpu_relocation=True, gpus=0) print(parallel_model.summary()) #parallel_model.layers[-2].set_weights(model.get_weights()) # you can check the index of the sequential model with parallel_model.summary() print("<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>") print("Training using multiple GPUs..") print("<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>") except ValueError: parallel_model = model print("<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>") print("Training using single GPU or CPU..") print("<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>") parallel_model.compile(optimizer=opt_f, loss=loss_func, metrics=['acc', 'mean_squared_error'] + additional_metrics) # model.summary() return model, parallel_model
def main(): #parse arguments parser = argparse.ArgumentParser() parser.add_argument('--epochs', default=10, type=int) parser.add_argument( '--remove_straight_angle', default=None, type=float, help= "Remove all training data with steering angle less than this. Useful for getting rid of straight bias" ) parser.add_argument('--save_generated_images', action='store_true', "Location to save generated images to") parser.add_argument( '--load_model', type=str, help="For transfer learning, here's the model to start with") parser.add_argument('--directory', type=str, default='data', help="Directory for training data") parser.add_argument('--learning_rate', type=float, default=.001) args = parser.parse_args() driving_log_filename = 'driving_log.csv' #Load data from computer into a dictionary data = get_driving_log_info( driving_log_filename, directory=args.directory, remove_straight_angle=args.remove_straight_angle) labels = np.array(data['steering']) train_test = train_test_split(data['center'], labels, test_size=.1, train_size=.4) center_train = np.array(train_test[0]) center_val = np.array(train_test[1]) y_train = np.array(train_test[2]) y_val = np.array(train_test[3]) #Get data set up for image generator train_datagen = ImageDataGenerator(width_shift_range=.1, height_shift_range=.1, rescale=1. / 255, fill_mode='constant', cval=0) val_datagen = ImageDataGenerator(rescale=1. / 255) center_generator = train_datagen.flow( center_train, y_train, batch_size=128, shuffle=True, save_to_dir='generated' if args.save_data else None) center_val_generator = val_datagen.flow(center_val, y_val, batch_size=128, shuffle=True) if args.load_model: #Load previous model print('loading model') model = load_model(args.load_model) #Make a new model else: model = Sequential() #Convolution 1. Input: (?, 66, 200, 3) Output: (?, 31, 98, 24) model.add( Convolution2D(24, 5, 5, activation='relu', subsample=(2, 2), input_shape=(66, 200, 3))) #Convolution 2. Input: (?, 31, 98, 24) Output: (?, 14, 47, 36) model.add(Convolution2D(36, 5, 5, activation='relu', subsample=(2, 2))) #Convolution 3. Input: (?, 14, 47, 36) Output: (?, 5, 22, 48) model.add(Convolution2D(48, 5, 5, activation='relu', subsample=(2, 2))) #Convolution 4. Input: (?, 5, 22, 48) Output: (?, 3, 20, 64) model.add(Convolution2D(64, 3, 3, activation='relu')) #Convolution 5. Input: (?, 3, 20, 64) Output: (?, 1, 18, 64) model.add(Convolution2D(64, 3, 3, activation='relu')) model.add(Dropout(.5)) #Flatten the layers. Input: (?, 1, 18, 64) Output: (?, 1152) model.add(Flatten()) #Fully Connected #1. Input: (?, 1152) Output: (?, 100) model.add(Dense(100, activation='relu')) #Fully connected #2. Input: (?, 100) Output: (?, 50) model.add(Dense(50, activation='relu')) #Fully connected #3. Input: (?, 50) Output: (?, 10) model.add(Dense(10, activation='relu')) model.add(Dropout(.5)) #Output layer: 1 output model.add(Dense(1)) for i in range(len(model.layers)): model.layers[i].name += str(i) optimizer = Nadam(lr=args.learning_rate) model.compile(optimizer=optimizer, loss='mean_squared_error') #Save json file with open('model.json', 'w') as file: json_model = json.loads(model.to_json()) json.dump(json_model, file) checkpoint_callback = ModelCheckpoint('model.h5', verbose=1, save_best_only=True) model.fit_generator(center_generator, nb_epoch=args.epochs, samples_per_epoch=len(center_train), validation_data=center_val_generator, nb_val_samples=len(y_val), callbacks=[checkpoint_callback]) model.save('model.md5')
import glob from custom_layers.scale_layer import Scale from keras.models import load_model from keras.preprocessing import image import numpy as np TEST_DIR = '/home/buiduchanh/WorkSpace/keras_sources/acr-view_KERAS_BAK/images' RESULTS_DIR = '/home/khach/hanh/acr/keras_sources/images/reuslt/' ERR_DIR = '/home/khach/hanh/acr/keras_sources/images/error/' LABELS = {0:'0_backview', 1:'1_fontview', 2:'2_sideview'} if __name__ == '__main__': img_rows, img_cols = 224, 224 model = load_model('model/weights.21-0.88502994.hdf5', custom_objects={"Scale": Scale}) nadam = Nadam(lr=1e-06, beta_1=0.9, beta_2=0.999, epsilon=1e-08, schedule_decay=0.004) model.compile(optimizer=nadam, loss='categorical_crossentropy', metrics=['accuracy']) images_folder = os.listdir(TEST_DIR) count_1 = 0 count_2 = 0 for folder in images_folder: images_dir = os.path.join(TEST_DIR, folder) imagefiles = glob.glob('%s/*' % images_dir) batch_size = 500 for block in range(0, len(imagefiles), batch_size): images = [] fnames = [] for pos in range(0, min(batch_size, len(imagefiles) - block)): filename = '' try:
'training4_high_lr/Nadam_std_+_firsthardrightfwddirt_lr0.00005', "track1_data_dirs": [ 'data_download', 'avoid_dirt', 'first_hard_right_fwd' ], "LOAD_MODEL": 'training4_high_lr/Nadam_std_+_firsthardrightfwddirt_lr0.0001/model_epoch_50.h5', "EPOCHS": 70, } } ] if __name__ == "__main__": # to_run = [instance1, instance2] to_run = all_instances for inst in to_run: pd.Series(inst).to_csv('params.csv') if inst["LOAD_MODEL"]: model = load_model(inst["LOAD_MODEL"]) if inst["optimizer"] == "Adam": opt = Adam(lr=inst["lr"]) elif inst["optimizer"] == "Nadam": opt = Nadam(lr=inst["lr"]) model.compile(optimizer=opt, loss='mean_squared_error') else: model = nvidia_model(learning_rate=inst['lr'], dropout=inst['dropout'], optimizer=inst['optimizer']) model.save('test.hd5') train(model, **inst)
print(y_train.shape) train_ids = np.array(f['train_ids']) batch_size = 128 nb_epoch = 4 history = History() callbacks = [ history, ] suffix = 'buildings_3_resnet_medium' + "{batch}_{epoch}".format( batch=batch_size, epoch=nb_epoch) model.compile(optimizer=Nadam(lr=1e-3), loss=jaccard_coef_loss, metrics=['binary_crossentropy', jaccard_coef_int]) from tensorflow.python.client import device_lib print(device_lib.list_local_devices()) model.fit_generator(batch_generator(X_train, y_train, batch_size, horizontal_flip=True, vertical_flip=True, swap_axis=True), nb_epoch=nb_epoch, verbose=1, samples_per_epoch=batch_size * 25, callbacks=callbacks, nb_worker=24)
def training_model(vec, ac_weights, rl_weights, output_folder, args): """Example function with types documented in the docstring. Args: param1 (int): The first parameter. param2 (str): The second parameter. Returns: bool: The return value. True for success, False otherwise. """ print('Build model...') print(args) # ============================================================================= # Input layer # ============================================================================= ac_input = Input(shape=(vec['prefixes']['x_ac_inp'].shape[1], ), name='ac_input') rl_input = Input(shape=(vec['prefixes']['x_rl_inp'].shape[1], ), name='rl_input') t_input = Input(shape=(vec['prefixes']['xt_inp'].shape[1], 1), name='t_input') # ============================================================================= # Embedding layer for categorical attributes # ============================================================================= ac_embedding = Embedding(ac_weights.shape[0], ac_weights.shape[1], weights=[ac_weights], input_length=vec['prefixes']['x_ac_inp'].shape[1], trainable=False, name='ac_embedding')(ac_input) rl_embedding = Embedding(rl_weights.shape[0], rl_weights.shape[1], weights=[rl_weights], input_length=vec['prefixes']['x_rl_inp'].shape[1], trainable=False, name='rl_embedding')(rl_input) # ============================================================================= # Layer 1 # ============================================================================= merged = Dot(name='merged', normalize=True, axes=2)([ac_embedding, rl_embedding]) concatenate = Concatenate(name='concatenated', axis=2)([merged, t_input]) if args['lstm_act'] is not None: l1_c1 = LSTM(args['l_size'], activation=args['lstm_act'], kernel_initializer='glorot_uniform', return_sequences=True, dropout=0.2, implementation=args['imp'])(concatenate) else: l1_c1 = LSTM(args['l_size'], kernel_initializer='glorot_uniform', return_sequences=True, dropout=0.2, implementation=args['imp'])(concatenate) # ============================================================================= # Batch Normalization Layer # ============================================================================= batch1 = BatchNormalization()(l1_c1) # ============================================================================= # The layer specialized in prediction # ============================================================================= l2_c1 = LSTM(args['l_size'], kernel_initializer='glorot_uniform', return_sequences=False, dropout=0.2, implementation=args['imp'])(batch1) # The layer specialized in role prediction l2_c2 = LSTM(args['l_size'], kernel_initializer='glorot_uniform', return_sequences=False, dropout=0.2, implementation=args['imp'])(batch1) # The layer specialized in role prediction if args['lstm_act'] is not None: l2_3 = LSTM(args['l_size'], activation=args['lstm_act'], kernel_initializer='glorot_uniform', return_sequences=False, dropout=0.2, implementation=args['imp'])(batch1) else: l2_3 = LSTM(args['l_size'], kernel_initializer='glorot_uniform', return_sequences=False, dropout=0.2, implementation=args['imp'])(batch1) # ============================================================================= # Output Layer # ============================================================================= act_output = Dense(ac_weights.shape[0], activation='softmax', kernel_initializer='glorot_uniform', name='act_output')(l2_c1) role_output = Dense(rl_weights.shape[0], activation='softmax', kernel_initializer='glorot_uniform', name='role_output')(l2_c2) if args['dense_act'] is not None: time_output = Dense(1, activation=args['dense_act'], kernel_initializer='glorot_uniform', name='time_output')(l2_3) else: time_output = Dense(1, kernel_initializer='glorot_uniform', name='time_output')(l2_3) model = Model(inputs=[ac_input, rl_input, t_input], outputs=[act_output, role_output, time_output]) if args['optim'] == 'Nadam': opt = Nadam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=1e-08, schedule_decay=0.004, clipvalue=3) elif args['optim'] == 'Adam': opt = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False) elif args['optim'] == 'SGD': opt = SGD(lr=0.01, momentum=0.0, decay=0.0, nesterov=False) elif args['optim'] == 'Adagrad': opt = Adagrad(lr=0.01, epsilon=None, decay=0.0) model.compile(loss={ 'act_output': 'categorical_crossentropy', 'role_output': 'categorical_crossentropy', 'time_output': 'mae' }, optimizer=opt) model.summary() early_stopping = EarlyStopping(monitor='val_loss', patience=20) # # # Output file output_file_path = os.path.join( output_folder, 'model_rd_' + str(args['l_size']) + ' ' + args['optim'] + '_{epoch:02d}-{val_loss:.2f}.h5') # Saving model_checkpoint = ModelCheckpoint(output_file_path, monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=False, mode='auto') lr_reducer = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=10, verbose=0, mode='auto', min_delta=0.0001, cooldown=0, min_lr=0) model.fit( { 'ac_input': vec['prefixes']['x_ac_inp'], 'rl_input': vec['prefixes']['x_rl_inp'], 't_input': vec['prefixes']['xt_inp'] }, { 'act_output': vec['next_evt']['y_ac_inp'], 'role_output': vec['next_evt']['y_rl_inp'], 'time_output': vec['next_evt']['yt_inp'] }, validation_split=0.2, verbose=2, callbacks=[early_stopping, model_checkpoint, lr_reducer], batch_size=vec['prefixes']['x_ac_inp'].shape[1], epochs=200)
class Model(object): def __init__(self, content, style, x): self.content_features = {} self.style_features = {} self.model = None # evaluate features of content and style images print("Pre-evaluate features...") requests.post('http://localhost:8000/setmsg', None, {'msg': "Pre-evaluate features..."}) # global msg # msg = "Pre-evaluate features..." self.gen_model(K.placeholder(input_shape)) self.f_output = K.function([self.input_tensor], list(self.outputs_dict.values())) self.writedown_content_feature(content) self.writedown_style_feature(style) # training model self.gen_model(K.variable(x)) self.optimizer = Nadam(lr=learning_rate) self.compile() def set_lr(self, learning_rate): """ set the learning rate of the optimizer :param learning_rate: lerning rate pass to the optimizer :return: """ # self.optimizer.lr.set_value(learning_rate) K.set_value(self.optimizer.lr, learning_rate) print('learning rate = {}'.format(learning_rate)) requests.post('http://localhost:8000/setmsg', None, {'msg': 'set learning rate to {}'.format(learning_rate)}) # global msg # msg = 'learning rate = {}'.format(learning_rate) def gen_model(self, x): """ Generate a VGG-19 model with certain inputs :param x: Input (numpy array) :return: A VGG-19 model """ self.model = model_with_input(x) self.layers_dict = dict([(layer.name, layer) for layer in self.model.layers]) self.outputs_dict = dict([(layer.name, layer.output) for layer in self.model.layers]) self.inputs_dict = dict([(layer.name, layer.input) for layer in self.model.layers]) self.input_tensor = x def get_feature(self, x): return dict(zip(self.outputs_dict.keys(), self.f_output([x]))) def writedown_content_feature(self, content): # work out and keep content features outputs = self.get_feature(content) for layer_name in content_feature_layers: self.content_features[layer_name] = outputs[layer_name] def writedown_style_feature(self, style): # work out and keep style features outputs = self.get_feature(style) for layer_name in style_feature_layers: self.style_features[layer_name] = K.eval(gram_matrix(K.variable(outputs[layer_name]))) def get_style_loss(self): """ The "style loss" is designed to maintain the style of the reference image in the generated image. It is based on the gram matrices (which capture style) of feature maps from the style reference image and from the generated image :return: style loss """ loss = K.variable(0.) for layer_name in style_feature_layers: style_features = K.variable(self.style_features[layer_name]) combination_features = gram_matrix(self.outputs_dict[layer_name]) loss += style_loss(style_features, combination_features) loss /= len(style_feature_layers) return loss def get_content_loss(self): """ content loss, designed to maintain the "content" of the base image in the generated image :return: content loss """ loss = K.variable(0.) for layer_name in content_feature_layers: content_features = K.variable(self.content_features[layer_name]) combination_features = self.outputs_dict[layer_name] loss += K.sum(K.square(combination_features - content_features)) loss /= len(content_feature_layers) return loss def total_variation_loss(self): """ Total variation loss, designed to keep the generated image locally coherent :return: total variation loss """ x = self.input_tensor assert K.ndim(x) == 4 a = K.square(x[:, :, :img_width - 1, :img_height - 1] - x[:, :, 1:, :img_height - 1]) b = K.square(x[:, :, :img_width - 1, :img_height - 1] - x[:, :, :img_width - 1, 1:]) return K.sum(K.pow(a + b, 1.25)) def get_loss(self): """ Get all loss and corresponding weights :return: [loss1,...],[weight1,...] """ loss_table = [self.get_content_loss(), self.get_style_loss(), self.total_variation_loss()] loss_weights = list(zip(*loss_set))[1] # loss_weights = [content_weight, style_weight, total_variation_weight] return loss_table, loss_weights def compile(self): """ Defines the way to calculate loss and do optimization :return: None """ # global msg print("Generate loss and grad...") requests.post('http://localhost:8000/setmsg', None, {'msg': "Generate loss and grad..."}) # msg = "Generate loss and grad..." losses = [l * w for l, w in zip(*self.get_loss())] total_loss = sum(losses) metrics = [total_loss] + losses constraints = [] # constraints = [lambda x: K.clip(x, 0., 255.)] training_updates = self.optimizer.get_updates([self.inputs_dict['input']], constraints, total_loss) # returns loss and metrics. Updates weights at each call. self.train_function = K.function([], metrics, updates=training_updates) def update(self): """ Step the optimization process :return: Info about loss and iteration result """ return self.train_function([]), K.eval(self.inputs_dict['input'])
from sklearn.neural_network import MLPClassifier from sklearn.preprocessing import LabelEncoder from sklearn.metrics import precision_score from sklearn.metrics import recall_score from sklearn.metrics import confusion_matrix from sklearn.decomposition import PCA from sklearn.preprocessing import StandardScaler from sklearn.metrics import accuracy_score Epoch = 50 Input = 24 Output = 1 Hidden = 20 OPTIMIZER = Nadam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=1e-08, schedule_decay=0.004) #learning rate = 0.01 ''' def calcTime(begin, end): bT = begin.split('/') eT = end.split('/') if eval(eT[0]) == eval(bT[0]): return eval(eT[1]) - eval(bT[1]) else: if eval(bT[0]) == 2: return 29+eval(eT[1]) - eval(bT[1]) elif eval(bT[0]) in [1, 3, 5, 7, 8, 10, 12]: return 31+eval(eT[1]) - eval(bT[1]) else: return 30+eval(eT[1]) - eval(bT[1])