Keras输入形状和维度问题

我在使用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)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注