tf$keras$callbacks

Published by onesixx on

tf$keras$callbacks

tf$keras로 training 중에 , 동작을 확장/수정하고자 모델로 전달하는 객체

그 중 유용한 callback.

  • tf$keras$callbacks$ModelCheckpoint
  • tf$keras$callbacks$LearningRateScheduler
  • tf$keras$callbacks$EarlyStopping
  • tf$keras$callbacks$TensorBoard

model & weight 저장

source(file.path(getwd(),"../00.global_dl.R"))

# https://tensorflow.rstudio.com/guide/tfdatasets/introduction/
# devtools::install_github("rstudio/tfdatasets")
library(tensorflow)
library(tfautograph)
library(keras)
library(tfdatasets)

mnist <- dataset_mnist()
c(c(train_imgData, train_labels), c(test_imgData, test_labels)) %<-% mnist

IMG_ROWS <- 28; IMG_COLS <- 28
train_x <- train_imgData %>% array_reshape(dim=c(nrow(train_imgData), IMG_ROWS*IMG_COLS))
test_x  <- test_imgData  %>% array_reshape(dim=c(nrow(test_imgData),  IMG_ROWS*IMG_COLS))

train_x <- train_x / 255
test_x  <- test_x  / 255

train_y <- to_categorical(train_labels, 10)
test_y  <- to_categorical(test_labels,  10)


model <- keras_model_sequential() %>%
  layer_dense(units=512, activation="relu", input_shape=c(28*28)) %>%
  layer_dense(units=10,  activation="softmax")

model %>% compile(
  optimizer = "rmsprop",
  loss = "categorical_crossentropy",
  metrics = c("accuracy")
)

BATCH_SIZE  <- 128
EPOCHS      <- 10
history <- model %>% fit(
  train_x, 
  train_y,
  batch_size = BATCH_SIZE,
  epochs = EPOCHS,
  validation_split = 0.2
)

metrics <- model %>% evaluate(test_x, test_y)

model.save_weights('./')

save_model_weights_tf(model, './weights/my_model')
load_model_weights_tf(model, './weights/my_model')

tf$data$dataset

import tensorflow as tf
import tensorflow_datasets as tfds

builders=tfds.list_builders()
print(builders)

data, info = tfds.load("mnist", with_info=True)
train_data, test_data = data['train'], data['test']

print(info)
print(train_data)
source('/home/sixx_skcc/RCODE/00.global_dl.R')
# https://tensorflow.rstudio.com/guide/tfdatasets/introduction/
# devtools::install_github("rstudio/tfdatasets")
library(tfdtasets)

mnist <- dataset_mnist()
c(c(train_imgData, train_labels), c(test_imgData, test_labels)) %<-% mnist
train_imgData <- mnist$train$x
train_labels <- mnist$train$y
test_imgData  <- mnist$test$x
test_labels  <- mnist$test$y
Categories: Keras

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x