knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(rTorch)
If you are in a hurry to test rTorch, here some examples:
library(rTorch) a <- torch$tensor(c(1, 2, 3, 4, 5)) a
Notice that this is a float
tensor (see the comma ,
). By default, rTorch will choose a 32-bit float
for you.
class(a) a$dtype
b <- torch$tensor(seq(1,10)) b
These are all integers.
class(b) b$dtype
c <- torch$arange(1, 10) c
torch$ones(c(2L, 3L))
torch$rand(5L, 4L)
torch$linspace(1L, 10L, steps = 100L)
torch$eye(5L)
arr <- array(seq(1, 60), dim = c(5, 4, 3)) torch$as_tensor(arr)
mat <- matrix(seq(1, 20), nrow = 5) torch$as_tensor(mat)
m <- torch$Tensor(list(list(1, 2), list(3, 4), list(5, 6) ) ) m
(shp <- m$shape) m$size() dim(m) length(m)
# tensor shape components
shp[[1]]
shp[[2]]
# a tensor representing a 28x20 pixels image in 3 channels (img <- torch$rand(3L, 28L, 28L))
# plot the image three channels image((img[1L,,]$numpy()), main = "channel 1") image((img[2L,,]$numpy()), main = "channel 2") image((img[3L,,]$numpy()), main = "channel 3")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.