python variable shape 不匹配_Python:keras形状不匹配错误(Python: keras shape mismatch error)...

Python:keras形状不匹配错误(Python: keras shape mismatch error)

我正在尝试在keras构建一个非常简单的多层感知器(MLP):

model = Sequential()

model.add(Dense(16, 8, init='uniform', activation='tanh'))

model.add(Dropout(0.5))

model.add(Dense(8, 2, init='uniform', activation='tanh'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)

model.compile(loss='mean_squared_error', optimizer=sgd)

model.fit(X_train, y_train, nb_epoch=1000, batch_size=50)

score = model.evaluate(X_test, y_test, batch_size=50)

我的训练数据形状: X_train.shape给出(34180, 16)

标签属于具有形状的二进制类: y_train.shape给出(34180,)

所以我的keras代码应该产生以下连接的网络: 16x8 => 8x2

这会产生形状不匹配误差:

ValueError: Input dimension mis-match. (input[0].shape[1] = 2, input[1].shape[1] = 1)

Apply node that caused the error: Elemwise{sub,no_inplace}(Elemwise{Composite{tanh((i0 + i1))}}[(0, 0)].0, )

Inputs types: [TensorType(float64, matrix), TensorType(float64, matrix)]

Inputs shapes: [(50, 2), (50, 1)]

Inputs strides: [(16, 8), (8, 8)]

在model.fit(X_train, y_train, nb_epoch=1000, batch_size=50) Epoch 0处。 我在监督Keras中显而易见的事情吗?

编辑:我在这里经历了这个问题,但没有解决我的问题

I am trying to build a very simple multilayer perceptron (MLP) in keras:

model = Sequential()

model.add(Dense(16, 8, init='uniform', activation='tanh'))

model.add(Dropout(0.5))

model.add(Dense(8, 2, init='uniform', activation='tanh'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)

model.compile(loss='mean_squared_error', optimizer=sgd)

model.fit(X_train, y_train, nb_epoch=1000, batch_size=50)

score = model.evaluate(X_test, y_test, batch_size=50)

My training data shape: X_train.shape gives (34180, 16)

The labels belong to binary class with shape: y_train.shape gives (34180,)

So my keras code should produce the network with following connection: 16x8 => 8x2

which produces the shape mismatch error:

ValueError: Input dimension mis-match. (input[0].shape[1] = 2, input[1].shape[1] = 1)

Apply node that caused the error: Elemwise{sub,no_inplace}(Elemwise{Composite{tanh((i0 + i1))}}[(0, 0)].0, )

Inputs types: [TensorType(float64, matrix), TensorType(float64, matrix)]

Inputs shapes: [(50, 2), (50, 1)]

Inputs strides: [(16, 8), (8, 8)]

At Epoch 0 at line model.fit(X_train, y_train, nb_epoch=1000, batch_size=50). Am I overseeing something obvious in Keras?

EDIT: I have gone through the question here but does not solve my problem

原文:https://stackoverflow.com/questions/31997366

更新时间:2020-01-13 14:20

最满意答案

我有同样的问题,然后发现这个线程;

看来你需要声明一个最终的输出层2或者任何数量的类别,标签需要是一个分类类型,其实质上这是每个观察的二进制向量,例如一个3类输出向量[0,2,1, 0,1,0]变为[[1,0,0],[0,0,1],[0,1,0],[1,0,0],[0,1,0],[1 ,0,0]。

np_utils.to_categorical函数为我解决了这个问题;

from keras.utils import np_utils, generic_utils

y_train, y_test = [np_utils.to_categorical(x) for x in (y_train, y_test)]

I had the same problem and then found this thread;

It appears for you to state a final output layer of 2 or for any number of categories the labels need to be of a categorical type where essentially this is a binary vector for each observation e.g a 3 class output vector [0,2,1,0,1,0] becomes [[1,0,0],[0,0,1],[0,1,0],[1,0,0],[0,1,0],[1,0,0]].

The np_utils.to_categorical function solved this for me;

from keras.utils import np_utils, generic_utils

y_train, y_test = [np_utils.to_categorical(x) for x in (y_train, y_test)]

2015-10-04

相关问答

错误不是关于模型而是关于图像生成器。 默认情况下 , flow_from_directory 会进行分类,并根据目录生成一个类输出,因此您可以获得类似于(32, 2) flow_from_directory (32, 2)内容,即模型需要实际图像时,每个图像都是类标签。 class_mode:“分类”,“二进制”,“稀疏”,“输入”或“无”之一。 默认值:“分类”。 确定返回的标签数组的类型:“分类”将是2D单热编码标签。 因此,您希望flow方法中的class_mode="input"作为目标返

...

model.predict接受一批样本,如果你给它一个形状错误的样本,它会将第一个维度解释为批处理。 一个简单的解决方案是添加值为1的维度: q_network.predict(obs.reshape(1, 8))

model.predict takes a batch of samples, if you give it a single sample with the wrong shape, it will interpret the first dimension as the batc

...

您的模型已被初始化(并经过训练)以接收来自形状(N,28)矩阵的输入。 它预计有28列。 解决此问题的方法是重塑您的单个输入行以匹配: z = z[:, np.newaxis].T #(1,28) shape

要么: z = z.reshape(1,-1) #reshapes to (1,whatever is in there)

z = z.reshape(-1,28) #probably better, reshapes to (amount of samples, input_dim)

...

最近的API更改https://groups.google.com/forum/#!topic/keras-users/iWVrWpR_eaQ后,示例http://keras.io/examples/尚未更新。 确保安装了最新版本的Keras: sudo pip install git+git://github.com/fchollet/keras.git --upgrade

并使用来自同一存储库的更新示例https://github.com/fchollet/keras/tree/master

...

事实上,这个函数期望的是一维张量,并且你有一个二维张量。 keras.backend.squeeze(x, axis=-1)确实有keras.backend.squeeze(x, axis=-1)函数。 你也可以使用keras.backend.reshape(x, (-1,)) 如果您在手术后需要回到旧的形状,您可以: keras.backend.expand_dims(x) keras.backend.reshape(x,(-1,1)) Indeed, the function is expec

...

我有同样的问题,然后发现这个线程; https://github.com/fchollet/keras/issues/68 看来你需要声明一个最终的输出层2或者任何数量的类别,标签需要是一个分类类型,其实质上这是每个观察的二进制向量,例如一个3类输出向量[0,2,1, 0,1,0]变为[[1,0,0],[0,0,1],[0,1,0],[1,0,0],[0,1,0],[1 ,0,0]。 np_utils.to_categorical函数为我解决了这个问题; from keras.utils impo

...

正如@ djk47463的评论中所提到的,您的输出现在每个样本有919个值,因为这是网络最后一层中的单位数。 要更正此问题,请将最后一个图层的单位设置为1,或者添加一个输出维度为1的新最终图层。 As mentioned in @djk47463's comment, your output is now has 919 values per sample, because that is the number of units in the last layer of your network.

...

不匹配是在预期的输出维度(98,10)和您正在使用的数据的维度(98,1)之间 那是因为你正在使用一个应该在10级数据库上进行分类的示例代码。 如果您想进行预测,请将最后一层更改为 model.add(Dense(output_dim=1, init="glorot_uniform"))

另外,我认为您的成本函数会有问题。 如果您有连续数据,则不应使用绝对百分比错误。 改变这个 model.compile(loss="mean_absolute_percentage_error", optimi

...

你不想要32个大小为39的数组,你想要一个大小的数组(32,39)。 因此,您必须将input_shape更改为(None,39),None将允许您动态更改batch_size,并将batch_x更改为形状为numpy的数组(32,39)。 You don't want 32 arrays of size 39, you want one array of size (32, 39). So you must change input_shape to (None, 39), the None a

...

您的ImageDataGenerator没有问题。 如错误消息中所述,模型输出的形状与其目标的形状之间存在不匹配。 您使用class_mode = 'binary' ,因此您的模型的预期输出是单个值,但它会产生shape的输出(batch_size, 64, 64, 4)因为您的模型中没有其他卷积层。 尝试这样的事情: model.add(Convolution2D(4, 4, 4, border_mode='same', input_shape=(64, 64,3), activation='r

...

你可能感兴趣的:(python,variable,shape,不匹配)