R 中的 TF 估计器 API
我们在第 2 章中了解了 TensorFlow 估计器 API。在 R 中,此 API 使用 tfestimator
R 包实现。
例如,我们提供了 MLP 模型的演练,用于在以下链接中对来自 MNIST 数据集的手写数字进行分类: https://tensorflow.rstudio.com/tfestimators/articles/examples/mnist.html 。
您可以按照 Jupyter R 笔记本中的代码ch-17b_TFE_Ttimator_in_R
。
- 首先,加载库:
library(tensorflow)
library(tfestimators)
- 定义超参数:
batch_size <- 128
n_classes <- 10
n_steps <- 100
- 准备数据:
# initialize data directory
data_dir <- "~/datasets/mnist"
dir.create(data_dir, recursive = TRUE, showWarnings = FALSE)
# download the MNIST data sets, and read them into R
sources <- list(
train = list(
x = "https://storage.googleapis.com/cvdf-datasets/mnist/train-images-idx3-ubyte.gz",
y = "https://storage.googleapis.com/cvdf-datasets/mnist/train-labels-idx1-ubyte.gz"
),
test = list(
x = "https://storage.googleapis.com/cvdf-datasets/mnist/t10k-images-idx3-ubyte.gz",
y = "https://storage.googleapis.com/cvdf-datasets/mnist/t10k-labels-idx1-ubyte.gz"
)
)
# read an MNIST file (encoded in IDX format)
read_idx <- function(file) {
# create binary connection to file
conn <- gzfile(file, open = "rb")
on.exit(close(conn), add = TRUE)
# read the magic number as sequence of 4 bytes
magic <- readBin(conn, what="raw", n=4, endian="big")
ndims <- as.integer(magic[[4]])
# read the dimensions (32-bit integers)
dims <- readBin(conn,what="integer",n=ndims,endian="big")
# read the rest in as a raw vector
data <- readBin(conn,what="raw",n=prod(dims),endian="big")
# convert to an integer vecto
converted <- as.integer(data)
# return plain vector for 1-dim array
if (length(dims) == 1)
return(converted)
# wrap 3D data into matrix
matrix(converted,nrow=dims[1],ncol=prod(dims[-1]),byrow=TRUE)
}
mnist <- rapply(sources,classes="character",how ="list",function(url) {
# download + extract the file at the URL
target <- file.path(data_dir, basename(url))
if (!file.exists(target))
download.file(url, target)
# read the IDX file
read_idx(target)
})
# convert training data intensities to 0-1 range
mnist$train$x <- mnist$train$x / 255
mnist$test$x <- mnist$test$x / 255
从下载的 gzip 文件中读取数据,然后归一化以落入[0,1]范围。
- 定义模型:
# construct a linear classifier
classifier <- linear_classifier(
feature_columns = feature_columns(
column_numeric("x", shape = shape(784L))
),
n_classes = n_classes # 10 digits
)
# construct an input function generator
mnist_input_fn <- function(data, ...) {
input_fn(
data,
response = "y",
features = "x",
batch_size = batch_size,
...
)
}
- 训练模型:
train(classifier,input_fn=mnist_input_fn(mnist$train),steps=n_steps)
- 评估模型:
evaluate(classifier,input_fn=mnist_input_fn(mnist$test),steps=200)
输出如下:
Evaluation completed after 79 steps but 200 steps was specified
average_loss | 损失 | global_step | 准确性 |
---|---|---|---|
0.35656 | 45.13418 | 100 | 0.9057 |
太酷!!
通过以下链接查找 R 中 TF 估计器的更多示例:https://tensorflow.rstudio.com/tfestimators/articles/examples/
有关tensorflow
R 包的更多文档可以在以下链接中找到:https://tensorflow.rstudio.com/tfestimators/reference/