keras lstm(100)和lstm(units = 100)会产生不同的结果吗? [英] keras lstm(100) and lstm(units=100) produces different results?

查看:682
本文介绍了keras lstm(100)和lstm(units = 100)会产生不同的结果吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用keras 2.0.2创建用于分类任务的lstm网络.网络拓扑如下:

I am using keras 2.0.2 to create a lstm network for a classification task. The network topology is as below:

from numpy.random import seed
seed(42)
from tensorflow import set_random_seed
set_random_seed(42)
import os
#os.environ['PYTHONHASHSEED'] = '0'

model = Sequential()
model.add(embedding_layer)    
model.add(LSTM(units=100)) #line A
model.add(Dropout(0.2))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

相同数据集上,使用当前行A,我得到的结果为:

On the same dataset, with the current line A, I obtain a result as:

    precision    recall  f1  support
0   0.68    0.58    0.63    305
2   0.92    0.95    0.93    1520
avg 0.8 0.76    0.78    1825

,其中0、2表示类别标签

, where 0, 2 indicate class labels

但是当A行更改为

model.add(LSTM(100))

我得到了不同的结果:

    precision    recall  f1  support
0   0.66    0.58    0.62    305
2   0.92    0.94    0.93    1520
avg 0.79    0.76    0.77    1825

根据keras文档 https://keras.io,这对我来说没有意义/layers/recurrent/#lstm ,我认为这两行应该相同吗?我有误会什么吗?

This does not make sense to me as according to the keras documentation https://keras.io/layers/recurrent/#lstm, I thought that the two lines should be same? Have I misunderstood anything?

推荐答案

model.add(LSTM(100))model.add(LSTM(units=100))是等效的.可能导致结果差异的是过程中的随机性.为了避免这种随机性并获得可重复的结果,您应该在代码的开头指定种子.

model.add(LSTM(100)) and model.add(LSTM(units=100)) are equivalent. What may cause the difference between the results is the randomness in the process. In order to avoid this randomness and get reproducible results, you should specify the seed in the beginning of your code.

对于theano后端,添加

For theano backend, add

from numpy.random import seed
seed(1)

位于代码顶部.

对于tensorflow后端,添加

For tensorflow backend, add

from numpy.random import seed
seed(1)
from tensorflow import set_random_seed
set_random_seed(2)

位于代码顶部.

以上代码摘自此博客文章.

这篇关于keras lstm(100)和lstm(units = 100)会产生不同的结果吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆