简体   繁体   English

如何将 keras 张量转换为 numpy 数组

[英]How to convert a keras tensor to a numpy array

I am trying to create a q-learning chess engine where the output of the last layer of the neural network (the density is equal to the number of legal moves) is run through a argmax() function which returns an integer that I am using as an index for the array where the legal moves are stored. I am trying to create a q-learning chess engine where the output of the last layer of the neural network (the density is equal to the number of legal moves) is run through a argmax() function which returns an integer that I am using作为存储合法移动的数组的索引。 Here is part of my code:这是我的代码的一部分:

#imports

env = gym.make('ChessAlphaZero-v0')   #builds environment
obs = env.reset()
type(obs)

done = False   #game is not won

num_actions = len(env.legal_moves)   #array where legal moves are stored

obs = chess.Board() 

model = models.Sequential()

def dqn(board):
    
    #dense layers
    
    action = layers.Dense(num_actions)(layer5)
    
    i = np.argmax(action)
    move = env.legal_moves[i]

    return keras.Model(inputs=inputs, outputs=move)

But when I run the code I get the following error:但是当我运行代码时,出现以下错误:

TypeError: Cannot convert a symbolic Keras input/output to a numpy array. This error may indicate that you're trying to pass a symbolic value to a NumPy call, which is not supported. Or, you may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model.

Any code examples would be appreciated, thanks.任何代码示例将不胜感激,谢谢。

The correct way to build a model and forward an input in keras is this:构建 model 并在 keras 中转发输入的正确方法是:

1. Building the model 1. 构建 model

model = models.Sequential()
model.add(layers.Input(observation_shape))
model.add(layers.Dense(units=128, activation='relu'))
model.add(layers.Dense(units=num_actions, activation='softmax'))
return model

or或者

inputs = layers.Input(observation_shape)
x = layers.Dense(units=128, activation='relu')(inputs)
outputs = layers.Dense(units=num_actions, activation='softmax')(x)

model = keras.Model(inputs, output)

Both ways are equal.两种方式都是平等的。

2. Forward an observation & Get the best possible action 2. 转发观察并采取最佳行动

action_values = model.predict(observation)
best_action_index = tf.argmax(action_values)
best_action = action_values[best_action_index]

Implementing DQN by yourself in keras can be quite frustrating.在 keras 中自己实现 DQN 可能会非常令人沮丧。 You might wanna use a DRL framework such as tf_agents that has implementations of lots of agents: https://www.tensorflow.org/agents您可能想使用一个 DRL 框架,例如tf_agents ,它实现了许多代理: https://www.tensorflow.org/agents

This repository contains a clean and easy to understand implementation of DQN for openai gym environments.此存储库包含用于 openai 健身房环境的干净且易于理解的 DQN 实现。 Also, it contains examples of using tf_agents library as well for more complex agents: https://github.com/kochlisGit/Tensorflow-DQN此外,它还包含使用 tf_agents 库以及更复杂代理的示例: https://github.com/kochlisGit/Tensorflow-DQN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM