【发布时间】:2018-10-30 19:03:52
【问题描述】:
我正在尝试向我的神经网络传递一个形状与形状 (1169909, 10, 10) 的数组。
但无论我做什么......
input_shape=(None,10,10)
input_shape=x_train.shape[1:]
input_shape=x_train.shape
input_shape=(1169909, 1)
input_shape=(10,10)
input_shape=(1169909,10)
input_shape=(1169909,10,10)
input_shape=(1,10,10)
我仍然收到错误消息。错误变化:
but got array with shape (1169909, 10, 10)
but got array with shape (1169909, 1)
取决于我输入的方式,这只会增加我的困惑。
实际的输入如下所示,它是由这些较小的 10x10 数组组成的数组:
array([[ 2, -1, -1, -1, -1, -1, -1, -1, -1, 0],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[11, -1, 11, 6, 3, 2, -1, -1, -1, 11],
[-1, -1, -1, -1, 5, 7, -1, -1, 2, 7],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, 22, 25, -1, -1, -1, 22],
[22, -1, -1, -1, 26, 29, -1, -1, 26, 25],
[27, -1, -1, -1, -1, -1, -1, -1, -1, 23],
[31, 24, 31, -1, -1, -1, -1, -1, -1, 31]])
我试图通过查看堆栈溢出和其他地方来理解这个问题,但我无法找出问题所在,并且这些解决方案对我不起作用。
这是目前的模型:
x_train, x_test, y_train, y_test = train_test_split(xving, ywing, test_size=0.2)
x_train = numpy.array(x_train)
y_train = numpy.array(y_train)
model = Sequential()
model.add(keras.layers.Dense(250,activation='tanh', input_shape=(None,10,10)))
model.add(keras.layers.Dense(150,activation='relu'))
model.add(keras.layers.Dense(25,activation='sigmoid'))
model.add(keras.layers.Dense(2,activation='softmax'))
optimizerr = keras.optimizers.SGD(lr=0.01, momentum=0.0, decay=0.0, nesterov=False)
model.compile(optimizer=optimizerr, loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train,epochs = 25, batch_size = 32, verbose=1)
编辑: 所以当我使用时:
input_shape=x_train.shape[1:]
错误变成了目标:
Error when checking target: expected dense_32 to have 3 dimensions, but got array with shape (1169909, 1)
但是目标是一个数组。当我将其保留为列表时,我得到了错误:
ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 1169909 arrays: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
所以现在我想我的问题是为什么目标是一个问题。
y_train.shape
产量:
(1169909,)
所以我还是一头雾水?
【问题讨论】:
-
你能显示其余的代码吗?至少第一层。
-
我添加了模型本身和它前面的位
-
至于目标错误:Dense layer is applied on the last axis,因此形状不匹配。
-
当我将最后一层更改为 model.add(keras.layers.Dense(1,activation='softmax')) (将 2 更改为 1)我仍然得到同样的错误?我希望它仍然给出两个类的概率,所以我选择了 2。即使我使用 np_utils.to_categorical(y_train) 它也会失败。
-
是的,您必须将最后一层的单元数更改为1。但是,正如我在之前的评论中提到的,另一个问题是模型的输出形状为
(None, 10, 1)并且您的标签的形状为(1169909,1),因此它们不匹配。解决此问题的一种方法是在模型的某处添加一个 Flatten 层(例如作为第一层)。或者,您可以将训练数据重新整形为(num_samples, 10*10),并相应地更改模型第一层的input_shape。
标签: python neural-network keras keras-layer