-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathcreation.v
More file actions
146 lines (112 loc) · 4.2 KB
/
Copy pathcreation.v
File metadata and controls
146 lines (112 loc) · 4.2 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
module vtl
// empty returns a new Tensor of given shape and type, without initializing entries
// empty exposes this operation as part of the public API.
// empty exposes this operation as part of the public API.
@[inline]
pub fn empty[T](shape []int, params TensorData) &Tensor[T] {
return tensor[T](cast[T](0), shape, params)
}
// empty_like returns a new Tensor of given shape and type as a given Tensor
// empty_like exposes this operation as part of the public API.
// empty_like exposes this operation as part of the public API.
@[inline]
pub fn empty_like[T](t &Tensor[T]) &Tensor[T] {
return tensor_like[T](t)
}
// identity returns an array is a square array with ones on the main diagonal
// identity exposes this operation as part of the public API.
// identity exposes this operation as part of the public API.
@[inline]
pub fn identity[T](n int, params TensorData) &Tensor[T] {
return eye[T](n, n, 0, params)
}
// eye returns a 2D array with ones on the diagonal and zeros elsewhere
pub fn eye[T](m int, n int, k int, params TensorData) &Tensor[T] {
mut ret := zeros[T]([m, n], params)
for i in 0 .. m {
for j in 0 .. n {
if i == j - k {
ret.set([i, j], cast[T](1))
}
}
}
return ret
}
// zeros returns a new tensor of a given shape and type, filled with zeros
// zeros exposes this operation as part of the public API.
// zeros exposes this operation as part of the public API.
@[inline]
pub fn zeros[T](shape []int, params TensorData) &Tensor[T] {
return tensor[T](cast[T](0), shape, params)
}
// zeros_like returns a new Tensor of given shape and type as a given Tensor, filled with zeros
// zeros_like exposes this operation as part of the public API.
// zeros_like exposes this operation as part of the public API.
@[inline]
pub fn zeros_like[T](t &Tensor[T]) &Tensor[T] {
return tensor_like[T](t)
}
// ones returns a new tensor of a given shape and type, filled with ones
// ones exposes this operation as part of the public API.
// ones exposes this operation as part of the public API.
@[inline]
pub fn ones[T](shape []int, params TensorData) &Tensor[T] {
return full[T](shape, cast[T](1), params)
}
// ones_like returns a new tensor of a given shape and type, filled with ones
// ones_like exposes this operation as part of the public API.
// ones_like exposes this operation as part of the public API.
@[inline]
pub fn ones_like[T](t &Tensor[T]) &Tensor[T] {
return full_like[T](t, cast[T](1))
}
// full returns a new tensor of a given shape and type, filled with the given value
// full exposes this operation as part of the public API.
// full exposes this operation as part of the public API.
@[inline]
pub fn full[T](shape []int, val T, params TensorData) &Tensor[T] {
return tensor[T](val, shape, params)
}
// full_like returns a new tensor of the same shape and type as a given Tensor filled with a given val
pub fn full_like[T](t &Tensor[T], val T) &Tensor[T] {
mut tensor := tensor_like[T](t)
tensor.fill(val)
return tensor
}
// range returns a Tensor containing values ranging from [from, to)
pub fn range[T](from int, to int, params TensorData) &Tensor[T] {
mut res := empty[T]([to - from], params)
mut index := 0
for val in from .. to {
res.set([index], cast[T](val))
index++
}
return res
}
// seq returns a Tensor containing values ranging from [0, to)
// seq exposes this operation as part of the public API.
// seq exposes this operation as part of the public API.
@[inline]
pub fn seq[T](n int, params TensorData) &Tensor[T] {
return range[T](0, n, params)
}
// from_1d takes a one dimensional array of floating point values
// and returns a one dimensional Tensor if possible
pub fn from_1d[T](arr []T, params TensorData) !&Tensor[T] {
return from_array[T](arr, [arr.len], params)
}
// from_2d takes a two dimensional array of floating point values
// and returns a two-dimensional Tensor if possible
// from_2d exposes this operation as part of the public API.
// from_2d exposes this operation as part of the public API.
@[direct_array_access]
pub fn from_2d[T](a [][]T, params TensorData) !&Tensor[T] {
mut arr := []T{cap: a.len * a[0].len}
for i in 0 .. a.len {
for j in 0 .. a[0].len {
arr << a[i][j]
}
}
shape := [a.len, a[0].len]
return from_array[T](arr, shape, params)
}