我在使用Keras进行一些强化学习(我主要使用Torch,这是我第二次或第三次使用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_fromtensorflow.python.framework.errors_impl.InvalidArgumentError: In[0] is not a matrix. Instead it has shape [4] [Op:MatMul]```
回答:
在计算预测时要注意管理批次维度… 你必须传递给模型一个维度为(批次大小, 特征数)的对象
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)