Skip to content

Commit

Permalink
e2e MNIST deep learning example for R (#1510)
Browse files Browse the repository at this point in the history
Rlang e2e deeplearning example

Signed-off-by: Botong Ou <[email protected]>
  • Loading branch information
oubotong authored Feb 28, 2023
1 parent 1e1819f commit 8c23bad
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
23 changes: 23 additions & 0 deletions e2e/v1/docs/rlang_mnist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package docs

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

e2e "github.com/tensorchord/envd/e2e/v1"
)

var _ = Describe("rlang_mnist", Ordered, func() {
exampleName := "rlang_mnist"
testcase := "e2e"
e := e2e.NewExample(e2e.BuildContextDirWithName(exampleName), testcase)
BeforeAll(e.BuildImage(true))
BeforeEach(e.RunContainer())
FIt("execute runtime command `Rscript`", func() {
res, err := e.ExecRuntimeCommand("rlang-mnist")
Expect(err).To(BeNil())
isNumeric := "TRUE"
Expect(res).To(BeEquivalentTo(isNumeric))
})
AfterEach(e.DestroyContainer())
})
10 changes: 10 additions & 0 deletions e2e/v1/docs/testdata/rlang_mnist/build.envd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# syntax=v1


def build():
base(dev=True)
install.r_lang()
install.python()
install.r_packages(name=["keras"])
install.python_packages(name=["tensorflow"])
runtime.command(commands={"rlang-mnist": "Rscript mnist.r 1> /dev/null"})
44 changes: 44 additions & 0 deletions e2e/v1/docs/testdata/rlang_mnist/mnist.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Sys.setenv(TF_CPP_MIN_LOG_LEVEL = "3")
library(keras)

mnist <- dataset_mnist()
x_train <- mnist$train$x
y_train <- mnist$train$y
x_test <- mnist$test$x
y_test <- mnist$test$y


# reshape
x_train <- array_reshape(x_train, c(nrow(x_train), 784))
x_test <- array_reshape(x_test, c(nrow(x_test), 784))
# rescale
x_train <- x_train / 255
x_test <- x_test / 255

y_train <- to_categorical(y_train, 10)
y_test <- to_categorical(y_test, 10)

model <- keras_model_sequential()
model %>%
layer_dense(units = 256, activation = 'relu', input_shape = c(784)) %>%
layer_dropout(rate = 0.4) %>%
layer_dense(units = 128, activation = 'relu') %>%
layer_dropout(rate = 0.3) %>%
layer_dense(units = 10, activation = 'softmax')

model %>% compile(
loss = 'categorical_crossentropy',
optimizer = optimizer_rmsprop(),
metrics = c('accuracy'),
)

history <- model %>% fit(
x_train, y_train,
epochs = 1, batch_size = 128,
validation_split = 0.2,
verbose=3
)

acc <- model %>% evaluate(x_test, y_test, verbose=3)

write(is.numeric(acc), stderr())

0 comments on commit 8c23bad

Please sign in to comment.