diff --git a/404.html b/404.html index 3ba21d9865feed1219f0e7cedb19ea8ca8684514..6204d0019c68acadfb044b606930d448056d815b 100644 --- a/404.html +++ b/404.html @@ -1,217 +1,122 @@ - - - - + + + + - - + Page not found (404) • torch - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + - - - - - - - - - - -
-
-
+
+ + +
-
- - - - +
- -
- +
+
+ - - diff --git a/CONTRIBUTING.html b/CONTRIBUTING.html index 7ee0fc664f39a5b5f6f045047cee1ba260406b72..e82b42c8962df7bee3ae5ba1aece4c0e4c94252e 100644 --- a/CONTRIBUTING.html +++ b/CONTRIBUTING.html @@ -1,232 +1,126 @@ - - - - - - - -Contributing to torch • torch - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Contributing to torch • torch - - - - - - - - - - -
-
-
+
+ +
+
-
- -
- +
+ - - - + diff --git a/LICENSE-text.html b/LICENSE-text.html index dfc578a8292bc0fe440342dda8cc0de03d7ef214..b4fb5b505c39c3fc044f6e5e66f5fd4e1c1f8ed7 100644 --- a/LICENSE-text.html +++ b/LICENSE-text.html @@ -1,219 +1,100 @@ - - - - - - - -License • torch - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -License • torch - - - - - - - - - - -
-
-
+
+ +
+
-
- - - - -
+
- -
- +
+ - - - + diff --git a/LICENSE.html b/LICENSE.html index 92264f0a3771ad4081de70df09bad7b2dfa0f3e6..c8d67127f87fe47309f417e0b66b7014b541f1ee 100644 --- a/LICENSE.html +++ b/LICENSE.html @@ -1,185 +1,78 @@ - - - - - - - -MIT License • torch - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -MIT License • torch - - - - - - - - - - -
-
-
+
+ +
+
-
- +
- +
+
+ - - - + diff --git a/articles/distributions.html b/articles/distributions.html index 7f5bd9ee982d9ba8be6000a3cc73973d7ec627bf..62761fda352fdf2951eac4eae30c9ef470520a2c 100644 --- a/articles/distributions.html +++ b/articles/distributions.html @@ -4,14 +4,14 @@ - + + Distributions • torch - - - + + + - - + + + - - - +
+ - -
-
- + diff --git a/articles/examples/basic-autograd.html b/articles/examples/basic-autograd.html index 9867663689bd2f6240fe5f72cffa808f3563dd09..37f5b03820a21714752b15210985e27592ec27d8 100644 --- a/articles/examples/basic-autograd.html +++ b/articles/examples/basic-autograd.html @@ -4,14 +4,14 @@ - + + basic-autograd • torch - - - + + + - - + + +
- -
- +
+ - -
-
- -
+ diff --git a/articles/examples/basic-autograd_files/anchor-sections-1.0/anchor-sections.css b/articles/examples/basic-autograd_files/anchor-sections-1.0/anchor-sections.css deleted file mode 100644 index 07aee5fcb8398a53ea2189cff95cbe6504116d96..0000000000000000000000000000000000000000 --- a/articles/examples/basic-autograd_files/anchor-sections-1.0/anchor-sections.css +++ /dev/null @@ -1,4 +0,0 @@ -/* Styles for section anchors */ -a.anchor-section {margin-left: 10px; visibility: hidden; color: inherit;} -a.anchor-section::before {content: '#';} -.hasAnchor:hover a.anchor-section {visibility: visible;} diff --git a/articles/examples/basic-autograd_files/anchor-sections-1.0/anchor-sections.js b/articles/examples/basic-autograd_files/anchor-sections-1.0/anchor-sections.js deleted file mode 100644 index 570f99a0a8775e26c9dac3b1dd7b82cbe6dbfae3..0000000000000000000000000000000000000000 --- a/articles/examples/basic-autograd_files/anchor-sections-1.0/anchor-sections.js +++ /dev/null @@ -1,33 +0,0 @@ -// Anchor sections v1.0 written by Atsushi Yasumoto on Oct 3rd, 2020. -document.addEventListener('DOMContentLoaded', function() { - // Do nothing if AnchorJS is used - if (typeof window.anchors === 'object' && anchors.hasOwnProperty('hasAnchorJSLink')) { - return; - } - - const h = document.querySelectorAll('h1, h2, h3, h4, h5, h6'); - - // Do nothing if sections are already anchored - if (Array.from(h).some(x => x.classList.contains('hasAnchor'))) { - return null; - } - - // Use section id when pandoc runs with --section-divs - const section_id = function(x) { - return ((x.classList.contains('section') || (x.tagName === 'SECTION')) - ? x.id : ''); - }; - - // Add anchors - h.forEach(function(x) { - const id = x.id || section_id(x.parentElement); - if (id === '') { - return null; - } - let anchor = document.createElement('a'); - anchor.href = '#' + id; - anchor.classList = ['anchor-section']; - x.classList.add('hasAnchor'); - x.appendChild(anchor); - }); -}); diff --git a/articles/examples/basic-nn-module.html b/articles/examples/basic-nn-module.html index 5e00ff06efa1f4e802d7bb40461586dc60cca2d4..402563bb0136788a7fb70c103ebb47fb0d047b41 100644 --- a/articles/examples/basic-nn-module.html +++ b/articles/examples/basic-nn-module.html @@ -4,14 +4,14 @@ - + + basic-nn-module • torch - - - + + + - - + + +
- -
- +
+ - -
-
- -
+ diff --git a/articles/examples/basic-nn-module_files/anchor-sections-1.0/anchor-sections.css b/articles/examples/basic-nn-module_files/anchor-sections-1.0/anchor-sections.css deleted file mode 100644 index 07aee5fcb8398a53ea2189cff95cbe6504116d96..0000000000000000000000000000000000000000 --- a/articles/examples/basic-nn-module_files/anchor-sections-1.0/anchor-sections.css +++ /dev/null @@ -1,4 +0,0 @@ -/* Styles for section anchors */ -a.anchor-section {margin-left: 10px; visibility: hidden; color: inherit;} -a.anchor-section::before {content: '#';} -.hasAnchor:hover a.anchor-section {visibility: visible;} diff --git a/articles/examples/basic-nn-module_files/anchor-sections-1.0/anchor-sections.js b/articles/examples/basic-nn-module_files/anchor-sections-1.0/anchor-sections.js deleted file mode 100644 index 570f99a0a8775e26c9dac3b1dd7b82cbe6dbfae3..0000000000000000000000000000000000000000 --- a/articles/examples/basic-nn-module_files/anchor-sections-1.0/anchor-sections.js +++ /dev/null @@ -1,33 +0,0 @@ -// Anchor sections v1.0 written by Atsushi Yasumoto on Oct 3rd, 2020. -document.addEventListener('DOMContentLoaded', function() { - // Do nothing if AnchorJS is used - if (typeof window.anchors === 'object' && anchors.hasOwnProperty('hasAnchorJSLink')) { - return; - } - - const h = document.querySelectorAll('h1, h2, h3, h4, h5, h6'); - - // Do nothing if sections are already anchored - if (Array.from(h).some(x => x.classList.contains('hasAnchor'))) { - return null; - } - - // Use section id when pandoc runs with --section-divs - const section_id = function(x) { - return ((x.classList.contains('section') || (x.tagName === 'SECTION')) - ? x.id : ''); - }; - - // Add anchors - h.forEach(function(x) { - const id = x.id || section_id(x.parentElement); - if (id === '') { - return null; - } - let anchor = document.createElement('a'); - anchor.href = '#' + id; - anchor.classList = ['anchor-section']; - x.classList.add('hasAnchor'); - x.appendChild(anchor); - }); -}); diff --git a/articles/examples/dataset.html b/articles/examples/dataset.html index 4d1dc1f1608312eb25641b1f4bcd901dabc9c79f..85a6e85ff16adbe7a9db94608793955a4e6861ae 100644 --- a/articles/examples/dataset.html +++ b/articles/examples/dataset.html @@ -4,14 +4,14 @@ - + + dataset • torch - - - + + + - - + + +
- -
- +
+ - -
-
- -
+ diff --git a/articles/examples/dataset_files/anchor-sections-1.0/anchor-sections.css b/articles/examples/dataset_files/anchor-sections-1.0/anchor-sections.css deleted file mode 100644 index 07aee5fcb8398a53ea2189cff95cbe6504116d96..0000000000000000000000000000000000000000 --- a/articles/examples/dataset_files/anchor-sections-1.0/anchor-sections.css +++ /dev/null @@ -1,4 +0,0 @@ -/* Styles for section anchors */ -a.anchor-section {margin-left: 10px; visibility: hidden; color: inherit;} -a.anchor-section::before {content: '#';} -.hasAnchor:hover a.anchor-section {visibility: visible;} diff --git a/articles/examples/dataset_files/anchor-sections-1.0/anchor-sections.js b/articles/examples/dataset_files/anchor-sections-1.0/anchor-sections.js deleted file mode 100644 index 570f99a0a8775e26c9dac3b1dd7b82cbe6dbfae3..0000000000000000000000000000000000000000 --- a/articles/examples/dataset_files/anchor-sections-1.0/anchor-sections.js +++ /dev/null @@ -1,33 +0,0 @@ -// Anchor sections v1.0 written by Atsushi Yasumoto on Oct 3rd, 2020. -document.addEventListener('DOMContentLoaded', function() { - // Do nothing if AnchorJS is used - if (typeof window.anchors === 'object' && anchors.hasOwnProperty('hasAnchorJSLink')) { - return; - } - - const h = document.querySelectorAll('h1, h2, h3, h4, h5, h6'); - - // Do nothing if sections are already anchored - if (Array.from(h).some(x => x.classList.contains('hasAnchor'))) { - return null; - } - - // Use section id when pandoc runs with --section-divs - const section_id = function(x) { - return ((x.classList.contains('section') || (x.tagName === 'SECTION')) - ? x.id : ''); - }; - - // Add anchors - h.forEach(function(x) { - const id = x.id || section_id(x.parentElement); - if (id === '') { - return null; - } - let anchor = document.createElement('a'); - anchor.href = '#' + id; - anchor.classList = ['anchor-section']; - x.classList.add('hasAnchor'); - x.appendChild(anchor); - }); -}); diff --git a/articles/examples/index.html b/articles/examples/index.html index 26ce1f2f4f8dd4f329f3b3778a1ac79e5a69b7cd..6b8bb3e7715f5fb6192998b8e31870bad983017c 100644 --- a/articles/examples/index.html +++ b/articles/examples/index.html @@ -4,14 +4,14 @@ - + + Examples • torch - - - + + + - - + + +
- -
- +
+ - -
-
- -
+ diff --git a/articles/examples/index_files/anchor-sections-1.0/anchor-sections.css b/articles/examples/index_files/anchor-sections-1.0/anchor-sections.css deleted file mode 100644 index 07aee5fcb8398a53ea2189cff95cbe6504116d96..0000000000000000000000000000000000000000 --- a/articles/examples/index_files/anchor-sections-1.0/anchor-sections.css +++ /dev/null @@ -1,4 +0,0 @@ -/* Styles for section anchors */ -a.anchor-section {margin-left: 10px; visibility: hidden; color: inherit;} -a.anchor-section::before {content: '#';} -.hasAnchor:hover a.anchor-section {visibility: visible;} diff --git a/articles/examples/index_files/anchor-sections-1.0/anchor-sections.js b/articles/examples/index_files/anchor-sections-1.0/anchor-sections.js deleted file mode 100644 index 570f99a0a8775e26c9dac3b1dd7b82cbe6dbfae3..0000000000000000000000000000000000000000 --- a/articles/examples/index_files/anchor-sections-1.0/anchor-sections.js +++ /dev/null @@ -1,33 +0,0 @@ -// Anchor sections v1.0 written by Atsushi Yasumoto on Oct 3rd, 2020. -document.addEventListener('DOMContentLoaded', function() { - // Do nothing if AnchorJS is used - if (typeof window.anchors === 'object' && anchors.hasOwnProperty('hasAnchorJSLink')) { - return; - } - - const h = document.querySelectorAll('h1, h2, h3, h4, h5, h6'); - - // Do nothing if sections are already anchored - if (Array.from(h).some(x => x.classList.contains('hasAnchor'))) { - return null; - } - - // Use section id when pandoc runs with --section-divs - const section_id = function(x) { - return ((x.classList.contains('section') || (x.tagName === 'SECTION')) - ? x.id : ''); - }; - - // Add anchors - h.forEach(function(x) { - const id = x.id || section_id(x.parentElement); - if (id === '') { - return null; - } - let anchor = document.createElement('a'); - anchor.href = '#' + id; - anchor.classList = ['anchor-section']; - x.classList.add('hasAnchor'); - x.appendChild(anchor); - }); -}); diff --git a/articles/examples/mnist-cnn.html b/articles/examples/mnist-cnn.html deleted file mode 100644 index 8c102afbd76efd8e1f23f2d48283a0e3a160eaf3..0000000000000000000000000000000000000000 --- a/articles/examples/mnist-cnn.html +++ /dev/null @@ -1,259 +0,0 @@ - - - - - - - -mnist-cnn • torch - - - - - - - - - - -
-
- - - - -
-
- - - - -
dir <- "~/Downloads/mnist"
-
-ds <- mnist_dataset(
-  dir,
-  download = TRUE,
-  transform = function(x) {
-    x <- x$to(dtype = torch_float())/256
-    x[newaxis,..]
-  }
-)
-dl <- dataloader(ds, batch_size = 32, shuffle = TRUE)
-
-net <- nn_module(
-  "Net",
-  initialize = function() {
-    self$conv1 <- nn_conv2d(1, 32, 3, 1)
-    self$conv2 <- nn_conv2d(32, 64, 3, 1)
-    self$dropout1 <- nn_dropout2d(0.25)
-    self$dropout2 <- nn_dropout2d(0.5)
-    self$fc1 <- nn_linear(9216, 128)
-    self$fc2 <- nn_linear(128, 10)
-  },
-  forward = function(x) {
-    x <- self$conv1(x)
-    x <- nnf_relu(x)
-    x <- self$conv2(x)
-    x <- nnf_relu(x)
-    x <- nnf_max_pool2d(x, 2)
-    x <- self$dropout1(x)
-    x <- torch_flatten(x, start_dim = 2)
-    x <- self$fc1(x)
-    x <- nnf_relu(x)
-    x <- self$dropout2(x)
-    x <- self$fc2(x)
-    output <- nnf_log_softmax(x, dim=1)
-    output
-  }
-)
-
-model <- net()
-optimizer <- optim_sgd(model$parameters, lr = 0.01)
-
-epochs <- 10
-
-for (epoch in 1:10) {
-
-  pb <- progress::progress_bar$new(
-    total = length(dl),
-    format = "[:bar] :eta Loss: :loss"
-  )
-  l <- c()
-
-  for (b in enumerate(dl)) {
-    optimizer$zero_grad()
-    output <- model(b[[1]])
-    loss <- nnf_nll_loss(output, b[[2]])
-    loss$backward()
-    optimizer$step()
-    l <- c(l, loss$item())
-    pb$tick(tokens = list(loss = mean(l)))
-  }
-
-  cat(sprintf("Loss at epoch %d: %3f\n", epoch, mean(l)))
-}
-
- - - -
- - - -
- -
-

Site built with pkgdown 1.5.1.

-
- -
-
- - - - - - diff --git a/articles/examples/mnist-cnn_files/accessible-code-block-0.0.1/empty-anchor.js b/articles/examples/mnist-cnn_files/accessible-code-block-0.0.1/empty-anchor.js deleted file mode 100644 index ca349fd6a570108bde9d7daace534cd651c5f042..0000000000000000000000000000000000000000 --- a/articles/examples/mnist-cnn_files/accessible-code-block-0.0.1/empty-anchor.js +++ /dev/null @@ -1,15 +0,0 @@ -// Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> -// v0.0.1 -// Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. - -document.addEventListener('DOMContentLoaded', function() { - const codeList = document.getElementsByClassName("sourceCode"); - for (var i = 0; i < codeList.length; i++) { - var linkList = codeList[i].getElementsByTagName('a'); - for (var j = 0; j < linkList.length; j++) { - if (linkList[j].innerHTML === "") { - linkList[j].setAttribute('aria-hidden', 'true'); - } - } - } -}); diff --git a/articles/examples/mnist-dcgan.html b/articles/examples/mnist-dcgan.html deleted file mode 100644 index a7a103c5d56851d1fc1fd3615b0922e3c62c964d..0000000000000000000000000000000000000000 --- a/articles/examples/mnist-dcgan.html +++ /dev/null @@ -1,340 +0,0 @@ - - - - - - - -mnist-dcgan • torch - - - - - - - - - - -
-
- - - - -
-
- - - - -
library(torch)
-
-dir <- "~/Downloads/mnist"
-
-ds <- mnist_dataset(
-  dir,
-  download = TRUE,
-  transform = function(x) {
-    x <- x$to(dtype = torch_float())/256
-    x <- 2*(x - 0.5)
-    x[newaxis,..]
-  }
-)
-dl <- dataloader(ds, batch_size = 32, shuffle = TRUE)
-
-generator <- nn_module(
-  "generator",
-  initialize = function(latent_dim, out_channels) {
-    self$main <- nn_sequential(
-      nn_conv_transpose2d(latent_dim, 512, kernel_size = 4,
-                          stride = 1, padding = 0, bias = FALSE),
-      nn_batch_norm2d(512),
-      nn_relu(),
-      nn_conv_transpose2d(512, 256, kernel_size = 4,
-                          stride = 2, padding = 1, bias = FALSE),
-      nn_batch_norm2d(256),
-      nn_relu(),
-      nn_conv_transpose2d(256, 128, kernel_size = 4,
-                          stride = 2, padding = 1, bias = FALSE),
-      nn_batch_norm2d(128),
-      nn_relu(),
-      nn_conv_transpose2d(128, out_channels, kernel_size = 4,
-                          stride = 2, padding = 3, bias = FALSE),
-      nn_tanh()
-    )
-  },
-  forward = function(input) {
-    self$main(input)
-  }
-)
-
-discriminator <- nn_module(
-  "discriminator",
-  initialize = function(in_channels) {
-    self$main <- nn_sequential(
-      nn_conv2d(in_channels, 16, kernel_size = 4, stride = 2, padding = 1, bias = FALSE),
-      nn_leaky_relu(0.2, inplace = TRUE),
-      nn_conv2d(16, 32, kernel_size = 4, stride = 2, padding = 1, bias = FALSE),
-      nn_batch_norm2d(32),
-      nn_leaky_relu(0.2, inplace = TRUE),
-      nn_conv2d(32, 64, kernel_size = 4, stride = 2, padding = 1, bias = FALSE),
-      nn_batch_norm2d(64),
-      nn_leaky_relu(0.2, inplace = TRUE),
-      nn_conv2d(64, 128, kernel_size = 4, stride = 2, padding = 1, bias = FALSE),
-      nn_leaky_relu(0.2, inplace = TRUE)
-    )
-    self$linear <- nn_linear(128, 1)
-    self$sigmoid <- nn_sigmoid()
-  },
-  forward = function(input) {
-    x <- self$main(input)
-    x <- torch_flatten(x, start_dim = 2)
-    x <- self$linear(x)
-    self$sigmoid(x)
-  }
-)
-
-plot_gen <- function(noise) {
-  img <- G(noise)
-  img <- img$cpu()
-  img <- img[1,1,,,newaxis]/2 + 0.5
-  img <- torch_stack(list(img, img, img), dim = 2)[..,1]
-  img <- as.raster(as_array(img))
-  plot(img)
-}
-
-device <- torch_device(ifelse(cuda_is_available(),  "cuda", "cpu"))
-
-G <- generator(latent_dim = 100, out_channels = 1)
-D <- discriminator(in_channels = 1)
-
-init_weights <- function(m) {
-  if (grepl("conv", m$.classes[[1]])) {
-    nn_init_normal_(m$weight$data(), 0.0, 0.02)
-  } else if (grepl("batch_norm", m$.classes[[1]])) {
-    nn_init_normal_(m$weight$data(), 1.0, 0.02)
-    nn_init_constant_(m$bias$data(), 0)
-  }
-}
-
-G[[1]]$apply(init_weights)
-D[[1]]$apply(init_weights)
-
-G$to(device = device)
-D$to(device = device)
-
-G_optimizer <- optim_adam(G$parameters, lr = 2 * 1e-4, betas = c(0.5, 0.999))
-D_optimizer <- optim_adam(D$parameters, lr = 2 * 1e-4, betas = c(0.5, 0.999))
-
-fixed_noise <- torch_randn(1, 100, 1, 1, device = device)
-
-loss <- nn_bce_loss()
-
-for (epoch in 1:10) {
-
-  pb <- progress::progress_bar$new(
-    total = length(dl),
-    format = "[:bar] :eta Loss D: :lossd Loss G: :lossg"
-  )
-  lossg <- c()
-  lossd <- c()
-
-  for (b in enumerate(dl)) {
-
-    y_real <- torch_ones(32, device = device)
-    y_fake <- torch_zeros(32, device = device)
-
-    noise <- torch_randn(32, 100, 1, 1, device = device)
-    fake <- G(noise)
-
-    img <- b[[1]]$to(device = device)
-
-    # train the discriminator ---
-    D_loss <- loss(D(img), y_real) + loss(D(fake$detach()), y_fake)
-
-    D_optimizer$zero_grad()
-    D_loss$backward()
-    D_optimizer$step()
-
-    # train the generator ---
-
-    G_loss <- loss(D(fake), y_real)
-
-    G_optimizer$zero_grad()
-    G_loss$backward()
-    G_optimizer$step()
-
-    lossd <- c(lossd, D_loss$item())
-    lossg <- c(lossg, G_loss$item())
-    pb$tick(tokens = list(lossd = mean(lossd), lossg = mean(lossg)))
-  }
-  plot_gen(fixed_noise)
-
-  cat(sprintf("Epoch %d - Loss D: %3f Loss G: %3f\n", epoch, mean(lossd), mean(lossg)))
-}
-
- - - -
- - - -
- -
-

Site built with pkgdown 1.5.1.

-
- -
-
- - - - - - diff --git a/articles/examples/mnist-dcgan_files/accessible-code-block-0.0.1/empty-anchor.js b/articles/examples/mnist-dcgan_files/accessible-code-block-0.0.1/empty-anchor.js deleted file mode 100644 index ca349fd6a570108bde9d7daace534cd651c5f042..0000000000000000000000000000000000000000 --- a/articles/examples/mnist-dcgan_files/accessible-code-block-0.0.1/empty-anchor.js +++ /dev/null @@ -1,15 +0,0 @@ -// Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> -// v0.0.1 -// Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. - -document.addEventListener('DOMContentLoaded', function() { - const codeList = document.getElementsByClassName("sourceCode"); - for (var i = 0; i < codeList.length; i++) { - var linkList = codeList[i].getElementsByTagName('a'); - for (var j = 0; j < linkList.length; j++) { - if (linkList[j].innerHTML === "") { - linkList[j].setAttribute('aria-hidden', 'true'); - } - } - } -}); diff --git a/articles/examples/mnist-mlp.html b/articles/examples/mnist-mlp.html deleted file mode 100644 index df53285e9cd0f55ede1887148ea1c8e1f9ef74bc..0000000000000000000000000000000000000000 --- a/articles/examples/mnist-mlp.html +++ /dev/null @@ -1,247 +0,0 @@ - - - - - - - -mnist-mlp • torch - - - - - - - - - - -
-
- - - - -
-
- - - - -
dir <- "~/Downloads/mnist"
-
-ds <- mnist_dataset(
-  dir,
-  download = TRUE,
-  transform = function(x) {
-    x$to(dtype = torch_float())/256
-  }
-)
-dl <- dataloader(ds, batch_size = 32, shuffle = TRUE)
-
-net <- nn_module(
-  "Net",
-  initialize = function() {
-    self$fc1 <- nn_linear(784, 128)
-    self$fc2 <- nn_linear(128, 10)
-  },
-  forward = function(x) {
-    x %>%
-      torch_flatten(start_dim = 2) %>%
-      self$fc1() %>%
-      nnf_relu() %>%
-      self$fc2() %>%
-      nnf_log_softmax(dim = 1)
-  }
-)
-
-model <- net()
-optimizer <- optim_sgd(model$parameters, lr = 0.01)
-
-epochs <- 10
-
-for (epoch in 1:10) {
-
-  pb <- progress::progress_bar$new(
-    total = length(dl),
-    format = "[:bar] :eta Loss: :loss"
-  )
-  l <- c()
-
-  for (b in enumerate(dl)) {
-    optimizer$zero_grad()
-    output <- model(b[[1]])
-    loss <- nnf_nll_loss(output, b[[2]])
-    loss$backward()
-    optimizer$step()
-    l <- c(l, loss$item())
-    pb$tick(tokens = list(loss = mean(l)))
-  }
-
-  cat(sprintf("Loss at epoch %d: %3f\n", epoch, mean(l)))
-}
-
- - - -
- - - -
- -
-

Site built with pkgdown 1.5.1.

-
- -
-
- - - - - - diff --git a/articles/examples/mnist-mlp_files/accessible-code-block-0.0.1/empty-anchor.js b/articles/examples/mnist-mlp_files/accessible-code-block-0.0.1/empty-anchor.js deleted file mode 100644 index ca349fd6a570108bde9d7daace534cd651c5f042..0000000000000000000000000000000000000000 --- a/articles/examples/mnist-mlp_files/accessible-code-block-0.0.1/empty-anchor.js +++ /dev/null @@ -1,15 +0,0 @@ -// Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> -// v0.0.1 -// Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. - -document.addEventListener('DOMContentLoaded', function() { - const codeList = document.getElementsByClassName("sourceCode"); - for (var i = 0; i < codeList.length; i++) { - var linkList = codeList[i].getElementsByTagName('a'); - for (var j = 0; j < linkList.length; j++) { - if (linkList[j].innerHTML === "") { - linkList[j].setAttribute('aria-hidden', 'true'); - } - } - } -}); diff --git a/articles/extending-autograd.html b/articles/extending-autograd.html index 429b67d1ead655d581e32e68f28088316757f3fc..e07b62b8e336f4f2e1ba4b739bc20df37670a86a 100644 --- a/articles/extending-autograd.html +++ b/articles/extending-autograd.html @@ -4,14 +4,14 @@ - + + Extending Autograd • torch - - - + + + - - + + +
- -
- +
+ - -
-
- + diff --git a/articles/extending-autograd_files/anchor-sections-1.0/anchor-sections.css b/articles/extending-autograd_files/anchor-sections-1.0/anchor-sections.css deleted file mode 100644 index 07aee5fcb8398a53ea2189cff95cbe6504116d96..0000000000000000000000000000000000000000 --- a/articles/extending-autograd_files/anchor-sections-1.0/anchor-sections.css +++ /dev/null @@ -1,4 +0,0 @@ -/* Styles for section anchors */ -a.anchor-section {margin-left: 10px; visibility: hidden; color: inherit;} -a.anchor-section::before {content: '#';} -.hasAnchor:hover a.anchor-section {visibility: visible;} diff --git a/articles/extending-autograd_files/anchor-sections-1.0/anchor-sections.js b/articles/extending-autograd_files/anchor-sections-1.0/anchor-sections.js deleted file mode 100644 index 570f99a0a8775e26c9dac3b1dd7b82cbe6dbfae3..0000000000000000000000000000000000000000 --- a/articles/extending-autograd_files/anchor-sections-1.0/anchor-sections.js +++ /dev/null @@ -1,33 +0,0 @@ -// Anchor sections v1.0 written by Atsushi Yasumoto on Oct 3rd, 2020. -document.addEventListener('DOMContentLoaded', function() { - // Do nothing if AnchorJS is used - if (typeof window.anchors === 'object' && anchors.hasOwnProperty('hasAnchorJSLink')) { - return; - } - - const h = document.querySelectorAll('h1, h2, h3, h4, h5, h6'); - - // Do nothing if sections are already anchored - if (Array.from(h).some(x => x.classList.contains('hasAnchor'))) { - return null; - } - - // Use section id when pandoc runs with --section-divs - const section_id = function(x) { - return ((x.classList.contains('section') || (x.tagName === 'SECTION')) - ? x.id : ''); - }; - - // Add anchors - h.forEach(function(x) { - const id = x.id || section_id(x.parentElement); - if (id === '') { - return null; - } - let anchor = document.createElement('a'); - anchor.href = '#' + id; - anchor.classList = ['anchor-section']; - x.classList.add('hasAnchor'); - x.appendChild(anchor); - }); -}); diff --git a/articles/getting-started/assets/mnist.png b/articles/getting-started/assets/mnist.png deleted file mode 100644 index 53c876a89d53ccb3ae4fb5167460e84248ad3672..0000000000000000000000000000000000000000 Binary files a/articles/getting-started/assets/mnist.png and /dev/null differ diff --git a/articles/getting-started/autograd.html b/articles/getting-started/autograd.html deleted file mode 100644 index 68eb39adc20d34e3eaa826df81ed84605697c072..0000000000000000000000000000000000000000 --- a/articles/getting-started/autograd.html +++ /dev/null @@ -1,369 +0,0 @@ - - - - - - - -Autograd: automatic differentiation • torch - - - - - - - - - - - -
-
- - - - -
-
-
- - - - -
-

Note: This is an R port of the official tutorial available here. All credits goes to Soumith Chintala.

-
- -

Central to all neural networks in torch is the autograd functionality. Let’s first briefly visit this, and we will then go to training our first neural network.

-

Autograd provides automatic differentiation for all operations on Tensors. It is a define-by-run framework, which means that your backprop is defined by how your code is run, and that every single iteration can be different.

-

Let us see this in more simple terms with some examples.

-
-

-Tensor

-

torch_tensor is the central class of the package. If you set its attribute $requires_grad as TRUE, it starts to track all operations on it. When you finish your computation you can call $backward() and have all the gradients computed automatically. The gradient for this tensor will be accumulated into $grad attribute.

-

To stop a tensor from tracking history, you can call $detach() to detach it from the computation history, and to prevent future computation from being tracked.

-

To prevent tracking history (and using memory), you can also wrap the code block in with_no_grad({<code>}). This can be particularly helpful when evaluating a model because the model may have trainable parameters with requires_grad=TRUE, but for which we don’t need the gradients.

-

There’s one more class which is very important for autograd implementation - a autograd_function.

-

Tensor and Function are interconnected and build up an acyclic graph, that encodes a complete history of computation. Each tensor has a $grad_fn attribute that references an autograd_function that has created the Tensor (except for Tensors created by the user - their grad_fn is NULL).

-

If you want to compute the derivatives, you can call $backward() on a Tensor. If Tensor is a scalar (i.e. it holds a one element data), you don’t need to specify any arguments to backward(), however if it has more elements, you need to specify a gradient argument that is a tensor of matching shape.

-

Create a tensor and set requires_grad=TRUE to track computation with it:

-
-x <- torch_ones(2, 2, requires_grad = TRUE)
-x
-#> torch_tensor
-#>  1  1
-#>  1  1
-#> [ CPUFloatType{2,2} ]
-

Do a tensor operation:

-
-y <- x + 2
-y
-#> torch_tensor
-#>  3  3
-#>  3  3
-#> [ CPUFloatType{2,2} ]
-

y was created as a result of an operation, so it has a grad_fn.

-
-y$grad_fn
-#> AddBackward1
-

Do more operations on y

-
-z <- y * y * 3
-z
-#> torch_tensor
-#>  27  27
-#>  27  27
-#> [ CPUFloatType{2,2} ]
-out <- z$mean()
-out
-#> torch_tensor
-#> 27
-#> [ CPUFloatType{} ]
-

$requires_grad_( ... ) changes an existing Tensor’s requires_grad flag in-place. The input flag defaults to FALSE if not given.

-
-a <- torch_randn(2, 2)
-a <- (a * 3) / (a - 1)
-a$requires_grad
-#> [1] FALSE
-a$requires_grad_(TRUE)
-#> torch_tensor
-#>  1.6068  1.0066
-#> -0.8837  1.9715
-#> [ CPUFloatType{2,2} ]
-a$requires_grad
-#> [1] TRUE
-b <- (a * a)$sum()
-b$grad_fn
-#> SumBackward0
-
-
-

-Gradients

-

Let’s backprop now. Because out contains a single scalar, out$backward() is equivalent to out$backward(torch.tensor(1.)).

-
-out$backward()
-

Print gradients d(out)/dx

-
-x$grad
-#> torch_tensor
-#>  4.5000  4.5000
-#>  4.5000  4.5000
-#> [ CPUFloatType{2,2} ]
-

You should have got a matrix of 4.5. Let’s call the out Tensor \(o\).

-

We have that \(o = \frac{1}{4}\sum_i z_i\), \(z_i = 3(x_i+2)^2\) and \(z_i\bigr\rvert_{x_i=1} = 27\). Therefore, \(\frac{\partial o}{\partial x_i} = \frac{3}{2}(x_i+2)\), hence \(\frac{\partial o}{\partial x_i}\bigr\rvert_{x_i=1} = \frac{9}{2} = 4.5\).

-

Mathematically, if you have a vector valued function \(\vec{y}=f(\vec{x})\), then the gradient of \(\vec{y}\) with respect to \(\vec{x}\) is a Jacobian matrix:

-

\[ - J=\left(\begin{array}{ccc} - \frac{\partial y_{1}}{\partial x_{1}} & \cdots & \frac{\partial y_{1}}{\partial x_{n}}\\ - \vdots & \ddots & \vdots\\ - \frac{\partial y_{m}}{\partial x_{1}} & \cdots & \frac{\partial y_{m}}{\partial x_{n}} - \end{array}\right) -\]

-

Generally speaking, autograd is an engine for computing vector-Jacobian product. That is, given any vector \(v=\left(\begin{array}{cccc} v_{1} & v_{2} & \cdots & v_{m}\end{array}\right)^{T}\), compute the product \(v^{T}\cdot J\). If \(v\) happens to be the gradient of a scalar function \(l=g\left(\vec{y}\right)\), that is, \(v=\left(\begin{array}{ccc}\frac{\partial l}{\partial y_{1}} & \cdots & \frac{\partial l}{\partial y_{m}}\end{array}\right)^{T}\), then by the chain rule, the vector-Jacobian product would be the gradient of \(l\) with respect to \(\vec{x}\):

-

\[ - J^{T}\cdot v=\left(\begin{array}{ccc} - \frac{\partial y_{1}}{\partial x_{1}} & \cdots & \frac{\partial y_{m}}{\partial x_{1}}\\ - \vdots & \ddots & \vdots\\ - \frac{\partial y_{1}}{\partial x_{n}} & \cdots & \frac{\partial y_{m}}{\partial x_{n}} - \end{array}\right)\left(\begin{array}{c} - \frac{\partial l}{\partial y_{1}}\\ - \vdots\\ - \frac{\partial l}{\partial y_{m}} - \end{array}\right)=\left(\begin{array}{c} - \frac{\partial l}{\partial x_{1}}\\ - \vdots\\ - \frac{\partial l}{\partial x_{n}} - \end{array}\right) -\]

-

(Note that \(v^{T}\cdot J\) gives a row vector which can be treated as a column vector by taking \(J^{T}\cdot v\).)

-

This characteristic of vector-Jacobian product makes it very convenient to feed external gradients into a model that has non-scalar output.

-

Now let’s take a look at an example of vector-Jacobian product:

-
-x <- torch_randn(3, requires_grad=TRUE)
-y <- 100 * x
-y
-#> torch_tensor
-#>   -5.4368
-#>  105.8399
-#>  118.4054
-#> [ CPUFloatType{3} ]
-

Now in this case y is no longer a scalar. autograd could not compute the full Jacobian directly, but if we just want the vector-Jacobian product, simply pass the vector to backward as argument:

-
-v <- torch_tensor(c(0.1, 1.0, 0.0001))
-y$backward(v)
-
-x$grad
-#> torch_tensor
-#>  1.0000e+01
-#>  1.0000e+02
-#>  1.0000e-02
-#> [ CPUFloatType{3} ]
-

You can also stop autograd from tracking history on Tensors with $requires_grad=TRUE either by wrapping the code block in with with_no_grad():

-
-x$requires_grad
-#> [1] TRUE
-(x ** 2)$requires_grad
-#> [1] TRUE
-
-with_no_grad({
-  print((x ** 2)$requires_grad)
-})
-#> [1] FALSE
-
-x$requires_grad
-#> [1] TRUE
-y <- x$detach()
-y$requires_grad
-#> [1] FALSE
-x$eq(y)$all()
-#> torch_tensor
-#> 1
-#> [ CPUBoolType{} ]
-

Read Later:

-

Document about help(autograd_function), vignette("using-autograd"), vignette("extending-autograd").

-
-
- - - -
- - - -
- -
-

Site built with pkgdown 1.6.1.

-
- -
-
- - - - - - diff --git a/articles/getting-started/autograd_files/accessible-code-block-0.0.1/empty-anchor.js b/articles/getting-started/autograd_files/accessible-code-block-0.0.1/empty-anchor.js deleted file mode 100644 index ca349fd6a570108bde9d7daace534cd651c5f042..0000000000000000000000000000000000000000 --- a/articles/getting-started/autograd_files/accessible-code-block-0.0.1/empty-anchor.js +++ /dev/null @@ -1,15 +0,0 @@ -// Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> -// v0.0.1 -// Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. - -document.addEventListener('DOMContentLoaded', function() { - const codeList = document.getElementsByClassName("sourceCode"); - for (var i = 0; i < codeList.length; i++) { - var linkList = codeList[i].getElementsByTagName('a'); - for (var j = 0; j < linkList.length; j++) { - if (linkList[j].innerHTML === "") { - linkList[j].setAttribute('aria-hidden', 'true'); - } - } - } -}); diff --git a/articles/getting-started/autograd_files/anchor-sections-1.0/anchor-sections.css b/articles/getting-started/autograd_files/anchor-sections-1.0/anchor-sections.css deleted file mode 100644 index 07aee5fcb8398a53ea2189cff95cbe6504116d96..0000000000000000000000000000000000000000 --- a/articles/getting-started/autograd_files/anchor-sections-1.0/anchor-sections.css +++ /dev/null @@ -1,4 +0,0 @@ -/* Styles for section anchors */ -a.anchor-section {margin-left: 10px; visibility: hidden; color: inherit;} -a.anchor-section::before {content: '#';} -.hasAnchor:hover a.anchor-section {visibility: visible;} diff --git a/articles/getting-started/autograd_files/anchor-sections-1.0/anchor-sections.js b/articles/getting-started/autograd_files/anchor-sections-1.0/anchor-sections.js deleted file mode 100644 index 570f99a0a8775e26c9dac3b1dd7b82cbe6dbfae3..0000000000000000000000000000000000000000 --- a/articles/getting-started/autograd_files/anchor-sections-1.0/anchor-sections.js +++ /dev/null @@ -1,33 +0,0 @@ -// Anchor sections v1.0 written by Atsushi Yasumoto on Oct 3rd, 2020. -document.addEventListener('DOMContentLoaded', function() { - // Do nothing if AnchorJS is used - if (typeof window.anchors === 'object' && anchors.hasOwnProperty('hasAnchorJSLink')) { - return; - } - - const h = document.querySelectorAll('h1, h2, h3, h4, h5, h6'); - - // Do nothing if sections are already anchored - if (Array.from(h).some(x => x.classList.contains('hasAnchor'))) { - return null; - } - - // Use section id when pandoc runs with --section-divs - const section_id = function(x) { - return ((x.classList.contains('section') || (x.tagName === 'SECTION')) - ? x.id : ''); - }; - - // Add anchors - h.forEach(function(x) { - const id = x.id || section_id(x.parentElement); - if (id === '') { - return null; - } - let anchor = document.createElement('a'); - anchor.href = '#' + id; - anchor.classList = ['anchor-section']; - x.classList.add('hasAnchor'); - x.appendChild(anchor); - }); -}); diff --git a/articles/getting-started/control-flow-and-weight-sharing.html b/articles/getting-started/control-flow-and-weight-sharing.html deleted file mode 100644 index 8b24bb321a4c1b2e7c8542e8866e0d2c6e87f829..0000000000000000000000000000000000000000 --- a/articles/getting-started/control-flow-and-weight-sharing.html +++ /dev/null @@ -1,321 +0,0 @@ - - - - - - - -Control flow & Weight sharing • torch - - - - - - - - - - - -
-
- - - - -
-
-
- - - - -
-

Note: This is an R port of the official tutorial available here. All credits goes to Justin Johnson.

-
- -

As an example of dynamic graphs and weight sharing, we implement a very strange model: a fully-connected ReLU network that on each forward pass chooses a random number between 1 and 4 and uses that many hidden layers, reusing the same weights multiple times to compute the innermost hidden layers.

-

For this model we can use normal R flow control to implement the loop, and we can implement weight sharing among the innermost layers by simply reusing the same Module multiple times when defining the forward pass.

-

We can easily implement this model using nn_module:

-
-dynamic_net <- nn_module(
-   "dynamic_net",
-   # In the constructor we construct three nn_linear instances that we will use
-   # in the forward pass.
-   initialize = function(D_in, H, D_out) {
-      self$input_linear <- nn_linear(D_in, H)
-      self$middle_linear <- nn_linear(H, H)
-      self$output_linear <- nn_linear(H, D_out)
-   },
-   # For the forward pass of the model, we randomly choose either 0, 1, 2, or 3
-   # and reuse the middle_linear Module that many times to compute hidden layer
-   # representations.
-   # 
-   # Since each forward pass builds a dynamic computation graph, we can use normal
-   # R control-flow operators like loops or conditional statements when
-   # defining the forward pass of the model.
-   # 
-   # Here we also see that it is perfectly safe to reuse the same Module many
-   # times when defining a computational graph. This is a big improvement from Lua
-   # Torch, where each Module could be used only once.
-   forward = function(x) {
-      h_relu <- self$input_linear(x)$clamp(min = 0)
-      for (i in seq_len(sample.int(4, size = 1))) {
-         h_relu <- self$middle_linear(h_relu)$clamp(min=0)
-      }
-      y_pred <- self$output_linear(h_relu)
-      y_pred
-   }
-)
-
-
-if (cuda_is_available()) {
-   device <- torch_device("cuda")
-} else {
-   device <- torch_device("cpu")
-}
-   
-# N is batch size; D_in is input dimension;
-# H is hidden dimension; D_out is output dimension.
-N <- 64
-D_in <- 1000
-H <- 100
-D_out <- 10
-
-# Create random input and output data
-# Setting requires_grad=FALSE (the default) indicates that we do not need to 
-# compute gradients with respect to these Tensors during the backward pass.
-x <- torch_randn(N, D_in, device=device)
-y <- torch_randn(N, D_out, device=device)
-
-# Construct our model by instantiating the class defined above
-model <- dynamic_net(D_in, H, D_out)
-
-# The nn package also contains definitions of popular loss functions; in this
-# case we will use Mean Squared Error (MSE) as our loss function.
-loss_fn <- nnf_mse_loss
-
-# Use the optim package to define an Optimizer that will update the weights of
-# the model for us. Here we will use Adam; the optim package contains many other
-# optimization algorithms. The first argument to the Adam constructor tells the
-# optimizer which Tensors it should update.
-learning_rate <- 1e-4
-optimizer <- optim_adam(model$parameters, lr=learning_rate)
-
-for (t in seq_len(500)) {
-   # Forward pass: compute predicted y by passing x to the model. Module objects
-   # can be called like functions. When doing so you pass a Tensor of input
-   # data to the Module and it produces a Tensor of output data.
-   y_pred <- model(x)
-   
-   # Compute and print loss. We pass Tensors containing the predicted and true
-   # values of y, and the loss function returns a Tensor containing the
-   # loss.
-   loss <- loss_fn(y_pred, y)
-   if (t %% 100 == 0 || t == 1)
-      cat("Step:", t, ":", as.numeric(loss), "\n")
-   
-   # Before the backward pass, use the optimizer object to zero all of the
-   # gradients for the variables it will update (which are the learnable
-   # weights of the model). This is because by default, gradients are
-   # accumulated in buffers( i.e, not overwritten) whenever $backward()
-   # is called. Checkout docs of `autograd_backward` for more details.
-   optimizer$zero_grad()
-
-   # Backward pass: compute gradient of the loss with respect to model
-   # parameters
-   loss$backward()
-
-   # Calling the step function on an Optimizer makes an update to its
-   # parameters
-   optimizer$step()
-}
-#> Step: 1 : 1.013146 
-#> Step: 100 : 0.9793269 
-#> Step: 200 : 0.4408751 
-#> Step: 300 : 0.1257476 
-#> Step: 400 : 0.05437518 
-#> Step: 500 : 0.01907376
-
- - - -
- - - -
- -
-

Site built with pkgdown 1.6.1.

-
- -
-
- - - - - - diff --git a/articles/getting-started/control-flow-and-weight-sharing_files/accessible-code-block-0.0.1/empty-anchor.js b/articles/getting-started/control-flow-and-weight-sharing_files/accessible-code-block-0.0.1/empty-anchor.js deleted file mode 100644 index ca349fd6a570108bde9d7daace534cd651c5f042..0000000000000000000000000000000000000000 --- a/articles/getting-started/control-flow-and-weight-sharing_files/accessible-code-block-0.0.1/empty-anchor.js +++ /dev/null @@ -1,15 +0,0 @@ -// Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> -// v0.0.1 -// Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. - -document.addEventListener('DOMContentLoaded', function() { - const codeList = document.getElementsByClassName("sourceCode"); - for (var i = 0; i < codeList.length; i++) { - var linkList = codeList[i].getElementsByTagName('a'); - for (var j = 0; j < linkList.length; j++) { - if (linkList[j].innerHTML === "") { - linkList[j].setAttribute('aria-hidden', 'true'); - } - } - } -}); diff --git a/articles/getting-started/control-flow-and-weight-sharing_files/anchor-sections-1.0/anchor-sections.css b/articles/getting-started/control-flow-and-weight-sharing_files/anchor-sections-1.0/anchor-sections.css deleted file mode 100644 index 07aee5fcb8398a53ea2189cff95cbe6504116d96..0000000000000000000000000000000000000000 --- a/articles/getting-started/control-flow-and-weight-sharing_files/anchor-sections-1.0/anchor-sections.css +++ /dev/null @@ -1,4 +0,0 @@ -/* Styles for section anchors */ -a.anchor-section {margin-left: 10px; visibility: hidden; color: inherit;} -a.anchor-section::before {content: '#';} -.hasAnchor:hover a.anchor-section {visibility: visible;} diff --git a/articles/getting-started/control-flow-and-weight-sharing_files/anchor-sections-1.0/anchor-sections.js b/articles/getting-started/control-flow-and-weight-sharing_files/anchor-sections-1.0/anchor-sections.js deleted file mode 100644 index 570f99a0a8775e26c9dac3b1dd7b82cbe6dbfae3..0000000000000000000000000000000000000000 --- a/articles/getting-started/control-flow-and-weight-sharing_files/anchor-sections-1.0/anchor-sections.js +++ /dev/null @@ -1,33 +0,0 @@ -// Anchor sections v1.0 written by Atsushi Yasumoto on Oct 3rd, 2020. -document.addEventListener('DOMContentLoaded', function() { - // Do nothing if AnchorJS is used - if (typeof window.anchors === 'object' && anchors.hasOwnProperty('hasAnchorJSLink')) { - return; - } - - const h = document.querySelectorAll('h1, h2, h3, h4, h5, h6'); - - // Do nothing if sections are already anchored - if (Array.from(h).some(x => x.classList.contains('hasAnchor'))) { - return null; - } - - // Use section id when pandoc runs with --section-divs - const section_id = function(x) { - return ((x.classList.contains('section') || (x.tagName === 'SECTION')) - ? x.id : ''); - }; - - // Add anchors - h.forEach(function(x) { - const id = x.id || section_id(x.parentElement); - if (id === '') { - return null; - } - let anchor = document.createElement('a'); - anchor.href = '#' + id; - anchor.classList = ['anchor-section']; - x.classList.add('hasAnchor'); - x.appendChild(anchor); - }); -}); diff --git a/articles/getting-started/custom-nn.html b/articles/getting-started/custom-nn.html deleted file mode 100644 index b0fae72be9126a67e44ac2c8c8e80831b3dbcf5f..0000000000000000000000000000000000000000 --- a/articles/getting-started/custom-nn.html +++ /dev/null @@ -1,305 +0,0 @@ - - - - - - - -Custom nn modules • torch - - - - - - - - - - - -
-
- - - - -
-
-
- - - - -
-

Note: This is an R port of the official tutorial available here. All credits goes to Justin Johnson.

-
- -

Sometimes you will want to specify models that are more complex than a sequence of existing Modules; for these cases you can define your own Modules by using nn_module function and defining a forward which receives input Tensors and produces output Tensors using other modules or other autograd operations on Tensors.

-

In this example we implement our two-layer network as a custom Module subclass:

-
-two_layer_net <- nn_module(
-   "two_layer_net",
-   initialize = function(D_in, H, D_out) {
-      self$linear1 <- nn_linear(D_in, H)
-      self$linear2 <- nn_linear(H, D_out)
-   },
-   forward = function(x) {
-      x %>% 
-         self$linear1() %>% 
-         nnf_relu() %>% 
-         self$linear2()
-   }
-)
-
-
-if (cuda_is_available()) {
-   device <- torch_device("cuda")
-} else {
-   device <- torch_device("cpu")
-}
-   
-# N is batch size; D_in is input dimension;
-# H is hidden dimension; D_out is output dimension.
-N <- 64
-D_in <- 1000
-H <- 100
-D_out <- 10
-
-# Create random input and output data
-# Setting requires_grad=FALSE (the default) indicates that we do not need to 
-# compute gradients with respect to these Tensors during the backward pass.
-x <- torch_randn(N, D_in, device=device)
-y <- torch_randn(N, D_out, device=device)
-
-# Construct our model by instantiating the class defined above
-model <- two_layer_net(D_in, H, D_out)
-
-# The nn package also contains definitions of popular loss functions; in this
-# case we will use Mean Squared Error (MSE) as our loss function.
-loss_fn <- nnf_mse_loss
-
-# Use the optim package to define an Optimizer that will update the weights of
-# the model for us. Here we will use Adam; the optim package contains many other
-# optimization algorithms. The first argument to the Adam constructor tells the
-# optimizer which Tensors it should update.
-learning_rate <- 1e-4
-optimizer <- optim_sgd(model$parameters, lr=learning_rate)
-
-for (t in seq_len(500)) {
-   # Forward pass: compute predicted y by passing x to the model. Module objects
-   # can be called like functions. When doing so you pass a Tensor of input
-   # data to the Module and it produces a Tensor of output data.
-   y_pred <- model(x)
-   
-   # Compute and print loss. We pass Tensors containing the predicted and true
-   # values of y, and the loss function returns a Tensor containing the
-   # loss.
-   loss <- loss_fn(y_pred, y)
-   if (t %% 100 == 0 || t == 1)
-      cat("Step:", t, ":", as.numeric(loss), "\n")
-   
-   # Before the backward pass, use the optimizer object to zero all of the
-   # gradients for the variables it will update (which are the learnable
-   # weights of the model). This is because by default, gradients are
-   # accumulated in buffers( i.e, not overwritten) whenever $backward()
-   # is called. Checkout docs of `autograd_backward` for more details.
-   optimizer$zero_grad()
-
-   # Backward pass: compute gradient of the loss with respect to model
-   # parameters
-   loss$backward()
-
-   # Calling the step function on an Optimizer makes an update to its
-   # parameters
-   optimizer$step()
-}
-#> Step: 1 : 1.04628 
-#> Step: 100 : 1.033974 
-#> Step: 200 : 1.021832 
-#> Step: 300 : 1.009992 
-#> Step: 400 : 0.9984027 
-#> Step: 500 : 0.9870082
-

In the next example we will about dynamic graphs in torch.

-
- - - -
- - - -
- -
-

Site built with pkgdown 1.6.1.

-
- -
-
- - - - - - diff --git a/articles/getting-started/custom-nn_files/accessible-code-block-0.0.1/empty-anchor.js b/articles/getting-started/custom-nn_files/accessible-code-block-0.0.1/empty-anchor.js deleted file mode 100644 index ca349fd6a570108bde9d7daace534cd651c5f042..0000000000000000000000000000000000000000 --- a/articles/getting-started/custom-nn_files/accessible-code-block-0.0.1/empty-anchor.js +++ /dev/null @@ -1,15 +0,0 @@ -// Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> -// v0.0.1 -// Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. - -document.addEventListener('DOMContentLoaded', function() { - const codeList = document.getElementsByClassName("sourceCode"); - for (var i = 0; i < codeList.length; i++) { - var linkList = codeList[i].getElementsByTagName('a'); - for (var j = 0; j < linkList.length; j++) { - if (linkList[j].innerHTML === "") { - linkList[j].setAttribute('aria-hidden', 'true'); - } - } - } -}); diff --git a/articles/getting-started/custom-nn_files/anchor-sections-1.0/anchor-sections.css b/articles/getting-started/custom-nn_files/anchor-sections-1.0/anchor-sections.css deleted file mode 100644 index 07aee5fcb8398a53ea2189cff95cbe6504116d96..0000000000000000000000000000000000000000 --- a/articles/getting-started/custom-nn_files/anchor-sections-1.0/anchor-sections.css +++ /dev/null @@ -1,4 +0,0 @@ -/* Styles for section anchors */ -a.anchor-section {margin-left: 10px; visibility: hidden; color: inherit;} -a.anchor-section::before {content: '#';} -.hasAnchor:hover a.anchor-section {visibility: visible;} diff --git a/articles/getting-started/custom-nn_files/anchor-sections-1.0/anchor-sections.js b/articles/getting-started/custom-nn_files/anchor-sections-1.0/anchor-sections.js deleted file mode 100644 index 570f99a0a8775e26c9dac3b1dd7b82cbe6dbfae3..0000000000000000000000000000000000000000 --- a/articles/getting-started/custom-nn_files/anchor-sections-1.0/anchor-sections.js +++ /dev/null @@ -1,33 +0,0 @@ -// Anchor sections v1.0 written by Atsushi Yasumoto on Oct 3rd, 2020. -document.addEventListener('DOMContentLoaded', function() { - // Do nothing if AnchorJS is used - if (typeof window.anchors === 'object' && anchors.hasOwnProperty('hasAnchorJSLink')) { - return; - } - - const h = document.querySelectorAll('h1, h2, h3, h4, h5, h6'); - - // Do nothing if sections are already anchored - if (Array.from(h).some(x => x.classList.contains('hasAnchor'))) { - return null; - } - - // Use section id when pandoc runs with --section-divs - const section_id = function(x) { - return ((x.classList.contains('section') || (x.tagName === 'SECTION')) - ? x.id : ''); - }; - - // Add anchors - h.forEach(function(x) { - const id = x.id || section_id(x.parentElement); - if (id === '') { - return null; - } - let anchor = document.createElement('a'); - anchor.href = '#' + id; - anchor.classList = ['anchor-section']; - x.classList.add('hasAnchor'); - x.appendChild(anchor); - }); -}); diff --git a/articles/getting-started/neural-networks.html b/articles/getting-started/neural-networks.html deleted file mode 100644 index afaa4302ddd43b9a7c67c1db4a7744027db33775..0000000000000000000000000000000000000000 --- a/articles/getting-started/neural-networks.html +++ /dev/null @@ -1,430 +0,0 @@ - - - - - - - -Neural networks • torch - - - - - - - - - - - -
-
- - - - -
-
-
- - - - -
-

Note: This is an R port of the official tutorial available here. All credits goes to Soumith Chintala.

-
- -

Neural networks can be constructed using the nn functionality.

-

Now that you had a glimpse of autograd, nn depends on autograd to define models and differentiate them. An nn.Module contains layers, and a method forward(input) that returns the output.

-

For example, look at this network that classifies digit images:

-
-

Convnet for mnist classification

-
-

It is a simple feed-forward network. It takes the input, feeds it through several layers one after the other, and then finally gives the output.

-

A typical training procedure for a neural network is as follows:

-
    -
  • Define the neural network that has some learnable parameters (or weights)
  • -
  • Iterate over a dataset of inputs
  • -
  • Process input through the network
  • -
  • Compute the loss (how far is the output from being correct)
  • -
  • Propagate gradients back into the network’s parameters
  • -
  • Update the weights of the network, typically using a simple update rule: weight = weight - learning_rate * gradient.
  • -
-
-

-Define the network

-

Let’s define this network:

-
-Net <- nn_module(
-  initialize = function() {
-    self$conv1 = nn_conv2d(1, 6, 3)
-    self$conv2 = nn_conv2d(6, 16, 3)
-    # an affine operation: y = Wx + b
-    self$fc1 = nn_linear(16 * 6 * 6, 120)  # 6*6 from image dimension
-    self$fc2 = nn_linear(120, 84)
-    self$fc3 = nn_linear(84, 10)
-  },
-  forward = function(x) {
-    x %>% 
-      
-      self$conv1() %>% 
-      nnf_relu() %>% 
-      nnf_max_pool2d(c(2,2)) %>% 
-      
-      self$conv2() %>% 
-      nnf_relu() %>% 
-      nnf_max_pool2d(c(2,2)) %>% 
-      
-      torch_flatten(start_dim = 2) %>% 
-      
-      self$fc1() %>% 
-      nnf_relu() %>% 
-      
-      self$fc2() %>% 
-      nnf_relu() %>% 
-      
-      self$fc3()
-  }
-)
-
-net <- Net()
-

You just have to define the forward function, and the backward function (where gradients are computed) is automatically defined for you using autograd. You can use any of the Tensor operations in the forward function.

-

The learnable parameters of a model are returned by net$parameters.

-
-str(net$parameters)
-#> List of 10
-#>  $ conv1.weight:Float [1:6, 1:1, 1:3, 1:3]
-#>  $ conv1.bias  :Float [1:6]
-#>  $ conv2.weight:Float [1:16, 1:6, 1:3, 1:3]
-#>  $ conv2.bias  :Float [1:16]
-#>  $ fc1.weight  :Float [1:120, 1:576]
-#>  $ fc1.bias    :Float [1:120]
-#>  $ fc2.weight  :Float [1:84, 1:120]
-#>  $ fc2.bias    :Float [1:84]
-#>  $ fc3.weight  :Float [1:10, 1:84]
-#>  $ fc3.bias    :Float [1:10]
-

Let’s try a random 32x32 input. Note: expected input size of this net (LeNet) is 32x32. To use this net on the MNIST dataset, please resize the images from the dataset to 32x32.

-
-input <- torch_randn(1, 1, 32, 32)
-out <- net(input)
-out
-#> torch_tensor
-#>  0.0673 -0.0700  0.0210  0.1113  0.1069  0.0171  0.0774  0.0898 -0.0002  0.0961
-#> [ CPUFloatType{1,10} ]
-

Zero the gradient buffers of all parameters and backprops with random gradients:

-
-net$zero_grad()
-out$backward(torch_randn(1, 10))
-
-

Note: nn only supports mini-batches. The entire torch.nn package only supports inputs that are a mini-batch of samples, and not a single sample. For example, nn_conv2d will take in a 4D Tensor of nSamples x nChannels x Height x Width. If you have a single sample, just use input$unsqueeze(1) to add a fake batch dimension.

-
-

Before proceeding further, let’s recap all the classes you’ve seen so far.

-
-

-Recap

-
    -
  • torch_tensor - A multi-dimensional array with support for autograd operations like backward(). Also holds the gradient w.r.t. the tensor.

  • -
  • nn_module - Neural network module. Convenient way of encapsulating parameters, with helpers for moving them to GPU, exporting, loading, etc.

  • -
  • nn_parameter - A kind of Tensor, that is automatically registered as a parameter when assigned as an attribute to a Module.

  • -
  • autograd_function - Implements forward and backward definitions of an autograd operation. Every Tensor operation creates at least a single Function node that connects to functions that created a Tensor and encodes its history.

  • -
-
-
-

-At this point, we covered

-
    -
  • Defining a neural network
  • -
  • Processing inputs and calling backward
  • -
-
-
-

-Still left

-
    -
  • Computing the loss
  • -
  • Updating the weights of the network
  • -
-
-
-
-

-Loss function

-

A loss function takes the (output, target) pair of inputs, and computes a value that estimates how far away the output is from the target.

-

There are several different loss functions under the nn package . A simple loss is: nnf_mse_loss which computes the mean-squared error between the input and the target.

-

For example:

-
-output <- net(input)
-target <- torch_randn(10)  # a dummy target, for example
-target <- target$view(c(1, -1))  # make it the same shape as output
-
-loss <- nnf_mse_loss(output, target)
-loss
-#> torch_tensor
-#> 1.33572
-#> [ CPUFloatType{} ]
-

Now, if you follow loss in the backward direction, using its $grad_fn attribute, you will see a graph of computations that looks like this:

-
input -> conv2d -> relu -> maxpool2d -> conv2d -> relu -> maxpool2d
-      -> view -> linear -> relu -> linear -> relu -> linear
-      -> MSELoss
-      -> loss
-

So, when we call loss$backward(), the whole graph is differentiated w.r.t. the loss, and all Tensors in the graph that has requires_grad=True will have their #grad Tensor accumulated with the gradient.

-

For illustration, let us follow a few steps backward:

-
-loss$grad_fn
-#> MseLossBackward
-loss$grad_fn$next_functions[[1]]
-#> AddmmBackward
-loss$grad_fn$next_functions[[1]]$next_functions[[1]]
-#> torch::autograd::AccumulateGrad
-
-
-

-Backprop

-

To backpropagate the error all we have to do is to loss$backward(). You need to clear the existing gradients though, else gradients will be accumulated to existing gradients.

-

Now we shall call loss$backward(), and have a look at conv1’s bias gradients before and after the backward.

-
-net$zero_grad()     # zeroes the gradient buffers of all parameters
-
-# conv1.bias.grad before backward
-net$conv1$bias$grad
-#> torch_tensor
-#>  0
-#>  0
-#>  0
-#>  0
-#>  0
-#>  0
-#> [ CPUFloatType{6} ]
-
-loss$backward()
-
-# conv1.bias.grad after backward
-net$conv1$bias$grad
-#> torch_tensor
-#> 0.01 *
-#> -0.4329
-#> -1.0576
-#>  0.9670
-#>  0.7630
-#> -0.6947
-#>  2.2084
-#> [ CPUFloatType{6} ]
-

Now, we have seen how to use loss functions.

-
-
-

-Update the weights

-

The simplest update rule used in practice is the Stochastic Gradient Descent (SGD):

-

\[weight = weight - learning_rate * gradient\]

-

We can implement this using simple R code:

-
-learning_rate <- 0.01
-for (f in net$parameters) {
-  with_no_grad({
-    f$sub_(f$grad * learning_rate)
-  })
-}
-
-

Note: Weight updates here is wraped around with_no_grad as we don’t the updates to be tracked by the autograd engine.

-
-

However, as you use neural networks, you want to use various different update rules such as SGD, Nesterov-SGD, Adam, RMSProp, etc.

-
-# create your optimizer
-optimizer <- optim_sgd(net$parameters, lr = 0.01)
-
-# in your training loop:
-optimizer$zero_grad()   # zero the gradient buffers
-output <- net(input)
-loss <- nnf_mse_loss(output, target)
-loss$backward()
-optimizer$step()    # Does the update
-#> NULL
-
-

Note: Observe how gradient buffers had to be manually set to zero using optimizer$zero_grad(). This is because gradients are accumulated as explained in the Backprop section.

-
-
-
- - - -
- - - -
- -
-

Site built with pkgdown 1.6.1.

-
- -
-
- - - - - - diff --git a/articles/getting-started/neural-networks_files/accessible-code-block-0.0.1/empty-anchor.js b/articles/getting-started/neural-networks_files/accessible-code-block-0.0.1/empty-anchor.js deleted file mode 100644 index ca349fd6a570108bde9d7daace534cd651c5f042..0000000000000000000000000000000000000000 --- a/articles/getting-started/neural-networks_files/accessible-code-block-0.0.1/empty-anchor.js +++ /dev/null @@ -1,15 +0,0 @@ -// Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> -// v0.0.1 -// Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. - -document.addEventListener('DOMContentLoaded', function() { - const codeList = document.getElementsByClassName("sourceCode"); - for (var i = 0; i < codeList.length; i++) { - var linkList = codeList[i].getElementsByTagName('a'); - for (var j = 0; j < linkList.length; j++) { - if (linkList[j].innerHTML === "") { - linkList[j].setAttribute('aria-hidden', 'true'); - } - } - } -}); diff --git a/articles/getting-started/neural-networks_files/anchor-sections-1.0/anchor-sections.css b/articles/getting-started/neural-networks_files/anchor-sections-1.0/anchor-sections.css deleted file mode 100644 index 07aee5fcb8398a53ea2189cff95cbe6504116d96..0000000000000000000000000000000000000000 --- a/articles/getting-started/neural-networks_files/anchor-sections-1.0/anchor-sections.css +++ /dev/null @@ -1,4 +0,0 @@ -/* Styles for section anchors */ -a.anchor-section {margin-left: 10px; visibility: hidden; color: inherit;} -a.anchor-section::before {content: '#';} -.hasAnchor:hover a.anchor-section {visibility: visible;} diff --git a/articles/getting-started/neural-networks_files/anchor-sections-1.0/anchor-sections.js b/articles/getting-started/neural-networks_files/anchor-sections-1.0/anchor-sections.js deleted file mode 100644 index 570f99a0a8775e26c9dac3b1dd7b82cbe6dbfae3..0000000000000000000000000000000000000000 --- a/articles/getting-started/neural-networks_files/anchor-sections-1.0/anchor-sections.js +++ /dev/null @@ -1,33 +0,0 @@ -// Anchor sections v1.0 written by Atsushi Yasumoto on Oct 3rd, 2020. -document.addEventListener('DOMContentLoaded', function() { - // Do nothing if AnchorJS is used - if (typeof window.anchors === 'object' && anchors.hasOwnProperty('hasAnchorJSLink')) { - return; - } - - const h = document.querySelectorAll('h1, h2, h3, h4, h5, h6'); - - // Do nothing if sections are already anchored - if (Array.from(h).some(x => x.classList.contains('hasAnchor'))) { - return null; - } - - // Use section id when pandoc runs with --section-divs - const section_id = function(x) { - return ((x.classList.contains('section') || (x.tagName === 'SECTION')) - ? x.id : ''); - }; - - // Add anchors - h.forEach(function(x) { - const id = x.id || section_id(x.parentElement); - if (id === '') { - return null; - } - let anchor = document.createElement('a'); - anchor.href = '#' + id; - anchor.classList = ['anchor-section']; - x.classList.add('hasAnchor'); - x.appendChild(anchor); - }); -}); diff --git a/articles/getting-started/new-autograd-functions.html b/articles/getting-started/new-autograd-functions.html deleted file mode 100644 index 2a87b30b243abddd81cc13a7a95e016db12bb498..0000000000000000000000000000000000000000 --- a/articles/getting-started/new-autograd-functions.html +++ /dev/null @@ -1,313 +0,0 @@ - - - - - - - -Defining new autograd functions • torch - - - - - - - - - - - -
-
- - - - -
-
-
- - - - -
-

Note: This is an R port of the official tutorial available here. All credits goes to Justin Johnson.

-
- -

Under the hood, each primitive autograd operator is really two functions that operate on Tensors. The forward function computes output Tensors from input Tensors. The backward function receives the gradient of the output Tensors with respect to some scalar value, and computes the gradient of the input Tensors with respect to that same scalar value.

-

In torch we can easily define our own autograd operator by defining a subclass of autograd_function and implementing the forward and backward functions. We can then use our new autograd operator by constructing an instance and calling it like a function, passing Tensors containing input data.

-

In this example we define our own custom autograd function for performing the ReLU nonlinearity, and use it to implement our two-layer network:

-
-# We can implement our own custom autograd Functions by subclassing
-# autograd_function and implementing the forward and backward passes
-# which operate on Tensors.
-my_relu <- autograd_function(
-   # In the forward pass we receive a Tensor containing the input and return
-   # a Tensor containing the output. ctx is a context object that can be used
-   # to stash information for backward computation. You can cache arbitrary
-   # objects for use in the backward pass using the ctx$save_for_backward method.
-   forward = function(ctx, input) {
-      ctx$save_for_backward(input = input)
-      input$clamp(min = 0)
-   },
-   # In the backward pass we receive a Tensor containing the gradient of the loss
-   # with respect to the output, and we need to compute the gradient of the loss
-   # with respect to the input.
-   backward = function(ctx, grad_output) {
-      v <- ctx$saved_variables
-      grad_input <- grad_output$clone()
-      grad_input[v$input < 0] <- 0
-      list(input = grad_input)
-   }
-)
-
-if (cuda_is_available()) {
-   device <- torch_device("cuda")
-} else {
-   device <- torch_device("cpu")
-}
-   
-# N is batch size; D_in is input dimension;
-# H is hidden dimension; D_out is output dimension.
-N <- 64
-D_in <- 1000
-H <- 100
-D_out <- 10
-
-# Create random input and output data
-# Setting requires_grad=FALSE (the default) indicates that we do not need to 
-# compute gradients with respect to these Tensors during the backward pass.
-x <- torch_randn(N, D_in, device=device)
-y <- torch_randn(N, D_out, device=device)
-
-# Randomly initialize weights
-# Setting requires_grad=TRUE indicates that we want to compute gradients with
-# respect to these Tensors during the backward pass.
-w1 <- torch_randn(D_in, H, device=device, requires_grad = TRUE)
-w2 <- torch_randn(H, D_out, device=device, requires_grad = TRUE)
-
-learning_rate <- 1e-6
-for (t in seq_len(500)) {
-   # Forward pass: compute predicted y using operations on Tensors; these
-   # are exactly the same operations we used to compute the forward pass using
-   # Tensors, but we do not need to keep references to intermediate values since
-   # we are not implementing the backward pass by hand.
-   y_pred <- my_relu(x$mm(w1))$mm(w2)
-   
-   # Compute and print loss using operations on Tensors.
-   # Now loss is a Tensor of shape (1,)
-   loss <- (y_pred - y)$pow(2)$sum()
-   if (t %% 100 == 0 || t == 1)
-      cat("Step:", t, ":", as.numeric(loss), "\n")
-   
-   # Use autograd to compute the backward pass. This call will compute the
-   # gradient of loss with respect to all Tensors with requires_grad=True.
-   # After this call w1$grad and w2$grad will be Tensors holding the gradient
-   # of the loss with respect to w1 and w2 respectively.
-   loss$backward()
-   
-   # Manually update weights using gradient descent. Wrap in `with_no_grad`
-   # because weights have requires_grad=TRUE, but we don't need to track this
-   # in autograd.
-   # You can also use optim_sgd to achieve this.
-   with_no_grad({
-      
-      # operations suffixed with an `_` operates on in-place on the tensor.
-      w1$sub_(learning_rate * w1$grad)
-      w2$sub_(learning_rate * w2$grad)
-      
-      # Manually zero the gradients after updating weights
-      w1$grad$zero_()
-      w2$grad$zero_()
-   })
-}
-#> Step: 1 : 24166310 
-#> Step: 100 : 714.7551 
-#> Step: 200 : 6.447984 
-#> Step: 300 : 0.08702322 
-#> Step: 400 : 0.00166944 
-#> Step: 500 : 0.000139622
-

In the next example we will learn how to use the neural networks abstractions in torch.

-
- - - -
- - - -
- -
-

Site built with pkgdown 1.6.1.

-
- -
-
- - - - - - diff --git a/articles/getting-started/new-autograd-functions_files/accessible-code-block-0.0.1/empty-anchor.js b/articles/getting-started/new-autograd-functions_files/accessible-code-block-0.0.1/empty-anchor.js deleted file mode 100644 index ca349fd6a570108bde9d7daace534cd651c5f042..0000000000000000000000000000000000000000 --- a/articles/getting-started/new-autograd-functions_files/accessible-code-block-0.0.1/empty-anchor.js +++ /dev/null @@ -1,15 +0,0 @@ -// Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> -// v0.0.1 -// Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. - -document.addEventListener('DOMContentLoaded', function() { - const codeList = document.getElementsByClassName("sourceCode"); - for (var i = 0; i < codeList.length; i++) { - var linkList = codeList[i].getElementsByTagName('a'); - for (var j = 0; j < linkList.length; j++) { - if (linkList[j].innerHTML === "") { - linkList[j].setAttribute('aria-hidden', 'true'); - } - } - } -}); diff --git a/articles/getting-started/new-autograd-functions_files/anchor-sections-1.0/anchor-sections.css b/articles/getting-started/new-autograd-functions_files/anchor-sections-1.0/anchor-sections.css deleted file mode 100644 index 07aee5fcb8398a53ea2189cff95cbe6504116d96..0000000000000000000000000000000000000000 --- a/articles/getting-started/new-autograd-functions_files/anchor-sections-1.0/anchor-sections.css +++ /dev/null @@ -1,4 +0,0 @@ -/* Styles for section anchors */ -a.anchor-section {margin-left: 10px; visibility: hidden; color: inherit;} -a.anchor-section::before {content: '#';} -.hasAnchor:hover a.anchor-section {visibility: visible;} diff --git a/articles/getting-started/new-autograd-functions_files/anchor-sections-1.0/anchor-sections.js b/articles/getting-started/new-autograd-functions_files/anchor-sections-1.0/anchor-sections.js deleted file mode 100644 index 570f99a0a8775e26c9dac3b1dd7b82cbe6dbfae3..0000000000000000000000000000000000000000 --- a/articles/getting-started/new-autograd-functions_files/anchor-sections-1.0/anchor-sections.js +++ /dev/null @@ -1,33 +0,0 @@ -// Anchor sections v1.0 written by Atsushi Yasumoto on Oct 3rd, 2020. -document.addEventListener('DOMContentLoaded', function() { - // Do nothing if AnchorJS is used - if (typeof window.anchors === 'object' && anchors.hasOwnProperty('hasAnchorJSLink')) { - return; - } - - const h = document.querySelectorAll('h1, h2, h3, h4, h5, h6'); - - // Do nothing if sections are already anchored - if (Array.from(h).some(x => x.classList.contains('hasAnchor'))) { - return null; - } - - // Use section id when pandoc runs with --section-divs - const section_id = function(x) { - return ((x.classList.contains('section') || (x.tagName === 'SECTION')) - ? x.id : ''); - }; - - // Add anchors - h.forEach(function(x) { - const id = x.id || section_id(x.parentElement); - if (id === '') { - return null; - } - let anchor = document.createElement('a'); - anchor.href = '#' + id; - anchor.classList = ['anchor-section']; - x.classList.add('hasAnchor'); - x.appendChild(anchor); - }); -}); diff --git a/articles/getting-started/nn.html b/articles/getting-started/nn.html deleted file mode 100644 index 3c1ccb9fdd96daea1526c118105460d6dd748bbf..0000000000000000000000000000000000000000 --- a/articles/getting-started/nn.html +++ /dev/null @@ -1,296 +0,0 @@ - - - - - - - -nn: neural networks with torch • torch - - - - - - - - - - - -
-
- - - - -
-
-
- - - - -
-

Note: This is an R port of the official tutorial available here. All credits goes to Justin Johnson.

-
- -

Computational graphs and autograd are a very powerful paradigm for defining complex operators and automatically taking derivatives; however for large neural networks raw autograd can be a bit too low-level.

-

When building neural networks we frequently think of arranging the computation into layers, some of which have learnable parameters which will be optimized during learning.

-

In TensorFlow, packages like Keras, TensorFlow-Slim, and TFLearn provide higher-level abstractions over raw computational graphs that are useful for building neural networks.

-

In torch, the nn functionality serves this same purpose. The nn feature defines a set of Modules, which are roughly equivalent to neural network layers. A Module receives input Tensors and computes output Tensors, but may also hold internal state such as Tensors containing learnable parameters. The nn collection also defines a set of useful loss functions that are commonly used when training neural networks.

-

In this example we use nn to implement our two-layer network:

-
-if (cuda_is_available()) {
-   device <- torch_device("cuda")
-} else {
-   device <- torch_device("cpu")
-}
-   
-# N is batch size; D_in is input dimension;
-# H is hidden dimension; D_out is output dimension.
-N <- 64
-D_in <- 1000
-H <- 100
-D_out <- 10
-
-# Create random input and output data
-# Setting requires_grad=FALSE (the default) indicates that we do not need to 
-# compute gradients with respect to these Tensors during the backward pass.
-x <- torch_randn(N, D_in, device=device)
-y <- torch_randn(N, D_out, device=device)
-
-# Use the nn package to define our model as a sequence of layers. nn_sequential
-# is a Module which contains other Modules, and applies them in sequence to
-# produce its output. Each Linear Module computes output from input using a
-# linear function, and holds internal Tensors for its weight and bias.
-model <- nn_sequential(
-    nn_linear(D_in, H),
-    nn_relu(),
-    nn_linear(H, D_out)
-)
-
-# The nn package also contains definitions of popular loss functions; in this
-# case we will use Mean Squared Error (MSE) as our loss function.
-loss_fn <- nnf_mse_loss
-
-learning_rate <- 1e-6
-for (t in seq_len(500)) {
-   # Forward pass: compute predicted y by passing x to the model. Module objects
-   # can be called like functions. When doing so you pass a Tensor of input
-   # data to the Module and it produces a Tensor of output data.
-   y_pred <- model(x)
-   
-   # Compute and print loss. We pass Tensors containing the predicted and true
-   # values of y, and the loss function returns a Tensor containing the
-   # loss.
-   loss <- loss_fn(y_pred, y)
-   if (t %% 100 == 0 || t == 1)
-      cat("Step:", t, ":", as.numeric(loss), "\n")
-   
-   # Zero the gradients before running the backward pass.
-   model$zero_grad()
-
-   # Backward pass: compute gradient of the loss with respect to all the learnable
-   # parameters of the model. Internally, the parameters of each Module are stored
-   # in Tensors with requires_grad=TRUE, so this call will compute gradients for
-   # all learnable parameters in the model.
-   loss$backward()
-   
-   # Update the weights using gradient descent. Each parameter is a Tensor, so
-   # we can access its gradients like we did before.
-   with_no_grad({
-      for (param in model$parameters) {
-         param$sub_(learning_rate * param$grad)
-      }
-   })
-}
-#> Step: 1 : 1.018742 
-#> Step: 100 : 1.018609 
-#> Step: 200 : 1.018475 
-#> Step: 300 : 1.018341 
-#> Step: 400 : 1.018206 
-#> Step: 500 : 1.018072
-

In the next example we will learn how to use optimizers implemented in torch.

-
- - - -
- - - -
- -
-

Site built with pkgdown 1.6.1.

-
- -
-
- - - - - - diff --git a/articles/getting-started/nn_files/accessible-code-block-0.0.1/empty-anchor.js b/articles/getting-started/nn_files/accessible-code-block-0.0.1/empty-anchor.js deleted file mode 100644 index ca349fd6a570108bde9d7daace534cd651c5f042..0000000000000000000000000000000000000000 --- a/articles/getting-started/nn_files/accessible-code-block-0.0.1/empty-anchor.js +++ /dev/null @@ -1,15 +0,0 @@ -// Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> -// v0.0.1 -// Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. - -document.addEventListener('DOMContentLoaded', function() { - const codeList = document.getElementsByClassName("sourceCode"); - for (var i = 0; i < codeList.length; i++) { - var linkList = codeList[i].getElementsByTagName('a'); - for (var j = 0; j < linkList.length; j++) { - if (linkList[j].innerHTML === "") { - linkList[j].setAttribute('aria-hidden', 'true'); - } - } - } -}); diff --git a/articles/getting-started/nn_files/anchor-sections-1.0/anchor-sections.css b/articles/getting-started/nn_files/anchor-sections-1.0/anchor-sections.css deleted file mode 100644 index 07aee5fcb8398a53ea2189cff95cbe6504116d96..0000000000000000000000000000000000000000 --- a/articles/getting-started/nn_files/anchor-sections-1.0/anchor-sections.css +++ /dev/null @@ -1,4 +0,0 @@ -/* Styles for section anchors */ -a.anchor-section {margin-left: 10px; visibility: hidden; color: inherit;} -a.anchor-section::before {content: '#';} -.hasAnchor:hover a.anchor-section {visibility: visible;} diff --git a/articles/getting-started/nn_files/anchor-sections-1.0/anchor-sections.js b/articles/getting-started/nn_files/anchor-sections-1.0/anchor-sections.js deleted file mode 100644 index 570f99a0a8775e26c9dac3b1dd7b82cbe6dbfae3..0000000000000000000000000000000000000000 --- a/articles/getting-started/nn_files/anchor-sections-1.0/anchor-sections.js +++ /dev/null @@ -1,33 +0,0 @@ -// Anchor sections v1.0 written by Atsushi Yasumoto on Oct 3rd, 2020. -document.addEventListener('DOMContentLoaded', function() { - // Do nothing if AnchorJS is used - if (typeof window.anchors === 'object' && anchors.hasOwnProperty('hasAnchorJSLink')) { - return; - } - - const h = document.querySelectorAll('h1, h2, h3, h4, h5, h6'); - - // Do nothing if sections are already anchored - if (Array.from(h).some(x => x.classList.contains('hasAnchor'))) { - return null; - } - - // Use section id when pandoc runs with --section-divs - const section_id = function(x) { - return ((x.classList.contains('section') || (x.tagName === 'SECTION')) - ? x.id : ''); - }; - - // Add anchors - h.forEach(function(x) { - const id = x.id || section_id(x.parentElement); - if (id === '') { - return null; - } - let anchor = document.createElement('a'); - anchor.href = '#' + id; - anchor.classList = ['anchor-section']; - x.classList.add('hasAnchor'); - x.appendChild(anchor); - }); -}); diff --git a/articles/getting-started/optim.html b/articles/getting-started/optim.html deleted file mode 100644 index 5d03d7e4e28058d1550b3df19e95143f2541f9da..0000000000000000000000000000000000000000 --- a/articles/getting-started/optim.html +++ /dev/null @@ -1,298 +0,0 @@ - - - - - - - -optim: optimizers in torch • torch - - - - - - - - - - - -
-
- - - - -
-
-
- - - - -
-

Note: This is an R port of the official tutorial available here. All credits goes to Justin Johnson.

-
- -

Up to this point we have updated the weights of our models by manually mutating the Tensors holding learnable parameters (with with_no_grad to avoid tracking history in autograd). This is not a huge burden for simple optimization algorithms like stochastic gradient descent, but in practice we often train neural networks using more sophisticated optimizers like AdaGrad, RMSProp, Adam, etc.

-

The optim package in torch abstracts the idea of an optimization algorithm and provides implementations of commonly used optimization algorithms.

-

In this example we will use the nn package to define our model as before, but we will optimize the model using the Adam algorithm provided by optim:

-
-if (cuda_is_available()) {
-   device <- torch_device("cuda")
-} else {
-   device <- torch_device("cpu")
-}
-   
-# N is batch size; D_in is input dimension;
-# H is hidden dimension; D_out is output dimension.
-N <- 64
-D_in <- 1000
-H <- 100
-D_out <- 10
-
-# Create random input and output data
-# Setting requires_grad=FALSE (the default) indicates that we do not need to 
-# compute gradients with respect to these Tensors during the backward pass.
-x <- torch_randn(N, D_in, device=device)
-y <- torch_randn(N, D_out, device=device)
-
-# Use the nn package to define our model as a sequence of layers. nn_sequential
-# is a Module which contains other Modules, and applies them in sequence to
-# produce its output. Each Linear Module computes output from input using a
-# linear function, and holds internal Tensors for its weight and bias.
-model <- nn_sequential(
-    nn_linear(D_in, H),
-    nn_relu(),
-    nn_linear(H, D_out)
-)
-
-# The nn package also contains definitions of popular loss functions; in this
-# case we will use Mean Squared Error (MSE) as our loss function.
-loss_fn <- nnf_mse_loss
-
-# Use the optim package to define an Optimizer that will update the weights of
-# the model for us. Here we will use Adam; the optim package contains many other
-# optimization algorithms. The first argument to the Adam constructor tells the
-# optimizer which Tensors it should update.
-learning_rate <- 1e-4
-optimizer <- optim_adam(model$parameters, lr=learning_rate)
-
-for (t in seq_len(500)) {
-   # Forward pass: compute predicted y by passing x to the model. Module objects
-   # can be called like functions. When doing so you pass a Tensor of input
-   # data to the Module and it produces a Tensor of output data.
-   y_pred <- model(x)
-   
-   # Compute and print loss. We pass Tensors containing the predicted and true
-   # values of y, and the loss function returns a Tensor containing the
-   # loss.
-   loss <- loss_fn(y_pred, y)
-   if (t %% 100 == 0 || t == 1)
-      cat("Step:", t, ":", as.numeric(loss), "\n")
-   
-   # Before the backward pass, use the optimizer object to zero all of the
-   # gradients for the variables it will update (which are the learnable
-   # weights of the model). This is because by default, gradients are
-   # accumulated in buffers( i.e, not overwritten) whenever $backward()
-   # is called. Checkout docs of `autograd_backward` for more details.
-   optimizer$zero_grad()
-
-   # Backward pass: compute gradient of the loss with respect to model
-   # parameters
-   loss$backward()
-
-   # Calling the step function on an Optimizer makes an update to its
-   # parameters
-   optimizer$step()
-}
-#> Step: 1 : 0.9679546 
-#> Step: 100 : 0.06467929 
-#> Step: 200 : 0.0009209086 
-#> Step: 300 : 9.68739e-06 
-#> Step: 400 : 3.821675e-07 
-#> Step: 500 : 1.036825e-08
-

In the next example we will learn how to create custom nn_modules.

-
- - - -
- - - -
- -
-

Site built with pkgdown 1.6.1.

-
- -
-
- - - - - - diff --git a/articles/getting-started/optim_files/accessible-code-block-0.0.1/empty-anchor.js b/articles/getting-started/optim_files/accessible-code-block-0.0.1/empty-anchor.js deleted file mode 100644 index ca349fd6a570108bde9d7daace534cd651c5f042..0000000000000000000000000000000000000000 --- a/articles/getting-started/optim_files/accessible-code-block-0.0.1/empty-anchor.js +++ /dev/null @@ -1,15 +0,0 @@ -// Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> -// v0.0.1 -// Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. - -document.addEventListener('DOMContentLoaded', function() { - const codeList = document.getElementsByClassName("sourceCode"); - for (var i = 0; i < codeList.length; i++) { - var linkList = codeList[i].getElementsByTagName('a'); - for (var j = 0; j < linkList.length; j++) { - if (linkList[j].innerHTML === "") { - linkList[j].setAttribute('aria-hidden', 'true'); - } - } - } -}); diff --git a/articles/getting-started/optim_files/anchor-sections-1.0/anchor-sections.css b/articles/getting-started/optim_files/anchor-sections-1.0/anchor-sections.css deleted file mode 100644 index 07aee5fcb8398a53ea2189cff95cbe6504116d96..0000000000000000000000000000000000000000 --- a/articles/getting-started/optim_files/anchor-sections-1.0/anchor-sections.css +++ /dev/null @@ -1,4 +0,0 @@ -/* Styles for section anchors */ -a.anchor-section {margin-left: 10px; visibility: hidden; color: inherit;} -a.anchor-section::before {content: '#';} -.hasAnchor:hover a.anchor-section {visibility: visible;} diff --git a/articles/getting-started/optim_files/anchor-sections-1.0/anchor-sections.js b/articles/getting-started/optim_files/anchor-sections-1.0/anchor-sections.js deleted file mode 100644 index 570f99a0a8775e26c9dac3b1dd7b82cbe6dbfae3..0000000000000000000000000000000000000000 --- a/articles/getting-started/optim_files/anchor-sections-1.0/anchor-sections.js +++ /dev/null @@ -1,33 +0,0 @@ -// Anchor sections v1.0 written by Atsushi Yasumoto on Oct 3rd, 2020. -document.addEventListener('DOMContentLoaded', function() { - // Do nothing if AnchorJS is used - if (typeof window.anchors === 'object' && anchors.hasOwnProperty('hasAnchorJSLink')) { - return; - } - - const h = document.querySelectorAll('h1, h2, h3, h4, h5, h6'); - - // Do nothing if sections are already anchored - if (Array.from(h).some(x => x.classList.contains('hasAnchor'))) { - return null; - } - - // Use section id when pandoc runs with --section-divs - const section_id = function(x) { - return ((x.classList.contains('section') || (x.tagName === 'SECTION')) - ? x.id : ''); - }; - - // Add anchors - h.forEach(function(x) { - const id = x.id || section_id(x.parentElement); - if (id === '') { - return null; - } - let anchor = document.createElement('a'); - anchor.href = '#' + id; - anchor.classList = ['anchor-section']; - x.classList.add('hasAnchor'); - x.appendChild(anchor); - }); -}); diff --git a/articles/getting-started/tensors-and-autograd.html b/articles/getting-started/tensors-and-autograd.html deleted file mode 100644 index 79b24c058f0b77acacd2301142816249a15e82d5..0000000000000000000000000000000000000000 --- a/articles/getting-started/tensors-and-autograd.html +++ /dev/null @@ -1,291 +0,0 @@ - - - - - - - -Tensors and autograd • torch - - - - - - - - - - - -
-
- - - - -
-
-
- - - - -
-

Note: This is an R port of the official tutorial available here. All credits goes to Justin Johnson.

-
- -

In the previous examples, we had to manually implement both the forward and backward passes of our neural network. Manually implementing the backward pass is not a big deal for a small two-layer network, but can quickly get very hairy for large complex networks.

-

Thankfully, we can use automatic differentiation to automate the computation of backward passes in neural networks. The autograd feature in torch provides exactly this functionality. When using autograd, the forward pass of your network will define a computational graph; nodes in the graph will be Tensors, and edges will be functions that produce output Tensors from input Tensors. Backpropagating through this graph then allows you to easily compute gradients.

-

This sounds complicated, it’s pretty simple to use in practice. Each Tensor represents a node in a computational graph. If x is a Tensor that has x$requires_grad=TRUE then x$grad is another Tensor holding the gradient of x with respect to some scalar value.

-

Here we use torch Tensors and autograd to implement our two-layer network; now we no longer need to manually implement the backward pass through the network:

-
-if (cuda_is_available()) {
-   device <- torch_device("cuda")
-} else {
-   device <- torch_device("cpu")
-}
-   
-# N is batch size; D_in is input dimension;
-# H is hidden dimension; D_out is output dimension.
-N <- 64
-D_in <- 1000
-H <- 100
-D_out <- 10
-
-# Create random input and output data
-# Setting requires_grad=FALSE (the default) indicates that we do not need to 
-# compute gradients with respect to these Tensors during the backward pass.
-x <- torch_randn(N, D_in, device=device)
-y <- torch_randn(N, D_out, device=device)
-
-# Randomly initialize weights
-# Setting requires_grad=TRUE indicates that we want to compute gradients with
-# respect to these Tensors during the backward pass.
-w1 <- torch_randn(D_in, H, device=device, requires_grad = TRUE)
-w2 <- torch_randn(H, D_out, device=device, requires_grad = TRUE)
-
-learning_rate <- 1e-6
-for (t in seq_len(500)) {
-   # Forward pass: compute predicted y using operations on Tensors; these
-   # are exactly the same operations we used to compute the forward pass using
-   # Tensors, but we do not need to keep references to intermediate values since
-   # we are not implementing the backward pass by hand.
-   y_pred <- x$mm(w1)$clamp(min=0)$mm(w2)
-   
-   # Compute and print loss using operations on Tensors.
-   # Now loss is a Tensor of shape (1,)
-   loss <- (y_pred - y)$pow(2)$sum()
-   if (t %% 100 == 0 || t == 1)
-      cat("Step:", t, ":", as.numeric(loss), "\n")
-   
-   # Use autograd to compute the backward pass. This call will compute the
-   # gradient of loss with respect to all Tensors with requires_grad=True.
-   # After this call w1$grad and w2$grad will be Tensors holding the gradient
-   # of the loss with respect to w1 and w2 respectively.
-   loss$backward()
-   
-   # Manually update weights using gradient descent. Wrap in `with_no_grad`
-   # because weights have requires_grad=TRUE, but we don't need to track this
-   # in autograd.
-   # You can also use optim_sgd to achieve this.
-   with_no_grad({
-      
-      # operations suffixed with an `_` operates on in-place on the tensor.
-      w1$sub_(learning_rate * w1$grad)
-      w2$sub_(learning_rate * w2$grad)
-      
-      # Manually zero the gradients after updating weights
-      w1$grad$zero_()
-      w2$grad$zero_()
-   })
-}
-#> Step: 1 : 24573142 
-#> Step: 100 : 423.7524 
-#> Step: 200 : 1.833858 
-#> Step: 300 : 0.01267943 
-#> Step: 400 : 0.0002803126 
-#> Step: 500 : 4.453462e-05
-

In the next example we will learn how to create new autograd functions.

-
- - - -
- - - -
- -
-

Site built with pkgdown 1.6.1.

-
- -
-
- - - - - - diff --git a/articles/getting-started/tensors-and-autograd_files/accessible-code-block-0.0.1/empty-anchor.js b/articles/getting-started/tensors-and-autograd_files/accessible-code-block-0.0.1/empty-anchor.js deleted file mode 100644 index ca349fd6a570108bde9d7daace534cd651c5f042..0000000000000000000000000000000000000000 --- a/articles/getting-started/tensors-and-autograd_files/accessible-code-block-0.0.1/empty-anchor.js +++ /dev/null @@ -1,15 +0,0 @@ -// Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> -// v0.0.1 -// Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. - -document.addEventListener('DOMContentLoaded', function() { - const codeList = document.getElementsByClassName("sourceCode"); - for (var i = 0; i < codeList.length; i++) { - var linkList = codeList[i].getElementsByTagName('a'); - for (var j = 0; j < linkList.length; j++) { - if (linkList[j].innerHTML === "") { - linkList[j].setAttribute('aria-hidden', 'true'); - } - } - } -}); diff --git a/articles/getting-started/tensors-and-autograd_files/anchor-sections-1.0/anchor-sections.css b/articles/getting-started/tensors-and-autograd_files/anchor-sections-1.0/anchor-sections.css deleted file mode 100644 index 07aee5fcb8398a53ea2189cff95cbe6504116d96..0000000000000000000000000000000000000000 --- a/articles/getting-started/tensors-and-autograd_files/anchor-sections-1.0/anchor-sections.css +++ /dev/null @@ -1,4 +0,0 @@ -/* Styles for section anchors */ -a.anchor-section {margin-left: 10px; visibility: hidden; color: inherit;} -a.anchor-section::before {content: '#';} -.hasAnchor:hover a.anchor-section {visibility: visible;} diff --git a/articles/getting-started/tensors-and-autograd_files/anchor-sections-1.0/anchor-sections.js b/articles/getting-started/tensors-and-autograd_files/anchor-sections-1.0/anchor-sections.js deleted file mode 100644 index 570f99a0a8775e26c9dac3b1dd7b82cbe6dbfae3..0000000000000000000000000000000000000000 --- a/articles/getting-started/tensors-and-autograd_files/anchor-sections-1.0/anchor-sections.js +++ /dev/null @@ -1,33 +0,0 @@ -// Anchor sections v1.0 written by Atsushi Yasumoto on Oct 3rd, 2020. -document.addEventListener('DOMContentLoaded', function() { - // Do nothing if AnchorJS is used - if (typeof window.anchors === 'object' && anchors.hasOwnProperty('hasAnchorJSLink')) { - return; - } - - const h = document.querySelectorAll('h1, h2, h3, h4, h5, h6'); - - // Do nothing if sections are already anchored - if (Array.from(h).some(x => x.classList.contains('hasAnchor'))) { - return null; - } - - // Use section id when pandoc runs with --section-divs - const section_id = function(x) { - return ((x.classList.contains('section') || (x.tagName === 'SECTION')) - ? x.id : ''); - }; - - // Add anchors - h.forEach(function(x) { - const id = x.id || section_id(x.parentElement); - if (id === '') { - return null; - } - let anchor = document.createElement('a'); - anchor.href = '#' + id; - anchor.classList = ['anchor-section']; - x.classList.add('hasAnchor'); - x.appendChild(anchor); - }); -}); diff --git a/articles/getting-started/tensors.html b/articles/getting-started/tensors.html deleted file mode 100644 index b415a6d1e576eafabd4dcd78d4a545010a91f0b8..0000000000000000000000000000000000000000 --- a/articles/getting-started/tensors.html +++ /dev/null @@ -1,276 +0,0 @@ - - - - - - - -torch Tensors • torch - - - - - - - - - - - -
-
- - - - -
-
-
- - - - -
-

Note: This is an R port of the official tutorial available here. All credits goes to Justin Johnson.

-
- -

R arrays are great, but they cannot utilize GPUs to accelerate its numerical computations. For modern deep neural networks, GPUs often provide speedups of 50x or greater, so unfortunately pure R won’t be enough for modern deep learning.

-

Here we introduce the most fundamental torch concept: the Tensor. A torch Tensor is conceptually similar to an R array: a Tensor is an n-dimensional array, and torch provides many functions for operating on these Tensors. Behind the scenes, Tensors can keep track of a computational graph and gradients, but they’re also useful as a generic tool for scientific computing.

-

Also unlike R, torch Tensors can utilize GPUs to accelerate their numeric computations. To run a torch Tensor on GPU, you simply need to cast it to a new datatype.

-

Here we use torch Tensors to fit a two-layer network to random data. Like the R before we need to manually implement the forward and backward passes through the network:

-
-if (cuda_is_available()) {
-   device <- torch_device("cuda")
-} else {
-   device <- torch_device("cpu")
-}
-   
-# N is batch size; D_in is input dimension;
-# H is hidden dimension; D_out is output dimension.
-N <- 64
-D_in <- 1000
-H <- 100
-D_out <- 10
-
-# Create random input and output data
-x <- torch_randn(N, D_in, device=device)
-y <- torch_randn(N, D_out, device=device)
-
-# Randomly initialize weights
-w1 <- torch_randn(D_in, H, device=device)
-w2 <- torch_randn(H, D_out, device=device)
-
-learning_rate <- 1e-6
-for (t in seq_len(500)) {
-   # Forward pass: compute predicted y
-   h <- x$mm(w1)
-   h_relu <- h$clamp(min=0)
-   y_pred <- h_relu$mm(w2)
-   
-   # Compute and print loss
-   loss <- as.numeric((y_pred - y)$pow(2)$sum())
-   if (t %% 100 == 0 || t == 1)
-      cat("Step:", t, ":", loss, "\n")
-   
-   # Backprop to compute gradients of w1 and w2 with respect to loss
-   grad_y_pred <- 2.0 * (y_pred - y)
-   grad_w2 <- h_relu$t()$mm(grad_y_pred)
-   grad_h_relu <- grad_y_pred$mm(w2$t())
-   grad_h <- grad_h_relu$clone()
-   grad_h[h < 0] <- 0
-   grad_w1 <- x$t()$mm(grad_h)
-   
-   # Update weights using gradient descent
-   w1 <- w1 - learning_rate * grad_w1
-   w2 <- w2 - learning_rate * grad_w2
-}
-#> Step: 1 : 28228804 
-#> Step: 100 : 330.6301 
-#> Step: 200 : 1.205863 
-#> Step: 300 : 0.006815678 
-#> Step: 400 : 0.0001808387 
-#> Step: 500 : 3.467693e-05
-

In the next example we will use autograd instead of computing the gradients manually.

-
- - - -
- - - -
- -
-

Site built with pkgdown 1.6.1.

-
- -
-
- - - - - - diff --git a/articles/getting-started/tensors_files/accessible-code-block-0.0.1/empty-anchor.js b/articles/getting-started/tensors_files/accessible-code-block-0.0.1/empty-anchor.js deleted file mode 100644 index ca349fd6a570108bde9d7daace534cd651c5f042..0000000000000000000000000000000000000000 --- a/articles/getting-started/tensors_files/accessible-code-block-0.0.1/empty-anchor.js +++ /dev/null @@ -1,15 +0,0 @@ -// Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> -// v0.0.1 -// Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. - -document.addEventListener('DOMContentLoaded', function() { - const codeList = document.getElementsByClassName("sourceCode"); - for (var i = 0; i < codeList.length; i++) { - var linkList = codeList[i].getElementsByTagName('a'); - for (var j = 0; j < linkList.length; j++) { - if (linkList[j].innerHTML === "") { - linkList[j].setAttribute('aria-hidden', 'true'); - } - } - } -}); diff --git a/articles/getting-started/tensors_files/anchor-sections-1.0/anchor-sections.css b/articles/getting-started/tensors_files/anchor-sections-1.0/anchor-sections.css deleted file mode 100644 index 07aee5fcb8398a53ea2189cff95cbe6504116d96..0000000000000000000000000000000000000000 --- a/articles/getting-started/tensors_files/anchor-sections-1.0/anchor-sections.css +++ /dev/null @@ -1,4 +0,0 @@ -/* Styles for section anchors */ -a.anchor-section {margin-left: 10px; visibility: hidden; color: inherit;} -a.anchor-section::before {content: '#';} -.hasAnchor:hover a.anchor-section {visibility: visible;} diff --git a/articles/getting-started/tensors_files/anchor-sections-1.0/anchor-sections.js b/articles/getting-started/tensors_files/anchor-sections-1.0/anchor-sections.js deleted file mode 100644 index 570f99a0a8775e26c9dac3b1dd7b82cbe6dbfae3..0000000000000000000000000000000000000000 --- a/articles/getting-started/tensors_files/anchor-sections-1.0/anchor-sections.js +++ /dev/null @@ -1,33 +0,0 @@ -// Anchor sections v1.0 written by Atsushi Yasumoto on Oct 3rd, 2020. -document.addEventListener('DOMContentLoaded', function() { - // Do nothing if AnchorJS is used - if (typeof window.anchors === 'object' && anchors.hasOwnProperty('hasAnchorJSLink')) { - return; - } - - const h = document.querySelectorAll('h1, h2, h3, h4, h5, h6'); - - // Do nothing if sections are already anchored - if (Array.from(h).some(x => x.classList.contains('hasAnchor'))) { - return null; - } - - // Use section id when pandoc runs with --section-divs - const section_id = function(x) { - return ((x.classList.contains('section') || (x.tagName === 'SECTION')) - ? x.id : ''); - }; - - // Add anchors - h.forEach(function(x) { - const id = x.id || section_id(x.parentElement); - if (id === '') { - return null; - } - let anchor = document.createElement('a'); - anchor.href = '#' + id; - anchor.classList = ['anchor-section']; - x.classList.add('hasAnchor'); - x.appendChild(anchor); - }); -}); diff --git a/articles/getting-started/warmup.html b/articles/getting-started/warmup.html deleted file mode 100644 index 1371db25b7388e5ac13f27ae28513ecd37d527fe..0000000000000000000000000000000000000000 --- a/articles/getting-started/warmup.html +++ /dev/null @@ -1,269 +0,0 @@ - - - - - - - -Warm-up • torch - - - - - - - - - - - -
-
- - - - -
-
-
- - - - -
-

Note: This is an R port of the official tutorial available here. All credits goes to Justin Johnson.

-
- -

A fully-connected ReLU network with one hidden layer and no biases, trained to predict y from x using Euclidean error.

-

This implementation uses pure R to manually compute the forward pass, loss, and backward pass.

-

An R array is a generic n-dimensional array; it does not know anything about deep learning or gradients or computational graphs, and is just a way to perform generic numeric computations.

-
-# N is batch size; D_in is input dimension;
-# H is hidden dimension; D_out is output dimension.
-N <- 64
-D_in <- 1000
-H <- 100
-D_out <- 10
-
-# Create random input and output data
-x <- array(rnorm(N*D_in), dim = c(N, D_in))
-y <- array(rnorm(N*D_out), dim = c(N, D_out))
-
-# Randomly initialize weights
-w1 <- array(rnorm(D_in*H), dim = c(D_in, H))
-w2 <- array(rnorm(H*D_out), dim = c(H, D_out))
-
-learning_rate <- 1e-6
-for (t in seq_len(500)) {
-   # Forward pass: compute predicted y
-   h <- x %*% w1
-   h_relu <- ifelse(h < 0, 0, h)
-   y_pred <- h_relu %*% w2
-   
-   # Compute and print loss
-   loss <- sum((y_pred - y)^2)
-   if (t %% 100 == 0 || t == 1)
-      cat("Step:", t, ":", loss, "\n")
-   
-   # Backprop to compute gradients of w1 and w2 with respect to loss
-   grad_y_pred <- 2 * (y_pred - y)
-   grad_w2 <- t(h_relu) %*% grad_y_pred
-   grad_h_relu <- grad_y_pred %*% t(w2)
-   grad_h <- grad_h_relu
-   grad_h[h < 0] <- 0
-   grad_w1 <- t(x) %*% grad_h
-   
-   # Update weights
-   w1 <- w1 - learning_rate * grad_w1
-   w2 <- w2 - learning_rate * grad_w2
-}
-#> Step: 1 : 24029709 
-#> Step: 100 : 553.7803 
-#> Step: 200 : 1.694745 
-#> Step: 300 : 0.0077663 
-#> Step: 400 : 5.43958e-05 
-#> Step: 500 : 6.158879e-07
-

In the next example we will replace the R array for a torch Tensor.

-
- - - -
- - - -
- -
-

Site built with pkgdown 1.6.1.

-
- -
-
- - - - - - diff --git a/articles/getting-started/warmup_files/accessible-code-block-0.0.1/empty-anchor.js b/articles/getting-started/warmup_files/accessible-code-block-0.0.1/empty-anchor.js deleted file mode 100644 index ca349fd6a570108bde9d7daace534cd651c5f042..0000000000000000000000000000000000000000 --- a/articles/getting-started/warmup_files/accessible-code-block-0.0.1/empty-anchor.js +++ /dev/null @@ -1,15 +0,0 @@ -// Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> -// v0.0.1 -// Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. - -document.addEventListener('DOMContentLoaded', function() { - const codeList = document.getElementsByClassName("sourceCode"); - for (var i = 0; i < codeList.length; i++) { - var linkList = codeList[i].getElementsByTagName('a'); - for (var j = 0; j < linkList.length; j++) { - if (linkList[j].innerHTML === "") { - linkList[j].setAttribute('aria-hidden', 'true'); - } - } - } -}); diff --git a/articles/getting-started/warmup_files/anchor-sections-1.0/anchor-sections.css b/articles/getting-started/warmup_files/anchor-sections-1.0/anchor-sections.css deleted file mode 100644 index 07aee5fcb8398a53ea2189cff95cbe6504116d96..0000000000000000000000000000000000000000 --- a/articles/getting-started/warmup_files/anchor-sections-1.0/anchor-sections.css +++ /dev/null @@ -1,4 +0,0 @@ -/* Styles for section anchors */ -a.anchor-section {margin-left: 10px; visibility: hidden; color: inherit;} -a.anchor-section::before {content: '#';} -.hasAnchor:hover a.anchor-section {visibility: visible;} diff --git a/articles/getting-started/warmup_files/anchor-sections-1.0/anchor-sections.js b/articles/getting-started/warmup_files/anchor-sections-1.0/anchor-sections.js deleted file mode 100644 index 570f99a0a8775e26c9dac3b1dd7b82cbe6dbfae3..0000000000000000000000000000000000000000 --- a/articles/getting-started/warmup_files/anchor-sections-1.0/anchor-sections.js +++ /dev/null @@ -1,33 +0,0 @@ -// Anchor sections v1.0 written by Atsushi Yasumoto on Oct 3rd, 2020. -document.addEventListener('DOMContentLoaded', function() { - // Do nothing if AnchorJS is used - if (typeof window.anchors === 'object' && anchors.hasOwnProperty('hasAnchorJSLink')) { - return; - } - - const h = document.querySelectorAll('h1, h2, h3, h4, h5, h6'); - - // Do nothing if sections are already anchored - if (Array.from(h).some(x => x.classList.contains('hasAnchor'))) { - return null; - } - - // Use section id when pandoc runs with --section-divs - const section_id = function(x) { - return ((x.classList.contains('section') || (x.tagName === 'SECTION')) - ? x.id : ''); - }; - - // Add anchors - h.forEach(function(x) { - const id = x.id || section_id(x.parentElement); - if (id === '') { - return null; - } - let anchor = document.createElement('a'); - anchor.href = '#' + id; - anchor.classList = ['anchor-section']; - x.classList.add('hasAnchor'); - x.appendChild(anchor); - }); -}); diff --git a/articles/getting-started/what-is-torch.html b/articles/getting-started/what-is-torch.html deleted file mode 100644 index e31fd5e3d3de8d7ec008f3da2c8f293cc86e9237..0000000000000000000000000000000000000000 --- a/articles/getting-started/what-is-torch.html +++ /dev/null @@ -1,425 +0,0 @@ - - - - - - - -What is torch? • torch - - - - - - - - - - - -
-
- - - - -
-
-
- - - - -
-

Note: This is an R port of the official tutorial available here. All credits goes to Soumith Chintala.

-
- -

It’s a scientific computing package targeted at two sets of audiences:

-
    -
  • An array library to use the power of GPUs
  • -
  • a deep learning research platform that provides maximum flexibility and speed
  • -
-
-

-Getting started

-
-

-Tensors

-

Tensors are similar to R arrays, with the addition being that Tensors can also be used on a GPU to accelerate computing.

-
-

Note: An uninitialized matrix is declared, but does not contain definite known values before it is used. When an uninitialized matrix is created, whatever values were in the allocated memory at the time will appear as the initial values.

-
-

Construct a 5x3 matrix, uninitialized:

-
-x <- torch_empty(5, 3)
-x
-#> torch_tensor
-#>  0.0000e+00  8.5899e+09 -2.5192e-13
-#>  1.0845e-19  1.1210e-44  1.6816e-43
-#>  7.1466e-44  4.4842e-44  1.5274e-43
-#>  1.3593e-43  1.6255e-43  1.5975e-43
-#>  1.4714e-43  1.6816e-43  6.1657e-44
-#> [ CPUFloatType{5,3} ]
-

Construct a randomly initialized matrix:

-
-x <- torch_rand(5, 3)
-x
-#> torch_tensor
-#>  0.8047  0.9970  0.3180
-#>  0.2996  0.6715  0.8695
-#>  0.9981  0.0952  0.8207
-#>  0.7128  0.9619  0.6479
-#>  0.4912  0.9580  0.4217
-#> [ CPUFloatType{5,3} ]
-

Construct a matrix filled zeros and of dtype long:

-
-x <- torch_zeros(5, 3, dtype = torch_long())
-x
-#> torch_tensor
-#>  0  0  0
-#>  0  0  0
-#>  0  0  0
-#>  0  0  0
-#>  0  0  0
-#> [ CPULongType{5,3} ]
-

Construct a tensor directly from data:

-
-x <- torch_tensor(c(5.5, 3))
-x
-#> torch_tensor
-#>  5.5000
-#>  3.0000
-#> [ CPUFloatType{2} ]
-

or create a tensor based on an existing tensor. These methods will reuse properties of the input tensor, e.g. dtype, unless new values are provided by user

-
-x <- torch_randn_like(x, dtype = torch_float()) # override dtype!
-x                                               # result has the same size
-#> torch_tensor
-#> -1.2922
-#> -0.6301
-#> [ CPUFloatType{2} ]
-

Get its size:

-
-x$size()
-#> [1] 2
-
-
-

-Operations

-

There are multiple syntaxes for operations. In the following example, we will take a look at the addition operation.

-

Addition: syntax 1

-
-x <- torch_rand(5, 3)
-y <- torch_rand(5, 3)
-x + y
-#> torch_tensor
-#>  0.1534  0.5196  1.1073
-#>  1.1957  0.8504  1.1173
-#>  1.0868  1.1021  1.8485
-#>  0.4676  1.0156  0.2103
-#>  1.0188  1.1065  0.8460
-#> [ CPUFloatType{5,3} ]
-

Addition: syntax 2

-
-torch_add(x, y)
-#> torch_tensor
-#>  0.1534  0.5196  1.1073
-#>  1.1957  0.8504  1.1173
-#>  1.0868  1.1021  1.8485
-#>  0.4676  1.0156  0.2103
-#>  1.0188  1.1065  0.8460
-#> [ CPUFloatType{5,3} ]
-

Addition: in-place

-
-y$add_(x)
-#> torch_tensor
-#>  0.1534  0.5196  1.1073
-#>  1.1957  0.8504  1.1173
-#>  1.0868  1.1021  1.8485
-#>  0.4676  1.0156  0.2103
-#>  1.0188  1.1065  0.8460
-#> [ CPUFloatType{5,3} ]
-y
-#> torch_tensor
-#>  0.1534  0.5196  1.1073
-#>  1.1957  0.8504  1.1173
-#>  1.0868  1.1021  1.8485
-#>  0.4676  1.0156  0.2103
-#>  1.0188  1.1065  0.8460
-#> [ CPUFloatType{5,3} ]
-
-

Note: Any operation that mutates a tensor in-place is post-fixed with an _. For example: x$copy_(y), x$t_(), will change x.

-
-

You can use standard R-like indexing with all bells and whistles! See more about indexing with vignette("indexing").

-
-x[, 1]
-#> torch_tensor
-#>  0.0319
-#>  0.2944
-#>  0.7091
-#>  0.2032
-#>  0.4828
-#> [ CPUFloatType{5} ]
-

Resizing: If you want to resize/reshape tensor, you can use torch_view:

-
-x <- torch_randn(4, 4)
-y <- x$view(16)
-z <- x$view(size = c(-1, 8))  # the size -1 is inferred from other dimensions
-x$size()
-#> [1] 4 4
-y$size()
-#> [1] 16
-z$size()
-#> [1] 2 8
-

If you have a one element tensor, use $item() to get the value as an R number

-
-x <- torch_randn(1)
-x
-#> torch_tensor
-#> -0.4257
-#> [ CPUFloatType{1} ]
-x$item()
-#> [1] -0.4256831
-

You can find a complete list of operations in the reference page.

-
-
-
-

-R bridge

-

Converting a Torch Tensor to an R array and vice versa is a breeze.

-
-

-Converting a torch tensor into an R array

-
-a <- torch_ones(5)
-a
-#> torch_tensor
-#>  1
-#>  1
-#>  1
-#>  1
-#>  1
-#> [ CPUFloatType{5} ]
-
-b <- as_array(a)
-b
-#> [1] 1 1 1 1 1
-
-
-

-Converting R arrays to torch tensors

-
-a <- rep(1, 5)
-a
-#> [1] 1 1 1 1 1
-b <- torch_tensor(a)
-b
-#> torch_tensor
-#>  1
-#>  1
-#>  1
-#>  1
-#>  1
-#> [ CPUFloatType{5} ]
-

Currently supported types are numerics and boolean types.

-
-
-
-

-CUDA tensors

-

Tensors can be moved onto any device using the $to method.

-
-if (cuda_is_available()) {
-  device <- torch_device("cuda")
-  y <- torch_ones_like(x, device = device)  # directly create a tensor on GPU
-  x <- x$to(device)                       # or just use strings ``.to("cuda")``
-  z <- x + y
-  print(z)
-  print(z$to(device = "cpu", torch_double())) # `$to` can also change dtype together!
-}
-
-
- - - -
- - - -
- -
-

Site built with pkgdown 1.6.1.

-
- -
-
- - - - - - diff --git a/articles/getting-started/what-is-torch_files/accessible-code-block-0.0.1/empty-anchor.js b/articles/getting-started/what-is-torch_files/accessible-code-block-0.0.1/empty-anchor.js deleted file mode 100644 index ca349fd6a570108bde9d7daace534cd651c5f042..0000000000000000000000000000000000000000 --- a/articles/getting-started/what-is-torch_files/accessible-code-block-0.0.1/empty-anchor.js +++ /dev/null @@ -1,15 +0,0 @@ -// Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> -// v0.0.1 -// Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. - -document.addEventListener('DOMContentLoaded', function() { - const codeList = document.getElementsByClassName("sourceCode"); - for (var i = 0; i < codeList.length; i++) { - var linkList = codeList[i].getElementsByTagName('a'); - for (var j = 0; j < linkList.length; j++) { - if (linkList[j].innerHTML === "") { - linkList[j].setAttribute('aria-hidden', 'true'); - } - } - } -}); diff --git a/articles/getting-started/what-is-torch_files/anchor-sections-1.0/anchor-sections.css b/articles/getting-started/what-is-torch_files/anchor-sections-1.0/anchor-sections.css deleted file mode 100644 index 07aee5fcb8398a53ea2189cff95cbe6504116d96..0000000000000000000000000000000000000000 --- a/articles/getting-started/what-is-torch_files/anchor-sections-1.0/anchor-sections.css +++ /dev/null @@ -1,4 +0,0 @@ -/* Styles for section anchors */ -a.anchor-section {margin-left: 10px; visibility: hidden; color: inherit;} -a.anchor-section::before {content: '#';} -.hasAnchor:hover a.anchor-section {visibility: visible;} diff --git a/articles/getting-started/what-is-torch_files/anchor-sections-1.0/anchor-sections.js b/articles/getting-started/what-is-torch_files/anchor-sections-1.0/anchor-sections.js deleted file mode 100644 index 570f99a0a8775e26c9dac3b1dd7b82cbe6dbfae3..0000000000000000000000000000000000000000 --- a/articles/getting-started/what-is-torch_files/anchor-sections-1.0/anchor-sections.js +++ /dev/null @@ -1,33 +0,0 @@ -// Anchor sections v1.0 written by Atsushi Yasumoto on Oct 3rd, 2020. -document.addEventListener('DOMContentLoaded', function() { - // Do nothing if AnchorJS is used - if (typeof window.anchors === 'object' && anchors.hasOwnProperty('hasAnchorJSLink')) { - return; - } - - const h = document.querySelectorAll('h1, h2, h3, h4, h5, h6'); - - // Do nothing if sections are already anchored - if (Array.from(h).some(x => x.classList.contains('hasAnchor'))) { - return null; - } - - // Use section id when pandoc runs with --section-divs - const section_id = function(x) { - return ((x.classList.contains('section') || (x.tagName === 'SECTION')) - ? x.id : ''); - }; - - // Add anchors - h.forEach(function(x) { - const id = x.id || section_id(x.parentElement); - if (id === '') { - return null; - } - let anchor = document.createElement('a'); - anchor.href = '#' + id; - anchor.classList = ['anchor-section']; - x.classList.add('hasAnchor'); - x.appendChild(anchor); - }); -}); diff --git a/articles/index.html b/articles/index.html index 09a605467447223fa7b41ff8f61555c34e92a5da..4aeacb29210244cd9eb9190c554277b7390a1df7 100644 --- a/articles/index.html +++ b/articles/index.html @@ -1,244 +1,130 @@ - - - - - - - -Articles • torch - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Articles • torch - - - - - - - - - - -
-
-
+
+ +
+
+ + +
- -
- - -
-

Site built with pkgdown 1.6.1.

+ -
-
+
+ - - - + diff --git a/articles/indexing.html b/articles/indexing.html index d01fce698512f690dbae98ff71528221e544f02c..570526c9095cd565367d51fda9f4227f97661389 100644 --- a/articles/indexing.html +++ b/articles/indexing.html @@ -4,14 +4,14 @@ - + + Indexing tensors • torch - - - + + + - - + + +
- -
- +
+ - -
-
-