我正在使用Keras做一些RL(我是火炬手,这是我第二次或第三次使用Keras),这是简化的代码
model=keras.models.Sequential([
keras.layers.Dense(10,activation='relu',input_shape=[4],name='layer1'),
keras.layers.Dense(4,activation='softmax',name='layer2'),
])
然后我称其为一些数据
obs=tf.convert_to_tensor([x1,y1,x2,y2],dtype=tf.float32)
pred=model(obs)
其中x1等是整数,但出现错误
WARNING:tensorflow:Model was constructed with shape Tensor("layer1_input:0", shape=(None, 4), dtype=float32) for input (None, 4), but it was re-called on a Tensor with incompatible shape (4,).
Traceback (most recent call last):
File "C:\Users\milok\ev_rl.py", line 131, in <module>
all_rewards,all_grads = play_multiple(env,n_episodes_per_update,n_max_steps,model,loss_fn)
File "C:\Users\milok\ev_rl.py", line 101, in play_multiple
obs,reward,grad = take_step(env,obs,model,loss_fn)
File "C:\Users\milok\ev_rl.py", line 81, in take_step
pred=model(obs.as_tensor())
File "C:\Users\milok\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 822, in __call__
outputs = self.call(cast_inputs, *args, **kwargs)
File "C:\Users\milok\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\sequential.py", line 267, in call
return super(Sequential, self).call(inputs, training=training, mask=mask)
File "C:\Users\milok\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 717, in call
convert_kwargs_to_constants=base_layer_utils.call_context().saving)
File "C:\Users\milok\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 891, in _run_internal_graph
output_tensors = layer(computed_tensors, **kwargs)
File "C:\Users\milok\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 822, in __call__
outputs = self.call(cast_inputs, *args, **kwargs)
File "C:\Users\milok\Anaconda3\lib\site-packages\tensorflow_core\python\keras\layers\core.py", line 1142, in call
outputs = gen_math_ops.mat_mul(inputs, self.kernel)
File "C:\Users\milok\Anaconda3\lib\site-packages\tensorflow_core\python\ops\gen_math_ops.py", line 5615, in mat_mul
_ops.raise_from_not_ok_status(e, name)
File "C:\Users\milok\Anaconda3\lib\site-packages\tensorflow_core\python\framework\ops.py", line 6606, in raise_from_not_ok_status
six.raise_from(core._status_to_exception(e.code, message), None)
File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: In[0] is not a matrix. Instead it has shape [4] [Op:MatMul]```
答案 0 :(得分:1)
在计算预测值时要小心管理批次尺寸...您必须将昏暗的对象(batch_size,n_feat)传递给模型
model=tf.keras.models.Sequential([
tf.keras.layers.Dense(10,activation='relu',input_shape=[4],name='layer1'),
tf.keras.layers.Dense(4,activation='softmax',name='layer2'),
])
### Error ###
obs=tf.constant([1,2,3,4],dtype=tf.float32)
pred=model(obs)
### OK ###
obs=tf.constant([[1,2,3,4]],dtype=tf.float32)
pred=model(obs)
答案 1 :(得分:1)
错误消息告诉您,您正在尝试在形状不兼容的张量上调用模型。
张量[x1,y1,x2,y2]
的形状为[4]
,但是在建立模型时,您使用了一个Dense
节点,该节点期望对象的形状为[batch, 4]
。
答案 2 :(得分:0)
obs
应该是形状为(None, 4)
的numpy数组/张量。
https://keras.io/guides/sequential_model/