R语言中不能进行深度学习?

简介: R语言现在能也进行深度学习了,而且和python一样好,快来试一试吧。

更多深度文章,请关注:https://yq.aliyun.com/cloud


众所周知,R语言是统计分析最好用的语言。但在Keras和TensorFlow的帮助下,R语言也可以进行深度学习了。

在机器学习的语言的选择上,R和Python之间选择一直是一个有争议的话题。但随着深度学习的爆炸性增长,越来越多的人选择了Python,因为它有一个很大的深度学习库和框架,而R却没有(直到现在)。

但是我就是想使用R语言进入深度学习空间,所以我就从Python领域转入到了R领域,继续我的深度学习的研究了。这可能看起来几乎不可能的。但是今天这变成了可能。

随着Keras在R上的推出,R与Python的斗争回到了中心。Python慢​​慢成为了最流行的深度学习模型。但是,随着Keras库在R后端的发布,并且在后台还可以使用张力流(TensorFlow)(CPU和GPU兼容性),所以在深度学习领域,R将再次与Python打成平手。

下面我们将看到如何使用Tensorflow在R中安装Keras,并在RStudio的经典MNIST数据集上构建我们的第一个神经网络模型。

目录:

1.在后端安装带有张量的Keras。

2.使用Keras可以在R中构建不同类型的模型。

3.在R中使用MLP对MNIST手写数字进行分类。

4.将MNIST结果与Python中的等效代码进行比较。

5.结束笔记。

1.在后端安装带有TensorFlow的Keras。

在RStudio中安装Keras的步骤非常简单。只需按照以下步骤,您将很顺利的在R中创建您的第一个神经网络模型。

install.packages("devtools")
devtools::install_github("rstudio/keras")

上述步骤将从GitHub仓库加载keras库。现在是将keras加载到R并安装TensorFlow的时候了。

library(keras)

默认情况下,RStudio加载TensorFlow的CPU版本。使用以下命令下载TensorFlow的CPU版本。

install_tensorflow()

要为单个用户/桌面系统安装具有GPU支持的TensorFlow版本,请使用以下命令。

install_tensorflow(gpu=TRUE)

有关更多的用户安装,请参阅本安装指南

现在我们在RStudio中安装了keras和TensorFlow,让我们在R中启动和构建我们的第一个神经网络来解决MNIST数据集

2.使用keras可以在R中构建的不同类型的模型

以下是使用Keras可以在R中构建的模型列表。

1.多层感知器

2.卷积神经网络

3.循环神经网络

4.Skip-Gram模型

5.使用预先训练的模型,如VGG16,RESNET等

6.微调预先训练的模型。

让我们开始构建一个非常简单的MLP模型,只需一个隐藏的层来尝试分类手写数字。

3.使用R中的MLP对MNIST手写数字进行分类

#loading keras library
library(keras)
#loading the keras inbuilt mnist dataset
data<-dataset_mnist()
#separating train and test file
train_x<-data$train$x
train_y<-data$train$y
test_x<-data$test$x
test_y<-data$test$y
rm(data)
# converting a 2D array into a 1D array for feeding into the MLP and normalising the matrix
train_x <- array(train_x, dim = c(dim(train_x)[1], prod(dim(train_x)[-1]))) / 255
test_x <- array(test_x, dim = c(dim(test_x)[1], prod(dim(test_x)[-1]))) / 255
#converting the target variable to once hot encoded vectors using keras inbuilt function
train_y<-to_categorical(train_y,10)
test_y<-to_categorical(test_y,10)
#defining a keras sequential model
model <- keras_model_sequential()
#defining the model with 1 input layer[784 neurons], 1 hidden layer[784 neurons] with dropout rate 0.4 and 1 output layer[10 neurons]
#i.e number of digits from 0 to 9
model %>% 
layer_dense(units = 784, input_shape = 784) %>% 
layer_dropout(rate=0.4)%>%
layer_activation(activation = 'relu') %>% 
layer_dense(units = 10) %>% 
layer_activation(activation = 'softmax')
#compiling the defined model with metric = accuracy and optimiser as adam.
model %>% compile(
loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = c('accuracy')
)
#fitting the model on the training dataset
model %>% fit(train_x, train_y, epochs = 100, batch_size = 128)
#Evaluating model on the cross validation dataset
loss_and_metrics <- model %>% evaluate(test_x, test_y, batch_size = 128)

上述代码的训练精度为99.14,验证准确率为96.89。代码在i5处理器上运行,运行时间为13.5秒,而在TITANx GPU上,验证精度为98.44,平均运行时间为2秒。

4.MLP使用keras–R VS Python

为了比较起见,我也在Python中实现了上述的MNIST问题。我觉得在keras-R和Python中应该没有任何区别,因为R中的keras创建了一个conda实例并在其中运行keras。你可以尝试运行一下下面等效的python代码。

#importing the required libraries for the MLP model
import  keras
from  keras.models  import Sequential
import  numpy  as  np
#loading  the  MNIST dataset  from  keras
from  keras.datasets  import  mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
#reshaping the x_train, y_train, x_test and y_test to conform to MLP input and output dimensions
x_train=np.reshape(x_train,(x_train.shape[0],-1))/255
x_test=np.reshape(x_test,(x_test.shape[0],-1))/255
import pandas as pd
y_train=pd.get_dummies(y_train)
y_test=pd.get_dummies(y_test)
#performing one-hot encoding on target variables for train and test
y_train=np.array(y_train)
y_test=np.array(y_test)
#defining model with one input layer[784 neurons], 1 hidden layer[784 neurons] with dropout rate 0.4 and 1 output layer [10 #neurons]
model=Sequential()
from keras.layers import Dense
model.add(Dense(784, input_dim=784, activation='relu'))
keras.layers.core.Dropout(rate=0.4)
model.add(Dense(10,input_dim=784,activation='softmax'))
# compiling model using adam optimiser and accuracy as metric
model.compile(loss='categorical_crossentropy', optimizer="adam", metrics=['accuracy'])
# fitting model and performing validation
model.fit(x_train,y_train,epochs=50,batch_size=128,validation_data=(x_test,y_test))

上述模型在同一GPU上实现了98.42的验证精度。所以,我们最初猜到的结果是正确的。

5.结束笔记

如果这是你在R的第一个深度学习模型,我希望你喜欢它。通过一个非常简单的代码,您可以有98%位准确率对是否为手写数字进行分类。这应该是足够的动力让你开始深度学习。

如果您已经在Python中使用keras深度学习库,那么您将在R中找到keras库的语法和结构与Python中相似的地方。事实上,R中的keras包创建了一个conda环境,并安装了在该环境中运行keras所需的一切。但是,让我更为激动的是,现在看到数据科学家在R中建立现实生活中的深层次的学习模型。据说 - 竞争应该永远不会停止。我也想听听你对这一新发展观点的看法。你可以在下面留言分享你的看法。

本文由北邮@爱可可-爱生活老师推荐,阿里云云栖社区组织翻译。

文章原标题《Getting started with Deep Learning using Keras and TensorFlow in R》,作者: NSS 

译者:袁虎,审阅: 阿福

文章为简译,更为详细的内容,请查看原文

相关实践学习
在云上部署ChatGLM2-6B大模型(GPU版)
ChatGLM2-6B是由智谱AI及清华KEG实验室于2023年6月发布的中英双语对话开源大模型。通过本实验,可以学习如何配置AIGC开发环境,如何部署ChatGLM2-6B大模型。
相关文章
|
8天前
|
机器人 API 调度
基于 DMS Dify+Notebook+Airflow 实现 Agent 的一站式开发
本文提出“DMS Dify + Notebook + Airflow”三位一体架构,解决 Dify 在代码执行与定时调度上的局限。通过 Notebook 扩展 Python 环境,Airflow实现任务调度,构建可扩展、可运维的企业级智能 Agent 系统,提升大模型应用的工程化能力。
|
人工智能 前端开发 API
前端接入通义千问(Qwen)API:5 分钟实现你的 AI 问答助手
本文介绍如何在5分钟内通过前端接入通义千问(Qwen)API,快速打造一个AI问答助手。涵盖API配置、界面设计、流式响应、历史管理、错误重试等核心功能,并提供安全与性能优化建议,助你轻松集成智能对话能力到前端应用中。
664 154
|
14天前
|
人工智能 数据可视化 Java
Spring AI Alibaba、Dify、LangGraph 与 LangChain 综合对比分析报告
本报告对比Spring AI Alibaba、Dify、LangGraph与LangChain四大AI开发框架,涵盖架构、性能、生态及适用场景。数据截至2025年10月,基于公开资料分析,实际发展可能随技术演进调整。
930 152
|
负载均衡 Java 微服务
OpenFeign:让微服务调用像本地方法一样简单
OpenFeign是Spring Cloud中声明式微服务调用组件,通过接口注解简化远程调用,支持负载均衡、服务发现、熔断降级、自定义拦截器与编解码,提升微服务间通信开发效率与系统稳定性。
354 156
|
7天前
|
分布式计算 监控 API
DMS Airflow:企业级数据工作流编排平台的专业实践
DMS Airflow 是基于 Apache Airflow 构建的企业级数据工作流编排平台,通过深度集成阿里云 DMS(Data Management Service)系统的各项能力,为数据团队提供了强大的工作流调度、监控和管理能力。本文将从 Airflow 的高级编排能力、DMS 集成的特殊能力,以及 DMS Airflow 的使用示例三个方面,全面介绍 DMS Airflow 的技术架构与实践应用。
|
7天前
|
人工智能 自然语言处理 前端开发
Qoder全栈开发实战指南:开启AI驱动的下一代编程范式
Qoder是阿里巴巴于2025年发布的AI编程平台,首创“智能代理式编程”,支持自然语言驱动的全栈开发。通过仓库级理解、多智能体协同与云端沙箱执行,实现从需求到上线的端到端自动化,大幅提升研发效率,重塑程序员角色,引领AI原生开发新范式。
439 2