Python tensorflow.keras.optimizers.Adam() Examples
The following are 30
code examples of tensorflow.keras.optimizers.Adam().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow.keras.optimizers
, or try the search function
.
Example #1
Source File: test_continuous.py From keras-rl2 with MIT License | 6 votes |
def test_ddpg(): # TODO: replace this with a simpler environment where we can actually test if it finds a solution env = gym.make('Pendulum-v0') np.random.seed(123) env.seed(123) random.seed(123) nb_actions = env.action_space.shape[0] actor = Sequential() actor.add(Flatten(input_shape=(1,) + env.observation_space.shape)) actor.add(Dense(16)) actor.add(Activation('relu')) actor.add(Dense(nb_actions)) actor.add(Activation('linear')) action_input = Input(shape=(nb_actions,), name='action_input') observation_input = Input(shape=(1,) + env.observation_space.shape, name='observation_input') flattened_observation = Flatten()(observation_input) x = Concatenate()([action_input, flattened_observation]) x = Dense(16)(x) x = Activation('relu')(x) x = Dense(1)(x) x = Activation('linear')(x) critic = Model(inputs=[action_input, observation_input], outputs=x) memory = SequentialMemory(limit=1000, window_length=1) random_process = OrnsteinUhlenbeckProcess(theta=.15, mu=0., sigma=.3) agent = DDPGAgent(nb_actions=nb_actions, actor=actor, critic=critic, critic_action_input=action_input, memory=memory, nb_steps_warmup_critic=50, nb_steps_warmup_actor=50, random_process=random_process, gamma=.99, target_model_update=1e-3) agent.compile([Adam(lr=1e-3), Adam(lr=1e-3)]) agent.fit(env, nb_steps=400, visualize=False, verbose=0, nb_max_episode_steps=100) h = agent.test(env, nb_episodes=2, visualize=False, nb_max_episode_steps=100) # TODO: evaluate history
Example #2
Source File: iic-13.5.1.py From Advanced-Deep-Learning-with-Keras with MIT License | 6 votes |
def build_model(self): """Build the n_heads of the IIC model """ inputs = Input(shape=self.train_gen.input_shape, name='x') x = self.backbone(inputs) x = Flatten()(x) # number of output heads outputs = [] for i in range(self.args.heads): name = "z_head%d" % i outputs.append(Dense(self.n_labels, activation='softmax', name=name)(x)) self._model = Model(inputs, outputs, name='encoder') optimizer = Adam(lr=1e-3) self._model.compile(optimizer=optimizer, loss=self.mi_loss) self._model.summary()
Example #3
Source File: model_triplet.py From image_search_engine with MIT License | 6 votes |
def text_model(vocab_size, lr=0.0001): input_2 = Input(shape=(None,)) embed = Embedding(vocab_size, 50, name="embed") gru = Bidirectional(GRU(256, return_sequences=True), name="gru_1") dense_2 = Dense(vec_dim, activation="linear", name="dense_text_1") x2 = embed(input_2) x2 = gru(x2) x2 = GlobalMaxPool1D()(x2) x2 = dense_2(x2) _norm = Lambda(lambda x: K.l2_normalize(x, axis=-1)) x2 = _norm(x2) model = Model([input_2], x2) model.compile(loss="mae", optimizer=Adam(lr)) model.summary() return model
Example #4
Source File: model_triplet.py From image_search_engine with MIT License | 6 votes |
def image_model(lr=0.0001): input_1 = Input(shape=(None, None, 3)) base_model = ResNet50(weights='imagenet', include_top=False) x1 = base_model(input_1) x1 = GlobalMaxPool2D()(x1) dense_1 = Dense(vec_dim, activation="linear", name="dense_image_1") x1 = dense_1(x1) _norm = Lambda(lambda x: K.l2_normalize(x, axis=-1)) x1 = _norm(x1) model = Model([input_1], x1) model.compile(loss="mae", optimizer=Adam(lr)) model.summary() return model
Example #5
Source File: train.py From object-localization with MIT License | 6 votes |
def main(): model = create_model() train_datagen = DataGenerator(TRAIN_CSV) validation_datagen = Validation(generator=DataGenerator(VALIDATION_CSV)) optimizer = Adam(lr=1e-3, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False) model.compile(loss={"coords" : log_mse, "classes" : focal_loss()}, loss_weights={"coords" : 1, "classes" : 1}, optimizer=optimizer, metrics=[]) checkpoint = ModelCheckpoint("model-{val_iou:.2f}.h5", monitor="val_iou", verbose=1, save_best_only=True, save_weights_only=True, mode="max") stop = EarlyStopping(monitor="val_iou", patience=PATIENCE, mode="max") reduce_lr = ReduceLROnPlateau(monitor="val_iou", factor=0.2, patience=10, min_lr=1e-7, verbose=1, mode="max") model.summary() model.fit_generator(generator=train_datagen, epochs=EPOCHS, callbacks=[validation_datagen, checkpoint, reduce_lr, stop], workers=THREADS, use_multiprocessing=MULTI_PROCESSING, shuffle=True, verbose=1)
Example #6
Source File: train.py From object-localization with MIT License | 6 votes |
def main(): model = create_model(trainable=TRAINABLE) model.summary() if TRAINABLE: model.load_weights(WEIGHTS) train_datagen = DataGenerator(TRAIN_CSV) validation_datagen = Validation(generator=DataGenerator(VALIDATION_CSV)) optimizer = Adam(lr=1e-4, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False) model.compile(loss=loss, optimizer=optimizer, metrics=[]) checkpoint = ModelCheckpoint("model-{val_dice:.2f}.h5", monitor="val_dice", verbose=1, save_best_only=True, save_weights_only=True, mode="max") stop = EarlyStopping(monitor="val_dice", patience=PATIENCE, mode="max") reduce_lr = ReduceLROnPlateau(monitor="val_dice", factor=0.2, patience=5, min_lr=1e-6, verbose=1, mode="max") model.fit_generator(generator=train_datagen, epochs=EPOCHS, callbacks=[validation_datagen, checkpoint, reduce_lr, stop], workers=THREADS, use_multiprocessing=MULTI_PROCESSING, shuffle=True, verbose=1)
Example #7
Source File: ml_agent.py From Grid2Op with Mozilla Public License 2.0 | 6 votes |
def construct_q_network(self): # construct double Q networks self.model_Q = self._build_q_NN() self.model_Q2 = self._build_q_NN() # state value function approximation self.model_value = self._build_model_value() self.model_value_target = self._build_model_value() self.model_value_target.set_weights(self.model_value.get_weights()) # policy function approximation self.model_policy = Sequential() # proba of choosing action a depending on policy pi input_states = Input(shape = (self.observation_size,)) lay1 = Dense(self.observation_size)(input_states) lay1 = Activation('relu')(lay1) lay2 = Dense(self.observation_size)(lay1) lay2 = Activation('relu')(lay2) lay3 = Dense(2*self.action_size)(lay2) lay3 = Activation('relu')(lay3) soft_proba = Dense(self.action_size, activation="softmax", kernel_initializer='uniform')(lay3) self.model_policy = Model(inputs=[input_states], outputs=[soft_proba]) self.model_policy.compile(loss='categorical_crossentropy', optimizer=Adam(lr=self.lr_)) print("Successfully constructed networks.")
Example #8
Source File: ml_agent.py From Grid2Op with Mozilla Public License 2.0 | 6 votes |
def _build_q_NN(self): input_states = Input(shape=(self.observation_size,)) input_action = Input(shape=(self.action_size,)) input_layer = Concatenate()([input_states, input_action]) lay1 = Dense(self.observation_size)(input_layer) lay1 = Activation('relu')(lay1) lay2 = Dense(self.observation_size)(lay1) lay2 = Activation('relu')(lay2) lay3 = Dense(2*self.action_size)(lay2) lay3 = Activation('relu')(lay3) advantage = Dense(1, activation = 'linear')(lay3) model = Model(inputs=[input_states, input_action], outputs=[advantage]) model.compile(loss='mse', optimizer=Adam(lr=self.lr_)) return model
Example #9
Source File: trainer.py From nni with MIT License | 6 votes |
def __init__(self, model, loss, metrics, reward_function, optimizer, batch_size, num_epochs, dataset_train, dataset_valid): self.model = model self.loss = loss self.metrics = metrics self.reward_function = reward_function self.optimizer = optimizer self.batch_size = batch_size self.num_epochs = num_epochs x, y = dataset_train split = int(len(x) * 0.9) self.train_set = tf.data.Dataset.from_tensor_slices((x[:split], y[:split])) self.valid_set = tf.data.Dataset.from_tensor_slices((x[split:], y[split:])) self.test_set = tf.data.Dataset.from_tensor_slices(dataset_valid) self.mutator = EnasMutator(model) self.mutator_optim = Adam(learning_rate=mutator_lr) self.baseline = 0.
Example #10
Source File: ml_agent.py From Grid2Op with Mozilla Public License 2.0 | 6 votes |
def construct_q_network(self): # replacement of the Convolution layers by Dense layers, and change the size of the input space and output space # Uses the network architecture found in DeepMind paper self.model = Sequential() input_layer = Input(shape=(self.observation_size * self.training_param.NUM_FRAMES,)) layer1 = Dense(self.observation_size * self.training_param.NUM_FRAMES)(input_layer) layer1 = Activation('relu')(layer1) layer2 = Dense(self.observation_size)(layer1) layer2 = Activation('relu')(layer2) layer3 = Dense(self.observation_size)(layer2) layer3 = Activation('relu')(layer3) layer4 = Dense(2 * self.action_size)(layer3) layer4 = Activation('relu')(layer4) output = Dense(self.action_size)(layer4) self.model = Model(inputs=[input_layer], outputs=[output]) self.model.compile(loss='mse', optimizer=Adam(lr=self.lr_)) self.target_model = Model(inputs=[input_layer], outputs=[output]) self.target_model.compile(loss='mse', optimizer=Adam(lr=self.lr_)) self.target_model.set_weights(self.model.get_weights())
Example #11
Source File: test_discrete.py From keras-rl2 with MIT License | 6 votes |
def test_duel_dqn(): env = TwoRoundDeterministicRewardEnv() np.random.seed(123) env.seed(123) random.seed(123) nb_actions = env.action_space.n # Next, we build a very simple model. model = Sequential() model.add(Dense(16, input_shape=(1,))) model.add(Activation('relu')) model.add(Dense(nb_actions, activation='linear')) memory = SequentialMemory(limit=1000, window_length=1) policy = EpsGreedyQPolicy(eps=.1) dqn = DQNAgent(model=model, nb_actions=nb_actions, memory=memory, nb_steps_warmup=50, target_model_update=1e-1, policy=policy, enable_double_dqn=False, enable_dueling_network=True) dqn.compile(Adam(lr=1e-3)) dqn.fit(env, nb_steps=2000, visualize=False, verbose=0) policy.eps = 0. h = dqn.test(env, nb_episodes=20, visualize=False) assert_allclose(np.mean(h.history['episode_reward']), 3.)
Example #12
Source File: test_discrete.py From keras-rl2 with MIT License | 6 votes |
def test_double_dqn(): env = TwoRoundDeterministicRewardEnv() np.random.seed(123) env.seed(123) random.seed(123) nb_actions = env.action_space.n # Next, we build a very simple model. model = Sequential() model.add(Dense(16, input_shape=(1,))) model.add(Activation('relu')) model.add(Dense(nb_actions)) model.add(Activation('linear')) memory = SequentialMemory(limit=1000, window_length=1) policy = EpsGreedyQPolicy(eps=.1) dqn = DQNAgent(model=model, nb_actions=nb_actions, memory=memory, nb_steps_warmup=50, target_model_update=1e-1, policy=policy, enable_double_dqn=True) dqn.compile(Adam(lr=1e-3)) dqn.fit(env, nb_steps=2000, visualize=False, verbose=0) policy.eps = 0. h = dqn.test(env, nb_episodes=20, visualize=False) assert_allclose(np.mean(h.history['episode_reward']), 3.)
Example #13
Source File: test_discrete.py From keras-rl2 with MIT License | 6 votes |
def test_dqn(): env = TwoRoundDeterministicRewardEnv() np.random.seed(123) env.seed(123) random.seed(123) nb_actions = env.action_space.n # Next, we build a very simple model. model = Sequential() model.add(Dense(16, input_shape=(1,))) model.add(Activation('relu')) model.add(Dense(nb_actions)) model.add(Activation('linear')) memory = SequentialMemory(limit=1000, window_length=1) policy = EpsGreedyQPolicy(eps=.1) dqn = DQNAgent(model=model, nb_actions=nb_actions, memory=memory, nb_steps_warmup=50, target_model_update=1e-1, policy=policy, enable_double_dqn=False) dqn.compile(Adam(lr=1e-3)) dqn.fit(env, nb_steps=2000, visualize=False, verbose=0) policy.eps = 0. h = dqn.test(env, nb_episodes=20, visualize=False) assert_allclose(np.mean(h.history['episode_reward']), 3.)
Example #14
Source File: dqn_agent.py From Multi-Commander with Apache License 2.0 | 5 votes |
def _build_model(self): # Neural Net for Deep-Q learning Model # input:state; output:action value model = Sequential() model.add(Dense(256, input_dim=self.state_size, activation='relu')) model.add(Dense(128, activation='relu')) model.add(Dropout(0.3)) #model.add((LSTM(128)) model.add(Dense(self.action_size, activation='linear')) model.compile(loss='mse', optimizer=Adam(lr=self.learning_rate)) return model
Example #15
Source File: dqn_agent.py From Multi-Commander with Apache License 2.0 | 5 votes |
def _build_model(self): # Neural Net for Deep-Q learning Model # input:state; output:action value model = Sequential() model.add(Dense(256, input_dim=self.state_size, activation='relu')) model.add(Dense(128, activation='relu')) model.add(Dropout(0.3)) #model.add((LSTM(128)) model.add(Dense(self.action_size, activation='linear')) model.compile(loss='mse', optimizer=Adam(lr=self.learning_rate)) return model
Example #16
Source File: dqn.py From sumolights with GNU General Public License v3.0 | 5 votes |
def __init__(self, input_d, hidden_d, hidden_act, output_d, output_act, lr, lre, learner=False): super().__init__(input_d, hidden_d, hidden_act, output_d, output_act, learner=learner) for model in self.models: #self.models[model].compile(Adam(learning_rate=lr, epsilon=lre), loss='mse') self.models[model].compile(Adam(lr=lr, epsilon=lre), loss='mse')
Example #17
Source File: model.py From MIScnn with GNU General Public License v3.0 | 5 votes |
def __init__(self, preprocessor, architecture=Architecture(), loss=tversky_loss, metrics=[dice_soft], learninig_rate=0.0001, batch_queue_size=2, workers=1, gpu_number=1): # Identify data parameters self.three_dim = preprocessor.data_io.interface.three_dim self.channels = preprocessor.data_io.interface.channels self.classes = preprocessor.data_io.interface.classes # Assemble the input shape input_shape = (None,) # Initialize model for 3D data if self.three_dim: input_shape = (None, None, None, self.channels) self.model = architecture.create_model_3D(input_shape=input_shape, n_labels=self.classes) # Initialize model for 2D data else: input_shape = (None, None, self.channels) self.model = architecture.create_model_2D(input_shape=input_shape, n_labels=self.classes) # Transform to Keras multi GPU model if gpu_number > 1: self.model = multi_gpu_model(self.model, gpu_number) # Compile model self.model.compile(optimizer=Adam(lr=learninig_rate), loss=loss, metrics=metrics) # Cache starting weights self.initialization_weights = self.model.get_weights() # Cache parameter self.preprocessor = preprocessor self.loss = loss self.metrics = metrics self.learninig_rate = learninig_rate self.batch_queue_size = batch_queue_size self.workers = workers #---------------------------------------------# # Class variables # #---------------------------------------------#
Example #18
Source File: model.py From MIScnn with GNU General Public License v3.0 | 5 votes |
def load(self, file_path, custom_objects={}): # Create model input path self.model = load_model(file_path, custom_objects, compile=False) # Compile model self.model.compile(optimizer=Adam(lr=self.learninig_rate), loss=self.loss, metrics=self.metrics)
Example #19
Source File: mnist.py From nni with MIT License | 5 votes |
def main(params): """ Main program: - Build network - Prepare dataset - Train the model - Report accuracy to tuner """ model = MnistModel( conv_size=params['conv_size'], hidden_size=params['hidden_size'], dropout_rate=params['dropout_rate'] ) optimizer = Adam(learning_rate=params['learning_rate']) model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy']) _logger.info('Model built') (x_train, y_train), (x_test, y_test) = load_dataset() _logger.info('Dataset loaded') model.fit( x_train, y_train, batch_size=params['batch_size'], epochs=10, verbose=0, callbacks=[ReportIntermediates()], validation_data=(x_test, y_test) ) _logger.info('Training completed') loss, accuracy = model.evaluate(x_test, y_test, verbose=0) nni.report_final_result(accuracy) # send final accuracy to NNI tuner and web UI _logger.info('Final accuracy reported: %s', accuracy)
Example #20
Source File: lstm_base.py From asreview with Apache License 2.0 | 5 votes |
def _get_optimizer(optimizer, lr_mult=1.0): "Get optimizer with correct learning rate." if optimizer == "sgd": return optimizers.SGD(lr=0.01*lr_mult) elif optimizer == "rmsprop": return optimizers.RMSprop(lr=0.001*lr_mult) elif optimizer == "adagrad": return optimizers.Adagrad(lr=0.01*lr_mult) elif optimizer == "adam": return optimizers.Adam(lr=0.001*lr_mult) elif optimizer == "nadam": return optimizers.Nadam(lr=0.002*lr_mult) raise NotImplementedError
Example #21
Source File: segmenter_model.py From brainstorm with MIT License | 5 votes |
def compile_models(self, run_options=None, run_metadata=None): if not run_options is None and not run_metadata is None: self.segmenter_model.compile( loss=self.loss_fn, optimizer=Adam(lr=self.arch_params['lr'], amsgrad=True), options=run_options, run_metadata=run_metadata ) else: self.segmenter_model.compile( loss=self.loss_fn, optimizer=Adam(lr=self.arch_params['lr'], amsgrad=True), ) self.loss_names = [self.loss_name] super(SegmenterTrainer, self).compile_models()
Example #22
Source File: keras_siamese_model.py From DeepPavlov with Apache License 2.0 | 5 votes |
def compile(self) -> None: optimizer = Adam(lr=self.learning_rate) loss = losses.binary_crossentropy self.model.compile(loss=loss, optimizer=optimizer)
Example #23
Source File: bilstm_siamese_network.py From DeepPavlov with Apache License 2.0 | 5 votes |
def compile(self) -> None: optimizer = Adam(lr=self.learning_rate) if self.triplet_mode: loss = self._triplet_loss else: loss = losses.binary_crossentropy self.model.compile(loss=loss, optimizer=optimizer) self.score_model = self.create_score_model()
Example #24
Source File: model.py From deepcut with MIT License | 5 votes |
def get_convo_nn2(no_word=200, n_gram=21, no_char=178): input1 = Input(shape=(n_gram,)) input2 = Input(shape=(n_gram,)) a = Embedding(no_char, 32, input_length=n_gram)(input1) a = SpatialDropout1D(0.15)(a) a = BatchNormalization()(a) a_concat = [] for i in range(1,9): a_concat.append(conv_unit(a, n_gram, no_word, window=i)) for i in range(9,12): a_concat.append(conv_unit(a, n_gram, no_word - 50, window=i)) a_concat.append(conv_unit(a, n_gram, no_word - 100, window=12)) a_sum = Maximum()(a_concat) b = Embedding(12, 12, input_length=n_gram)(input2) b = SpatialDropout1D(0.15)(b) x = Concatenate(axis=-1)([a, a_sum, b]) #x = Concatenate(axis=-1)([a_sum, b]) x = BatchNormalization()(x) x = Flatten()(x) x = Dense(100, activation='relu')(x) out = Dense(1, activation='sigmoid')(x) model = Model(inputs=[input1, input2], outputs=out) model.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['acc']) return model
Example #25
Source File: model.py From keras-YOLOv3-model-set with MIT License | 5 votes |
def get_yolo2_train_model(model_type, anchors, num_classes, weights_path=None, freeze_level=1, optimizer=Adam(lr=1e-3, decay=1e-6), label_smoothing=0, model_pruning=False, pruning_end_step=10000): '''create the training model, for YOLOv2''' #K.clear_session() # get a new session num_anchors = len(anchors) # y_true in form of relative x, y, w, h, objectness, class y_true_input = Input(shape=(None, None, num_anchors, 6)) model_body, backbone_len = get_yolo2_model(model_type, num_anchors, num_classes, model_pruning=model_pruning, pruning_end_step=pruning_end_step) print('Create YOLOv2 {} model with {} anchors and {} classes.'.format(model_type, num_anchors, num_classes)) print('model layer number:', len(model_body.layers)) if weights_path: model_body.load_weights(weights_path, by_name=True)#, skip_mismatch=True) print('Load weights {}.'.format(weights_path)) if freeze_level in [1, 2]: # Freeze the backbone part or freeze all but final feature map & input layers. num = (backbone_len, len(model_body.layers)-2)[freeze_level-1] for i in range(num): model_body.layers[i].trainable = False print('Freeze the first {} layers of total {} layers.'.format(num, len(model_body.layers))) elif freeze_level == 0: # Unfreeze all layers. for i in range(len(model_body.layers)): model_body.layers[i].trainable= True print('Unfreeze all of the layers.') model_loss, location_loss, confidence_loss, class_loss = Lambda(yolo2_loss, name='yolo_loss', arguments={'anchors': anchors, 'num_classes': num_classes, 'label_smoothing': label_smoothing})( [model_body.output, y_true_input]) model = Model([model_body.input, y_true_input], model_loss) loss_dict = {'location_loss':location_loss, 'confidence_loss':confidence_loss, 'class_loss':class_loss} add_metrics(model, loss_dict) model.compile(optimizer=optimizer, loss={ # use custom yolo_loss Lambda layer. 'yolo_loss': lambda y_true, y_pred: y_pred}) return model
Example #26
Source File: model_utils.py From keras-YOLOv3-model-set with MIT License | 5 votes |
def get_optimizer(optim_type, learning_rate, decay_type='cosine', decay_steps=100000): optim_type = optim_type.lower() lr_scheduler = get_lr_scheduler(learning_rate, decay_type, decay_steps) if optim_type == 'adam': optimizer = Adam(learning_rate=lr_scheduler, amsgrad=False) elif optim_type == 'rmsprop': optimizer = RMSprop(learning_rate=lr_scheduler, rho=0.9, momentum=0.0, centered=False) elif optim_type == 'sgd': optimizer = SGD(learning_rate=lr_scheduler, momentum=0.0, nesterov=False) else: raise ValueError('Unsupported optimizer type') return optimizer
Example #27
Source File: train_imagenet.py From keras-YOLOv3-model-set with MIT License | 5 votes |
def get_optimizer(optim_type, learning_rate): if optim_type == 'sgd': optimizer = SGD(lr=learning_rate, decay=5e-4, momentum=0.9) elif optim_type == 'rmsprop': optimizer = RMSprop(lr=learning_rate) elif optim_type == 'adam': optimizer = Adam(lr=learning_rate, decay=5e-4) else: raise ValueError('Unsupported optimizer type') return optimizer
Example #28
Source File: train.py From TF.Keras-Commonly-used-models with Apache License 2.0 | 5 votes |
def train(): with open('config.json', 'r') as f: cfg = json.load(f) save_dir = cfg['save_dir'] shape = (int(cfg['height']), int(cfg['width']), 3) n_class = int(cfg['class_number']) batch = int(cfg['batch']) if not os.path.exists(save_dir): os.mkdir(save_dir) if cfg['model'] == 'large': from mobilenet_v3_large import MobileNetV3_Large model = MobileNetV3_Large(shape, n_class).build() if cfg['model'] == 'small': from mobilenet_v3_small import MobileNetV3_Small model = MobileNetV3_Small(shape, n_class).build() opt = Adam(lr=float(cfg['learning_rate'])) earlystop = EarlyStopping(monitor='val_acc', patience=5, verbose=0, mode='auto') model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) train_generator, validation_generator, count1, count2 = generate(batch, shape[:2], cfg['train_dir'], cfg['eval_dir']) hist = model.fit_generator( train_generator, validation_data=validation_generator, steps_per_epoch=count1 // batch, validation_steps=count2 // batch, epochs=cfg['epochs'], callbacks=[earlystop]) df = pd.DataFrame.from_dict(hist.history) df.to_csv(os.path.join(save_dir, 'hist.csv'), encoding='utf-8', index=False) model.save_weights(os.path.join(save_dir, '{}_weights.h5'.format(cfg['model'])))
Example #29
Source File: test_continuous.py From keras-rl2 with MIT License | 5 votes |
def test_cdqn(): # TODO: replace this with a simpler environment where we can actually test if it finds a solution env = gym.make('Pendulum-v0') np.random.seed(123) env.seed(123) random.seed(123) nb_actions = env.action_space.shape[0] V_model = Sequential() V_model.add(Flatten(input_shape=(1,) + env.observation_space.shape)) V_model.add(Dense(16)) V_model.add(Activation('relu')) V_model.add(Dense(1)) mu_model = Sequential() mu_model.add(Flatten(input_shape=(1,) + env.observation_space.shape)) mu_model.add(Dense(16)) mu_model.add(Activation('relu')) mu_model.add(Dense(nb_actions)) action_input = Input(shape=(nb_actions,), name='action_input') observation_input = Input(shape=(1,) + env.observation_space.shape, name='observation_input') x = Concatenate()([action_input, Flatten()(observation_input)]) x = Dense(16)(x) x = Activation('relu')(x) x = Dense(((nb_actions * nb_actions + nb_actions) // 2))(x) L_model = Model(inputs=[action_input, observation_input], outputs=x) memory = SequentialMemory(limit=1000, window_length=1) random_process = OrnsteinUhlenbeckProcess(theta=.15, mu=0., sigma=.3, size=nb_actions) agent = NAFAgent(nb_actions=nb_actions, V_model=V_model, L_model=L_model, mu_model=mu_model, memory=memory, nb_steps_warmup=50, random_process=random_process, gamma=.99, target_model_update=1e-3) agent.compile(Adam(lr=1e-3)) agent.fit(env, nb_steps=400, visualize=False, verbose=0, nb_max_episode_steps=100) h = agent.test(env, nb_episodes=2, visualize=False, nb_max_episode_steps=100) # TODO: evaluate history
Example #30
Source File: main.py From DEC-DA with MIT License | 5 votes |
def _get_data_and_model(args): # prepare dataset if args.method in ['FcDEC', 'FcIDEC', 'FcDEC-DA', 'FcIDEC-DA']: x, y = load_data(args.dataset) elif args.method in ['ConvDEC', 'ConvIDEC', 'ConvDEC-DA', 'ConvIDEC-DA']: x, y = load_data_conv(args.dataset) else: raise ValueError("Invalid value for method, which can only be in ['FcDEC', 'FcIDEC', 'ConvDEC', 'ConvIDEC', " "'FcDEC-DA', 'FcIDEC-DA', 'ConvDEC-DA', 'ConvIDEC-DA']") # prepare optimizer if args.optimizer in ['sgd', 'SGD']: optimizer = SGD(args.lr, 0.9) else: optimizer = Adam() # prepare the model n_clusters = len(np.unique(y)) if 'FcDEC' in args.method: model = FcDEC(dims=[x.shape[-1], 500, 500, 2000, 10], n_clusters=n_clusters) model.compile(optimizer=optimizer, loss='kld') elif 'FcIDEC' in args.method: model = FcIDEC(dims=[x.shape[-1], 500, 500, 2000, 10], n_clusters=n_clusters) model.compile(optimizer=optimizer, loss=['kld', 'mse'], loss_weights=[0.1, 1.0]) elif 'ConvDEC' in args.method: model = ConvDEC(input_shape=x.shape[1:], filters=[32, 64, 128, 10], n_clusters=n_clusters) model.compile(optimizer=optimizer, loss='kld') elif 'ConvIDEC' in args.method: model = ConvIDEC(input_shape=x.shape[1:], filters=[32, 64, 128, 10], n_clusters=n_clusters) model.compile(optimizer=optimizer, loss=['kld', 'mse'], loss_weights=[0.1, 1.0]) else: raise ValueError("Invalid value for method, which can only be in ['FcDEC', 'FcIDEC', 'ConvDEC', 'ConvIDEC', " "'FcDEC-DA', 'FcIDEC-DA', 'ConvDEC-DA', 'ConvIDEC-DA']") # if -DA method, we'll force aug_pretrain and aug_cluster is True if '-DA' in args.method: args.aug_pretrain = True args.aug_cluster = True return (x, y), model