简体   繁体   English

禁止R中Keras的控制台输出

[英]Suppress console output from Keras in R

I am trying to use an R-script in a docker environment to do some classification on the input that I am piping to it. 我正在尝试在docker环境中使用R脚本对要传递给它的输入进行一些分类。 The script should only print the output of the classification, however I always get Using TensorFlow backend. 该脚本只应打印分类的输出,但是我总是得到Using TensorFlow backend. as output of the first line where a function of Keras is called. 作为第一行的输出,其中调用了Keras函数。

I made a small working example. 我做了一个小的工作例子。

library(keras)

v <- (c(1,2,3))
print(v)
vCat<-to_categorical(v)

I want to display this as output: 我想将其显示为输出:

[1] 1 2 3

I get however 我明白了

[1] 1 2 3
Using TensorFlow backend.

So I searched around on google and stackoverflow about sink and suppress... and other stuff, from for example Suppress automatic output to console in R , Suppress one command's output in R and Suppress automatic output to console in R . 因此,我在Google和stackoverflow上搜索了有关接收sinksuppress...以及其他内容的信息,例如,从R中自动输出抑制到控制台,在R抑制一个命令的输出,抑制 R中 输出到控制台的自动输出 I then tried the following piece of code: 然后,我尝试了以下代码:

library(keras, quietly = T)

v <- (c(1,2,3))
print(v) 
sink("/dev/null")
capture.output(suppressMessages(suppressWarnings(
suppressPackageStartupMessages(
vCat<-to_categorical(v) ))), file = "/dev/null")
sink()

This still doesn't suppress the Using TensorFlow backend. 这仍然不会抑制Using TensorFlow backend. message. 信息。 The script is called in a docker environment using littler, from the command line: r test.R . 该脚本是在r test.R环境中使用r test.Rr test.R Note: when just running the script in Rstudio I don't get the message. 注意:仅在Rstudio中运行脚本时,不会收到此消息。

This is a Keras issue (ie not an R or Tensorflow one) and, as I have already commented, there have been several people complaining about this , but a solution has not been proposed by Keras maintainers so far. 这是Keras的问题(即不是R或Tensorflow的问题),正如我已经评论过的,有好几个人对此表示抱怨 ,但是到目前为止,Keras维护人员尚未提出解决方案。

A hack , as already suggested in the issue thread above , is to locate the keras/backend/__init__.py file and comment out the following line : 正如上面问题线程中所建议的那样, 黑客是要找到keras/backend/__init__.py文件并注释掉以下行

sys.stderr.write('Using TensorFlow backend.\n')

Last time I checked, Keras in R only uses the Tensorflow backend, so you shouldn't need to repeat this for the other backend options in __init__.py . 上次我检查时,R中的Keras仅使用Tensorflow后端,因此您无需为__init__.py的其他后端选项重复此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM