- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在尝试制作一个 RL 代理几天后,我终于成功地创造了它的体验,但是当我尝试训练它时,我得到了这个错误。我已经尽我所能:不同的经验,改变了步骤参数......我只是没有想法。
import pyxinput
import time
import cv2
from PIL import ImageGrab
import numpy as np
import keyboard
import tensorflow
import tf_agents
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import torch
#from tf_agents.networks import actor_distribution_networ
from tf_agents.policies import random_py_policy
Tensod_spec = tf_agents.specs.BoundedArraySpec(
(15,),
dtype=np.float32,
name="XimputSpecs",
minimum=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
maximum=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
)
Tensod_spec2 = tf_agents.specs.TensorSpec(
[440, 600, 1], dtype=tf.int32, name="ScreenSpecs"
)
Tensor_reward_spe = tf_agents.specs.TensorSpec(
[1, 1], dtype=tf.int32, name="Reward"
)
FromEnv = tf_agents.specs.BoundedTensorSpec(
shape=(440, 600, 1),
dtype='uint8',
name='observation',
minimum=0,
maximum=255
)
FromEnv2 = tf_agents.specs.BoundedTensorSpec(
shape=(1, 440, 600, 1),
dtype=tf.int32,
name='observation',
minimum=0,
maximum=255
)
fullscreen = [110, 130, 710, 570]
screenpil = ImageGrab.grab(bbox=fullscreen)
showprint = np.array(screenpil)
grayscreen = cv2.cvtColor(showprint, cv2.COLOR_BGR2GRAY)
screenrect = cv2.cvtColor(grayscreen, cv2.COLOR_GRAY2BGR)
grayscreen = grayscreen.reshape(440, 600, 1)
time_step_spec2 = tf_agents.trajectories.time_step.time_step_spec(
observation_spec=FromEnv,
#reward_spec = Tensor_reward_spec
)
time_step_spec = tf_agents.trajectories.time_step.time_step_spec(
observation_spec=FromEnv,
#reward_spec = Tensor_reward_spec
)
actor_net = tf_agents.networks.actor_distribution_network.ActorDistributionNetwork(
input_tensor_spec=FromEnv,
output_tensor_spec=tf_agents.specs.tensor_spec.from_spec(Tensod_spec),
activation_fn='relu',
#conv_layer_params=[(25, 40, 2)],
fc_layer_params=(50, 25, 15),
#dtype='int32'
)
print(actor_net)
train_step_counter = tf.dtypes.cast(1, tf.int32)
optimizer = tf.keras.optimizers.Adam(learning_rate=0.003)
tf_agent = tf_agents.agents.ReinforceAgent(
time_step_spec=time_step_spec,
action_spec=tf_agents.specs.tensor_spec.from_spec(Tensod_spec),
actor_network=actor_net,
optimizer=optimizer,
normalize_returns=True,
#train_step_counter=tf.Variable(1, name="global_step")
)
tf_agent.initialize()
grayscreen2 = grayscreen
grayscreen2 = grayscreen2.reshape(1, 440, 600, 1)
time_step2 = tf_agents.trajectories.time_step.TimeStep(
step_type=tf_agents.trajectories.time_step.StepType.FIRST,
reward=tf.dtypes.cast(1, tf.float32),
discount=tf.dtypes.cast(1, tf.float32),
observation=grayscreen2
)
policy_state = tf_agent.policy.get_initial_state(batch_size=1)
policy_step = tf_agent.policy.action(time_step2, policy_state)
print(policy_step)
observe = time_step2.observation
#print(observe.dtype)
#observe = observe.astype(int)
#print(observe.shape)
experience = tf_agents.trajectories.trajectory.Trajectory(
action=tf.compat.v2.Variable([
tf.compat.v2.Variable(policy_step.action),
tf.compat.v2.Variable(policy_step.action),
tf.compat.v2.Variable(policy_step.action)
]),
reward=tf.compat.v2.Variable([[
tf.compat.v2.Variable(time_step2.reward),
tf.compat.v2.Variable(time_step2.reward),
tf.compat.v2.Variable(time_step2.reward)
]]),
step_type=tf.compat.v2.Variable([[
tf.compat.v2.Variable(tf_agents.trajectories.time_step.StepType.FIRST),
tf.compat.v2.Variable(tf_agents.trajectories.time_step.StepType.MID),
tf.compat.v2.Variable(tf_agents.trajectories.time_step.StepType.LAST)
]]),
observation=tf.compat.v2.Variable([
tf.compat.v2.Variable(observe),
tf.compat.v2.Variable(observe),
tf.compat.v2.Variable(observe)
]),
policy_info=tf_agent.policy.info_spec,
next_step_type=tf.compat.v2.Variable([[
tf.compat.v2.Variable(tf_agents.trajectories.time_step.StepType.MID),
tf.compat.v2.Variable(tf_agents.trajectories.time_step.StepType.LAST),
tf.compat.v2.Variable(tf_agents.trajectories.time_step.StepType.LAST)
]]),
discount=tf.compat.v2.Variable([[
tf.dtypes.cast(1, tf.float32),
tf.dtypes.cast(1, tf.float32),
tf.dtypes.cast(1, tf.float32)
]]),
)
train_loss = tf_agent.train(experience)
print(train_loss)
我得到这个错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-15-4dd3966a32b6> in <module>
1 #
----> 2 train_loss = tf_agent.train(experience)
3 print(train_loss)
~\AppData\Local\Programs\Python\Python38\lib\site-packages\tf_agents\agents\tf_agent.py in train(self, experience, weights, **kwargs)
516
517 if self._enable_functions:
--> 518 loss_info = self._train_fn(
519 experience=experience, weights=weights, **kwargs)
520 else:
~\AppData\Local\Programs\Python\Python38\lib\site-packages\tf_agents\utils\common.py in with_check_resource_vars(*fn_args, **fn_kwargs)
183 # We're either in eager mode or in tf.function mode (no in-between); so
184 # autodep-like behavior is already expected of fn.
--> 185 return fn(*fn_args, **fn_kwargs)
186 if not resource_variables_enabled():
187 raise RuntimeError(MISSING_RESOURCE_VARIABLES_ERROR)
~\AppData\Local\Programs\Python\Python38\lib\site-packages\tf_agents\agents\reinforce\reinforce_agent.py in _train(self, experience, weights)
286 self.train_step_counter)
287
--> 288 self._optimizer.apply_gradients(
289 grads_and_vars, global_step=0)
290
TypeError: apply_gradients() got an unexpected keyword argument 'global_step'
这个全局步骤是什么,这个错误来自哪里?为什么我不能训练我的代理人?
最佳答案
您应该尝试使用不同的优化器。那些在tf.keras.optimizer
不要拿global_steps
作为 apply_gradients
中的参数功能。
相反,使用这些来自 tf.compat.v1.train
,例如,
optimizer = tf.compat.v1.train.AdamOptimizer(learn_rate=0.003)
请注意,这通过了运行时检查,但它使训练无法完成。
global_step
应该带一个
Variable
它的值将是
+1
当
apply_gradients
叫做。但是,在这里您会看到
global_step=0
通过使其根本没有效果。
train_step_counter
您在上面定义的将保留
0
.
关于python - 类型错误:apply_gradients() 得到一个意外的关键字参数 'global_step',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67014270/
在尝试制作一个 RL 代理几天后,我终于成功地创造了它的体验,但是当我尝试训练它时,我得到了这个错误。我已经尽我所能:不同的经验,改变了步骤参数......我只是没有想法。 import pyxinp
我使用 Tensorflow 的 compute_gradients() 和 apply_gradients() 函数进行反向传播。通过打印梯度值,我确实看到梯度正在计算,但在调用 apply_gra
我有以下几行作为程序的一部分: tensor_gradients = optimizer.compute_gradients(cross_entropy) with tf.Session() as s
文档对此不太清楚。我认为通过 opt.compute_gradients(E, [v]) 可以获得的梯度包含每个元素的 ∂E/∂x = g(x) x 是 v 存储的张量。 opt.apply_grad
问题:在 TensorFlow 网络中以最有效的方式获得权重增量的最有效方法是什么? 背景 :我已经按如下方式连接了运算符(operator)(感谢这个 SO question ): self.cos
我对 tensorflow 中优化器的apply_gradients和minimize之间的差异感到困惑。例如, optimizer = tf.train.AdamOptimizer(1e-3) gr
我想使用 AdamOptimizer,但我也想每一步编辑我的渐变。 典型用法如下: train_step = tf.train.AdamOptimizer(learning_rate).minimiz
我在 TensorFlow 2.0 中有一个 UNet 实现。该代码运行良好,没有任何错误,但 custom_loss 的值保持不变。 首先,保存模型的 UNet 类, class UNet( obj
我正在尝试使用 MNIST 数据集构建一个简单的全连接单隐藏层神经网络。 for epoch in range(training_epoch): for step in range(total
我是一名优秀的程序员,十分优秀!