[英]Keras Input Shape and Dimension Issues
I'm doing some RL using Keras (I'm a Torch guy and this is my 2nd or 3rd time using Keras), here is the simplified code我正在使用 Keras 做一些强化学习(我是火炬手,这是我第二次或第三次使用 Keras),这是简化的代码
model=keras.models.Sequential([
keras.layers.Dense(10,activation='relu',input_shape=[4],name='layer1'),
keras.layers.Dense(4,activation='softmax',name='layer2'),
])
then I call it on some data然后我在一些数据上调用它
obs=tf.convert_to_tensor([x1,y1,x2,y2],dtype=tf.float32)
pred=model(obs)
where x1 etc are integers and I get the error其中 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]```
take care to manage the batch dimension when you compute the predictions... you have to pass to your model an object of dim (batch_size, n_feat)在计算预测时注意管理批次维度...您必须将暗淡的 model 传递给 object (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)
The error message is telling you that you are trying to call your model on a tensor with incompatible shape.错误消息告诉您您正在尝试在形状不兼容的张量上调用 model。
The tensor [x1,y1,x2,y2]
has shape [4]
, but when you set up the model you used a Dense
node that expects objects of shape [batch, 4]
.张量
[x1,y1,x2,y2]
的形状为[4]
,但是当您设置 model 时,您使用了一个期望形状为[batch, 4]
的对象的Dense
节点。
obs
should be a numpy array/tensor with shape (None, 4)
. obs
应该是形状为(None, 4)
的 numpy 数组/张量。 https://keras.io/guides/sequential_model/ https://keras.io/guides/sequential_model/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.