【问题标题】:Error when running ANN with reccurent layer使用循环层运行 ANN 时出错
【发布时间】:2016-10-10 07:48:11
【问题描述】:

我创建了下面的 ANN,它有 2 个全连接层和一个循环层。但是在运行它时出现错误:Exception: Input 0 is incompatible with layer lstm_11: expected ndim=3, found ndim=2 为什么会发生?

from keras.models import Sequential
from keras.layers import Dense
from sklearn.cross_validation import train_test_split
import numpy
from sklearn.preprocessing import StandardScaler
from keras.layers import LSTM 




seed = 7
numpy.random.seed(seed)

dataset = numpy.loadtxt("sorted_output.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:15]
scaler = StandardScaler(copy=True, with_mean=True, with_std=True ) #data normalization
X = scaler.fit_transform(X) #data normalization
Y = dataset[:,15]
# split into 67% for train and 33% for test
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=seed)
# create model
model = Sequential()
model.add(Dense(12, input_dim=15, init='uniform', activation='relu'))
model.add(LSTM(10, return_sequences=True))
model.add(Dense(15, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X_train, y_train, validation_data=(X_test,y_test), nb_epoch=150, batch_size=10)

【问题讨论】:

    标签: python neural-network theano keras


    【解决方案1】:

    基于以上所有层都是 LSTM 中的“密集”层,您将序列返回为真。您应该设置 return_sequences=False 因为所有密集层,它应该可以工作。

    【讨论】:

      【解决方案2】:

      这个错误的原因是LSTM 期望输入具有 3 维的形状(对于批处理、序列长度和输入维度)。但是Dense之前的层会输出一个二维的形状(用于批处理和输出维度)。

      您可以通过执行以下代码行来查看Dense 层的输出形状

      >>> model = Sequential()
      >>> model.add(Dense(12, input_dim=15, init='uniform', activation='relu'))
      >>> model.summary()
      ____________________________________________________________________________________________________
      Layer (type)                     Output Shape          Param #     Connected to                     
      ====================================================================================================
      dense_4 (Dense)                  (None, 12)            192         dense_input_2[0][0]              
      ====================================================================================================
      Total params: 192
      Trainable params: 192
      Non-trainable params: 0
      ____________________________________________________________________________________________________
      

      但是,您没有用模型解释您的意图,因此我无法为您提供有关此问题的进一步指导。你的输入数据是什么?您希望输入是一个序列吗?

      如果您的输入是一个序列,那么我建议您删除第一个Dense 层。但是如果你输入的不是序列,那么我建议你去掉LSTM层。

      【讨论】:

        猜你喜欢
        • 2019-01-19
        • 2015-05-30
        • 2013-07-21
        • 1970-01-01
        • 1970-01-01
        • 2020-07-02
        • 2016-02-14
        • 2011-12-19
        • 1970-01-01
        相关资源
        最近更新 更多