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

时间:2017-03-30 23:03:21

标签: python machine-learning scikit-learn keras recurrent-neural-network

我正在尝试使用带有return_sequences = True的Keras LSTM进行分类。我有'数据'和'标签'数据集,它们都是相同的形状 - 按位置排列的二维矩阵和按时间间隔排列的列(单元格值是我的'信号'特征)。因此RNN w / return_sequences = True似乎是一种直观的方法。

在重新塑造我的数据from HTMLParser import HTMLParser trans = { 'History': 'History TRANSLATED', 'Acknowledgements': 'Acknowledgements TRANSLATED' } inputHTML = """<table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="section-title"><a href="/about%20/history">History</a> </td> <td class="section-title"><a href="/about/team">Project Team</a></td> <td class="section-title"><a href="/about/data">Contributors of data</a></td> <td class="section-title"><a href= "/about/acknowledgements">Acknowledgements</a></td> <td class="section-title"><a href="/about/origins">African Origins Project</a></td> <td class="section-title"><a href="/about/contacts">Contact us</a></td> </tr> </tbody> </table> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="section-desc">A brief account of the origins of a single multi-source dataset of the trans-Atlantic slave trade and its realization first as a CD-ROM published by Cambridge University Press in 1999 and now, in an expanded version, on the Voyages website.</td> <td class="section-desc">Names of the principal investigators, of members of the project development team, and of individuals serving on the steering committee and advisory board.</td> <td class="section-desc">Names of scholars and researchers whose findings on the trans-Atlantic slave trade have been incorporated into the Voyages Database.</td> <td class="section-desc">Major sponsors and institutional partners of the Voyages website, as well as other organizations and individuals who have assisted the work of the project team.</td> <td class="section-desc">A scholar-public collaborative project using audio recordings of names in African Names Database to trace the geographic origins of Africans transported in the transatlantic slave trade.</td> <td class="section-desc">Members of the Voyages editorial board and the email address for contacting the website.</td> </tr> </tbody> </table>""" class TransParser(HTMLParser): def __init__(self): TransParser.__init__(self) self.trans_data = self.rawdata def handle_data(self, data): data = data.strip() if data: section = trans.get(data, data) #self.trans_data = self.trans_data.replace(data, section) parser = TransParser() parser.feed(inputHTML) 并将(X)标记为形状为(Y)的3D张量后,我调用了(rows, cols, 1),但收到以下错误:

  

ValueError('y'的形状无效)

它指出了类KerasClassifier()的fit方法的代码,该方法检查model.fit(X, Y)

好的,也许我应该将我的2D len(y.shape)==2重塑为3D Tensor of shape(rows,cols,1),但是将我的标签留作2D用于sklearn界面?但是当我尝试时,我得到了另一个Keras错误:

  

ValueError:检查模型目标时出错:预期lstm_17有   3个尺寸,但有阵列形状(500,2880)

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

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

'X'
  

ValueError:y

的形状无效

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

我认为这是KerasClassifier类的功能。在多步骤,多功能LSTM上使用类时,我遇到了相同的问题。出于某种原因,如果我通过Keras构建模型并在compile()之后运行fit()方法,则该模型将正常进行训练而不会出错。但是,当我在函数中创建模型并使用KerasClassifier调用该函数时,我遇到了错误。通过查看keras模块中的KerasClassifier类(搜索wrapper / scikit_learn.py),我发现“ y”必须是特定形状,否则该函数会引发异常。这个形状是2D'y'张量(n_samples,n_outputs)或1D'y'张量(n_samples)与我所期望的不兼容。因此,我将仅使用模型的fit()方法,而不使用包装器。希望这会有所帮助。

顺便说一句。我的Keras版本是2.2.4,Tensorflow是1.15.0。这可能不适用于较新的版本。