Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:
Focussing on {greta}

Nicholas Tierney njtierney

:octocat:
Focussing on {greta}
View GitHub Profile
View example-nested-for-loop.md
for (i in seq_len(3)){
  for(j in seq_len(3)){
    msg <- glue::glue("if i = {i}, and j = {j}, then i + j = {i+j}
                      
                      ")
    cat(msg)
  }
}
#&gt; if i = 1, and j = 1, then i + j = 2
@njtierney
njtierney / tf-fun-no-lexical-scope.md
Created Jun 17, 2022
tensorflow tf_function doesn't support lexical scoping
View tf-fun-no-lexical-scope.md
library(tensorflow)
inner_fun <- function(x, y, b){
  z <- tf$square(x)
  z + y + b
}

tf_inner_fun <- tf_function(inner_fun)
#> Loaded Tensorflow version 2.8.0
View map-safely.md
library(purrr)
library(tidyverse)

safe_sum <- safely(sum)

safe_sum(5)
#> $result
#> [1] 5
#&gt; 
View applying-classes.md
thingy <- list(
  x = letters,
  y = LETTERS
)

thingy
#> $x
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#&gt; [20] "t" "u" "v" "w" "x" "y" "z"
View demo-tf-function.md
library(tensorflow)
f <- function(x){
  z <- tf$square(x)
  z + z
}

tf_f <- tf_function(f)
#> Loaded Tensorflow version 2.8.0
View tf-function.R
# in TF1
x <- tensor(x, shape..)
y <- tensor(y, shape..)
z <- add(x,y)
sess <- session
sess.run(z, dict = c(x = 1, y = 2))
# in TF2
new_add <- function(x, y){
tensorflow::tf$math$add(x, y)
View remove-nulls-purrr.md
list_of_things <- list(
  NULL,
  "result",
  NULL,
  NULL,
  "result"
)

list_of_things
View map2_lgl-for-comparison.md
mat_1 <- list(
  matrix(1:10),
  matrix(11:20)
)

mat_2 <- list(
  matrix(11:20),
  matrix(21:30)
)
View gghalves.md
library(ggplot2)
library(palmerpenguins)

ggplot(penguins,
       aes(x = species,
           y = flipper_length_mm,
           fill = island,
           colour = island)) + 
 geom_jitter() 
View read-optimiser-table.md
tbl_chr <- "| greta optimiser  | TF 1.x function | TF2.0 function |
  | - | - | - |
  | `nelder_mead`  | `tf$contrib$opt$ScipyOptimizerInterface` | `tfp$optimizer$nelder_mead_minimize` |
  | `powell`  | `tf$contrib$opt$ScipyOptimizerInterface` | ? |
  | `cg`  | `tf$contrib$opt$ScipyOptimizerInterface` | ? |
  | `bfgs`  | `tf$contrib$opt$ScipyOptimizerInterface` | `tfp$optimizer$bfgs_minimize` |
  | `newton_cg`  | `tf$contrib$opt$ScipyOptimizerInterface` | ? |
  | `l_bfgs_b`  | `tf$contrib$opt$ScipyOptimizerInterface` | ? |
 | `tnc` | `tf$contrib$opt$ScipyOptimizerInterface` | ? |