-
Notifications
You must be signed in to change notification settings - Fork 0
/
tensor_ctor.go
134 lines (123 loc) · 3.71 KB
/
tensor_ctor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package gotorch
// #cgo CFLAGS: -I ${SRCDIR}
// #cgo LDFLAGS: -L ${SRCDIR}/cgotorch -Wl,-rpath ${SRCDIR}/cgotorch -lcgotorch
// #cgo LDFLAGS: -L ${SRCDIR}/cgotorch/libtorch/lib -Wl,-rpath ${SRCDIR}/cgotorch/libtorch/lib -lc10 -ltorch -ltorch_cpu
// #include "cgotorch/cgotorch.h"
import "C"
import (
"unsafe"
)
// RandN returns a tensor filled with standard normal distribution, torch.randn
func RandN(shape []int64, requiresGrad bool) Tensor {
rg := 0
if requiresGrad {
rg = 1
}
var t C.Tensor
MustNil(unsafe.Pointer(C.RandN((*C.int64_t)(unsafe.Pointer(&shape[0])),
C.int64_t(len(shape)), C.int64_t(rg), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Rand torch.rand
func Rand(shape []int64, requireGrad bool) Tensor {
rg := 0
if requireGrad {
rg = 1
}
var t C.Tensor
MustNil(unsafe.Pointer(C.Rand((*C.int64_t)(unsafe.Pointer(&shape[0])),
C.int64_t(len(shape)), C.int64_t(rg), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Empty returns a tensor filled with random number, torch.empty
func Empty(shape []int64, requiresGrad bool) Tensor {
rg := 0
if requiresGrad {
rg = 1
}
var t C.Tensor
MustNil(
unsafe.Pointer(C.Empty((*C.int64_t)(unsafe.Pointer(&shape[0])),
C.int64_t(len(shape)), C.int64_t(rg), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Ones return a tensor filled with 1
func Ones(shape []int64, requiresGrad bool) Tensor {
rg := 0
if requiresGrad {
rg = 1
}
var t C.Tensor
MustNil(
unsafe.Pointer(C.Ones((*C.int64_t)(unsafe.Pointer(&shape[0])),
C.int64_t(len(shape)), C.int64_t(rg), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Eye returns a tensor with 1s on diagonal and 0s elsewhere
func Eye(n, m int64, requiresGrad bool) Tensor {
rg := 0
if requiresGrad {
rg = 1
}
var t C.Tensor
MustNil(unsafe.Pointer(C.Eye(C.int64_t(n), C.int64_t(m), C.int64_t(rg), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Full returns a tensor with all elements being given `value`
func Full(shape []int64, value float32, requiresGrad bool) Tensor {
rg := 0
if requiresGrad {
rg = 1
}
var t C.Tensor
MustNil(unsafe.Pointer(C.Full((*C.int64_t)(unsafe.Pointer(&shape[0])),
C.int64_t(len(shape)), C.float(value), C.int64_t(rg), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Arange returns a 1-D tensor in range [begin, end) with
// common difference step beginning from begin
func Arange(begin, end, step float32, requiresGrad bool) Tensor {
rg := 0
if requiresGrad {
rg = 1
}
var t C.Tensor
MustNil(unsafe.Pointer(C.Arange(C.float(begin),
C.float(end), C.float(step), C.int64_t(rg), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Linspace returns a 1-D Tensor in range [begin, end] with steps points
func Linspace(begin, end float32, steps int64, requiresGrad bool) Tensor {
rg := 0
if requiresGrad {
rg = 1
}
var t C.Tensor
MustNil(unsafe.Pointer(C.Linspace(C.float(begin),
C.float(end), C.int64_t(steps), C.int64_t(rg), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Logspace returns a 1-D Tensor of steps points
// logarithmically spaced with base base between
// pow(base, begin) and pow(base, end)
func Logspace(begin, end float32, steps int64,
base float64, requiresGrad bool) Tensor {
rg := 0
if requiresGrad {
rg = 1
}
var t C.Tensor
MustNil(unsafe.Pointer(C.Logspace(C.float(begin),
C.float(end), C.int64_t(steps),
C.double(base), C.int64_t(rg), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}