Learn R Programming

⚠️There's a newer version (0.14.2) of this package.Take me there.

torch

Installation

torch can be installed from CRAN with:

install.packages("torch")

You can also install the development version with:

remotes::install_github("mlverse/torch")

At the first package load additional software will be installed.

Examples

You can create torch tensors from R objects with the torch_tensor function and convert them back to R objects with as_array.

library(torch)
x <- array(runif(8), dim = c(2, 2, 2))
y <- torch_tensor(x, dtype = torch_float64())
y
#> torch_tensor
#> (1,.,.) = 
#>   0.1512  0.8540
#>   0.3250  0.3191
#> 
#> (2,.,.) = 
#>   0.8256  0.1999
#>   0.1343  0.4721
#> [ CPUDoubleType{2,2,2} ]
identical(x, as_array(y))
#> [1] TRUE

Simple Autograd Example

In the following snippet we let torch, using the autograd feature, calculate the derivatives:

x <- torch_tensor(1, requires_grad = TRUE)
w <- torch_tensor(2, requires_grad = TRUE)
b <- torch_tensor(3, requires_grad = TRUE)
y <- w * x + b
y$backward()
x$grad
#> torch_tensor
#>  2
#> [ CPUFloatType{1} ]
w$grad
#> torch_tensor
#>  1
#> [ CPUFloatType{1} ]
b$grad
#> torch_tensor
#>  1
#> [ CPUFloatType{1} ]

Contributing

No matter your current skills it’s possible to contribute to torch development. See the contributing guide for more information.

Copy Link

Version

Install

install.packages('torch')

Monthly Downloads

11,102

Version

0.1.1

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Daniel Falbel

Last Published

October 20th, 2020

Functions in torch (0.1.1)

cuda_is_available

Returns a bool indicating if CUDA is currently available.
autograd_set_grad_mode

Set grad mode
cuda_device_count

Returns the number of GPUs available.
as_array

Converts to array
cuda_current_device

Returns the index of a currently selected device.
AutogradContext

Class representing the context.
autograd_backward

Computes the sum of gradients of given tensors w.r.t. graph leaves.
autograd_grad

Computes and returns the sum of gradients of outputs w.r.t. the inputs.
dataloader

Data loader. Combines a dataset and a sampler, and provides single- or multi-process iterators over the dataset.
dataset

An abstract class representing a Dataset.
is_torch_device

Checks if object is a device
is_undefined_tensor

Checks if a tensor is undefined
install_torch

Install Torch
is_dataloader

Checks if the object is a dataloader
dataloader_make_iter

Creates an iterator from a DataLoader
autograd_function

Records operation history and defines formulas for differentiating ops.
dataloader_next

Get the next element of a dataloader iterator
is_torch_dtype

Check if object is a torch data type
nn_adaptive_avg_pool3d

Applies a 3D adaptive average pooling over an input signal composed of several input planes.
nn_conv_transpose3d

ConvTranpose3D module
nn_conv_transpose2d

ConvTranpose2D module
nn_adaptive_log_softmax_with_loss

AdaptiveLogSoftmaxWithLoss module
nn_avg_pool3d

Applies a 3D average pooling over an input signal composed of several input planes.
nn_avg_pool2d

Applies a 2D average pooling over an input signal composed of several input planes.
enumerate

Enumerate an iterator
is_torch_qscheme

Checks if an object is a QScheme
nn_bilinear

Bilinear module
lr_lambda

Sets the learning rate of each parameter group to the initial lr times a given function. When last_epoch=-1, sets initial lr as lr.
nn_dropout3d

Dropout3D module
nn_init_constant_

Constant initialization
nn_init_calculate_gain

Calculate gain
nn_dropout2d

Dropout2D module
nn_buffer

Creates a nn_buffer
nn_adaptive_avg_pool1d

Applies a 1D adaptive average pooling over an input signal composed of several input planes.
nn_linear

Linear module
nn_adaptive_avg_pool2d

Applies a 2D adaptive average pooling over an input signal composed of several input planes.
load_state_dict

Load a state dict file
nn_init_kaiming_uniform_

Kaiming uniform initialization
enumerate.dataloader

Enumerate an iterator
nn_embedding

Embedding module
nn_init_zeros_

Zeros initialization
nn_elu

ELU module
nn_max_pool1d

MaxPool1D module
nn_poisson_nll_loss

Poisson NLL loss
nn_conv2d

Conv2D module
nn_dropout

Dropout module
nn_ctc_loss

The Connectionist Temporal Classification loss.
nn_init_kaiming_normal_

Kaiming normal initialization
nn_conv1d

Conv1D module
nn_max_pool2d

MaxPool2D module
nn_max_unpool2d

Computes a partial inverse of MaxPool2d.
nn_max_unpool3d

Computes a partial inverse of MaxPool3d.
nn_log_sigmoid

LogSigmoid module
nn_multilabel_margin_loss

Multilabel margin loss
nn_hardshrink

Hardshwink module
is_nn_module

Checks if the object is an nn_module
is_nn_buffer

Checks if the object is a nn_buffer
nn_gelu

GELU module
nn_adaptive_max_pool1d

Applies a 1D adaptive max pooling over an input signal composed of several input planes.
is_torch_layout

Check if an object is a torch layout.
nn_init_eye_

Eye initialization
is_torch_memory_format

Check if an object is a memory format
nnf_fold

Fold
nnf_adaptive_avg_pool1d

Adaptive_avg_pool1d
nnf_adaptive_avg_pool2d

Adaptive_avg_pool2d
nn_tanh

Tanh module
nn_multihead_attention

MultiHead attention
nn_softsign

Softsign module
nn_adaptive_max_pool2d

Applies a 2D adaptive max pooling over an input signal composed of several input planes.
nnf_adaptive_max_pool1d

Adaptive_max_pool1d
nn_bce_loss

Binary cross entropy loss
lr_scheduler

Creates learning rate schedulers
lr_step

Step learning rate decay
nn_prelu

PReLU module
nn_utils_clip_grad_value_

Clips gradient of an iterable of parameters at specified value.
nn_sigmoid

Sigmoid module
nn_smooth_l1_loss

Smooth L1 loss
nnf_conv1d

Conv1d
torch_set_default_dtype

Gets and sets the default floating point dtype.
nn_init_normal_

Normal initialization
nn_init_dirac_

Dirac initialization
nn_margin_ranking_loss

Margin ranking loss
nn_batch_norm1d

BatchNorm1D module
nn_batch_norm2d

BatchNorm2D
nn_mse_loss

MSE loss
nnf_one_hot

One_hot
nnf_lp_pool2d

Lp_pool2d
nnf_binary_cross_entropy

Binary_cross_entropy
nnf_bilinear

Bilinear
nnf_interpolate

Interpolate
nnf_dropout2d

Dropout2d
nnf_softmin

Softmin
nnf_adaptive_avg_pool3d

Adaptive_avg_pool3d
nn_utils_clip_grad_norm_

Clips gradient norm of an iterable of parameters.
nnf_contrib_sparsemax

Sparsemax
nn_cosine_embedding_loss

Cosine embedding loss
nnf_dropout

Dropout
is_nn_parameter

Checks if an object is a nn_parameter
nn_glu

GLU module
nn_fractional_max_pool2d

Applies a 2D fractional max pooling over an input signal composed of several input planes.
nn_conv_transpose1d

ConvTranspose1D
nn_conv3d

Conv3D module
nn_bce_with_logits_loss

BCE with logits loss
nn_multilabel_soft_margin_loss

Multi label soft margin loss
nn_fractional_max_pool3d

Applies a 3D fractional max pooling over an input signal composed of several input planes.
nn_softmax2d

Softmax2d module
is_optimizer

Checks if the object is a torch optimizer
torch_addr

Addr
nn_init_sparse_

Sparse initialization
nn_cross_entropy_loss

CrossEntropyLoss module
nnf_hardtanh

Hardtanh
nn_hardsigmoid

Hardsigmoid module
optim_adagrad

Adagrad optimizer
torch_matrix_power

Matrix_power
nnf_normalize

Normalize
nnf_margin_ranking_loss

Margin_ranking_loss
nnf_kl_div

Kl_div
nn_init_orthogonal_

Orthogonal initialization
nnf_affine_grid

Affine_grid
nn_relu

ReLU module
nn_hinge_embedding_loss

Hinge embedding loss
nn_avg_pool1d

Applies a 1D average pooling over an input signal composed of several input planes.
lr_multiplicative

Multiply the learning rate of each parameter group by the factor given in the specified function. When last_epoch=-1, sets initial lr as lr.
nn_adaptive_max_pool3d

Applies a 3D adaptive max pooling over an input signal composed of several input planes.
nn_identity

Identity module
lr_one_cycle

Once cycle learning rate
nnf_pdist

Pdist
torch_conv3d

Conv3d
nn_celu

CELU module
nnf_pixel_shuffle

Pixel_shuffle
nnf_softplus

Softplus
torch_addmm

Addmm
torch_floor_divide

Floor_divide
torch_ceil

Ceil
optim_adam

Implements Adam algorithm.
torch_tril_indices

Tril_indices
torch_addcmul

Addcmul
torch_dtype

Torch data types
nn_hardtanh

Hardtanh module
torch_bincount

Bincount
torch_addmv

Addmv
torch_is_floating_point

Is_floating_point
torch_celu

Celu
torch_conj

Conj
torch_bitwise_and

Bitwise_and
torch_conv2d

Conv2d
torch_load

Loads a saved object
nnf_alpha_dropout

Alpha_dropout
%>%

Pipe operator
nnf_softmax

Softmax
nn_hardswish

Hardswish module
nnf_cosine_similarity

Cosine_similarity
nn_softmax

Softmax module
nn_utils_rnn_pack_sequence

Packs a list of variable length Tensors
nn_init_xavier_normal_

Xavier normal initialization
nn_init_xavier_uniform_

Xavier uniform initialization
nnf_mse_loss

Mse_loss
torch_ormqr

Ormqr
torch_diagflat

Diagflat
nn_contrib_sparsemax

Sparsemax activation
nn_init_ones_

Ones initialization
nn_l1_loss

L1 loss
torch_bitwise_or

Bitwise_or
torch_arange

Arange
nnf_hardswish

Hardswish
nnf_selu

Selu
nn_init_uniform_

Uniform initialization
nn_init_trunc_normal_

Truncated normal initialization
nn_lp_pool1d

Applies a 1D power-average pooling over an input signal composed of several input planes.
nn_sequential

A sequential container
nnf_fractional_max_pool2d

Fractional_max_pool2d
nn_log_softmax

LogSoftmax module
nn_lp_pool2d

Applies a 2D power-average pooling over an input signal composed of several input planes.
nn_utils_rnn_pad_sequence

Pad a list of variable length Tensors with padding_value
nnf_soft_margin_loss

Soft_margin_loss
nnf_binary_cross_entropy_with_logits

Binary_cross_entropy_with_logits
nn_tanhshrink

Tanhshrink module
nn_rnn

RNN module
nnf_logsigmoid

Logsigmoid
nn_multi_margin_loss

Multi margin loss
nnf_embedding_bag

Embedding_bag
torch_celu_

Celu_
nn_kl_div_loss

Kullback-Leibler divergence loss
nn_soft_margin_loss

Soft margin loss
nn_max_pool3d

Applies a 3D max pooling over an input signal composed of several input planes.
nnf_lp_pool1d

Lp_pool1d
torch_conv1d

Conv1d
torch_det

Det
torch_promote_types

Promote_types
torch_logical_or

Logical_or
nnf_multi_head_attention_forward

Multi head attention forward
torch_floor

Floor
torch_eig

Eig
torch_device

Create a Device object
nnf_ctc_loss

Ctc_loss
nn_rrelu

RReLU module
nn_parameter

Creates an nn_parameter
nn_softplus

Softplus module
nn_max_unpool1d

Computes a partial inverse of MaxPool1d.
nn_pairwise_distance

Pairwise distance
nnf_rrelu

Rrelu
torch_addcdiv

Addcdiv
torch_argmax

Argmax
torch_combinations

Combinations
nnf_triplet_margin_with_distance_loss

Triplet margin with distance loss
optim_asgd

Averaged Stochastic Gradient Descent optimizer
torch_addbmm

Addbmm
nnf_triplet_margin_loss

Triplet_margin_loss
optim_required

Dummy value indicating a required value.
torch_clamp

Clamp
torch_expm1

Expm1
nn_leaky_relu

LeakyReLU module
nnf_gumbel_softmax

Gumbel_softmax
nnf_conv2d

Conv2d
nn_threshold

Threshoold module
optim_sgd

SGD optimizer
torch_argmin

Argmin
nn_softshrink

Softshrink module
nnf_avg_pool3d

Avg_pool3d
torch_frac

Frac
nnf_cosine_embedding_loss

Cosine_embedding_loss
nn_utils_rnn_pad_packed_sequence

Pads a packed batch of variable length sequences.
nnf_instance_norm

Instance_norm
nn_selu

SELU module
nnf_layer_norm

Layer_norm
torch_cross

Cross
torch_diagonal

Diagonal
nn_softmin

Softmin
nn_nll_loss

Nll loss
torch_matrix_rank

Matrix_rank
nnf_conv_transpose2d

Conv_transpose2d
nn_utils_rnn_pack_padded_sequence

Packs a Tensor containing padded sequences of variable length.
nn_triplet_margin_with_distance_loss

Triplet margin with distance loss
nn_triplet_margin_loss

Triplet margin loss
torch_logical_not

Logical_not
torch_is_installed

Verifies if torch is installed
torch_linspace

Linspace
nnf_batch_norm

Batch_norm
nn_module

Base class for all neural network modules.
nn_module_list

Holds submodules in a list.
torch_div

Div
torch_bitwise_not

Bitwise_not
torch_logspace

Logspace
nnf_avg_pool2d

Avg_pool2d
nnf_relu6

Relu6
torch_ger

Ger
torch_cosine_similarity

Cosine_similarity
torch_eq

Eq
torch_quantize_per_channel

Quantize_per_channel
torch_log1p

Log1p
torch_cholesky

Cholesky
nn_relu6

ReLu6 module
nnf_conv3d

Conv3d
nnf_tanhshrink

Tanhshrink
torch_dot

Dot
nnf_max_unpool2d

Max_unpool2d
torch_mm

Mm
nnf_local_response_norm

Local_response_norm
torch_conv_tbc

Conv_tbc
nnf_max_pool1d

Max_pool1d
nnf_conv_transpose3d

Conv_transpose3d
nnf_dropout3d

Dropout3d
torch_quantize_per_tensor

Quantize_per_tensor
torch_pdist

Pdist
nnf_group_norm

Group_norm
torch_angle

Angle
torch_allclose

Allclose
torch_equal

Equal
torch_tan

Tan
torch_prod

Prod
torch_mode

Mode
nnf_glu

Glu
nnf_conv_tbc

Conv_tbc
torch_reduction

Creates the reduction objet
nnf_adaptive_max_pool2d

Adaptive_max_pool2d
nnf_cross_entropy

Cross_entropy
nnf_avg_pool1d

Avg_pool1d
nnf_multilabel_soft_margin_loss

Multilabel_soft_margin_loss
nnf_log_softmax

Log_softmax
torch_argsort

Argsort
nnf_hardsigmoid

Hardsigmoid
torch_cholesky_solve

Cholesky_solve
nnf_pairwise_distance

Pairwise_distance
nnf_adaptive_max_pool3d

Adaptive_max_pool3d
torch_randint

Randint
torch_rsqrt

Rsqrt
torch_pinverse

Pinverse
torch_flatten

Flatten
nnf_max_pool2d

Max_pool2d
nnf_elu

Elu
torch_rrelu_

Rrelu_
nnf_relu

Relu
nnf_nll_loss

Nll_loss
nnf_hardshrink

Hardshrink
torch_randint_like

Randint_like
nnf_softshrink

Softshrink
nnf_leaky_relu

Leaky_relu
nnf_gelu

Gelu
nnf_linear

Linear
torch_cdist

Cdist
nnf_fractional_max_pool3d

Fractional_max_pool3d
nnf_max_unpool3d

Max_unpool3d
nnf_pad

Pad
nnf_embedding

Embedding
optim_rprop

Implements the resilient backpropagation algorithm.
torch_bmm

Bmm
nnf_celu

Celu
nnf_grid_sample

Grid_sample
nnf_max_pool3d

Max_pool3d
torch_hamming_window

Hamming_window
torch_broadcast_tensors

Broadcast_tensors
nnf_conv_transpose1d

Conv_transpose1d
tensor_dataset

Dataset wrapping tensors.
nnf_softsign

Softsign
torch_cumsum

Cumsum
nnf_l1_loss

L1_loss
torch_digamma

Digamma
torch_relu

Relu
nnf_multi_margin_loss

Multi_margin_loss
nnf_unfold

Unfold
torch_adaptive_avg_pool1d

Adaptive_avg_pool1d
torch_lu

LU
optim_rmsprop

RMSprop optimizer
nnf_hinge_embedding_loss

Hinge_embedding_loss
torch_chunk

Chunk
torch_bitwise_xor

Bitwise_xor
torch_hann_window

Hann_window
torch_lt

Lt
torch_var_mean

Var_mean
optim_adadelta

Adadelta optimizer
torch_triu

Triu
torch_triangular_solve

Triangular_solve
torch_take

Take
torch_add

Add
torch_mv

Mv
torch_cat

Cat
torch_fft

Fft
nnf_sigmoid

Sigmoid
torch_abs

Abs
nnf_multilabel_margin_loss

Multilabel_margin_loss
nnf_smooth_l1_loss

Smooth_l1_loss
torch_acos

Acos
torch_atan

Atan
torch_empty_like

Empty_like
torch_tril

Tril
torch_gt

Gt
torch_manual_seed

Sets the seed for generating random numbers.
torch_logical_xor

Logical_xor
torch_where

Where
torch_zeros

Zeros
torch_conv_transpose1d

Conv_transpose1d
threads

Number of threads
torch_erfc

Erfc
nnf_prelu

Prelu
torch_log2

Log2
torch_fmod

Fmod
torch_cummax

Cummax
torch_cummin

Cummin
torch_dist

Dist
torch_conv_transpose3d

Conv_transpose3d
torch_index_select

Index_select
torch_empty_strided

Empty_strided
nnf_max_unpool1d

Max_unpool1d
torch_asin

Asin
torch_avg_pool1d

Avg_pool1d
torch_diag

Diag
torch_as_strided

As_strided
torch_cholesky_inverse

Cholesky_inverse
nnf_poisson_nll_loss

Poisson_nll_loss
torch_bernoulli

Bernoulli
torch_bartlett_window

Bartlett_window
torch_matmul

Matmul
nnf_threshold

Threshold
torch_gather

Gather
torch_empty

Empty
torch_pixel_shuffle

Pixel_shuffle
torch_conv_transpose2d

Conv_transpose2d
torch_selu

Selu
torch_tanh

Tanh
torch_mvlgamma

Mvlgamma
torch_orgqr

Orgqr
torch_atan2

Atan2
torch_selu_

Selu_
torch_randn_like

Randn_like
torch_cos

Cos
torch_randn

Randn
torch_baddbmm

Baddbmm
with_no_grad

Temporarily modify gradient recording.
torch_blackman_window

Blackman_window
torch_erfinv

Erfinv
torch_chain_matmul

Chain_matmul
torch_cartesian_prod

Cartesian_prod
torch_can_cast

Can_cast
torch_einsum

Einsum
torch_median

Median
torch_geqrf

Geqrf
torch_generator

Create a Generator object
torch_ones

Ones
torch_max

Max
torch_qr

Qr
torch_histc

Histc
torch_ne

Ne
torch_cumprod

Cumprod
torch_kthvalue

Kthvalue
torch_isnan

Isnan
torch_rfft

Rfft
torch_finfo

Floating point type info
torch_lerp

Lerp
torch_sort

Sort
torch_erf

Erf
torch_isfinite

Isfinite
torch_full

Full
torch_mean

Mean
torch_logdet

Logdet
torch_rand

Rand
torch_diag_embed

Diag_embed
torch_logical_and

Logical_and
torch_triu_indices

Triu_indices
torch_cosh

Cosh
torch_flip

Flip
torch_iinfo

Integer type info
torch_imag

Imag
torch_narrow

Narrow
torch_eye

Eye
torch_layout

Creates the corresponding layout
torch_norm

Norm
torch_split

Split
torch_sqrt

Sqrt
torch_tensor

Converts R objects to a torch tensor
torch_ifft

Ifft
torch_logsumexp

Logsumexp
torch_full_like

Full_like
torch_remainder

Remainder
torch_lstsq

Lstsq
torch_real

Real
torch_normal

Normal
torch_isinf

Isinf
torch_round

Round
torch_range

Range
torch_unique_consecutive

Unique_consecutive
torch_memory_format

Memory format
torch_symeig

Symeig
torch_lu_solve

Lu_solve
torch_lgamma

Lgamma
torch_result_type

Result_type
torch_reshape

Reshape
torch_neg

Neg
torch_save

Saves an object to a disk file.
torch_sparse_coo_tensor

Sparse_coo_tensor
torch_trapz

Trapz
torch_scalar_tensor

Scalar tensor
torch_ge

Ge
torch_exp

Exp
torch_log

Log
torch_meshgrid

Meshgrid
torch_t

T
torch_inverse

Inverse
torch_nonzero

Nonzero
torch_randperm

Randperm
torch_stack

Stack
torch_transpose

Transpose
torch_trace

Trace
torch_unsqueeze

Unsqueeze
torch_le

Le
torch_tensordot

Tensordot
torch_poisson

Poisson
torch_log10

Log10
torch_is_complex

Is_complex
torch_masked_select

Masked_select
torch_renorm

Renorm
torch_irfft

Irfft
torch_stft

Stft
torch_sigmoid

Sigmoid
torch_rot90

Rot90
with_enable_grad

Enable grad
torch_sin

Sin
torch_roll

Roll
torch_sinh

Sinh
torch_rand_like

Rand_like
torch_polygamma

Polygamma
torch_min

Min
torch_reciprocal

Reciprocal
torch_ones_like

Ones_like
torch_mul

Mul
torch_multinomial

Multinomial
torch_zeros_like

Zeros_like
torch_sum

Sum
torch_std

Std
torch_qscheme

Creates the corresponding Scheme object
torch_square

Square
torch_std_mean

Std_mean
torch_relu_

Relu_
torch_pow

Pow
torch_squeeze

Squeeze
torch_var

Var
torch_threshold_

Threshold_
torch_trunc

Trunc
torch_unbind

Unbind
torch_topk

Topk
torch_true_divide

TRUE_divide
torch_repeat_interleave

Repeat_interleave
torch_sign

Sign
torch_solve

Solve
torch_slogdet

Slogdet
torch_svd

Svd