Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 输入0与层lstm_93不兼容:预期ndim=3,发现ndim=2_Python_Keras_Lstm - Fatal编程技术网

Python 输入0与层lstm_93不兼容:预期ndim=3,发现ndim=2

Python 输入0与层lstm_93不兼容:预期ndim=3,发现ndim=2,python,keras,lstm,Python,Keras,Lstm,我的X_列形状是171,10,1,y_列形状是171,包含从1到19的值。 输出应为19类中每个类的概率。 我尝试使用RNN对19个类进行分类 from sklearn.preprocessing import LabelEncoder,OneHotEncoder label_encoder_X=LabelEncoder() label_encoder_y=LabelEncoder() y_train=label_encoder_y.fit_transform(y_train) y_train

我的X_列形状是171,10,1,y_列形状是171,包含从1到19的值。 输出应为19类中每个类的概率。 我尝试使用RNN对19个类进行分类

from sklearn.preprocessing import LabelEncoder,OneHotEncoder
label_encoder_X=LabelEncoder()
label_encoder_y=LabelEncoder()

y_train=label_encoder_y.fit_transform(y_train)
y_train=np.array(y_train)

X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))


from keras.models import Sequential
from keras.layers import Dense,Flatten
from keras.layers import LSTM
from keras.layers import Dropout


regressor = Sequential()

regressor.add(LSTM(units = 100, return_sequences = True, input_shape=( 
(X_train.shape[1], 1)))
regressor.add(Dropout(rate=0.15))

regressor.add(LSTM(units = 100, return_sequences =False))#False caused the 
exception ndim
regressor.add(Dropout(rate=0.15))


regressor.add(Flatten())
regressor.add(Dense(units= 19,activation='sigmoid'))
regressor.compile(optimizer = 'rmsprop', loss = 'mean_squared_error')

regressor.fit(X_train, y_train, epochs = 250, batch_size = 16)
在第二个LSTM层中设置return_sequences=False时,结果是None,100不再需要展平。您可以在第二个LSTM层中设置return_sequences=True,或者根据需要删除regressor.addflant

此外,如果您想获得19个类中每个类的概率,那么标签数据应该是一种热形式。使用keras.utils.to_分类:


谢谢我试过那段代码。但是,当我在解释器中运行keras.utils.to_categoricaly_列,num_classes=19时,它会抛出一个不同的错误索引,对于大小为19的轴1,20超出范围。你能帮忙吗。我无法纠正它。但是,当我给num_classes=21时,程序执行得很好。但考虑到我只有19个标签,我认为以这种方式运行是无效的。@Suvab此错误表示您的y_列车中有20个标签。您应该确保y_列车中的标签号属于[0,18]。
one_hot_labels = keras.utils.to_categorical(y_train, num_classes=19) #(None,19)
regressor.fit(X_train, one_hot_labels, epochs = 250, batch_size = 16)