Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need help with the method of connecting self RL agent to showdown #378

Open
AegeanYan opened this issue May 26, 2023 · 1 comment
Open

Comments

@AegeanYan
Copy link

Following the tutorial, I've trained my agent in DQN(in fact I modified it to D3QN by adding parameter enable_dueling_network=True).
Then I got the model.summary():


Layer (type) Output Shape Param #

dense_input (InputLayer) [(None, 1, 31)] 0


dense (Dense) (None, 1, 256) 8192


flatten (Flatten) (None, 256) 0


dense_1 (Dense) (None, 64) 16448


dense_3 (Dense) (None, 15) 975


lambda (Lambda) (None, 14) 0

Total params: 25,615
Trainable params: 25,615
Non-trainable params: 0

Here is my code:

import asyncio
import numpy as np
from poke_env.player import RandomPlayer,SimpleHeuristicsPlayer, Player
from poke_env import PlayerConfiguration, ShowdownServerConfiguration
from rl.memory import SequentialMemory
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
import tensorflow as tf
from rl.agents.dqn import DQNAgent
from rl.policy import LinearAnnealedPolicy, EpsGreedyQPolicy
from newbie import SimpleRLPlayer

class TrainedRLPlayer(Player):
  def __init__(self, model, *args, **kwargs):
    Player.__init__(self, *args, **kwargs)
    self.model = model

  def choose_move(self, battle):
    state = SimpleRLPlayer.embed_battle(None,battle)
    predictions = self.model.predict([state.reshape(1,1,31)],batch_size=1, verbose=1)[0]
    action = np.argmax(predictions)
    return SimpleRLPlayer._action_to_move(None,action)

async def main():
    model = Sequential()
    model.add(Dense(256, activation="elu", input_shape=(1,31)))
    model.add(Flatten())
    model.add(Dense(64, activation="elu"))
    model.add(Dense(14, activation="linear"))
    policy = EpsGreedyQPolicy(eps=0.1)
    memory = SequentialMemory(limit=10000, window_length=1)
    dqn = DQNAgent(
        model=model,
        nb_actions=14,
        policy=policy,
        memory=memory,
        nb_steps_warmup=1000,
        gamma=0.95,
        target_model_update=1,
        delta_clip=0.01,
        enable_double_dqn=True,
        enable_dueling_network=True,
    )
    dqn.compile(Adam(learning_rate=0.00025), metrics=["mae"])
    dqn.load_weights("")#in tutorial you can add dqn.save_weights("xx") to get
    model = dqn.model
    model._make_predict_function()

    player = TrainedRLPlayer(
        model = model,
        player_configuration=PlayerConfiguration("bot", "bot"),
        server_configuration=ShowdownServerConfiguration,
    )

    # Accepting one challenge from any user
    await player.accept_challenges(None, 1)

if __name__ == "__main__":
    asyncio.get_event_loop().run_until_complete(main())

I've modified the embed and action so there maybe a little difference in net. And notice that the last two layers are changed because of dueling.
The problem is that I cannot go through the line of model.predict. I'm not familiar with concepts of keras. Could anyone help me to debug it out? So that then we have a good example code to connect to PS!

@AegeanYan
Copy link
Author

The error info is : ValueError: Tensor Tensor("lambda/sub:0", shape=(None, 14), dtype=float32) is not an element of this graph.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant