model = Model(inputs=inputs, outputs=outputs) model.summary() #3. 컴파일, 훈련 model.compile(loss='mse', optimizer='adam', metrics=['mae']) from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint early_stopping = EarlyStopping(monitor='val_loss', patience=20, mode='auto') modelpath = '../data/modelcheckpoint/k46_4_boston_{epoch:02d}-{val_loss:.4f}.hdf5' cp = ModelCheckpoint(modelpath, monitor='val_loss', save_best_only=True, mode='auto') model.fit(x_train, y_train, batch_size=8, epochs=1000, validation_data=(x_val, y_val), callbacks=[early_stopping, cp]) #4. 평가, 예측 loss, mae = model.evaluate(x_test, y_test, batch_size=8) y_predict = model.predict(x_test) print("loss, mae : ", loss, mae) from sklearn.metrics import mean_squared_error def RMSE(y_test, y_predict): return np.sqrt(mean_squared_error(y_test, y_predict))
optimizer=tf.keras.optimizers.SGD( learning_rate=lr_extension ), #function used to minimize the loss (back propogation and gradient descent) loss=tf.keras.losses.BinaryCrossentropy( ), #defines how far off a prediction is from correct answer - loss is high if model predicts small prob classified correctly metrics=[ tf.keras.metrics.MeanSquaredError(), tf.keras.metrics.CategoricalAccuracy() ]) #sending the same augmented input through both models, fit the model to the data history1 = model.fit(x=aug.flow(X_train, y_train, batch_size=BATCH_SIZE), shuffle=True, epochs=NUM_EPOCHS, callbacks=[ tf.keras.callbacks.EarlyStopping( monitor='loss', patience=3, min_delta=0.001, restore_best_weights=True) ]) elif OPTION == "AVG": print("Using average option") #develop a new model that will average the outputs of each model before prediction inputs = Input(shape=(50, 50, 3)) model1_layer = model1(inputs) model2_layer = model2(inputs) avg_layer = Average()([model1_layer, model2_layer]) #average layer dropout1 = Dropout(ENSEMBLE_DR_RATE)(avg_layer) layer1 = Dense(512, activation='relu')(dropout1) dropout2 = Dropout(ENSEMBLE_DR_RATE)(layer1)
#print(kl) #recon= K.sum(K.square(y_pred-y_true)) # compute the KL loss #kl= - 0.5 * K.sum(1 + variance - K.square(mean) - K.exp(variance), axis=-1) return K.mean(recon + kl) mnist = tf.keras.datasets.mnist (x_train, y_train),(x_test, y_test) = mnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0 x_train[0].shape x_train=x_train.reshape(60000,784) x_train[50].shape vae.compile(optimizer='adam',loss=vae_loss,metrics=['accuracy']) vae.fit(x_train, x_train, batch_size=m, epochs=20) vae decoder.summary() vae.summary() x_test=x_test.reshape(10000,784) pred=vae.predict(x_train,batch_size=m) pred[0].shape import matplotlib.pyplot as plt pred[0].shape pred=pred.reshape(60000,28,28)
distance = Lambda(euclid_dis, output_shape=eucl_dist_output_shape)( [processed_a, processed_b]) #print(processed_a) model = Model([input_a, input_b], distance) #print("start") import keras rms = RMSprop() opt = keras.optimizers.Adam(lr=0.00001) model.compile(loss=contrastive_loss, optimizer=opt, metrics=[accuracy]) train_history = model.fit([train_pairs[:, 0], train_pairs[:, 1]], train_y, batch_size=200, epochs=25, validation_data=([val_pairs[:, 0], val_pairs[:, 1]], val_y), verbose=0) # compute final accuracy on training and test sets y_pred_train = model.predict([train_pairs[:, 0], train_pairs[:, 1]]) train_acc = compute_accuracy(train_y, y_pred_train) y_pred_val = model.predict([val_pairs[:, 0], val_pairs[:, 1]]) val_acc = compute_accuracy(val_y, y_pred_val) test_pred = [] y_pred_test = model.predict([test_pairs[:, 0], test_pairs[:, 1]]) test_acc = compute_accuracy(test_y, y_pred_test) for i in y_pred_test: if (i > 0.5): #print(i)
list_tokenized_validation = tokenizer.texts_to_sequences(validation_sequences) x_train = pad_sequences(list_tokenized_train, maxlen=input_length) x_validation = pad_sequences(list_tokenized_validation, maxlen=input_length) encoder = LabelBinarizer() encoder.fit(data.label.unique()) encoder_path = Path('', 'encoder.pickle') with encoder_path.open('wb') as file: pickle.dump(encoder, file) y_train = encoder.transform(train.label) y_validation = encoder.transform(validation.label) batch_size = 128 epochs = 20 model.fit(x_train, y=y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_validation, y_validation)) model_file = Path('model_weights.h5').resolve() model.save_weights(model_file.as_posix()) train_acc = model.evaluate(x_train, y_train, verbose=0) test_acc = model.evaluate(x_validation, y_validation, verbose=0) print(train_acc, test_acc)
x = Dense(256, activation='relu')(x) x = GlobalMaxPooling2D()(x) pred = Dense(1, activation='sigmoid')(x) model = Model(inputs=input, outputs=pred) # compile model opt = Adam(lr=0.01) model.compile(optimizer=opt,loss='binary_crossentropy',metrics=['accuracy']) # fit model nTraining = 1750 nValidation = 437 model.fit(train_generator, steps_per_epoch=nTraining // batch_size, epochs=20, validation_data=validation_generator, validation_steps=nValidation // batch_size) # evaluate model model.evaluate(validation_generator, steps = nValidation // batch_size) # Get AUC ## Get predictions validation_generator = test_datagen.flow_from_directory( '/content/val_data', # the directory for the validation data target_size=(250,250), batch_size=437, class_mode='binary', shuffle=False)
dense2 = Dense(10, activation='relu')(dense2) dense2 = Dense(1, activation='relu')(dense2) #모델병합concatenate from tensorflow.keras.layers import concatenate merge1 = concatenate([dense1, dense2]) #중간층 모델구성 middle1 = Dense(15)(merge1) middle1 = Dense(15)(middle1) #모델 분기1 output1 = Dense(30)(middle1) output1 = Dense(7)(output1) output1 = Dense(1)(output1) #모델 선언 model = Model(inputs=[input1, input2], outputs=output1) #3.컴파일, 훈련 model.compile(loss='mse', optimizer='adam', metrics='mae') model.fit([x1, x2], y, epochs=10, verbose=1) #4.평가, 예측 result = model.evaluate([x1, x2], y) print(result) y_pred = model.predict([x1_predict, x2_predict]) print(y_pred)
class TripRecommendation(object): def __init__(self): Inputs, labels = get_data() self.X = Inputs self.Y = labels self.num_classes = len(set(self.Y)) self.label_encoder = joblib.load(label_encoder_weights) self.hotel_dict = joblib.load(hotel_dict_path) def classifier(self): inputs = Input(shape=(n_features, ), name='inputs') x = Dense(dense1, activation='relu', name='dense1')(inputs) x = Dense(dense2, activation='relu', name='dense2')(x) x = Dense(dense3, activation='relu', name='dense3')(x) x = Dropout(keep_prob)(x) outputs = Dense(self.num_classes, activation='softmax', name='output')(x) self.model = Model(inputs, outputs) def train(self, verbose): self.model.compile( loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'], ) self.model.fit(self.X, self.Y, batch_size=batch_size, epochs=num_epoches, validation_split=validation_split, verbose=verbose) def finetune(self): inputs = Input(shape=(n_features, ), name='inputs') x = inputs for layer in self.model.layers[1:-1]: layer.trainable = False x = layer(x) outputs = Dense(self.num_classes, activation='softmax', name='output')(x) self.model = Model(inputs, outputs) def save_model(self): self.model.save(model_weights) def load_model(self): loaded_model = load_model(model_weights) loaded_model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy']) self.model = loaded_model def run(self, verbose=1): if os.path.exists(model_weights): self.load_model() else: self.classifier() self.train(verbose) self.save_model() def run_finetune(self, verbose=0): self.load_model() self.finetune() self.train(verbose) self.save_model() def prediction(self, user_id): user_id = int(user_id) try: input_ = np.array([self.X[user_id, :]]) except: IndexError P = self.model.predict(input_).squeeze() Pred = np.argsort(P)[-n_recommendation:] labels = self.label_encoder.inverse_transform(Pred) return [str(self.hotel_dict[label]) for label in labels]
r = Dense(128, activation='relu')(r) r = Dense(32, activation='relu')(r) r = Dense(16, activation='relu')(r) r = Dense(1, name='regression')(r) return r input = Input(shape=(6,)) encoder = func_encoder(input) decoder = func_decoder(encoder) regression = func_regr(encoder) model = Model(input, outputs=[regression, decoder]) model.compile(optimizer='adam', loss='mse', metrics=['mae']) model.fit(train_data, [train_labels, train_data], epochs=100, batch_size=25, validation_split=0.3) model.evaluate(test_data, [test_labels, test_data]) encoder_model = Model(input, encoder) decoder_model = Model(input, decoder) regression_model = Model(input, regression) encoder_model.save('encoder.h5') decoder_model.save('decoder.h5') regression_model.save('regression.h5') encode_data = encoder_model.predict(test_data) decode_data = decoder_model.predict(test_data) regression_data = regression_model.predict(test_data) decode_data = decode_data * std + mean
l_cov3 = Conv1D(num_filters, 4, activation='relu')(embedding) l_pool1 = GlobalMaxPooling1D()(l_cov1) l_pool2 = GlobalMaxPooling1D()(l_cov2) l_pool3 = GlobalMaxPooling1D()(l_cov3) tensors = Concatenate(axis=1)([l_pool1, l_pool2, l_pool3]) dense = Dense(300, activation='relu')(tensors) dropout = Dropout(0.5)(dense) flatten = Flatten()(dropout) preds = Dense(1, activation='sigmoid')(flatten) checkpoint = ModelCheckpoint('Model.hdf5', monitor='val_acc', verbose=1, save_best_only=True, mode='auto') model = Model(inputs=inputs, outputs=preds) adam = Adam(lr=1e-5, decay=1e-5) model.compile(optimizer=adam, loss='binary_crossentropy', metrics=['accuracy']) model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, callbacks=[checkpoint, tensor_board], validation_data=(X_val, y_val), shuffle=True)
Y_gender = Y_gender - 1 # %% if debug: Y_gender = Y_gender[:900000//100] # %% checkpoint = ModelCheckpoint("tmp/gender_epoch_{epoch:02d}.hdf5", monitor='val_loss', verbose=0, save_best_only=False, mode='auto', period=1) # %% try: mail('start train lstm') model.fit(X_train, Y_gender, validation_split=0.1, epochs=100, batch_size=768, callbacks=[checkpoint], ) mail('train gender lstm done!!!') except Exception as e: e = str(e) mail('train lstm failed!!! ' + e) # %% model.load_weights('tmp\gender_epoch_01.hdf5') # %% if debug:
class Network(): """ This class contains all the functions and models and things related to the neural netork. """ def __init__(self): # game params self.lr = 0.001 self.dropout = 0.3 self.epochs = 10 self.batch_size = 128 self.num_channels = 512 self.action_space = 8192 # Neural Net self.input_boards = Input(shape=(8, 8, 12)) # s: batch_size x board_x x number of possible pieces x_image = Reshape((8, 8, 12, 1))(self.input_boards) h_conv1 = Activation('relu')(BatchNormalization(axis=3)(Conv2D(self.num_channels, 3, padding='same', kernel_initializer='random_normal')(x_image))) h_conv2 = Activation('relu')(BatchNormalization(axis=3)(Conv2D(self.num_channels, 3, padding='same', kernel_initializer='random_normal')(h_conv1))) h_conv3 = Activation('relu')(BatchNormalization(axis=3)(Conv2D(self.num_channels, 3, padding='valid', kernel_initializer='random_normal')(h_conv2))) h_conv4 = Activation('relu')(BatchNormalization(axis=3)(Conv2D(self.num_channels, 3, padding='valid', kernel_initializer='random_normal')(h_conv3))) h_conv5_flat = Flatten()(h_conv4) s_fc1 = Dropout(self.dropout)(Activation('relu')(BatchNormalization(axis=1)(Dense(1024)(h_conv5_flat)))) s_fc2 = Dropout(self.dropout)(Activation('relu')(BatchNormalization(axis=1)(Dense(512)(s_fc1)))) self.pi = Dense(self.action_space, activation='softmax', name='pi')(s_fc2) self.v = Dense(1, activation='tanh', name='v')(s_fc2) self.model = Model(inputs=self.input_boards, outputs=[self.pi, self.v]) self.model.compile(loss=['categorical_crossentropy','mean_squared_error'], optimizer=Adam(self.lr), metrics=['accuracy']) print(self.model.summary()) def train(self, examples): """ examples: list of examples, each example is of form (board, pi, v) """ input_boards, target_pis, target_vs = list(zip(*examples)) input_boards = np.asarray(input_boards) target_pis = np.asarray(target_pis) target_vs = np.asarray(target_vs) self.model.fit(x = input_boards, y = [target_pis, target_vs], batch_size = self.batch_size, epochs = self.epochs) def predict(self, board): """ board: np array with board """ # timing start = time.time() # preparing input board = board[np.newaxis, :, :] # run pi, v = self.model.predict(board) # print('PREDICTION TIME TAKEN : {0:03f}'.format(time.time()-start)) return pi[0], v[0] def save_checkpoint(self, folder='checkpoint', filename='checkpoint.h5'): filepath = os.path.join(folder, filename) self.model.save(filepath) def load_checkpoint(self, folder='checkpoint', filename='checkpoint.h5'): filepath = os.path.join(folder, filename) self.model.load_weights(filepath)
# RNN 모델을 생성 및 학습 xInput = Input(batch_shape=(None, x_train.shape[1], x_train.shape[2])) x = LSTM(64, return_sequences = True)(xInput) x = LSTM(64)(x) x = Dense(64,activation = 'relu')(x) x = Dense(64,activation = 'relu')(x) xOutput = Dense(1, activation = 'linear')(x) model = Model(xInput, xOutput) model.compile(loss='mse', optimizer='adam') # 학습 model.fit(x_train, y_train, epochs=500, batch_size=train_num,verbose=1) # 예측 y_hat = model.predict(x_test, batch_size=1) a_axis = np.arange(0, len(y_train)) b_axis = np.arange(len(y_train), len(y_train) + len(y_hat)) plt.figure(figsize=(10,6)) plt.plot(a_axis, y_train.reshape(x_train,), '-') plt.plot(b_axis, y_hat.reshape(test_num,), '-', color='red', label='Predicted') plt.plot(b_axis, y_test.reshape(test_num,), '-', color='green', alpha=0.2, label='Actual') plt.legend() plt.show()
y_pred, weight=weight, from_logits=from_logits), axis=-1) model = Model(inputs=[input_main, input_aux], outputs=[output_main, output_aux]) # model.summary() model.compile(optimizer=tf.train.AdamOptimizer(learning_rate=1e-3), loss=WBCE, metrics=[ML.pion_con]) model.fit( [X, I], [T, T], batch_size=2**9, epochs=10, validation_data=([Xv, Iv], [Tv, Tv]), ) #callbacks=[tensorboard, csvlogger]) model.summary() """ input_main = Input(shape=X.shape[1:], name="X-in") x = Conv2D(cs_1, [2,3], activation='relu', padding ='same')(input_main) x = MaxPool2D([2,2], 2, padding='valid')(x) x = Conv2D(cs_2, [2,3], activation='relu', padding='same')(x) x = MaxPool2D([2,2], 2, padding='valid')(x) x = Flatten()(x) input_aux = Input(shape=I.shape[1:], name="gain")
class LSTMSeq2Seq(BaseModel): def __init__(self, check_optional_config=True, future_seq_len=2): """ Constructor of LSTM Seq2Seq model """ self.model = None self.past_seq_len = None self.future_seq_len = future_seq_len self.feature_num = None self.target_col_num = None self.metric = None self.latent_dim = None self.batch_size = None self.check_optional_config = check_optional_config def _build_train(self, mc=False, **config): """ build LSTM Seq2Seq model :param config: :return: """ super()._check_config(**config) self.metric = config.get('metric', 'mean_squared_error') self.latent_dim = config.get('latent_dim', 128) self.dropout = config.get('dropout', 0.2) self.lr = config.get('lr', 0.001) # for restore in continuous training self.batch_size = config.get('batch_size', 64) training = True if mc else None # Define an input sequence and process it. self.encoder_inputs = Input(shape=(None, self.feature_num), name="encoder_inputs") encoder = LSTM(units=self.latent_dim, dropout=self.dropout, return_state=True, name="encoder_lstm") encoder_outputs, state_h, state_c = encoder(self.encoder_inputs, training=training) # We discard `encoder_outputs` and only keep the states. self.encoder_states = [state_h, state_c] # Set up the decoder, using `encoder_states` as initial state. self.decoder_inputs = Input(shape=(None, self.target_col_num), name="decoder_inputs") # We set up our decoder to return full output sequences, # and to return internal states as well. We don't use the # return states in the training model, but we will use them in inference. self.decoder_lstm = LSTM(self.latent_dim, dropout=self.dropout, return_sequences=True, return_state=True, name="decoder_lstm") decoder_outputs, _, _ = self.decoder_lstm( self.decoder_inputs, training=training, initial_state=self.encoder_states) self.decoder_dense = Dense(self.target_col_num, name="decoder_dense") decoder_outputs = self.decoder_dense(decoder_outputs) # Define the model that will turn # `encoder_input_data` & `decoder_input_data` into `decoder_target_data` self.model = Model([self.encoder_inputs, self.decoder_inputs], decoder_outputs) self.model.compile(loss='mse', metrics=[self.metric], optimizer=keras.optimizers.RMSprop(lr=self.lr)) return self.model def _restore_model(self): self.encoder_inputs = self.model.input[0] # input_1 encoder_outputs, state_h_enc, state_c_enc = self.model.layers[ 2].output # lstm_1 self.encoder_states = [state_h_enc, state_c_enc] self.decoder_inputs = self.model.input[1] # input_2 self.decoder_lstm = self.model.layers[3] self.decoder_dense = self.model.layers[4] def _build_inference(self, mc=False): training = True if mc else None # from our previous model - mapping encoder sequence to state vectors encoder_model = Model(self.encoder_inputs, self.encoder_states) # A modified version of the decoding stage that takes in predicted target inputs # and encoded state vectors, returning predicted target outputs and decoder state vectors. # We need to hang onto these state vectors to run the next step of the inference loop. decoder_state_input_h = Input(shape=(self.latent_dim, )) decoder_state_input_c = Input(shape=(self.latent_dim, )) decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c] decoder_outputs, state_h, state_c = self.decoder_lstm( self.decoder_inputs, training=training, initial_state=decoder_states_inputs) decoder_states = [state_h, state_c] decoder_outputs = self.decoder_dense(decoder_outputs) decoder_model = Model([self.decoder_inputs] + decoder_states_inputs, [decoder_outputs] + decoder_states) return encoder_model, decoder_model def _decode_sequence(self, input_seq, mc=False): encoder_model, decoder_model = self._build_inference(mc=mc) # Encode the input as state vectors. states_value = encoder_model.predict(input_seq) # Generate empty target sequence of length 1. target_seq = np.zeros((len(input_seq), 1, self.target_col_num)) # Populate the first target sequence with end of encoding series value target_seq[:, 0] = input_seq[:, -1, :self.target_col_num] # Sampling loop for a batch of sequences - we will fill decoded_seq with predictions # (to simplify, here we assume a batch of size 1). decoded_seq = np.zeros( (len(input_seq), self.future_seq_len, self.target_col_num)) for i in range(self.future_seq_len): output, h, c = decoder_model.predict([target_seq] + states_value) decoded_seq[:, i] = output[:, 0] # Update the target sequence (of length 1). target_seq = np.zeros((len(input_seq), 1, self.target_col_num)) target_seq[:, 0] = output[:, 0] # Update states states_value = [h, c] return decoded_seq def _get_decoder_inputs(self, x, y): """ lagged target series for teacher forcing decoder_input data is one timestamp ahead of y :param x: 3-d array in format of (sample_num, past_sequence_len, feature_num) :param y: 3-d array in format of (sample_num, future_sequence_len, target_col_num) Need to expand dimension if y is a 2-d array with one target col :return: 3-d array of decoder inputs """ decoder_input_data = np.zeros(y.shape) decoder_input_data[1:, ] = y[:-1, ] decoder_input_data[0, 0] = x[-1, -1, :self.target_col_num] decoder_input_data[0, 1:] = y[0, :-1] return decoder_input_data def _get_len(self, x, y): self.past_seq_len = x.shape[1] self.feature_num = x.shape[2] # self.future_seq_len = y.shape[1] self.target_col_num = y.shape[2] def _expand_y(self, y): """ expand dims for y. :param y: :return: """ while len(y.shape) < 3: y = np.expand_dims(y, axis=2) return y def _pre_processing(self, x, y, validation_data): """ pre_process input data. 1. expand dims for y and val_y 2. get decoder inputs for train data 3. get decoder inputs for validation data :param x: train_x :param y: train_y :param validation_data: :return: network input """ y = self._expand_y(y) self._get_len(x, y) decoder_input_data = self._get_decoder_inputs(x, y) if validation_data is not None: val_x, val_y = validation_data val_y = self._expand_y(val_y) val_decoder_input = self._get_decoder_inputs(val_x, val_y) validation_data = ([val_x, val_decoder_input], val_y) return x, y, decoder_input_data, validation_data def fit_eval(self, x, y, validation_data=None, mc=False, verbose=0, **config): """ fit for one iteration :param x: 3-d array in format (no. of samples, past sequence length, 2+feature length), in the last dimension, the 1st col is the time index (data type needs to be numpy datetime type, e.g. "datetime64"), the 2nd col is the target value (data type should be numeric) :param y: 2-d numpy array in format (no. of samples, future sequence length) if future sequence length > 1, or 1-d numpy array in format (no. of samples, ) if future sequence length = 1 :param validation_data: tuple in format (x_test,y_test), data used for validation. If this is specified, validation result will be the optimization target for automl. Otherwise, train metric will be the optimization target. :param config: optimization hyper parameters :return: the resulting metric """ x, y, decoder_input_data, validation_data = self._pre_processing( x, y, validation_data) # if model is not initialized, __build the model if self.model is None: self._build_train(mc=mc, **config) # batch_size = config.get('batch_size', 64) # lr = self.lr # name = "seq2seq-batch_size-{}-epochs-{}-lr-{}-time-{}"\ # .format(batch_size, epochs, lr, time()) # tensorboard = TensorBoard(log_dir="logs/" + name) hist = self.model.fit( [x, decoder_input_data], y, validation_data=validation_data, batch_size=self.batch_size, epochs=config.get("epochs", 10), verbose=verbose, # callbacks=[tensorboard] ) # print(hist.history) if validation_data is None: # get train metrics # results = self.model.evaluate(x, y) result = hist.history.get(self.metric)[-1] else: result = hist.history.get('val_' + str(self.metric))[-1] return result def evaluate(self, x, y, metric=['mse']): """ Evaluate on x, y :param x: input :param y: target :param metric: a list of metrics in string format :return: a list of metric evaluation results """ y_pred = self.predict(x) # y = np.squeeze(y, axis=2) if self.target_col_num == 1: return [Evaluator.evaluate(m, y, y_pred) for m in metric] else: return [ np.array([ Evaluator.evaluate(m, y[:, i, :], y_pred[:, i, :]) for i in range(self.future_seq_len) ]) for m in metric ] def predict(self, x, mc=False): """ Prediction on x. :param x: input :return: predicted y (expected dimension = 2) """ y_pred = self._decode_sequence(x, mc=mc) if self.target_col_num == 1: y_pred = np.squeeze(y_pred, axis=2) return y_pred def predict_with_uncertainty(self, x, n_iter=100): result = np.array([self.predict(x, mc=True) for i in range(n_iter)]) prediction = result.mean(axis=0) uncertainty = result.var(axis=0) return prediction, uncertainty def save(self, model_path, config_path): """ save model to file. :param model_path: the model file path to be saved to. :param config_path: the config file path to be saved to. :return: """ self.model.save(model_path) config_to_save = { "past_seq_len": self.past_seq_len, "feature_num": self.feature_num, "future_seq_len": self.future_seq_len, "target_col_num": self.target_col_num, "metric": self.metric, "latent_dim": self.latent_dim, "batch_size": self.batch_size } save_config(config_path, config_to_save) def restore(self, model_path, **config): """ restore model from file :param model_path: the model file :param config: the trial config :return: the restored model """ self.past_seq_len = config["past_seq_len"] self.feature_num = config["feature_num"] self.future_seq_len = config["future_seq_len"] self.target_col_num = config["target_col_num"] self.metric = config["metric"] self.latent_dim = config["latent_dim"] self.batch_size = config["batch_size"] self.model = keras.models.load_model(model_path) self._restore_model() # self.model.load_weights(file_path) def _get_required_parameters(self): return { # 'input_shape_x', # 'input_shape_y', # 'out_units' } def _get_optional_parameters(self): return { 'past_seq_len' 'latent_dim' 'dropout', 'metric', 'lr', 'epochs', 'batch_size' }
# construct a dictionary for our target training outputs trainTargets = {"class_label": trainLabels, "bounding_box": trainBBoxes} # construct a second dictionary, this one for our target testing # outputs testTargets = {"class_label": testLabels, "bounding_box": testBBoxes} # train the network for bounding box regression and class label # prediction print("training model...") # H = load_model('../model_weights/loaded_model.h5') # H.compile(loss=losses, optimizer=opt, metrics=["accuracy"], loss_weights=lossWeights) H = model.fit(trainImages, trainTargets, validation_data=(testImages, testTargets), batch_size=Batch_Size, epochs=Num_Epoch, verbose=1) # serialize the model to disk print("saving object detector model...") model.save('../model_weights/new_basic_model_b' + str(Batch_Size) + '_e' + str(Num_Epoch) + '.h5', save_format="h5") # serialize the label binarizer to disk print("saving label...") f = open( '../model_weights/new_basic_model' + '_b' + str(Batch_Size) + '_e' + str(Num_Epoch) + '.pickle', "wb") f.write(pickle.dumps(lb))
fm = FM(field_name=['gender', 'age', 'occupation', 'zip', 'genres'], fields_count=[2, 5, 21, 18, 3359], embedding_size=16) x_input = Input(shape=(5, ), batch_size=1024, dtype='int32') output = fm(x_input) model = Model(inputs=x_input, outputs=output) model.compile(loss='sparse_categorical_crossentropy', optimizer=Adam(1e-2), metrics=['accuracy']) class Evaluate(Callback): def __init__(self): super().__init__() self.lowest = 1e10 def on_epoch_end(self, epoch, logs=None): # 保存最优 if logs['loss'] <= self.lowest: self.lowest = logs['loss'] model.save_weights('best_model.weights') print('save model success') call_back = Evaluate() model.fit(x, y, batch_size=1024, validation_split=0.1, epochs=20)
Y = np.array(Y) N = len(X) print(X.shape, Y.shape) #Build the model........................ i = Input(shape=(T, 1)) x = LSTM(5)(i) x = Dense(1)(x) model = Model(i, x) model.compile( loss='mse', optimizer=Adam(lr=0.1), ) r = model.fit( X[:-N // 2], Y[:-N // 2], epochs=80, validation_data=(X[-N // 2:], Y[-N // 2:]), ) #One step forecast.................. outputs = model.predict(X) print(outputs.shape) prediction = outputs[:, 0] plt.plot(Y, label='targets') plt.plot(prediction, label='prediciton') plt.legend() plt.show() #multi step forecast..................... validation_target = Y[-N // 2:]
if multimodal == 2: if 'coord' in args.input and 'lidar' in args.input: combined_model = concatenate([coord_model.output, lidar_model.output]) z = Dense(num_classes, activation="relu")(combined_model) model = Model(inputs=[coord_model.input, lidar_model.input], outputs=z) model.compile(loss=categorical_crossentropy, optimizer=opt, metrics=[ metrics.categorical_accuracy, metrics.top_k_categorical_accuracy, top_50_accuracy ]) model.summary() hist = model.fit( [X_coord_train, X_lidar_train], y_train, validation_data=([X_coord_validation, X_lidar_validation], y_validation), epochs=num_epochs, batch_size=batch_size) elif 'coord' in args.input and 'img' in args.input: combined_model = concatenate([coord_model.output, img_model.output]) z = Dense(num_classes, activation="relu")(combined_model) model = Model(inputs=[coord_model.input, img_model.input], outputs=z) model.compile(loss=categorical_crossentropy, optimizer=opt, metrics=[ metrics.categorical_accuracy, metrics.top_k_categorical_accuracy, top_50_accuracy ]) model.summary()
#모델 분기1 output1 = Dense(30)(middle1) output1 = Dense(7)(output1) output1 = Dense(1)(output1) #아웃풋 노드의 값을 3으로하면 y프레딕트값이 3개가 나옴 #모델 선언 model = Model( inputs=[input1, input2], #2개이상은 리스트로 묶는다.[] outputs=output1) #model.summary() #3.컴파일, 훈련 model.compile(loss='mse', optimizer='adam', metrics='mae') model.fit([x1, x2], y, epochs=10, verbose=1, batch_size=1) #4.평가, 예측 #.로스외의 평가지표 예측 loss = model.evaluate([x1, x2], y, batch_size=1) print(loss) ''' #keras15_2 # [1번째는 대표loss,2첫번재 모델loss, 3두번재모델loss, 4첫번째 모델metrics(mse), 5두번째 모델(mse)] # 1번째=2번째+3번째, 1번째=4번째+5번째 #metrics가 mae일 경우, 29,38이런식으로 나옴 #print("model.metrics_names:",model.metrics_names) #model.metrics_names: ['loss', 'dense_12_loss', 'dense_16_loss', 'dense_12_mse', 'dense_16_mse'] ''' #y 프레딕트값 예측
class KerasMultiInput(BaseEstimator, ClassifierMixin): DAYS_AHEAD = 1 LAGGED_DAYS = 0 VALID_DAYS = 15 def __enter__(self): return (self) def info(self, txt): if self.log: self.log.info(txt) else: print(txt) def debug(self, txt): if self.log: self.log.debug(txt) else: print(txt) def excption(self, ex): if self.log: self.log.exception(ex) else: print(ex) def __exit__(self, exc_type, exc_val, exc_tb): pass def best_weights(self, name=None): if not name: name = self.name return os.path.join(self.data_dir, name + '_weights.hdf5') def __init__(self, verbose=1, batch_size=128, epochs=200, lr=0.001, sequence_length=20, l2_reg=0.01, logger=None, ts_dim=1): self.log = logger self.current_directory = os.path.dirname(os.path.realpath(__file__)) self.output_directory = os.path.join(self.current_directory, 'data') self.D = ts_dim self.lr = lr self.name = 'keras_multiInput' self.data_dir = os.path.join(self.current_directory, 'data') self.batch_size = batch_size self.epochs = epochs self.verbose = verbose self.ts_scaler = MinMaxScaler(feature_range=(0, .9), copy=True) self.window_size = sequence_length self.l2_reg = l2_reg self.pickle_file = os.path.join(self.output_directory, 'ts.pickle') def find_categorical(self, X): # Get list of categorical column names self.categorical_columns = [ 'LCLid', 'Acorn', 'Acorn_grouped', "icon", "stdorToU", "Type", "day.of.week", 'precipType', 'summary', 'before_holiday', 'after_holiday', 'month', 'year' ] # Get list of non-categorical column names self.non_categorical_columns = list( filter(lambda x: x not in self.categorical_columns, X.columns)) self.sequence_columns = ['energy_sum'] self.label_column = 'energy_sum' def proces_categorical_columns(self, df): for v in self.categorical_columns: df[v] = df[v].astype('category').cat.as_ordered() for v in self.non_categorical_columns: df[v] = df[v].astype('float32') self.cat_sz = [(c, len(df[c].cat.categories) + 1) for c in self.categorical_columns] self.emb_szs = [(c, min(50, (c + 1) // 2)) for _, c in self.cat_sz] self.ts_scaler.fit( df[self.label_column].fillna(method='ffill').values.reshape(-1, 1)) def create_dataset(self, df): y = df[self.label_column] ahead = y.groupby(level=0).shift(-self.DAYS_AHEAD) df['Value'] = ahead.to_frame() df = df.dropna() self.proces_categorical_columns(df) df, y, self.nas, self.mapper = proc_df(df, y_fld='Value', do_scale=True) df = df[self.categorical_columns + self.non_categorical_columns] self.info(df.dtypes) self.df = df self.y = self.ts_scaler.fit_transform(y.reshape(-1, 1)) self.ts = self.window_transform_series(self.window_size, df.index) return (self.df, self.ts, self.y) def window_transform_series(self, window_size, index): v = self.df[self.label_column] # x values ends 1 before the end X = [] # Create window_size columns of shiffted x values for lclid, new_df in v.groupby(level=0): d = np.asarray([x[1] for x in new_df.index]) for id in d: r = new_df[lclid][:id].values[-window_size:] if (len(r) < window_size): s = np.zeros(window_size - len(r)) r = np.insert(r, 0, s, axis=0) X.append(r) # reshape each X = np.asarray(X) return X def load_pickled_data(self): self.X_train = pickle.load( open(os.path.join(self.output_directory, 'x_train_picle.pickle'), "rb")) self.X_valid = pickle.load( open(os.path.join(self.output_directory, 'x_valid_picle.pickle'), "rb")) self.ts_train = pickle.load( open(os.path.join(self.output_directory, 'ts_train_picle.pickle'), "rb")) self.ts_valid = pickle.load( open(os.path.join(self.output_directory, 'ts_valid_picle.pickle'), "rb")) self.y_train = pickle.load( open(os.path.join(self.output_directory, 'y_train_picle'), "rb")) self.y_valid = pickle.load( open(os.path.join(self.output_directory, 'y_valid_picle'), "rb")) return self.X_train, self.ts_train, self.y_train, self.X_valid, self.ts_valid, self.y_valid def separate_train_valid(self, X, ts, y): max_date = max(X.index)[1] valid_start_day = max_date - timedelta(days=KerasMultiInput.VALID_DAYS) train_idx = list(filter(lambda t: t[1] < valid_start_day, X.index)) valid_idx = list(filter(lambda t: t[1] >= valid_start_day, X.index)) ts = pd.DataFrame(ts, index=X.index) y_pd = pd.DataFrame(y, index=X.index) self.X_train = X.loc[train_idx] self.ts_train = ts.loc[train_idx].values.reshape( -1, self.window_size, self.D) self.y_train = y_pd.loc[train_idx].values.reshape(-1, 1) self.X_valid = X.loc[valid_idx] self.ts_valid = ts.loc[valid_idx].values.reshape( -1, self.window_size, self.D) self.y_valid = y_pd.loc[valid_idx].values.reshape(-1, 1) pickle.dump( self.X_train, open(os.path.join(self.output_directory, 'x_train_picle.pickle'), "wb")) pickle.dump( self.X_valid, open(os.path.join(self.output_directory, 'x_valid_picle.pickle'), "wb")) pickle.dump( self.ts_train, open(os.path.join(self.output_directory, 'ts_train_picle.pickle'), "wb")) pickle.dump( self.ts_valid, open(os.path.join(self.output_directory, 'ts_valid_picle.pickle'), "wb")) pickle.dump( self.y_train, open(os.path.join(self.output_directory, 'y_train_picle'), "wb")) pickle.dump( self.y_valid, open(os.path.join(self.output_directory, 'y_valid_picle'), "wb")) return self.X_train, self.ts_train, self.y_train, self.X_valid, self.ts_valid, self.y_valid def model_setup(self): cat_input = [ Input(shape=(1, ), dtype='int32', name=c) for c in self.categorical_columns ] seq_input = Input(shape=(None, 1), dtype='float32', name='rnn_input') all_layers = [] for i in range(len(cat_input)): emb = layers.Embedding(self.emb_szs[i][0], self.emb_szs[i][1])(cat_input[i]) flat = layers.Flatten()(emb) all_layers.append(flat) contInput = Input(shape=(len(self.non_categorical_columns), ), dtype='float32', name='continuouse') # seq_lay = layers.LSTM(32,return_sequences=True, activation='tanh', # kernel_regularizer=regularizers.l2(self.l2_reg))(seq_input) # seq_lay = layers.LSTM(64, return_sequences=True, activation='tanh', # kernel_regularizer=regularizers.l2(self.l2_reg))(seq_lay) seq_lay = layers.LSTM(16, return_sequences=False, activation='tanh', kernel_regularizer=regularizers.l2( self.l2_reg))(seq_input) #concatenated = layers.concatenate([categDense, continuousDense, seq_lay1], axis =-1) all_layers.append(contInput) all_layers.append(seq_lay) lay = layers.concatenate(all_layers, axis=-1) lay = BatchNormalization()(lay) # lay = Dense(64, kernel_regularizer=regularizers.l2(self.l2_reg))(lay) # lay = Dense(128, activation='tanh', kernel_regularizer=regularizers.l2(self.l2_reg))(lay) #lay = Dropout(0.5)(lay) #lay = Dense(128, activation='relu', kernel_regularizer=regularizers.l2(self.l2_reg))(lay) #lay = BatchNormalization()(lay) lay = Dense(64, activation='relu', kernel_regularizer=regularizers.l2(self.l2_reg))(lay) lay = BatchNormalization()(lay) #lay = Dense(64, activation='relu', kernel_regularizer=regularizers.l2(self.l2_reg))(lay) lay = Dense(4, activation='relu', kernel_regularizer=regularizers.l2(self.l2_reg))(lay) #lay = BatchNormalization()(lay) answer = layers.Dense(1, activation='sigmoid')(lay) inputs_all = cat_input inputs_all.append(contInput) inputs_all.append(seq_input) self.model = Model(inputs_all, answer) self.info(self.model.summary()) adam = Adam(lr=self.lr) self.model.compile(loss='binary_crossentropy', optimizer=adam, metrics=['mse', 'mae']) def load_model(self): self.model.load_weights(self.best_weights(self.name)) # learning rate schedul @staticmethod def step_decay(epoch): initial_lrate = 0.003 final_lrate = 1e-6 if epoch < 20: e = epoch if e < 10: lrate = final_lrate * np.exp( -e * (np.log(final_lrate) - np.log(initial_lrate)) / 10) else: e = e - 10 lrate = initial_lrate * np.exp( e * (np.log(final_lrate) - np.log(initial_lrate)) / 10) elif epoch < 40: e = epoch - 20 if e < 10: lrate = final_lrate * np.exp( -e * (np.log(final_lrate) - np.log(initial_lrate)) / 10) else: e = e - 10 lrate = initial_lrate * np.exp( e * (np.log(final_lrate) - np.log(initial_lrate)) / 10) elif epoch < 60: e = epoch - 40 if e < 10: lrate = final_lrate * np.exp( -e * (np.log(final_lrate) - np.log(initial_lrate)) / 10) else: e = e - 10 lrate = initial_lrate * np.exp( e * (np.log(final_lrate) - np.log(initial_lrate)) / 10) else: e = epoch - 60 if e < 15: lrate = final_lrate * np.exp( -e * (np.log(final_lrate) - np.log(initial_lrate)) / 15) else: e = e - 15 lrate = initial_lrate * np.exp( e * (np.log(final_lrate) - np.log(initial_lrate)) / 15) return lrate def fit_data(self, show_figures=True): checkpointer = ModelCheckpoint(filepath=self.best_weights(self.name), verbose=self.verbose, save_best_only=True) train_values = {c: self.X_train[c] for c in self.categorical_columns} train_values['continuouse'] = self.X_train[ self.non_categorical_columns] train_values['rnn_input'] = self.ts_train val_values = {c: self.X_valid[c] for c in self.categorical_columns} val_values['continuouse'] = self.X_valid[self.non_categorical_columns] val_values['rnn_input'] = self.ts_valid lrate = LearningRateScheduler(self.step_decay, verbose=2) history = self.model.fit( train_values, self.y_train, epochs=self.epochs, batch_size=self.batch_size, validation_data=(val_values, self.y_valid), verbose=self.verbose, shuffle=True, #validation_split=0.2, callbacks=[checkpointer, lrate]) if show_figures: fig, ax = plt.subplots(figsize=(10, 5)) # plot history ax.plot(history.history['loss'], label='train') ax.plot(history.history['val_loss'], label='test') ax.legend() figure_name = os.path.join(self.output_directory, self.name + "_history.png") plt.savefig(figure_name) plt.show() self.model.load_weights(self.best_weights(self.name)) p = self.model.evaluate(val_values, self.y_valid) self.info(f"Validation Score: {p}") def _meaning(self, x): # returns True/False according to fitted classifier # notice underscore on the beginning return (True) def unscale_y_value(self, y): return self.ts_scaler.inverse_transform(y) def predict(self, X, ts, y=None): try: test_values = {c: X[c] for c in self.categorical_columns} test_values['continuouse'] = X[self.non_categorical_columns] test_values['rnn_input'] = ts test_predict = self.model.predict(test_values) return test_predict except AttributeError: raise RuntimeError( "You must train classifer before predicting data!") return None def score(self, X, ts, y=None): # counts number of values bigger than mean test_values = {c: X[c] for c in self.categorical_columns} test_values['continuouse'] = X[self.non_categorical_columns] test_values['rnn_input'] = ts t = self.model.evaluate(x=test_values, y=y, batch_size=self.batch_size) self.log.info(t) return t
def create_model(log, output_folder, epochs, early_stop): from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint from tensorflow.keras.layers import Input, Embedding, Dropout, Concatenate, LSTM, Dense, BatchNormalization from tensorflow.keras.models import Model, load_model from tensorflow.keras.optimizers import Nadam vec = vectorization(log) vocab_act_size = len(log.values["event"]) + 1 vocab_role_size = len(log.values["role"]) + 1 # Create embeddings + Concat act_input = Input(shape=(vec['prefixes']['x_ac_inp'].shape[1],), name="act_input") role_input = Input(shape=(vec['prefixes']['x_rl_inp'].shape[1],), name="role_input") act_embedding = Embedding(vocab_act_size, 100, input_length=vec['prefixes']['x_ac_inp'].shape[1],)(act_input) act_dropout = Dropout(0.2)(act_embedding) act_e_lstm_1 = LSTM(32, return_sequences=True)(act_dropout) act_e_lstm_2 = LSTM(100, return_sequences=True)(act_e_lstm_1) role_embedding = Embedding(vocab_role_size, 100, input_length=vec['prefixes']['x_rl_inp'].shape[1],)(role_input) role_dropout = Dropout(0.2)(role_embedding) role_e_lstm_1 = LSTM(32, return_sequences=True)(role_dropout) role_e_lstm_2 = LSTM(100, return_sequences=True)(role_e_lstm_1) concat1 = Concatenate(axis=1)([act_e_lstm_2, role_e_lstm_2]) normal = BatchNormalization()(concat1) act_modulator = Modulator(attr_idx=0, num_attrs=1, time=log.k)(normal) role_modulator = Modulator(attr_idx=1, num_attrs=1, time=log.k)(normal) # Use LSTM to decode events act_d_lstm_1 = LSTM(100, return_sequences=True)(act_modulator) act_d_lstm_2 = LSTM(32, return_sequences=False)(act_d_lstm_1) role_d_lstm_1 = LSTM(100, return_sequences=True)(role_modulator) role_d_lstm_2 = LSTM(32, return_sequences=False)(role_d_lstm_1) act_output = Dense(vocab_act_size, name="act_output", activation='softmax')(act_d_lstm_2) role_output = Dense(vocab_role_size, name="role_output", activation="softmax")(role_d_lstm_2) model = Model(inputs=[act_input, role_input], outputs=[act_output, role_output]) opt = Nadam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=1e-08, schedule_decay=0.004, clipvalue=3) model.compile(loss={'act_output': 'categorical_crossentropy', 'role_output': 'categorical_crossentropy'}, optimizer=opt) model.summary() output_file_path = os.path.join(output_folder, 'model_{epoch:03d}-{val_loss:.2f}.h5') # Saving model_checkpoint = ModelCheckpoint(output_file_path, monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=False, mode='auto') early_stopping = EarlyStopping(monitor='val_loss', patience=early_stop) model.fit({'act_input':vec['prefixes']['x_ac_inp'], 'role_input':vec['prefixes']['x_rl_inp']}, {'act_output':vec['next_evt']['y_ac_inp'], 'role_output':vec['next_evt']['y_rl_inp']}, validation_split=0.2, verbose=2, batch_size=5, callbacks=[early_stopping, model_checkpoint], epochs=epochs) return model
h_layer = Flatten()(h_layer) h_layer = Dropout(0.4)(h_layer) h_layer = Dense(128, activation='relu', kernel_initializer=tf.zeros_initializer())(h_layer) h_layer = Dropout(0.4)(h_layer) o_layer = Dense(classes, activation='softmax')(h_layer) model = Model(i_layer, o_layer) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) report = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=1) """## ONES""" i_layer = Input(shape=input_shape) h_layer = Conv2D(64, (3, 3), strides=2, activation='relu', kernel_initializer=tf.ones_initializer())(i_layer) h_layer = Flatten()(h_layer) h_layer = Dropout(0.4)(h_layer) h_layer = Dense(128, activation='relu', kernel_initializer=tf.ones_initializer())(h_layer) h_layer = Dropout(0.4)(h_layer) o_layer = Dense(classes, activation='softmax')(h_layer)
for layer in model.layers: layer.trainable = True model.compile(Adam(lr = 0.001, decay=0.5), loss=triplet_loss) ############################## # 콜백 생성 ############################## log_dir=os.path.join('logs', 'fit', datetime.datetime.now().strftime("%Y%m%d-%H%M%S")) if not os.path.exists(log_dir): os.mkdir(log_dir) # 텐서보드 tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir) # 모델 저장 checkpoint = ModelCheckpoint('outputs/siamese.densenet169.h5', monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=False, mode='min') ############################## # 모델 학습 ############################## model.fit( x=train_dataset, steps_per_epoch=int(train_df.shape[0]/BATCH_SIZE), epochs=30, validation_data=valid_dataset, validation_steps=int(valid_df.shape[0]/BATCH_SIZE_VALID), workers=4, use_multiprocessing=True, verbose=1, callbacks=[tensorboard_callback, checkpoint])
y[0] # %% plt.figure() plt.imshow(x[1]) plt.colorbar() # %% x = x/255.0 x_test = x_test/255.0 # %% model.summary() # %% [markdown] ## Compile # %% model.compile(optimizer = 'adam',loss = 'sparse_categorical_crossentropy',metrics = ['accuracy']) #%% # x = np.expand_dims(x, -1) # %% model.fit(x=x, y=y, batch_size=64, epochs=10, verbose=1, steps_per_epoch=1) # %% # %%
encoder = LSTM(latent_dim, return_state=True) encoder_outputs, state_h, state_c = encoder(encoder_inputs) encoder_states = [state_h, state_c] decoder_inputs = Input(shape=(None, num_decoder_tokens)) decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True) decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state=encoder_states) decoder_dense = Dense(num_decoder_tokens, activation='softmax') decoder_outputs = decoder_dense(decoder_outputs) model = Model([encoder_inputs, decoder_inputs], decoder_outputs) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) model.fit([encoder_input_data, decoder_input_data], decoder_target_data, batch_size=batch_size, epochs=epochs, validation_split=0.2) model.save('s2s.h5') # buiding the inference models encoder_model = Model(encoder_inputs, encoder_states) decoder_state_input_h = Input(shape=(latent_dim,)) decoder_state_input_c = Input(shape=(latent_dim,)) decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c] decoder_outputs, state_h, state_c = decoder_lstm( decoder_inputs, initial_state=decoder_states_inputs) decoder_states = [state_h, state_c] decoder_outputs = decoder_dense(decoder_outputs) decoder_model = Model( [decoder_inputs] + decoder_states_inputs,
# loop over all layers in the base model and freeze them so they will # *not* be updated during the first training process for layer in baseModel.layers: layer.trainable = False # compile our model print("[INFO] compiling model...") opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS) model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the head of the network print("[INFO] training head...") H = model.fit(aug.flow(trainX, trainY, batch_size=BS), steps_per_epoch=len(trainX) // BS, validation_data=(testX, testY), validation_steps=len(testX) // BS, epochs=EPOCHS) # make predictions on the testing set print("[INFO] evaluating network...") predIdxs = model.predict(testX, batch_size=BS) # for each image in the testing set we need to find the index of the # label with corresponding largest predicted probability predIdxs = np.argmax(predIdxs, axis=1) # show a nicely formatted classification report print( classification_report(testY.argmax(axis=1), predIdxs,
class ConvNet(object): def __init__(self, img_rows, img_cols, n_classes, optimizer=Adam, loss='categorical_crossentropy', metrics=['acc'], learning_rate=3e-04): self.img_rows = img_rows self.img_cols = img_cols self.n_classes = n_classes self.optimizer = Adam(lr=learning_rate) self.loss = loss self.metrics = metrics self.model = None def build_model(self): input_layer = Input(shape=(self.img_rows, self.img_cols)) # add channel dimension (required by conv layers) if tf.keras.backend.image_data_format() == 'channels_last': latent = Reshape((self.img_rows, self.img_cols, 1))(input_layer) else: latent = Reshape((1, self.img_rows, self.img_cols))(input_layer) # define the network architecture latent = Conv2D(filters=32, kernel_size=(3, 3), activation='relu')(latent) latent = Conv2D(filters=64, kernel_size=(3, 3), activation='relu')(latent) latent = MaxPooling2D(pool_size=(2, 2))(latent) latent = Dropout(rate=0.25)(latent) latent = Flatten()(latent) latent = Dense(units=128, activation='relu')(latent) latent = Dropout(rate=0.5)(latent) output_layer = Dense(units=self.n_classes, activation='softmax')(latent) self.model = Model(inputs=input_layer, outputs=output_layer) self.model.compile(optimizer=self.optimizer, loss=self.loss, metrics=self.metrics) def maybe_train(self, data_train, data_valid, batch_size, epochs): DIR_ASSETS = 'assets/' PATH_MODEL = DIR_ASSETS + 'nn-model.hdf5' if os.path.exists(PATH_MODEL): print('Loading trained model from {}.'.format(PATH_MODEL)) self.model = load_model(PATH_MODEL) else: print('No checkpoint found on {}. Training from scratch.'.format( PATH_MODEL)) self.build_model() x_train, y_train = data_train self.model.fit(x_train, y_train, validation_data=data_valid, batch_size=batch_size, epochs=epochs) print('Saving trained model to {}.'.format(PATH_MODEL)) if not os.path.isdir(DIR_ASSETS): os.mkdir(DIR_ASSETS) self.model.save(PATH_MODEL) def evaluate(self, x, y): if self.model: score = self.model.evaluate(x, y) print('accuracy: {:.2f}% | loss: {}'.format( 100 * score[1], score[0])) else: print('Missing model instance.') def predict(self, x): if self.model: return self.model.predict(x, verbose=1) else: print('Missing model instance.')
x = Dropout(rate=0.25)(x) x = Conv2D(64, kernel_size=(4, 4), strides=2)(x) x = BatchNormalization(scale=False, beta_initializer=Constant(0.01))(x) x = Activation('relu')(x) x = Dropout(rate=0.25)(x) x = Flatten()(x) x = Dense(200)(x) x = BatchNormalization(scale=False, beta_initializer=Constant(0.01))(x) x = Activation('relu')(x) x = Dropout(rate=0.25)(x) predications = Dense(NUM_CLASSES, activation='softmax', name='output')(x) model = Model(inputs=inputs, outputs=predications) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.summary() lr_decay = lambda epoch: 0.0001 + 0.02 * math.pow(1.0 / math.e, epoch / 3.0) decay_callback = LearningRateScheduler(lr_decay, verbose=1) history = model.fit(x_train, y_train, batch_size=128, epochs=20, verbose=1, validation_data=(x_test, y_test), callbacks=[decay_callback]) model.save('mnist.h5') converter = tf.compat.v1.lite.TFLiteConverter.from_keras_model_file('mnist.h5') tflite_model = converter.convert() open('mnist_tf2_saveh5.tflite', 'wb').write(tflite_model)
class Agent(): """ Agent object which initalizes and trains the keras model. """ def __init__(self, actions, height=80, width=80, channels=1, discount=0.95, loss="huber", env="Breakout-v0", model_dir=None): """ Initializes the parameters of the model. Args: height: Height of the image width: Width of the image channels: Number of channels, history of past frame discount: Discount_Factor for Q Learning update """ self.height = height self.width = width self.channels = channels self.discount = discount self.actions = actions self.env = env self.loss = loss self.epoch_num = 0 self.model_dir = model_dir self.max_reward = 0 self.cur_reward = 0 self.reward_tensor = K.variable(value=0) if model_dir is not None: self.tbCallBack = TensorBoard( log_dir=model_dir, histogram_freq=0, write_graph=True, write_images=True) def create_model( self, lr, type="vanilla", rescale_value=255.0, ): """ Builds the DQN Agent architecture. Source:https://cs.corp.google.com/piper///depot/google3/third_party/py/ dopamine/agents/dqn/dqn_agent.py?q=DQN&dr=CSs&l=15 This initializes the model as per the specifications mentioned in the DQN paper by Deepmind. This is a sequential model implemention of tf.keras. The compiled model is returned by the Method. Args: Returns: Model: Compiled Model """ #with tf.device('/gpu:0'): self.image_frames = Input(shape=(self.height, self.width, self.channels)) #self.normalize = Lambda(lambda input: input/255.0) self.conv1 = Conv2D( filters=32, kernel_size=(8, 8), strides=(4, 4), activation="relu", name="conv1")( Lambda(lambda input: input / float(rescale_value))( self.image_frames)) self.conv2 = Conv2D( filters=64, kernel_size=(4, 4), strides=(2, 2), activation="relu", name="conv2")( self.conv1) self.conv3 = Conv2D( filters=64, kernel_size=(3, 3), strides=(1, 1), activation="relu", name="conv3")( self.conv2) self.flattened = Flatten(name="flattened")(self.conv3) self.fully_connected_1 = Dense( units=512, activation="relu", name="fully_connected_1")( self.flattened) self.q_values = Dense( units=self.actions, activation="linear", name="q_values")( self.fully_connected_1) self.model = Model(inputs=[self.image_frames], outputs=[self.q_values]) self.optimizer = Adam(lr=lr) if self.loss == "huber": self.loss = huber_loss K.get_session().run(tf.global_variables_initializer()) def reward(y_true, y_pred): return self.reward_tensor self.model.compile( optimizer=self.optimizer, loss=self.loss, metrics=["mse", reward]) return self.model def batch_train(self, curr_state, next_state, immediate_reward, action, done, target, type="Double"): """ Computes the TD Error for a given batch of tuples. Here, we randomly sample episodes from the Experience buffer and use this to train our model. This method computes this for a batch and trains the model. Args: curr_state(array): Numpy array representing an array of current states of game next_state(array): Numpy array for immediate next state of the game action(array): List of actions taken to go from current state to the next reward(array): List of rewards for the given transition done(bool): if this is a terminal state or not. target(keras.model object): Target network for computing TD error """ if type == "Double": forward_action = np.argmax(self.model.predict(next_state), axis=1) predicted_qvalue = target.predict(next_state) # BxN matrix B = forward_action.size forward_qvalue = predicted_qvalue[np.arange(B), forward_action] # Bx1 vec elif type == "Vanilla": forward_qvalue = np.max(target.predict(next_state), axis=1) discounted_reward = (self.discount * forward_qvalue * (1 - done)) Q_value = immediate_reward + discounted_reward target_values = self.model.predict(curr_state) target_values[range(target_values.shape[0]), action] = Q_value """ for i, target in enumerate(target_values): target_values[i, action[i]] = Q_value[i] """ callbacks = [] # Update epoch number for TensorBoard. K.set_value(self.reward_tensor, self.cur_reward) if self.model_dir is not None and self.epoch_num % TB_LOGGING_EPOCHS == 0: callbacks.append(self.tbCallBack) self.model.fit( curr_state, target_values, verbose=0, initial_epoch=self.epoch_num, callbacks=callbacks, epochs=self.epoch_num + 1) self.epoch_num += 1 def predict_action(self, state): """ Predict the action for a given state. Args: state(float): Numpy array Return: action(int): Discrete action to sample """ #state = downsample_state(convert_greyscale(state)) #state = np.expand_dims(state, axis=0) if np.ndim(state) == 3: state = np.expand_dims(state, axis=0) return np.argmax(self.model.predict(state)) def play(self, env, directory, mode): """ Returns the total reward for an episode of the game.""" steps = [] state = env.reset() done = False tot_reward = 0 actions = [0] * self.actions while not done: if mode != "Train": s = env.render("rgb_array") steps.append(s) action = self.predict_action(state) actions[action] += 1 state, reward, done, _ = env.step(action) tot_reward += reward self.cur_reward = tot_reward if mode != "Train" and tot_reward > self.max_reward: print("New high reward: ", tot_reward) clip = ImageSequenceClip(steps, fps=30) clip.write_gif("~/breakout.gif", fps=30) self.max_reward = tot_reward print("ACTIONS TAKEN", actions) return tot_reward
training_img = np.array(training_img) train_input_length = np.array(train_input_length) train_label_length = np.array(train_label_length) valid_img = np.array(valid_img) valid_input_length = np.array(valid_input_length) valid_label_length = np.array(valid_label_length) batch_size = 128 epochs = 10 model.load_weights('best_model_.hdf5', by_name=True) model.fit( x=[training_img, train_padded_txt, train_input_length, train_label_length], y=np.zeros(len(training_img)), batch_size=batch_size, epochs=epochs, validation_data=([ valid_img, valid_padded_txt, valid_input_length, valid_label_length ], [np.zeros(len(valid_img))]), verbose=1, callbacks=callbacks_list) # load the saved best model weights act_model.load_weights('crnn_model.hdf5') # original trained model act_model.load_weights( 'better_trained_model.h5' ) # better_trained_model on local gpu with cropped data from synthtext dataset # predict outputs on validation images #create a test dataset def prepare_image(path):