Max*_*wer 5 python machine-learning scikit-learn keras recurrent-neural-network
我有一个序列正在尝试使用带有 return_sequences=True 的 Keras LSTM 进行分类。我有“数据”和“标签”数据集,它们的形状相同 - 二维矩阵,行按位置排列,列按时间间隔排列(单元格值是我的“信号”特征)。因此,带有 return_sequences=True 的 RNN 似乎是一种直观的方法。
将我的数据(X)和标签重塑(Y)为 shape 的 3D 张量后(rows, cols, 1),我调用model.fit(X, Y)但收到以下错误:
ValueError('y 的形状无效')
它向我指出了类 KerasClassifier() 的 fit 方法的代码,该方法检查len(y.shape)==2.
好吧,也许我应该将我的 2D 重塑'X'为形状(行、列、1)的 3D 张量,但将我的标签保留为 sklearn 界面的 2D?但当我尝试时,我收到另一个 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)
Run Code Online (Sandbox Code Playgroud)
ValueError:y 的形状无效
此代码适用于 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2130 次 |
| 最近记录: |