layers

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2025 License: Apache-2.0 Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package layers implements layers and graph functions that commonly used in GNN operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SparseSoftmax

func SparseSoftmax(logits, logitsMask, indices *Node, maxIndex int, sorted bool) *Node

SparseSoftmax takes a logits vector [N] and the indices where they should aggregate [N, 1], and returns the probabilities:

denominator[j] = \sum_{i \in set{indices[i] = j}}(exp(logits[i]))
softmask[i] = exp(logits[i]) / denominator[indices[i]]

If logitsMask is not nil, exp(logits[i]) is replaced with 0 where logitsMask is false.

Args:

  • logits: vector shaped [N] of some float dtype.
  • logitsMask: nil or a vector shaped [N] of bools.
  • indices: shapes [N, 1], with the indices on which the logits are part of.
  • maxIndex: it's the max(indices), it is required statically (not a graph Node) because we reduce sum the exponentials on a temporary node with this shape. It must be known in the graph building time.
  • sorted: if the indices are sorted set this to true, it will lead to faster runtime in some platforms. In doubt, leave this as false: it could lead to undefined behavior if the indices are not actually sorted.

It returns a vector shaped [N] with the same dtype as logits.

Example:

logits := Const(g, []float32{ln1, ln2, ln1, ln2, ln1, 0, 0, 0})
indices := Const(g, []int32{0, 0, 0, 1, 1, 0, 0, 0})
mask := Const(g, []bool{true, true, true, true, true, false, false, false})
inputs = []*Node{logits, indices, mask}
softmax := SparseSoftmax(logits, mask, ExpandAxes(indices, -1), 2, false)
// softmax == []float32{1.0 / 4, 2.0 / 4, 1.0 / 4, 2.0 / 3, 1.0 / 3, 0, 0, 0}

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL