forked from gorgonia/tensor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_dense_matop_test.go
428 lines (377 loc) · 11.4 KB
/
example_dense_matop_test.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package tensor
import (
"fmt"
)
func ExampleDense_Slice() {
var T Tensor
T = New(WithBacking(Range(Float64, 0, 9)), WithShape(3, 3))
fmt.Printf("T:\n%v\n", T)
// T[0:2, 0:2]
T, _ = T.Slice(makeRS(0, 2), makeRS(0, 2)) // makeRS is an unexported function that creates a Slice.
fmt.Printf("T[0:2, 0:2]:\n%v\n", T)
// T[:, 1]
T, _ = T.(Slicer).Slice(nil, ss(1)) // ss is unexported
fmt.Printf("T[:, 1]:\n%v\n", T)
// Output:
// T:
// ⎡0 1 2⎤
// ⎢3 4 5⎥
// ⎣6 7 8⎦
//
// T[0:2, 0:2]:
// ⎡0 1⎤
// ⎣3 4⎦
//
// T[:, 1]:
// [1 4]
}
// Slicing works on one dimensional arrays too:
func ExampleDense_Slice_oneDimension() {
var T Tensor
T = New(WithBacking(Range(Float64, 0, 9)))
fmt.Printf("T:\n%v\n\n", T)
T, _ = T.Slice(makeRS(0, 5))
fmt.Printf("T[0:5]:\n%v\n", T)
// Output:
// T:
// [0 1 2 3 ... 5 6 7 8]
//
// T[0:5]:
// [0 1 2 3 4]
}
// Any modifications to the sliced value modifies the original slice as well
func ExampleDense_Slice_viewMutation() {
var T, V Tensor
T = New(WithBacking(Range(Int, 0, 16)), WithShape(4, 4))
fmt.Printf("T:\n%v\n", T)
V, _ = T.Slice(makeRS(1, 3), makeRS(1, 3))
fmt.Printf("V:\n%v\n", V)
// Now we modify V's 0th value
V.(*Dense).Set(0, 1000)
fmt.Printf("V[0] = 1000:\n%v\n", V)
fmt.Printf("T is also mutated:\n%v", T)
// Output:
// T:
// ⎡ 0 1 2 3⎤
// ⎢ 4 5 6 7⎥
// ⎢ 8 9 10 11⎥
// ⎣12 13 14 15⎦
//
// V:
// ⎡ 5 6⎤
// ⎣ 9 10⎦
//
// V[0] = 1000:
// ⎡1000 6⎤
// ⎣ 9 10⎦
//
// T is also mutated:
// ⎡ 0 1 2 3⎤
// ⎢ 4 1000 6 7⎥
// ⎢ 8 9 10 11⎥
// ⎣ 12 13 14 15⎦
//
}
func ExampleView() {
// Slicing creates a "view" on the original tensor
T := New(WithBacking(Range(Int, 0, 16)), WithShape(4, 4))
fmt.Printf("T:\n%v\n", T)
V, _ := T.Slice(makeRS(1, 3), makeRS(1, 3))
fmt.Printf("V:\n%v\n", V)
// Now we modify V's 0th value
V.(*Dense).Set(0, 1000)
fmt.Printf("V[0] = 1000:\n%v\n", V)
fmt.Printf("T is also mutated:\n%v\n", T)
// Now we materialize the views
fmt.Printf("V is Materializable: %v\n", V.IsMaterializable())
T2 := V.Materialize()
fmt.Printf("T2 == V:\n%v\n", T2)
// Once materialized, it is decoupled from the original tensor
T2.(*Dense).Set(0, 999)
fmt.Printf("T2 is mutated:\n%v\nBut T is not mutated:\n%v\nNeither is V:\n%v", T2, T, V)
// Output:
// T:
// ⎡ 0 1 2 3⎤
// ⎢ 4 5 6 7⎥
// ⎢ 8 9 10 11⎥
// ⎣12 13 14 15⎦
//
// V:
// ⎡ 5 6⎤
// ⎣ 9 10⎦
//
// V[0] = 1000:
// ⎡1000 6⎤
// ⎣ 9 10⎦
//
// T is also mutated:
// ⎡ 0 1 2 3⎤
// ⎢ 4 1000 6 7⎥
// ⎢ 8 9 10 11⎥
// ⎣ 12 13 14 15⎦
//
// V is Materializable: true
// T2 == V:
// ⎡1000 6⎤
// ⎣ 9 10⎦
//
// T2 is mutated:
// ⎡999 6⎤
// ⎣ 9 10⎦
//
// But T is not mutated:
// ⎡ 0 1 2 3⎤
// ⎢ 4 1000 6 7⎥
// ⎢ 8 9 10 11⎥
// ⎣ 12 13 14 15⎦
//
// Neither is V:
// ⎡1000 6⎤
// ⎣ 9 10⎦
}
func ExampleDense_Hstack() {
var T, T1, T2, T3 *Dense
var err error
T = New(WithBacking(Range(Float64, 0, 4)), WithShape(2, 2))
T1 = New(WithBacking([]float64{1000, 2000}), WithShape(2, 1))
// Simple example
if T2, err = T.Hstack(T1); err == nil {
fmt.Printf("T.Hstack(T1):\n%v\n", T2)
}
// This fails, because they are not the same shape
T1.Reshape(2)
if _, err = T.Hstack(T1); err != nil {
fmt.Printf("Error: %v\n\n", err)
}
// You can stack more than one, as long as all the tensors have the same shape
T1.Reshape(2, 1)
T3 = T1.Clone().(*Dense)
if T2, err = T.Hstack(T1, T3); err == nil {
fmt.Printf("T.Hstack(T1, T3):\n%v\n", T2)
}
// Compatible shapes can be stacked
T1 = New(Of(Float64), WithShape(2, 3))
if T2, err = T.Hstack(T1); err == nil {
fmt.Printf("Hstacking (2,2) with (2,3):\n%v\n", T2)
}
// Special attention to vectors - vectors can only be stacked with vectors
T = New(WithBacking([]float64{1000, 2000}))
T1 = New(WithBacking([]float64{0, 1}), WithShape(1, 2))
if _, err = T.Hstack(T1); err != nil {
fmt.Printf("Hstacking (2) with (1,2): %v\n", err)
}
// Now let's look at failure conditions, or unhandled situations
// Incompatible shapes cannot be stacked
T1.Reshape(3, 2)
if _, err = T.Hstack(T1); err != nil {
fmt.Printf("Hstacking (2,2) with (3,2): %v\n", err)
}
// Obviously you can't stack a scalar onto tensors (or the other way around)
T1 = New(FromScalar(1.0))
if _, err = T.Hstack(T1); err != nil {
fmt.Printf("Hstacking a scalar onto a tensor: %v\n", err)
}
if _, err = T1.Hstack(T); err != nil {
fmt.Printf("Hstacking a tensor onto a scalar: %v\n", err)
}
// Output:
// T.Hstack(T1):
// ⎡ 0 1 1000⎤
// ⎣ 2 3 2000⎦
//
// Error: Failed to perform Concat: Unable to find new shape that results from concatenation: Dimension mismatch. Expected 2, got 1
//
// T.Hstack(T1, T3):
// ⎡ 0 1 1000 1000⎤
// ⎣ 2 3 2000 2000⎦
//
// Hstacking (2,2) with (2,3):
// ⎡0 1 0 0 0⎤
// ⎣2 3 0 0 0⎦
//
// Hstacking (2) with (1,2): Failed to perform Concat: Unable to find new shape that results from concatenation: Dimension mismatch. Expected 1, got 2
// Hstacking (2,2) with (3,2): Failed to perform Concat: Unable to find new shape that results from concatenation: Dimension mismatch. Expected 1, got 2
// Hstacking a scalar onto a tensor: Tensor has to be at least 1 dimensions
// Hstacking a tensor onto a scalar: Tensor has to be at least 1 dimensions
}
func ExampleDense_Vstack() {
var T, T1, T2, T3 *Dense
var err error
T = New(WithBacking(Range(Float64, 0, 4)), WithShape(2, 2))
T1 = New(WithBacking([]float64{1000, 2000}), WithShape(1, 2))
// Simple example
if T2, err = T.Vstack(T1); err == nil {
fmt.Printf("T.Vstack(T1):\n%v\n", T2)
} else {
fmt.Printf("%+v", err)
}
// You can stack more than one, as long as all the tensors have the same shape
T3 = T1.Clone().(*Dense)
if T2, err = T.Vstack(T1, T3); err == nil {
fmt.Printf("T.Vstack(T1, T3):\n%v\n", T2)
} else {
fmt.Printf("====\nerr %v\n%v\n===\n", err, T3.Shape())
}
// Let's look at failure conditions
// All tensors must be at least 2D
T.Reshape(4)
if _, err = T.Vstack(T1); err != nil {
fmt.Printf("Vstacking (4) with (1, 2): %v\n", err)
}
if _, err = T1.Vstack(T); err != nil {
fmt.Printf("Vstacking (1, 2) with (4): %v\n", err)
}
// Output:
// T.Vstack(T1):
// ⎡ 0 1⎤
// ⎢ 2 3⎥
// ⎣1000 2000⎦
//
// T.Vstack(T1, T3):
// ⎡ 0 1⎤
// ⎢ 2 3⎥
// ⎢1000 2000⎥
// ⎣1000 2000⎦
//
// Vstacking (4) with (1, 2): Tensor has to be at least 2 dimensions
// Vstacking (1, 2) with (4): Tensor has to be at least 2 dimensions
}
func ExampleRepeatReuse() {
var T, T1 *Dense
T = New(WithBacking([]float64{1, 2, 3, 4}), WithShape(1, 4))
T1 = New(Of(Float64), WithShape(3, 4))
var T2 Tensor
var err error
if T2, err = RepeatReuse(T, T1, 0, 3); err != nil {
fmt.Printf("Err %v", err)
}
fmt.Printf("RepeatReuse(T, T1):\n%v", T2)
fmt.Printf("T1 == T2: %t\n", T1 == T2)
// But if your reuse is wrongly shaped, an error occurs
T1 = New(Of(Float64), WithShape(1, 4)) // too small
if _, err = RepeatReuse(T, T1, 0, 3); err != nil {
fmt.Printf("Expected Error: %v\n", err)
}
// Output:
// RepeatReuse(T, T1):
// ⎡1 2 3 4⎤
// ⎢1 2 3 4⎥
// ⎣1 2 3 4⎦
// T1 == T2: true
// Expected Error: Reuse shape is (1, 4). Expected shape is (3, 4)
}
func ExampleRepeat_uncommonUses() {
T := New(WithBacking([]int{1, 2, 3, 4, 5, 6}), WithShape(2, 3))
fmt.Printf("T:\n%v", T)
fmt.Println("Axis 0 has 2 elements. So we will need to write the number of times each element is to be repeated")
fmt.Println("Here, Repeat(T, 0, 3, 2) results in this:")
T1, err := Repeat(T, 0, 3, 2)
if err != nil {
fmt.Printf("Err %v", err)
}
fmt.Printf("%v", T1)
fmt.Println("Observe the 0th element ([1 2 3]) has been repeated 3 times, and the 1st element ([4 5 6]) has been repeated twice")
fmt.Println("")
fmt.Println("We can also repeat on Axis 1. Now along Axis 1 there are 3 elements: ([1 4], [2 5], [3 6])")
fmt.Println("So we have to specify how many times to repeat each element.")
fmt.Println("Repeat(T, 1, 2, 3, 2) yields the following result:")
T1, err = Repeat(T, 1, 2, 3, 2)
if err != nil {
fmt.Printf("Err %v", err)
}
fmt.Printf("%v", T1)
fmt.Println("Once again, observe that the 1st element ([2 5]) has been repeated 3 times, while the rest have been repeated twice")
/*
// TODO break this out to another example
T1, err = Repeat(T, AllAxes, 2, 3, 2, 2, 2, 2)
if err != nil {
fmt.Printf("Err %v", err)
}
fmt.Printf("%#v", T1)
*/
// Output:
// T:
// ⎡1 2 3⎤
// ⎣4 5 6⎦
// Axis 0 has 2 elements. So we will need to write the number of times each element is to be repeated
// Here, Repeat(T, 0, 3, 2) results in this:
// ⎡1 2 3⎤
// ⎢1 2 3⎥
// ⎢1 2 3⎥
// ⎢4 5 6⎥
// ⎣4 5 6⎦
// Observe the 0th element ([1 2 3]) has been repeated 3 times, and the 1st element ([4 5 6]) has been repeated twice
//
// We can also repeat on Axis 1. Now along Axis 1 there are 3 elements: ([1 4], [2 5], [3 6])
// So we have to specify how many times to repeat each element.
// Repeat(T, 1, 2, 3, 2) yields the following result:
// ⎡1 1 2 2 2 3 3⎤
// ⎣4 4 5 5 5 6 6⎦
// Once again, observe that the 1st element ([2 5]) has been repeated 3 times, while the rest have been repeated twice
}
func ExampleT() {
// Usual example of 2D matrix being transposed:
M := New(WithBacking([]int{1, 2, 3, 4, 5, 6}), WithShape(2, 3))
M2, err := T(M)
if err != nil {
fmt.Printf("Err: %v\n", err)
}
fmt.Printf("M:\n%v\nM2:\n%v\n", M, M2)
// T accepts optional parameters describing the permutation of axes.
// In a 2D case, there are only two options: (0, 1) or (1, 0).
// The latter is default if no parameters are passed in.
// The former is a no-op as rearranging a matrix so that the 0th axis becomes the 0th axis
// and the first axis becomes the first axis is not going to do anything.
//
// However, note that M3 is a different result.
M3, err := T(M, 0, 1)
if err != nil {
fmt.Printf("Err: %v\n", err)
}
fmt.Printf("M3:\n%v\nM == M3: %t", M3, M == M3)
// Output:
// M:
// ⎡1 2 3⎤
// ⎣4 5 6⎦
//
// M2:
// ⎡1 4⎤
// ⎢2 5⎥
// ⎣3 6⎦
//
// M3:
// ⎡1 2 3⎤
// ⎣4 5 6⎦
//
// M == M3: false
}
func ExampleT_scalarlike() {
// Be aware when dealing with scalarlike tensors
// scalar/scalarlikes have no effect when calling T()
// but the result is put into a new tensor
S := New(WithBacking([]float32{3.14}), WithShape())
S2, err := T(S)
if err != nil {
fmt.Printf("Err %v", err)
}
fmt.Printf("S: %v S2 %v S == S2: %t\n", S, S2, S == S2)
// however do note that scalars and scalarlikes are not the same thing.
// for example, consider this:
_, err = T(S, 1, 0)
fmt.Printf("error when the axes are more than the shape's dims: %v\n", err)
// but if you have a tensor that is a scalar-like:
S.Reshape(1, 1)
S2, err = T(S, 1, 0)
if err != nil {
fmt.Printf("Err: %v\n", err)
}
fmt.Printf("S:\n%v\nS2:\n%v\nS == S2: %t\n", S, S2, S == S2)
// Output:
// S: 3.14 S2 3.14 S == S2: false
// error when the axes are more than the shape's dims: Dimension mismatch. Expected 0, got 2
// S:
// [[3.14]]
// S2:
// [[3.14]]
// S == S2: false
}