那么,我们怎么能从 R 中使用 TensorFlow?

您是否曾经想过为什么可以从 R 调用 TensorFlow(通常称为 Python 框架)?如果没有 - 那应该是这样的,因为 R 包 keras 和 tensorflow 旨在使这个过程对用户尽可能透明。但要让它们成为这些有用的精灵,首先必须有人驯服 Python。

来源:RStudio AI博客

哪种计算机语言与 TensorFlow 最密切相关?虽然在 TensorFlow for R 博客上,我们当然希望答案是 R,但很有可能是 Python(尽管 TensorFlow 也有 C++、Swift、Javascript、Java 和 Go 的官方绑定)。

TensorFlow TensorFlow for R 博客 R

那么为什么你可以将 Keras 模型定义为

library(keras)model <- keras_model_Sequence() %>% layer_dense(units = 32,activation = "relu") %>% layer_dense(units = 1)
library(keras)model <- keras_model_Sequence() %>% layer_dense(units = 32,activation = "relu") %>% layer_dense(units = 1) library(keras) library library ( keras keras ) 模型 <- keras_model_sequential() %>% 模型 <- keras_model_sequential ( ) %>% layer_dense(units = 32,activation = "relu") %>% ( = 32 = "relu" ) %>% layer_dense(units = 1) layer_dense ( = 1 ) (%>%s 和所有内容都很棒!)——然后训练和评估它,获得预测并绘制它们,所有这些都无需离开 R?

%>%

简短的回答是,您已经安装了 keras、tensorflow 和 reticulate。reticulate 在 R 进程中嵌入了一个 Python 会话。单个进程意味着单个地址空间:相同的对象存在,并且可以对其进行操作,无论它们是由 R 还是 Python 看到的。在此基础上,tensorflow 和 keras 包装了各自的 Python 库,让您可以编写实际上看起来像 R 的 R 代码。

keras

tensorflow reticulate reticulate embeds within tensorflow keras 这篇文章首先对简短的答案进行了一些阐述。然后我们更深入地了解后台发生的事情。

keras

tensorflow TensorFlow Keras 存在 tf.keras Keras TensorFlow 那么 keras、tensorflow、reticulate 是做什么用的?

首先,如果没有 reticulate,这一切都不可能实现。reticulate 是一个 R 包,旨在实现 R 和 Python 之间的无缝互操作性。如果我们确实想要,我们可以构建一个这样的 Keras 模型:

reticulate

reticulate

                                    

                                        m
                                    

                                        $