为了加强学习,我想明确地
probabilities
我创建了一个具有简单策略网络的代理:
def simple_policy_model(self):
inputs = Input(shape=(self.state_size,), name="Input")
outputs = Dense(self.action_size, activation='softmax', name="Output")(inputs)
predict_model = Model(inputs=[inputs], outputs=[outputs])
return predict_model然后我试着得到梯度:
agent = REINFORCE_Agent(state_size=env.observation_space.shape[0],
action_size=env.action_space.n)
print(agent.predict_model.summary())
state_memory = np.random.uniform(size=(3,4))/10
#state_memory = tf.convert_to_tensor(state_memory)
print(state_memory)
print(agent.predict_model.predict(state_memory))
with tf.GradientTape() as tape:
probs = agent.predict_model.predict(state_memory)
### fails below ###
grads = tape.gradient(probs, agent.predict_model.trainable_weights)输出:
Model: "model_18"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
Input (InputLayer) (None, 4) 0
_________________________________________________________________
Output (Dense) (None, 2) 10
=================================================================
Total params: 10
Trainable params: 10
Non-trainable params: 0
_________________________________________________________________
None
state_memory [[0.01130021 0.01476066 0.09524527 0.05552276]
[0.02018996 0.03127809 0.07232339 0.07146596]
[0.08925738 0.08890574 0.04845396 0.0056015 ]]
prediction [[0.5127161 0.4872839 ]
[0.5063317 0.49366832]
[0.4817074 0.51829267]]
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
...
AttributeError: 'numpy.dtype' object has no attribute 'is_floating'如果我通过取消注释state_memory将convert_to_tensor转换为张量,那么它将在.predict()处失败:
ValueError: If your data is in the form of symbolic tensors, you should specify the `steps` argument (instead of the `batch_size` argument, because symbolic tensors are expected to produce batches of input data).看起来很简单,但却被困住了,知道什么是获得梯度的正确方法吗?
发布于 2020-01-09 20:40:17
问题是,
probs = agent.predict_model.predict(state_memory)
产生一个numpy张量作为输出。你不能得到梯度,w.r.t,numpy张量。相反,您需要来自您的模型的tf.Tensor。为此,请执行以下操作。
with tf.GradientTape() as tape:
probs = agent.predict_model(state_memory)
### fails below ###
grads = tape.gradient(probs, agent.predict_model.trainable_weights)https://stackoverflow.com/questions/59671612
复制相似问题