Documentation ¶
Index ¶
- Constants
- func AllClose(a, b Tensor) bool
- func Equal(a, b Tensor) bool
- func FinishGC()
- func GC()
- func IsCUDAAvailable() bool
- func IsCUDNNAvailable() bool
- func MustNil(err unsafe.Pointer)
- func SetCurrentCUDAStream(stream CUDAStream)
- func SetNumThreads(n int32)
- func SetTensorFinalizer(t *unsafe.Pointer)
- func TopK(a Tensor, k, dim int64, largest, sorted bool) (Tensor, Tensor)
- type CUDAStream
- type Device
- type Optimizer
- type Tensor
- func Add(a, other Tensor, alpha float32) Tensor
- func Arange(begin, end, step float32, requiresGrad bool) Tensor
- func Div(a, other Tensor) Tensor
- func Empty(shape []int64, requiresGrad bool) Tensor
- func Eq(a, other Tensor) Tensor
- func ExpandAs(a, other Tensor) Tensor
- func Eye(n, m int64, requiresGrad bool) Tensor
- func Flatten(a Tensor, startDim, endDim int64) Tensor
- func FromBlob(data unsafe.Pointer, dtype int8, sizes []int64) Tensor
- func Full(shape []int64, value float32, requiresGrad bool) Tensor
- func IndexSelect(a Tensor, dim int64, index Tensor) Tensor
- func LeakyRelu(t Tensor, negativeSlope float64) Tensor
- func Linspace(begin, end float32, steps int64, requiresGrad bool) Tensor
- func Load(path string) Tensor
- func LogSoftmax(t Tensor, dim int64) Tensor
- func Logspace(begin, end float32, steps int64, base float64, requiresGrad bool) Tensor
- func MM(a, b Tensor) Tensor
- func Mean(t Tensor) Tensor
- func Mul(a, other Tensor) Tensor
- func NewTensor(data interface{}, options ...map[string]interface{}) Tensor
- func Ones(shape []int64, requiresGrad bool) Tensor
- func Rand(shape []int64, requireGrad bool) Tensor
- func RandN(shape []int64, requiresGrad bool) Tensor
- func Relu(t Tensor) Tensor
- func Sigmoid(t Tensor) Tensor
- func Squeeze(t Tensor, dim ...int64) Tensor
- func Stack(tensors []Tensor, dim int64) Tensor
- func Sub(a, other Tensor, alpha float32) Tensor
- func Sum(a Tensor, opt ...map[string]interface{}) Tensor
- func Tanh(t Tensor) Tensor
- func To(a Tensor, device Device, dtype int8) Tensor
- func Transpose(a Tensor, dim0, dim1 int64) Tensor
- func View(a Tensor, shape ...int64) Tensor
- func (a *Tensor) Add(other Tensor, alpha float32) Tensor
- func (a *Tensor) AddI(other Tensor, alpha float32) Tensor
- func (a Tensor) Argmax(opts ...interface{}) Tensor
- func (a Tensor) Argmin(opts ...interface{}) Tensor
- func (a Tensor) Backward()
- func (a Tensor) CUDA(device Device, nonBlocking bool) Tensor
- func (a Tensor) CastTo(dtype int8) Tensor
- func (a Tensor) CopyTo(device Device) Tensor
- func (a *Tensor) Detach() Tensor
- func (a Tensor) Dim() int64
- func (a *Tensor) Div(other Tensor) Tensor
- func (a *Tensor) DivI(other Tensor) Tensor
- func (a Tensor) Dtype() int8
- func (a Tensor) Eq(other Tensor) Tensor
- func (a Tensor) ExpandAs(other Tensor) Tensor
- func (t *Tensor) GobDecode(buf []byte) error
- func (t Tensor) GobEncode() ([]byte, error)
- func (a Tensor) Grad() Tensor
- func (a Tensor) Index(index ...int64) Tensor
- func (a Tensor) IndexSelect(dim int64, index Tensor) Tensor
- func (a Tensor) Item() interface{}
- func (a *Tensor) LeakyRelu(negativeSlope float64) Tensor
- func (a Tensor) LogSoftmax(dim int64) Tensor
- func (a Tensor) Mean() Tensor
- func (a *Tensor) Mul(other Tensor) Tensor
- func (a *Tensor) MulI(other Tensor) Tensor
- func (a *Tensor) Permute(dims []int64) Tensor
- func (a Tensor) PinMemory() Tensor
- func (a Tensor) Print()
- func (a *Tensor) Relu() Tensor
- func (a Tensor) Save(path string)
- func (a Tensor) SetData(b Tensor)
- func (a Tensor) Shape() []int64
- func (a Tensor) Sigmoid() Tensor
- func (a Tensor) Squeeze(dim ...int64) Tensor
- func (a Tensor) String() string
- func (a *Tensor) Sub(other Tensor, alpha float32) Tensor
- func (a *Tensor) SubI(other Tensor, alpha float32) Tensor
- func (a Tensor) Sum(opt ...map[string]interface{}) Tensor
- func (a Tensor) Tanh() Tensor
- func (a Tensor) To(device Device, dtype ...int8) Tensor
- func (a Tensor) Transpose(dim0, dim1 int64) Tensor
- func (a Tensor) View(shape ...int64) Tensor
Examples ¶
Constants ¶
const ( // Byte Dtype 0 Byte int8 = iota // Char Dtype 1 Char // Short Dtype 2 Short // Int Dtype 3 Int // Long Dtype 4 Long // Half Dtype 5 Half // Float Dtype 6 Float // Double Dtype 7 Double // ComplexHalf Dtype 8 ComplexHalf // ComplexFloat Dtype 9 ComplexFloat // ComplexDouble Dtype 10 ComplexDouble // Bool Dtype 11 Bool // QInt8 Dtype 12 QInt8 // QUInt8 Dtype 13 QUInt8 // QInt32 Dtype 14 QInt32 // BFloat16 Dtype 15 BFloat16 // Invalid Dtype Invalid = -1 )
Variables ¶
This section is empty.
Functions ¶
func IsCUDAAvailable ¶
func IsCUDAAvailable() bool
IsCUDAAvailable returns true if CUDA is available
func IsCUDNNAvailable ¶
func IsCUDNNAvailable() bool
IsCUDNNAvailable returns true if cuDNN is available
func SetCurrentCUDAStream ¶
func SetCurrentCUDAStream(stream CUDAStream)
SetCurrentCUDAStream set stream as the current CUDA stream
func SetNumThreads ¶
func SetNumThreads(n int32)
SetNumThreads sets the number of threads created by GoTorch
func SetTensorFinalizer ¶
SetTensorFinalizer sets a finalizer to the tensor
Types ¶
type CUDAStream ¶
type CUDAStream struct {
P C.CUDAStream
}
CUDAStream struct wrapped Nvidia CUDA Stream
func GetCurrentCUDAStream ¶
func GetCurrentCUDAStream(device Device) CUDAStream
GetCurrentCUDAStream returns the current stream on device
func NewCUDAStream ¶
func NewCUDAStream(device Device) CUDAStream
NewCUDAStream returns a new CUDA stream from the pool
func (CUDAStream) Query ¶
func (s CUDAStream) Query() bool
Query returns true if all tasks completed on this CUDA stream
func (CUDAStream) Synchronize ¶
func (s CUDAStream) Synchronize()
Synchronize wait until all tasks completed on this CUDA stream
type Optimizer ¶
Optimizer struct
func SGD ¶
SGD creates a SGD Optimizer
Example ¶
package main import ( "fmt" torch "github.com/wangkuiyi/gotorch" nn "github.com/wangkuiyi/gotorch/nn" ) type myNet struct { nn.Module L1, L2 *nn.LinearModule } // MyNet returns a MyNet instance func newMyNet() *myNet { n := &myNet{ L1: nn.Linear(100, 200, false), L2: nn.Linear(200, 10, false), } n.Init(n) return n } // Forward executes the calculation func (n *myNet) Forward(x torch.Tensor) torch.Tensor { x = n.L1.Forward(x) x = n.L2.Forward(x) return x } func main() { net := newMyNet() np := net.NamedParameters() fmt.Println(len(np)) opt := torch.SGD(0.1, 0, 0, 0, false) opt.AddParameters(net.Parameters()) for i := 0; i < 100; i++ { torch.GC() data := torch.RandN([]int64{32, 100}, false) pre := net.Forward(data) loss := torch.Sum(pre) opt.ZeroGrad() loss.Backward() opt.Step() } torch.FinishGC() opt.Close() }
Output: 2
func (Optimizer) AddParameters ¶
AddParameters adds parameters
type Tensor ¶
Tensor wrappers a pointer to C.Tensor
func Arange ¶
Arange returns a 1-D tensor in range [begin, end) with common difference step beginning from begin
func Eq ¶
Eq wraps torch.eq, which does element-wise comparison between two tensors and returns a tensor of the same size as the operands.
func IndexSelect ¶
IndexSelect torch.index_select
func LogSoftmax ¶
LogSoftmax returns log softmax of the input tensor
func Logspace ¶
Logspace returns a 1-D Tensor of steps points logarithmically spaced with base base between pow(base, begin) and pow(base, end)
func NewTensor ¶
NewTensor creates a tensor from a Go slice. We use variadic parameters of type map[string]interface{} to mimic named variadic parameters.
func To ¶
To returns a Tensor on the specified device with the same content as the a. If the specified device doesn't exist, To panics.
func (Tensor) GobEncode ¶
GobEncode calls C.Tensor_Encode to encode a tensor into a pickle. If the tensor is on GPU, C.Tenosr_Encode clones it in CPU before encoding, so the result always encodes a CPU tensor.
func (Tensor) Index ¶
Index calls Tensor::index to return a single-element tensor of the element at the given index.
func (Tensor) IndexSelect ¶
IndexSelect torch.index_select
func (Tensor) Item ¶
func (a Tensor) Item() interface{}
Item returns 0-dim tensor's value as an interface users should do type assertion and get the value like: v, ok := a.Item().(float64) Currently not support unsigned Tensor.
func (Tensor) LogSoftmax ¶
LogSoftmax returns log softmax of the current tensor
func (Tensor) To ¶
To returns a Tensor on the specified device with the same content as the a. If the specified device doesn't exist, To panics.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
example
|
|
tool
|
|
tarball_divide
tarball_divide takes a .tar.gz file of images and outputs one or more .tar.gz of images, where each contains images in a certain base directory.
|
tarball_divide takes a .tar.gz file of images and outputs one or more .tar.gz of images, where each contains images in a certain base directory. |
tarball_merge
This program is supposed to run together with tarball_divide, which divides a .tar.gz file into one or more smaller tarball files, where each contains files in the same base directory and named by the base directory.
|
This program is supposed to run together with tarball_divide, which divides a .tar.gz file into one or more smaller tarball files, where each contains files in the same base directory and named by the base directory. |