Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.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 Keras:预期为3维,但得到了形状密集模型的阵列_Python_Machine Learning_Neural Network_Keras - Fatal编程技术网

Python Keras:预期为3维,但得到了形状密集模型的阵列

Python Keras:预期为3维,但得到了形状密集模型的阵列,python,machine-learning,neural-network,keras,Python,Machine Learning,Neural Network,Keras,我想基于使用TfidfVectorizer的矢量化单词进行多标签分类(20个不同的输出标签)。我有一组39974行,每行包含2739项(零或一) 我想使用Keras模型对这些数据进行分类,该模型将包含1个隐藏层(~20个节点,activation='relu'),输出层等于20个可能的输出值(activation='softmax'选择最佳拟合) 以下是我目前的代码: model = Sequential() model.add(Dense(units=20, activation='relu'

我想基于使用TfidfVectorizer的矢量化单词进行多标签分类(20个不同的输出标签)。我有一组39974行,每行包含2739项(零或一)

我想使用Keras模型对这些数据进行分类,该模型将包含1个隐藏层(~20个节点,activation='relu'),输出层等于20个可能的输出值(activation='softmax'选择最佳拟合)

以下是我目前的代码:

model = Sequential()
model.add(Dense(units=20, activation='relu', input_shape=tfidf_matrix.shape))
model.add(Dense(units=20, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(tfidf_matrix, train_data['cuisine_id'], epochs=10)
但有一个错误:

ValueError:检查输入时出错:应具有密集\u 1\u输入 3维,但具有形状的数组(397742739)

如何使用此矩阵指定此NN以进行拟合

行数(训练样本数)不是网络输入形状的一部分,因为训练过程为网络提供每批一个样本(或者更准确地说,每批一个批量大小的样本)

因此,在您的情况下,网络的输入形状是
(2739,)
,正确的代码应该如下所示:

model = Sequential()
# the shape of one training example is
input_shape = tfidf_matrix[0].shape
model.add(Dense(units=20, activation='relu', input_shape=input_shape))
model.add(Dense(units=20, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', 
metrics=['accuracy'])
model.fit(tfidf_matrix, train_data['cuisine_id'], epochs=10)

非常感谢您的回复!那个输入层工作人员把我弄糊涂了。现在一切正常!