2

強化学習モデルをジム環境で予備的にトレーニングし、それを実際の環境に展開して、実際の環境で強化学習を続けたいと考えています。

初期トレーニングに TF、Keras RL + ジムを使用しています。コードは次のとおりです。それを管理する方法は何ですか? それのグーグルで失われた

私の推測では、実際の環境では 2 つのエージェントが必要で、1 つは予測用、もう 1 つはさらなるトレーニング用です。 トレーニング エージェントは、実行時に収集されたステート アクション サンプルに基づいて機能し、この新しいトレーニング済みモデルを予測モデルにマージする必要があります。それが正しい仮定である場合、どのように実装できますか?

import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.optimizers import Adam

# custom gym environment     
env = FooEnv()
env.seed(0)

states = env.observation_space.shape
actions = env.action_space.n

def build_model(states, actions):
    model = Sequential()
    model.add(Flatten(input_shape=(1,) + states))
    model.add(Dense(24, activation='relu'))
    model.add(Dense(24, activation='relu'))
    model.add(Dense(actions, activation='linear'))
    return model

from rl.agents import DQNAgent
from rl.callbacks import ModelIntervalCheckpoint, FileLogger
from rl.policy import LinearAnnealedPolicy, EpsGreedyQPolicy
from rl.memory import SequentialMemory

model = build_model(states, actions)
model.summary()

def build_agent(model, actions):
    policy = LinearAnnealedPolicy(EpsGreedyQPolicy(), attr='eps', value_max=1, value_min=0.1, value_test=0.05,
                                  nb_steps=500)
    memory = SequentialMemory(limit=10000, window_length=1)

    dqn = DQNAgent(model=model, memory=memory, policy=policy, enable_double_dqn=True,
                   nb_actions=actions, gamma=.98, nb_steps_warmup=100, target_model_update=1e-2)
    return dqn

def build_callbacks(env_name):
    checkpoint_weights_filename = 'weights/dqn_' + env_name + '_weights_{step}.h5f'
    log_filename = 'weights/dqn_{}_log.json'.format(env_name)
    callbacks = [ModelIntervalCheckpoint(checkpoint_weights_filename, interval=1000)]
    callbacks += [FileLogger(log_filename, interval=100)]
    return callbacks

callbacks = build_callbacks('FooEnv')

dqn = build_agent(model, actions)
dqn.compile(Adam(lr=1e-3), metrics=['mae'])
dqn.fit(env, nb_steps=30000, log_interval=1000, nb_max_episode_steps=50, visualize=False, verbose=1, callbacks=callbacks)

scores = dqn.test(env, nb_episodes=1, visualize=True)

dqn.save_weights('weights/saved_weights')
4

1 に答える 1