Keras输入形状的定义-只能将元组而不是“ int”连接到元组

问题描述

在完成本教程CartPole Neural Network之后,我正在尝试创建自己的版本。在教程代码本身中,在定义状态numpay数组时,replay函数中存在python错误,其中batch_size为32,而state_size为4(帧)* 160(像素高度)+ 240(像素宽度):

 state = np.zeros((self.batch_size,) + self.state_size)

引发错误

can only concatenate tuple (not "int") to tuple

然后按照教程填充state变量:

for i in range(len(minibatch)): state[i] = minibatch[i][0]

这是我的困惑: 由于教程的初始声明state行不正确,因此此np.zeros变量的正确形状是什么?我尝试提供形状为(32,4,160,240)的numpy数组,即32个单独输入的数组。导致此错误

ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 153600 but received input with shape [32,240]

然后我想,我需要使用以下两行将其整形为一维数组,该数组的第零维为空:

state = np.reshape(state,(4 * 160 * 240 )) state = np.expand_dims(state,axis=0)

但是,这会给我带来错误

ValueError: cannot reshape array of size 4915200 into shape (153600,)

TLDR:此np.zeros((shape)+ int)调用的目的是什么,为什么对我来说失败,以及输入numpy数组的预期形状是什么,总共有32个批处理样本153600输入节点?

谢谢。

解决方法

np.zeros在您定义的大小上创建一个NumPy零数组。

您正在尝试将batch_size与state_size连接起来,但是您可以这样做:

(self.batch_size,) + self.state_size

尝试删除括号和逗号:

 state = np.zeros(self.batch_size + self.state_size)