首页 文章

Keras LSTM的“y形状无效”,带有return_sequences = True(和sklearn API)

提问于
浏览
3

我有一个我试图分类的序列,使用带有return_sequences = True的Keras LSTM . 我有'数据'和'标签'数据集,两者都是相同的形状 - 二维矩阵,按行位置排列,按时间间隔排列(单元格值是我的'信号'功能) . 因此RNN w / return_sequences = True似乎是一种直观的方法 .

重新塑造我的数据 (X) 并将 (Y) 标记为形状 (rows, cols, 1) 的3D张量后,我调用 model.fit(X, Y) 但得到以下错误:

ValueError('y'的形状无效)

它指出了类KerasClassifier()的fit方法的代码,该方法检查 len(y.shape)==2 .

好的,也许我应该将我的2D 'X' 重新塑造为3D Tensor of shape(行,列,1),但是将我的标签留作2D用于sklearn界面?但是当我尝试时,我得到另一个Keras错误:

ValueError:检查模型目标时出错:预期lstm_17有3个维度,但得到的是带有形状的数组(500,2880)

...那么如何使Sklearn风格的Keras RNN返回序列呢? Keras的不同部分似乎要求我的目标是2D和3D . 或者(更有可能)我误解了一些东西 .

...这是一个可重现的代码示例:

from keras.layers import LSTM
from keras.wrappers.scikit_learn import KerasClassifier

# Raw Data/Targets    
X = np.array([1,2,3,4,5,6,7,8,9,10,11,12]).reshape(3,4)
Y = np.array([1,0,1,1,0,1,0,1,0,1,0,1]).reshape(3,4)

# Convert X to 3D tensor per Keras doc for recurrent layers
X = X.reshape(X.shape[0], X.shape[1], 1)

# .fit() at bottom will throw an error whether or not this line is used to reshape Y
to reshape Y
Y = Y.reshape(Y.shape[0], Y.shape[1], 1)


# Define function to return compiled Keras Model (to pass to Sklearn API)
def keras_rnn(timesteps, num_features):
    '''Function to return compiled Keras Classifier to pass to sklearn wrapper'''

    model = Sequential()
    model.add(LSTM(8, return_sequences=True, input_shape=(timesteps, num_features)))
    model.add(LSTM(1, return_sequences=True, activation = 'sigmoid'))

    model.compile(optimizer = 'RMSprop', loss = 'categorical_crossentropy')
    return model

# Convert compiled Keras model to Scikit-learn-style classifier (compatible w/ sklearn model-tuning methods)
rnn_sklearn = KerasClassifier(build_fn=keras_rnn, 
                        timesteps=4,
                        num_features=1) 

# Fit RNN Model to Data, Target                            
rnn_sklearn.fit(X, Y)

ValueError:y的形状无效

1 回答

  • 0

    此代码适用于Keras 2.0.2:

    import numpy as np
    from keras.models import Sequential
    from keras.layers import LSTM, Flatten
    from keras.wrappers.scikit_learn import KerasClassifier
    
    # Raw Data/Targets    
    X = np.array([1,2,3,4,5,6,7,8,9,10,11,12]).reshape(3,4)
    Y = np.array([1,0,1,1,0,1,0,1,0,1,0,1]).reshape(3,4)
    
    # Convert X to 3D tensor per Keras doc for recurrent layers
    X = X.reshape(X.shape[0], X.shape[1], 1)
    
    # .fit() at bottom will throw an error whether or not this line is used to reshape Y to reshape Y
    Y = Y.reshape(Y.shape[0], Y.shape[1], 1)
    
    
    # Define function to return compiled Keras Model (to pass to Sklearn API)
    def keras_rnn(timesteps, num_features):
        '''Function to return compiled Keras Classifier to pass to sklearn wrapper'''
    
        model = Sequential()
        model.add(LSTM(8, return_sequences=True, input_shape=(timesteps, num_features)))
        model.add(LSTM(1, return_sequences=True, activation = 'sigmoid'))
    
        model.compile(optimizer = 'RMSprop', loss = 'binary_crossentropy')
        return model
    
    # Convert compiled Keras model to Scikit-learn-style classifier (compatible w/ sklearn model-tuning methods)
    rnn_sklearn = KerasClassifier(build_fn=keras_rnn, 
                            timesteps=4,
                            num_features=1) 
    
    # Fit RNN Model to Data, Target                            
    rnn_sklearn.fit(X, Y)
    

相关问题