输入层的形状

时间:2018-08-26 09:26:33

标签: python machine-learning neural-network keras

在我的Keras模型中,我不知道应该输入什么作为输入层的形状。该模型可对1080个具有12288个样本的向量进行训练。

我有以下输入形状:

X_train shape: (12288, 1080)
Y_train shape: (6, 1080)
X_test shape: (12288, 120)
Y_test shape: (6, 120)
layers_dims =[12288,64,64,64,6]

并拥有NN:

def net_model(X_train, Y_train, X_test, Y_test, batch_size=32):
   model.add(keras.layers.InputLayer(input_shape=(layers_dims[0],)))
   for l in range(1,len(layers_dims)-2):
      model.add(keras.layers.Dense(layers_dims[l],activation=activation))
      if dropout:
          model.add(dropout(keep_prob[l]))
    model.add(keras.layers.Dense(layers_dims[-1],activation=keras.activations.softmax))
    model.compile(loss=keras.losses.categorical_crossentropy,optimizer=optimizer, metrics=[keras.metrics.categorical_accuracy])
    model.fit(X_train,Y_train,batch_size=batch_size, epochs=epochs)
    result_train = model.evaluate(X_train,Y_train)
    result_test = model.evaluate(X_test,Y_test)
    return result_train,result_test

result_train,result_test = net_model(X_train,Y_train,X_test,Y_test)

我遇到此错误:

ValueError: Error when checking input: expected input_10 to have shape (12288,) but got array with shape (1080,)

再次查看文档后,我尝试使用(1080,)作为输入形状,但这也不起作用。

ValueError: Error when checking target: expected dense_12 to have shape (6,) but got array with shape (1080,)

我想念什么?

1 个答案:

答案 0 :(得分:2)

训练数据和标签的形状应分别为(num_samples,num_features)(num_samples, num_labels)。因此,X的形状应为(1080, 12288),而不是(12288, 1080)。要解决此问题,请首先转置数组:

import numpy as np

X_train = np.transpose(X_train)
Y_train = np.transpose(Y_train)
X_test = np.transpose(X_test)
Y_test = np.transpose(Y_test)

输入层的输入形状应为(num_features,)(即(12288,))。