在创建NN时,我的耳朵非常绿。现在,我收到以下错误:
ValueError:检查时出错:预期density_1_input具有3 尺寸,但数组的形状为(8,8)
背景:我正在使用8x8电路板,这是初始化它的方式:
self.state = np.zeros((LENGTH, LENGTH))
这是构建模型的代码:
def build_model(self):
#builds the NN for Deep-Q Model
model = Sequential()
model.add(Dense(24,input_shape = (LENGTH, LENGTH), activation='relu'))
model.add(Flatten())
model.add(Dense(24, activation='relu'))
model.add(Dense(self.action_size, activation = 'linear'))
model.compile(loss='mse', optimizer='Adam')
return model
由于板的形状为(8,8),因此我认为input_size应该相同。不确定我在做什么错吗?
以防万一:
我制作的游戏非常简单,涉及5个棋盘:
玩家1的目标是到达棋盘的另一端 玩家2的目标是困住玩家1,使其无法移动
任何帮助将不胜感激!
答案 0 :(得分:0)
I managed to get it to run... but this is how: I changed input_shape to this
input_shape = (LENGTH, )
the problem is I have no idea why it accepts this? and if I am even doing it right?
No, the first input shape you have used (i.e. (LENGTH,LENGTH)
) was the correct one. Note that the input_shape
argument specifies the shape of one and only one single training sample, and not all the training data you have. For example, if you have 1000 boards of 8x8 then the training data has a shape of (1000, 8, 8)
but the input_shape
argument must be assigned (8,8)
, i.e. the shape of one training sample.
Further, as you may or may not know the Dense layer is applied on the last axis and since you have defined the input shape of Dense layer as (LENGTH,LENGTH)
, the Dense layer would be applied not on all of the input (i.e. board) but on the second axis (i.e. rows of the board). I guess this is not what you are looking for, so you have two options here: 1) you can move the Flatten layer to the top and put it as the first layer of the model:
model = Sequential()
model.add(Flatten(input_shape=(LENGTH, LENGTH)))
model.add(Dense(24, activation='relu'))
# the rest of the model
or 2) you can reshape the training data to make it have a shape of (num_boards, LENGTH*LENGTH)
and adjust the input_shape
argument accordingly (in this case there is no need for the Flatten layer you have in your model, you can remove it):
training_data = np.reshape(training_data, (num_boards, LENGTH*LENGTH))
model = Sequential()
model.add(Dense(24,input_shape=(LENGTH*LENGTH,), activation='relu'))
As a side note, if you have only one training/test sample (which is odd!) or whatever number of training/test samples, the first axis of the training/test data array must corresponds to samples, i.e. the shape of all the training/test data array must be (num_sample, ...)
. Otherwise, you would get errors complaining about shape, as you have gotten, when calling fit
/predict
/evaluate
methods. The same thing applies to the array that contains the training/test labels.