/// <summary> /// A simple lookup table that stores embeddings of a fixed dictionary and size. /// This module is often used to store word embeddings and retrieve them using indices. The input to the module is a list of indices, and the output is the corresponding word embeddings. /// </summary> /// <param name="x">An input tensor of arbitrary shape.</param> /// <param name="num_embeddings">Size of the dictionary of embeddings, the vocabulary size.</param> /// <param name="embedding_dims">The size of each embedding vector</param> /// <param name="max_norm">If given, each embedding vector with norm larger than max_norm is renormalized to have norm max_norm.</param> /// <param name="norm_type">The p of the p-norm to compute for the max_norm option. Default 2.</param> /// <param name="scale_grad_by_freq">If given, this will scale gradients by the inverse of frequency of the words in the mini-batch. Default: false.</param> /// <param name="mode">"sum", "mean" or "max". Specifies the way to reduce the bag. /// "sum" computes the weighted sum, taking per_sample_weights into consideration. /// "mean" computes the average of the values in the bag, "max" computes the max value over each bag. Default: "mean"</param> /// <param name="sparse">If true, gradient w.r.t. weight matrix will be a sparse tensor. Default: false</param> /// <param name="include_last_offset">if true, offsets has one additional element, where the last element is equivalent to the size of indices.</param> /// <returns></returns> /// <remarks>Keep in mind that only a limited number of optimizers support sparse gradients: currently it’s optim.SGD (CUDA and CPU), optim.SparseAdam (CUDA and CPU) and optim.Adagrad (CPU)</remarks> static public TorchTensor EmbeddingBag(TorchTensor x, long num_embeddings, long embedding_dims, double?max_norm = null, double norm_type = 2.0, bool scale_grad_by_freq = false, EmbeddingBagMode mode = EmbeddingBagMode.Mean, bool sparse = false, bool include_last_offset = false) { using (var d = Modules.EmbeddingBag(num_embeddings, embedding_dims, max_norm, norm_type, scale_grad_by_freq, mode, sparse, include_last_offset)) { return(d.forward(x)); } }
/// <summary> /// A simple lookup table that stores embeddings of a fixed dictionary and size. /// This module is often used to store word embeddings and retrieve them using indices. The input to the module is a list of indices, and the output is the corresponding word embeddings. /// </summary> /// <param name="embeddings">FloatTensor containing weights for the EmbeddingBag in two dimensions. First dimension is being passed to EmbeddingBag as num_embeddings, second as embedding_dim.</param> /// <param name="freeze">If true (the default), the tensor does not get updated in the learning</param> /// <param name="max_norm">If given, each embedding vector with norm larger than max_norm is renormalized to have norm max_norm.</param> /// <param name="norm_type">The p of the p-norm to compute for the max_norm option. Default 2.</param> /// <param name="scale_grad_by_freq">If given, this will scale gradients by the inverse of frequency of the words in the mini-batch. Default: false.</param> /// <param name="mode"></param> /// <param name="sparse">If true, gradient w.r.t. weight matrix will be a sparse tensor. Default: false</param> /// <param name="include_last_offset">If true, offsets has one additional element, where the last element is equivalent to the size of indices. This matches the CSR format.</param> /// <param name="padding_index"> If specified, the entries at padding_idx do not contribute to the gradient; therefore, the embedding vector at padding_idx is not updated during training, i.e. it remains as a fixed “pad”. For a newly constructed EmbeddingBag, the embedding vector at padding_idx will default to all zeros, but can be updated to another value to be used as the padding vector. Note that the embedding vector at padding_idx is excluded from the reduction.</param> /// <returns></returns> /// <remarks>Keep in mind that only a limited number of optimizers support sparse gradients: currently it’s optim.SGD (CUDA and CPU), optim.SparseAdam (CUDA and CPU) and optim.Adagrad (CPU)</remarks> public static EmbeddingBag EmbeddingBag_from_pretrained(Tensor embeddings, bool freeze = true, double?max_norm = null, double norm_type = 2.0, bool scale_grad_by_freq = false, EmbeddingBagMode mode = EmbeddingBagMode.Mean, bool sparse = false, bool include_last_offset = false, long padding_index = -1) { var res = THSNN_EmbeddingBag_from_pretrained(embeddings.Handle, freeze, max_norm.HasValue ? max_norm.Value : 0.0, max_norm.HasValue, norm_type, scale_grad_by_freq, (long)mode, sparse, include_last_offset, padding_index, out var boxedHandle); if (res == IntPtr.Zero) { torch.CheckForErrors(); } return(new EmbeddingBag(res, boxedHandle)); }
/// <summary> /// A simple lookup table that stores embeddings of a fixed dictionary and size. /// This module is often used to store word embeddings and retrieve them using indices. The input to the module is a list of indices, and the output is the corresponding word embeddings. /// </summary> /// <param name="num_embeddings">Size of the dictionary of embeddings, the vocabulary size.</param> /// <param name="embedding_dims">The size of each embedding vector</param> /// <param name="max_norm">If given, each embedding vector with norm larger than max_norm is renormalized to have norm max_norm.</param> /// <param name="norm_type">The p of the p-norm to compute for the max_norm option. Default 2.</param> /// <param name="scale_grad_by_freq">If given, this will scale gradients by the inverse of frequency of the words in the mini-batch. Default: false.</param> /// <param name="mode">"sum", "mean" or "max". Specifies the way to reduce the bag. /// "sum" computes the weighted sum, taking per_sample_weights into consideration. /// "mean" computes the average of the values in the bag, "max" computes the max value over each bag. Default: "mean"</param> /// <param name="sparse">If true, gradient w.r.t. weight matrix will be a sparse tensor. Default: false</param> /// <param name="include_last_offset">if true, offsets has one additional element, where the last element is equivalent to the size of indices.</param> /// <returns></returns> /// <remarks>Keep in mind that only a limited number of optimizers support sparse gradients: currently it’s optim.SGD (CUDA and CPU), optim.SparseAdam (CUDA and CPU) and optim.Adagrad (CPU)</remarks> static public EmbeddingBag EmbeddingBag(long num_embeddings, long embedding_dims, double?max_norm = null, double norm_type = 2.0, bool scale_grad_by_freq = false, EmbeddingBagMode mode = EmbeddingBagMode.Mean, bool sparse = false, bool include_last_offset = false) { var res = THSNN_EmbeddingBag_ctor(num_embeddings, embedding_dims, max_norm.HasValue ? max_norm.Value : 0.0, max_norm.HasValue, norm_type, scale_grad_by_freq, (long)mode, sparse, include_last_offset, out var boxedHandle); if (res == IntPtr.Zero) { Torch.CheckForErrors(); } return(new EmbeddingBag(res, boxedHandle)); }