本文整理汇总了Python中tensorflow.keras.optimizers.Adam方法的典型用法代码示例。如果您正苦于以下问题:Python optimizers.Adam方法的具体用法?Python optimizers.Adam怎么用?Python optimizers.Adam使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.keras.optimizers
的用法示例。
在下文中一共展示了optimizers.Adam方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_dqn
# 需要导入模块: from tensorflow.keras import optimizers [as 别名]
# 或者: from tensorflow.keras.optimizers import Adam [as 别名]
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.)
示例2: test_double_dqn
# 需要导入模块: from tensorflow.keras import optimizers [as 别名]
# 或者: from tensorflow.keras.optimizers import Adam [as 别名]
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.)
示例3: test_duel_dqn
# 需要导入模块: from tensorflow.keras import optimizers [as 别名]
# 或者: from tensorflow.keras.optimizers import Adam [as 别名]
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.)
示例4: construct_q_network
# 需要导入模块: from tensorflow.keras import optimizers [as 别名]
# 或者: from tensorflow.keras.optimizers import Adam [as 别名]
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())
示例5: _build_q_NN
# 需要导入模块: from tensorflow.keras import optimizers [as 别名]
# 或者: from tensorflow.keras.optimizers import Adam [as 别名]
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
示例6: main
# 需要导入模块: from tensorflow.keras import optimizers [as 别名]
# 或者: from tensorflow.keras.optimizers import Adam [as 别名]
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)
示例7: main
# 需要导入模块: from tensorflow.keras import optimizers [as 别名]
# 或者: from tensorflow.keras.optimizers import Adam [as 别名]
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)
示例8: image_model
# 需要导入模块: from tensorflow.keras import optimizers [as 别名]
# 或者: from tensorflow.keras.optimizers import Adam [as 别名]
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
示例9: text_model
# 需要导入模块: from tensorflow.keras import optimizers [as 别名]
# 或者: from tensorflow.keras.optimizers import Adam [as 别名]
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
示例10: build_model
# 需要导入模块: from tensorflow.keras import optimizers [as 别名]
# 或者: from tensorflow.keras.optimizers import Adam [as 别名]
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()
示例11: __init__
# 需要导入模块: from tensorflow.keras import optimizers [as 别名]
# 或者: from tensorflow.keras.optimizers import Adam [as 别名]
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.
示例12: test_cdqn
# 需要导入模块: from tensorflow.keras import optimizers [as 别名]
# 或者: from tensorflow.keras.optimizers import Adam [as 别名]
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
示例13: test_ddpg
# 需要导入模块: from tensorflow.keras import optimizers [as 别名]
# 或者: from tensorflow.keras.optimizers import Adam [as 别名]
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
示例14: train
# 需要导入模块: from tensorflow.keras import optimizers [as 别名]
# 或者: from tensorflow.keras.optimizers import Adam [as 别名]
def train(
self,
mbatch_size=8,
max_epochs=20,
):
self.mbatch_size=mbatch_size
self.max_epochs=max_epochs
self.batch_size=100
self.model.compile(
sample_weight_mode="temporal",
loss="binary_crossentropy",
optimizer=Adam(lr=0.001, clipvalue=1.0)
)
train_dataset = self.dataset()
self.model.fit(
train_dataset,
epochs=max_epochs,
steps_per_epoch=math.ceil(self.batch_size/self.mbatch_size)
)
x_test, y_test, _ = list(train_dataset.take(1).as_numpy_iterator())[0]
y_hat = self.model.predict(x_test[0:1])
np.set_printoptions(precision=2, suppress=True)
print("Target:")
print(np.asarray(y_test[0,0:5,0:self.n_feat]))
print("Prediction:")
print(y_hat[0,0:5,0:self.n_feat])
示例15: _build_model_value
# 需要导入模块: from tensorflow.keras import optimizers [as 别名]
# 或者: from tensorflow.keras.optimizers import Adam [as 别名]
def _build_model_value(self):
input_states = Input(shape=(self.observation_size,))
lay1 = Dense(self.observation_size)(input_states)
lay1 = Activation('relu')(lay1)
lay3 = Dense(2 * self.action_size)(lay1)
lay3 = Activation('relu')(lay3)
advantage = Dense(self.action_size, activation='relu')(lay3)
state_value = Dense(1, activation='linear')(advantage)
model = Model(inputs=[input_states], outputs=[state_value])
model.compile(loss='mse', optimizer=Adam(lr=self.lr_))
return model