-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathstack.v
More file actions
153 lines (138 loc) · 3.91 KB
/
stack.v
File metadata and controls
153 lines (138 loc) · 3.91 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
147
148
149
150
151
152
153
module vtl
@[params]
pub struct AxisData {
pub:
axis int
}
// vstack stack arrays in sequence vertically (row wise)
pub fn vstack[T](ts []&Tensor[T]) !&Tensor[T] {
return concatenate[T](ts, axis: 0)
}
// hstack stacks arrays in sequence horizontally (column wise)
pub fn hstack[T](ts []&Tensor[T]) !&Tensor[T] {
if ts[0].rank() == 1 {
return concatenate[T](ts, axis: 0)
}
return concatenate[T](ts, axis: 1)
}
// dstack stacks arrays in sequence depth wise (along third axis)
pub fn dstack[T](ts []&Tensor[T]) !&Tensor[T] {
first_tensor := ts[0]
assert_shape[T](first_tensor.shape, ts)!
if first_tensor.rank() > 2 {
return error('dstack was given arrays with more than two dimensions')
}
if first_tensor.rank() == 1 {
mut next_ts := []&Tensor[T]{cap: ts.len}
for t in ts {
next_ts << t.reshape[T]([1, t.size, 1])!
}
return concatenate[T](next_ts, axis: 2)
} else {
mut next_ts := []&Tensor[T]{cap: ts.len}
for t in ts {
mut newshape := t.shape.clone()
newshape << 1
next_ts << t.reshape[T](newshape)!
}
return concatenate[T](next_ts, axis: 2)
}
}
// column_stack stacks 1-D arrays as columns into a 2-D array.
pub fn column_stack[T](ts []&Tensor[T]) !&Tensor[T] {
first_tensor := ts[0]
assert_shape[T](first_tensor.shape, ts)!
if first_tensor.rank() > 2 {
return error('column_stack was given arrays with more than two dimensions')
}
if first_tensor.rank() == 1 {
mut next_ts := []&Tensor[T]{cap: ts.len}
for t in ts {
next_ts << t.reshape[T]([t.size, 1])!
}
return concatenate[T](next_ts, axis: 1)
}
return concatenate[T](ts, axis: 1)
}
// stack join a sequence of arrays along a new axis.
pub fn stack[T](ts []&Tensor[T], data AxisData) !&Tensor[T] {
assert_shape[T](ts[0].shape, ts)!
mut expanded := []&Tensor[T]{cap: ts.len}
for t in ts {
expanded << t.expand_dims[T](data)!
}
return concatenate[T](expanded, data)
}
// unsqueeze adds a dimension of size one to a Tensor
pub fn (t &Tensor[T]) unsqueeze[T](data AxisData) !&Tensor[T] {
axis := data.axis
mut newshape := t.shape.clone()
newaxis := match axis < 0 {
true { axis + t.rank() + 1 }
else { axis }
}
actual_axis := if newaxis > t.rank() { t.rank() } else { newaxis }
newshape.insert(actual_axis, 1)
return t.reshape[T](newshape)
}
// squeeze removes dimensions of size one from a Tensor.
// If dim is provided, only that dimension is removed (must be size 1).
// If no dim is provided, all size-1 dimensions are removed.
pub fn (t &Tensor[T]) squeeze[T](data AxisData) !&Tensor[T] {
mut newshape := []int{}
if data.axis < 0 {
// Remove all size-1 dimensions
for dim in t.shape {
if dim != 1 {
newshape << dim
}
}
} else {
// Remove only the specified dimension
axis := if data.axis >= t.rank() { t.rank() - 1 } else { data.axis }
for i, dim in t.shape {
if i != axis || dim != 1 {
newshape << dim
}
}
}
if newshape.len == 0 {
newshape = [1]
}
return t.reshape[T](newshape)
}
// concatenate concatenates two Tensors together
pub fn concatenate[T](ts []&Tensor[T], data AxisData) !&Tensor[T] {
mut newshape := ts[0].shape.clone()
// just a check for negative axes, so that negative axes can be inferred.
axis := clip_axis(data.axis, newshape.len)!
newshape[axis] = 0
shape := assert_shape_off_axis[T](ts, axis, newshape)!
mut ret := tensor[T](cast[T](0), shape)
mut lo := []int{len: shape.len}
mut hi := shape.clone()
hi[axis] = 0
for t in ts {
if t.shape[axis] != 0 {
hi[axis] += t.shape[axis]
mut slice := ret.slice_hilo(lo, hi)!
slice.assign(t)!
lo[axis] = hi[axis]
}
}
return ret
}
// expand_dims adds an axis to a Tensor in order to support
// broadcasting operations
pub fn (t &Tensor[T]) expand_dims[T](data AxisData) !&Tensor[T] {
axis := data.axis
mut newshape := []int{}
newaxis := match axis < 0 {
true { axis + t.rank() + 1 }
else { axis }
}
newshape << t.shape[..newaxis]
newshape << 1
newshape << t.shape[newaxis..]
return t.reshape[T](newshape)
}