重塑的输入是一个具有 2 * "batch_size" 值的张量,但请求的形状具有 "batch_size"

Input to reshape is a tensor with 2 * "batch_size" values, but the requested shape has "batch_size"

我想使用带有 tensorflow 后端的 Keras 顺序模型制作 RNN。当我执行以下代码时:

batch_size = 8
batch_inputshape = (batch_size,x_train.shape[1],x_train.shape[2])
print(batch_inputshape) #(8, 600, 103)
​
model = Sequential()
model.add(LSTM(103, 
               batch_input_shape = batch_inputshape, 
               return_sequences = True,
              stateful = True))
model.add(Dropout(0.2))
​
model.add(LSTM(50, 
               return_sequences = True,
              stateful = True))
model.add(Dropout(0.2))
​
​
model.add(TimeDistributed(Dense(10)))
model.add(TimeDistributed(Dense(2)))
model.add(Activation('softmax'))
model.compile(loss= ncce, optimizer='adam')    ​
​
print (model.output_shape) #(8, 600, 2)

model.fit(x_train,y_train, batch_size = batch_size,
                           nb_epoch = 1, validation_split=0.25)

我收到以下错误消息:

reshape 的输入是一个有 16 个值的张量,但请求的形状有 8 个

但无论我将 batch_size 更改为错误,都将遵循以下公式:

重塑的输入是一个具有 2 * batch_size 值的张量,但请求的形状具有 batch_size

我看过其他 ,但我认为它们对我帮助不大。或者我对答案的理解不够好。

如有任何帮助,我们将不胜感激!

编辑: 根据要求输入和目标的形状:

print(x_train.shape) #(512,600,103)
print(y_train.shape) #(512,600,2)

编辑 2:

from functools import partial
import keras.backend as K 
from itertools import product
​
def w_categorical_crossentropy(y_true, y_pred, weights):
    # https://github.com/fchollet/keras/issues/2115#issuecomment-274101310 #
    nb_cl = len(weights)
    final_mask = K.zeros_like(y_pred[:, 0])
    y_pred_max = K.max(y_pred, axis=1)
    y_pred_max = K.reshape(y_pred_max, (K.shape(y_pred)[0], 1))
    y_pred_max_mat = K.cast(K.equal(y_pred, y_pred_max), K.floatx())
    for c_p, c_t in product(range(nb_cl), range(nb_cl)):
        final_mask += (weights[c_t, c_p] * y_pred_max_mat[:, c_p] * y_true[:, c_t])
    return K.categorical_crossentropy(y_pred, y_true) * final_mask
​
w_array = np.ones((2,2))
w_array[1, 0] = 100
​
​
print(w_array)
ncce = partial(w_categorical_crossentropy, weights=w_array)
ncce.__name__ ='w_categorical_crossentropy

编辑 3:更新

在@Nassim Ben 的帮助下,他发现问题出在损失函数上。他发布了带有常规损失函数的代码,然后它就可以正常工作了。但是,对于代码不起作用的自定义损失函数。正如这个问题的任何读者都看到的那样,我在上面发布了我的服装损失函数并且存在问题。目前我还不知道为什么会出现此错误,但这是当前状态。

编辑: 这段代码对我有用,我只是为了简单起见改变了损失。

import keras
from keras.layers import *
from keras.models import Sequential
from keras.objectives import *
import numpy as np

x_train = np.random.random((512,600, 103))
y_train = np.random.random((512,600,2))
batch_size = 8
batch_inputshape = (batch_size,x_train.shape[1],x_train.shape[2]) 
print(batch_inputshape) #(8, 600, 103)

model = Sequential()
model.add(LSTM(103,
           batch_input_shape = batch_inputshape,
           return_sequences = True,
          stateful = True))
model.add(Dropout(0.2))
model.add(LSTM(50,
           return_sequences = True,
          stateful = True))
model.add(Dropout(0.2))


model.add(TimeDistributed(Dense(10)))
model.add(TimeDistributed(Dense(2)))
model.add(Activation('softmax'))
model.compile(loss= "mse", optimizer='adam')

print (model.output_shape) #(8, 600, 2)

model.fit(x_train,y_train, batch_size = batch_size,
                       nb_epoch = 1, validation_split=0.25)

编辑 2:

所以错误来自损失函数。在您从 github 复制的用于 ncce 损失的代码中,它们的输出形状为 (batch,10)。您的输出形状为 (batch, 600, 2)。所以这是我对函数的编辑:

def w_categorical_crossentropy(y_true, y_pred, weights):
# https://github.com/fchollet/keras/issues/2115#issuecomment-274101310 #
    nb_cl = len(weights)
    # Create a mask with zeroes
    final_mask = K.zeros_like(y_pred[:,:,0])
    # get the maximum probability value for every output (shape = (batch,600,1))
    y_pred_max = K.max(y_pred, axis=2, keepdims=True)
    # Get the actual predictions for every output (shape = (batch,600,2))
    # This K.equal uses broadcasting, we compare two tensors of different sizes but it works (magic)
    y_pred_max_mat = K.equal(y_pred, y_pred_max)
    for c_p, c_t in product(range(nb_cl), range(nb_cl)):
        # Create the mask of weights to apply to the result of the cat_crossentropy
        final_mask += (weights[c_t, c_p] * K.cast(y_pred_max_mat[:,:, c_p], K.floatx()) * y_true[:,:, c_t])
    return K.categorical_crossentropy(y_pred, y_true) * final_mask

w_array = np.ones((2,2))
w_array[1, 0] = 100

如您所见,我只是根据您的特殊形状修改了索引播放。 面具必须有形状(批次,600)。 最大值必须在第 3 个维度上完成,因为那里是您想要输出的概率。 由于张量的形状,构建最大值的矩阵乘法也需要更新。

这应该有效。

如果您需要更详细的解释,请随时询问:-)