Skip to content

Commit

Permalink
2024 (#53)
Browse files Browse the repository at this point in the history
* bump version, suggest keras3

* update tf version

* the default version of keras-tuner is 1.4.7

* update tests

* update cran comments

* update tests

* fix integer issues

* switch to keras3

* fix test

* remove files

* typo

* typo

* no r code evaluation
  • Loading branch information
turgut090 authored Apr 13, 2024
1 parent c98ade5 commit d9ab72f
Show file tree
Hide file tree
Showing 22 changed files with 59 additions and 486 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/keras_tuner_stable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
fail-fast: false
matrix:
config:
- { os: macOS-latest, tf: '2.13.0', keras-tuner: '1.3.5'}
- { os: windows-latest, tf: '2.13.0', keras-tuner: '1.3.5'}
- { os: windows-latest, tf: 'nightly', keras-tuner: '1.3.5'}
- { os: macOS-latest, tf: 'nightly', keras-tuner: '1.3.5'}
- { os: macOS-latest, tf: '2.16.1', keras-tuner: '1.4.7'}
- { os: windows-latest, tf: '2.16.1', keras-tuner: '1.4.7'}
- { os: windows-latest, tf: 'nightly', keras-tuner: '1.4.7'}
- { os: macOS-latest, tf: 'nightly', keras-tuner: '1.4.7'}
env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
TF_VERSION: ${{ matrix.config.tf }}
Expand Down
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: kerastuneR
Type: Package
Title: Interface to 'Keras Tuner'
Version: 0.1.0.6
Version: 0.1.0.7
Authors@R: c(
person("Turgut", "Abdullayev", role = c("aut", "cre"),
email = "[email protected]"),
Expand All @@ -20,7 +20,7 @@ BugReports: https://github.com/EagerAI/kerastuneR/issues/
SystemRequirements: TensorFlow >= 2.0 (https://www.tensorflow.org/)
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Imports:
reticulate,
tensorflow,
Expand All @@ -35,7 +35,7 @@ Imports:
crayon,
magick
Suggests:
keras,
keras3,
knitr,
tfdatasets,
testthat,
Expand Down
2 changes: 1 addition & 1 deletion R/install.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ install_kerastuner <- function(version = NULL, ..., bayesian = TRUE, restart_ses


if (is.null(version) & !from_git) {
module_string <- paste0("keras-tuner==", '1.3.5')
module_string <- paste0("keras-tuner==", '1.4.7')
} else if (!is.null(version)) {
module_string <- paste0("keras-tuner==", version)
} else if (isTRUE(from_git)) {
Expand Down
3 changes: 2 additions & 1 deletion R/random_search.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ RandomSearch = function(hypermodel, objective, max_trials, seed = NULL,
if(is.null(hyperparameters))
args$hyperparameters <- NULL


if(!is.null(args$executions_per_trial))
args$executions_per_trial <- as.integer(args$executions_per_trial)

do.call(kerastuner$tuners$RandomSearch, args)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ y_data2 <- ifelse(runif(50,0,1) > 0.6, 1L,0L) %>% as.matrix()
This function returns a compiled model.

```
library(keras)
library(keras3)
library(tensorflow)
library(kerastuneR)
Expand Down
2 changes: 1 addition & 1 deletion cran-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This is a 4th submission of 'kerastuneR' 0.1.0.6

* Github actions: windows-latest
* Github actions: macOS-latest
* Github actions: ubuntu-16.04
* Github actions: ubuntu-20.04


## R CMD check results
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
library(testthat)
library(tensorflow)
library(kerastuneR)
library(keras)
library(keras3)

test_check("kerastuneR")

3 changes: 1 addition & 2 deletions tests/testthat/test-BayesOptim.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ source("utils.R")

test_succeeds("Can run Bayesian Optimization", {

library(keras)
library(tensorflow)
library(dplyr)
library(tfdatasets)
Expand Down Expand Up @@ -118,7 +117,7 @@ test_succeeds("Can run Bayesian Optimization", {

mnist_train$x = tf$cast(mnist_train$x, 'float32') / 255

mnist_train$x = k_reshape(mnist_train$x,shape = c(6e4,28,28,1))
mnist_train$x = tf$reshape(mnist_train$x,shape = c(6e4L,28L,28L,1L))
mnist_train$y = tf$dtypes$cast(mnist_train$y, 'float32')

if (!Sys.info()[1] %in% 'Windows') {
Expand Down
67 changes: 32 additions & 35 deletions tests/testthat/test-MNIST-conv.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,50 @@ context("build(hp) - MNIST")
source("utils.R")

test_succeeds("Can run hyper_class", {
library(keras)
library(dplyr)
library(tfdatasets)
library(kerastuneR)

conv_build_model = function(hp) {
'Builds a convolutional model.'
inputs = tf$keras$Input(shape=list(28L, 28L, 1L))
conv_build_model <- function(hp) {
inputs <- tf$keras$Input(shape = list(28L, 28L, 1L))
x <- inputs

x = inputs

for (i in 1:hp$Int('conv_layers', 1L, 3L, default=3L)) {
x = tf$keras$layers$Conv2D(filters = hp$Int(paste('filters_', i, sep = ''), 4L, 32L, step=4L, default=8L),
kernel_size = hp$Int(paste('kernel_size_', i, sep = ''), 3L, 5L),
activation ='relu',
padding='same')(x)
if (hp$Choice(paste('pooling', i, sep = ''), c('max', 'avg')) == 'max') {
x = tf$keras$layers$MaxPooling2D()(x)
for (i in 1:hp$Int('conv_layers', 1L, 3L, default = 3L)) {
x <- tf$keras$layers$Conv2D(
filters = hp$Int(paste('filters_', i, sep = ''), 4L, 32L, step = 4L, default = 8L),
kernel_size = hp$Int(paste('kernel_size_', i, sep = ''), 3L, 5L),
activation = 'relu',
padding = 'same'
)(x)

pool_type <- hp$Choice(paste('pooling', i, sep = ''), c('max', 'avg'))
if (pool_type == 'max') {
x <- tf$keras$layers$MaxPooling2D(pool_size = c(2L, 2))(x) # Adding pool_size argument
} else {
x = tf$keras$layers$AveragePooling2D()(x)
x <- tf$keras$layers$AveragePooling2D(pool_size = c(2L, 2))(x) # Adding pool_size argument
}
x = tf$keras$layers$BatchNormalization()(x)
x = tf$keras$layers$ReLU()(x)

x <- tf$keras$layers$BatchNormalization()(x)
x <- tf$keras$layers$ReLU()(x)
}
if (hp$Choice('global_pooling', c('max', 'avg')) == 'max') {
x = tf$keras$layers$GlobalMaxPool2D()(x)

global_pooling_type <- hp$Choice('global_pooling', c('max', 'avg'))
if (global_pooling_type == 'max') {
x <- tf$keras$layers$GlobalMaxPool2D()(x)
} else {
x = tf$keras$layers$GlobalAveragePooling2D()(x)
x <- tf$keras$layers$GlobalAveragePooling2D()(x)
}

outputs = tf$keras$layers$Dense(10L, activation='softmax')(x)
model = tf$keras$Model(inputs, outputs)
optimizer = hp$Choice('optimizer', c('adam', 'sgd'))
model %>% compile(optimizer, loss='sparse_categorical_crossentropy', metrics='accuracy')
outputs <- tf$keras$layers$Dense(10L, activation = 'softmax')(x)
model <- tf$keras$Model(inputs, outputs)

optimizer <- hp$Choice('optimizer', c('adam', 'sgd'))
model %>% compile(optimizer, loss = 'sparse_categorical_crossentropy', metrics = 'accuracy')

return(model)
}


testthat::expect_length(class(Hyperband(
hypermodel = conv_build_model,
objective='val_accuracy',
Expand All @@ -51,14 +57,6 @@ test_succeeds("Can run hyper_class", {
project_name = 'mnist')),5)


testthat::expect_match(Hyperband(
hypermodel = conv_build_model,
objective = 'val_accuracy',
max_epochs = 1,
factor = 2,
hyperband_iterations = 1,
directory = 'results_dir',
project_name = 'mnist') %>% capture.output(), 'keras_tuner.tuners.hyperband.Hyperband')

main = function() {
tuner = Hyperband(
Expand All @@ -74,8 +72,8 @@ test_succeeds("Can run hyper_class", {
c(mnist_train, mnist_test) %<-% mnist_data
rm(mnist_data)

mnist_train$x = k_reshape(mnist_train$x,shape = c(6e4,28,28,1))
mnist_test$x = k_reshape(mnist_test$x,shape = c(1e4,28,28,1))
mnist_train$x = tf$reshape(mnist_train$x,shape = c(6e4L,28L,28L,1L))
mnist_test$x = tf$reshape(mnist_test$x,shape = c(1e4L,28L,28L,1L))

mnist_train = tensor_slices_dataset(list(tf$dtypes$cast(mnist_train$x, 'float32') / 255., mnist_train$y)) %>%
dataset_shuffle(1e3) %>% dataset_batch(1e2) %>% dataset_repeat()
Expand All @@ -88,8 +86,7 @@ test_succeeds("Can run hyper_class", {
steps_per_epoch=600,
validation_data=mnist_test,
validation_steps=100,
epochs=1,
callbacks=c(tf$keras$callbacks$EarlyStopping('val_accuracy'))
epochs=1
)
}
})
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/test-build.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ context("build(hp)")
source("utils.R")

test_succeeds("Can run build(hp) and plot_tuner()", {
library(keras)
library(tensorflow)
library(dplyr)
library(kerastuneR)
Expand Down Expand Up @@ -75,7 +74,7 @@ test_succeeds("Can run build(hp) and plot_tuner()", {
best_model = tuner2$hypermodel$build(best_hps)

# Train the best model
best_model %>% fit(X_train, y_train, epochs=50, validation_split=0.2)
best_model %>% fit(X_train, y_train, epochs=5, validation_split=0.2)

}
})
Expand Down
9 changes: 3 additions & 6 deletions tests/testthat/test-hp-space.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ context('hp space')
source("utils.R")

test_succeeds("Can run hp-space", {
library(keras)
library(keras3)
library(dplyr)
library(kerastuneR)

Expand All @@ -14,16 +14,14 @@ test_succeeds("Can run hp-space", {
mnist_train$x = tf$dtypes$cast(mnist_train$x, 'float32') / 255.
mnist_test$x = tf$dtypes$cast(mnist_test$x, 'float32') / 255.

mnist_train$x = k_reshape(mnist_train$x,shape = c(6e4,28,28))
mnist_test$x = k_reshape(mnist_test$x,shape = c(1e4,28,28))
mnist_train$x = tf$reshape(mnist_train$x,shape = c(6e4L,28L,28L))
mnist_test$x = tf$reshape(mnist_test$x,shape = c(1e4L,28L,28L))


hp = HyperParameters()
hp$Choice('learning_rate',values =c(1e-1, 1e-3))
hp$Int('num_layers', 2L, 20L)

testthat::expect_match(capture.output(hp),'keras_tuner.engine.hyperparameters.hyperparameters.HyperParameters')


mnist_model = function(hp) {

Expand Down Expand Up @@ -55,7 +53,6 @@ test_succeeds("Can run hp-space", {
validation_split=0.2,
epochs=1)

testthat::expect_match(capture.output(tuner),'keras_tuner.tuners.randomsearch.RandomSearch')
})


Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-hyper_class.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ context("build(hp) - Hyperclass")
source("utils.R")

test_succeeds("Can run hyper_class", {
library(keras)
library(tensorflow)
library(dplyr)
library(kerastuneR)
Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/test-resnet.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ context("build(hp) - ResNet")
source("utils.R")

test_succeeds("Can run hyper_class", {
library(keras)

library(dplyr)
library(kerastuneR)

Expand All @@ -12,7 +12,6 @@ test_succeeds("Can run hyper_class", {
hypermodel = HyperResNet(input_shape = list(300L, 300L, 3L), classes = 10L)
hypermodel2 = HyperXception(input_shape = list(300L, 300L, 3L), classes = 10L)

testthat::expect_match(hypermodel %>% capture.output(),'keras_tuner.applications.resnet.HyperResNet')

tuner = Hyperband(
hypermodel = hypermodel,
Expand All @@ -21,7 +20,6 @@ test_succeeds("Can run hyper_class", {
directory = 'my_dir',
project_name='helloworld')

testthat::expect_match(tuner %>% capture.output(),'keras_tuner.tuners.hyperband.Hyperband')

train_data = cifar$train$x[1:30,1:32,1:32,1:3]
test_data = cifar$train$y[1:30,1] %>% as.matrix()
Expand Down
Loading

0 comments on commit d9ab72f

Please sign in to comment.