计算LSTM的培训和测试精度
我正在构建具有以下代码的LSTM模型,并希望计算模型的培训和测试精度。我是机器学习的新手,我知道的唯一计算准确性的方法是使用Sklearn的“准确性得分”。
y_train = pd.Series(y_train)
lstm_model = Sequential()
lstm_model.add(Embedding(top_words, 32, input_length=req_length))
lstm_model.add(Flatten())
input = (req_length, 32)
lstm_model.add(Reshape(input))
lstm_model.add(LSTM(units = 50, return_sequences = True))
lstm_model.add(Dropout(0.2))
lstm_model.add(Dense(256, activation='relu'))
lstm_model.add(Dropout(0.2))
lstm_model.add(Dense(1, activation='sigmoid'))
lstm_model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])
lstm = lstm_model.fit(X_train, y_train, epochs = 30, batch_size = 10)
要计算y_pred,我将其写成y_pred = lstm_model.predict(y_test)
。但是,由于其形状为(600,401,1)
,因此在y_pred上的精度得分函数。
我该怎么办或共享一些代码?
I am building an LSTM model with the following code and I wish to calculate the training and testing accuracies of the model. I am a novice in machine learning and the only method I know for calculating the accuracy is using sklearn's "accuracy score".
y_train = pd.Series(y_train)
lstm_model = Sequential()
lstm_model.add(Embedding(top_words, 32, input_length=req_length))
lstm_model.add(Flatten())
input = (req_length, 32)
lstm_model.add(Reshape(input))
lstm_model.add(LSTM(units = 50, return_sequences = True))
lstm_model.add(Dropout(0.2))
lstm_model.add(Dense(256, activation='relu'))
lstm_model.add(Dropout(0.2))
lstm_model.add(Dense(1, activation='sigmoid'))
lstm_model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])
lstm = lstm_model.fit(X_train, y_train, epochs = 30, batch_size = 10)
To calculate y_pred, I wrote it as y_pred = lstm_model.predict(y_test)
. However, the accuracy score function on y_pred as its shape is (600, 401, 1)
.
What can I do regarding this or share some code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您打印
lstm_model.summary()
,您可能会看到:正如我们所注意到的,最后一个密集的层会产生Shape
的输出(无,401,1)
。这个401数字出现在整个网络中,指序列中的元素数量(单词)。据我所知,它来自您的req_length
变量。现在,让我们仔细查看此代码线:
在这里,您指定LSTM层。但是,等等,
return_sequences = true
是什么意思?根据文档:您在这里设置的内容取决于您的任务。在这里,您基本上告诉LSTM层具有与序列中单词数量一样多的输出。这样,您的LSTM输出中有401个向量。但是,正如我从您的模型体系结构中看到的那样,您想解决一些二进制分类任务。在这种情况下,仅从LSTM输出一个向量是更合乎逻辑的。因此,我建议将此参数设置为
false
:将新的模型体系结构与上一个进行比较:
现在您的LSTM层仅输出每个批次元素的大小50的向量,而不是401矢量。因此,最后,每个批次元素没有401个值。您只有一个值,这是输入序列的预测。
If you print
lstm_model.summary()
, you might see:As we can notice, last Dense layer produces output of shape
(None, 401, 1)
. This 401 number appears in the whole network and means the number of elements in the sequence (words). As far as I understand, it comes from yourreq_length
variable.Now let's look closer at this line of code:
Here, you specify LSTM layer. But wait, what does
return_sequences = True
mean? According to the documentation:What you set here depends on your task. Here, you basically tell LSTM layer to have as many outputs as the number of words in the sequence. This way, you have 401 vectors in the output from LSTM. However, as I see from your model architecture, you want to solve some binary classification task. In this case, it would be more logical to output only one vector from LSTM. Thus, I suggest setting this parameter to
False
:Compare new model architecture with the previous one:
Now your LSTM layer outputs only one vector of size 50 for each batch element, not 401 vectors. Therefore, in the end, you don't have 401 values for each batch element; you have only one value which is a prediction for the input sequence.