我尝试对序列进行分类,使用带有return_sequences=True的Keras。我有“数据”和“标签”数据集,它们都是形状相同的二维矩阵,按位置排列,按时间间隔排列(单元格值是我的“信号”特性)。因此,RNN / return_sequences=True似乎是一种直观的方法。
在将数据(X)和标签(Y)重组为形状为(rows, cols, 1)的3D张量之后,我调用model.fit(X, Y),但得到以下错误:
ValueError(“y的形状无效”)
它指向KerasClassifier()类的fit方法的代码,该方法检查len(y.shape)==2。
好吧,也许我应该把我的2D 'X'重塑成一个三维形状张量(行,科尔,1),但是把我的标签留给学习界面呢?但是当我尝试得到另一个Keras错误时:
ValueError:检查模型目标时的错误:期望lstm_17具有三维,但得到形状为(500,2880)的数组
...So如何适合Sklearn风格的Keras返回序列?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的无效形状
发布于 2020-03-07 16:03:27
我认为这是KerasClassifier类的一个特性。当我在多步、多功能的LSTM上使用这个类时,我遇到了同样的问题。出于某种原因,如果我通过Keras构建模型,并在编译()之后运行fit()方法,那么模型将正常训练,不会出错。但是,当我在一个函数中创建模型并使用KerasClassifier调用该函数时,我会遇到您所遇到的错误。通过查看keras模块中的KerasClassifier类(搜索wrappers/scikit_learn.py),我发现'y‘必须是一个特定的形状,否则函数会引发异常。这个形状是二维'y‘张量(n_samples,n_outputs)或一维'y’张量(n_samples),与我所期望的不相容。因此,我将使用模型的fit()方法,而不是使用包装器。希望这能有所帮助。
顺便说一下。我的Keras版本是2.2.4,Tensorflow是1.15.0。这可能不适用于较新的版本。
发布于 2017-03-31 21:13:47
此代码用于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)https://stackoverflow.com/questions/43129758
复制相似问题