R中的python keras和keras之间的准确性不同 [英] Different accuracy between python keras and keras in R
问题描述
我在keras的R中为R建立了图像分类模型.
I build a image classification model in R by keras for R.
大约有98%的准确性,而python的准确性却很差.
Got about 98% accuracy, while got terrible accuracy in python.
R的Keras版本是2.1.3,而python中是2.1.5
Keras version for R is 2.1.3, and 2.1.5 in python
以下是R模型代码:
model=keras_model_sequential()
model=model %>%
layer_conv_2d(filters = 32,kernel_size = c(3,3),padding = 'same',input_shape = c(187,256,3),activation = 'elu')%>%
layer_max_pooling_2d(pool_size = c(2,2)) %>%
layer_dropout(.25) %>% layer_batch_normalization() %>%
layer_conv_2d(filters = 64,kernel_size = c(3,3),padding = 'same',activation = 'relu') %>%
layer_max_pooling_2d(pool_size = c(2,2)) %>%
layer_dropout(.25) %>% layer_batch_normalization() %>% layer_flatten() %>%
layer_dense(128,activation = 'relu') %>%
layer_dropout(.25)%>%
layer_batch_normalization() %>%
layer_dense(6,activation = 'softmax')
model %>%compile(
loss='categorical_crossentropy',
optimizer='adam',
metrics='accuracy'
)
我尝试使用相同的输入数据在python中重建相同的模型.
I try to rebuild a same model in python, with same input data.
而性能却完全不同.准确度甚至不到30%
While, got totally different performance. The accuracy even less than 30%
因为R keras调用python来运行keras.在相同的模型架构下,它们应该获得相似的性能.
Because R keras is calling python for run keras. With same model architecture, they should get similar performance.
我想知道这个问题是否是由预处理引起的,但仍然显示我的python代码:
I wonder if this issue caused by preprocess, but still show my python code:
model=Sequential()
model.add(Conv2D(32,kernel_size=(3,3),activation='relu',input_shape=(187,256,3),padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(BatchNormalization())
model.add(Conv2D(64, (3, 3), activation='relu',padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(BatchNormalization())
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.25))
model.add(BatchNormalization())
model.add(Dense(len(label[1]), activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
这是一个简单的分类.我和大多数指令一样.
This is a simple classification. I just do as same as most instruction.
找不到其他面临相同问题的人.因此想问一下它是如何发生的以及如何解决.谢谢
Can not find others faced same problem. So want to ask how it happen and how to solve. Thx
推荐答案
这是一个巨大的差异,因此也许代码中有错误或数据中有意外内容,但从Python
中的R
复制了Keras
结果由于将种子设置在R
一侧是不够的,所以它比看起来要困难的多.代替set.seed
,您应该使用use_session_with_seed
,它随tensorflow
和keras
的R库一起提供.请注意,要完全再现,您需要use_session_with_seed(..., disable_gpu=TRUE, disable_parallel_cpu=TRUE)
.另请参见 stack 和 tf 文档.另外,这是一个示例,它使用kerasformula
和一个公共数据集.另外,请注意像layer_dropout
这样的接受seed
作为参数的函数.
That is a dramatic difference so perhaps there's a bug in the code or something unexpected in the data but reproducing Keras
results from R
in Python
is more difficult than it may seem since setting the seed on the R
side is insufficient. Instead of set.seed
you should use use_session_with_seed
, which comes with the R libraries for tensorflow
and keras
. Note that for full reproducibility you need to use_session_with_seed(..., disable_gpu=TRUE, disable_parallel_cpu=TRUE)
. See also stack and tf docs. Also, here is an example using the github version of kerasformula
and a public dataset. Also, watch out for functions like layer_dropout
that accept seed
as a parameter.
这篇关于R中的python keras和keras之间的准确性不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!