-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtensor.go
470 lines (444 loc) · 11.1 KB
/
tensor.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/* Love Saroha
lovesaroha1994@gmail.com (email address)
https://www.lovesaroha.com (website)
https://github.com/lovesaroha (github)
*/
package lnn
import (
"fmt"
"log"
"./lmath"
)
// Tensor structure.
type TensorObject struct {
Shape []int
Values interface{}
}
// Print tensor.
func (ts TensorObject) Print() {
switch v := ts.Values.(type) {
case float64:
fmt.Printf("\n %f \n %s \n\n", v, "Scalar")
return
case [][]float64:
fmt.Printf("\n")
for i := 0; i < ts.Shape[0]; i++ {
fmt.Printf(" [ ")
for j := 0; j < ts.Shape[1]; j++ {
fmt.Printf(" %f ", v[i][j])
}
fmt.Printf(" ] \n")
}
fmt.Printf(" %s %v %s %v %s \n\n", "(", ts.Shape[0], "x", ts.Shape[1], ")")
}
}
// Copy tensor values.
func (ts TensorObject) Copy() TensorObject {
var newTensor = TensorObject{}
// Copy shape.
for i := 0; i < len(ts.Shape); i++ {
newTensor.Shape = append(newTensor.Shape, ts.Shape[i])
}
// Check shape.
switch len(ts.Shape) {
case 0:
newTensor.Values = ts.Values
return newTensor
case 2:
newTensor.Values = [][]float64{}
matrix := ts.Values.([][]float64)
for i := 0; i < ts.Shape[0]; i++ {
r := make([]float64, ts.Shape[1])
for j := 0; j < ts.Shape[1]; j++ {
r[j] = matrix[i][j]
}
newTensor.Values = append(newTensor.Values.([][]float64), r)
}
}
return newTensor
}
// Add tensor.
func (ts TensorObject) Add(arg TensorObject) TensorObject {
return elementWise(ts, arg, 0)
}
// Subtract tensor.
func (ts TensorObject) Sub(arg TensorObject) TensorObject {
return elementWise(ts, arg, 1)
}
// Multiplication.
func (ts TensorObject) Mul(arg TensorObject) TensorObject {
return elementWise(ts, arg, 2)
}
// Square.
func (ts TensorObject) Square() TensorObject {
return elementWise(ts, ts, 2)
}
// Divide.
func (ts TensorObject) Divide(arg TensorObject) TensorObject {
return elementWise(ts, arg, 3)
}
// Dot product.
func (ts TensorObject) Dot(arg TensorObject) TensorObject {
var newTensor TensorObject
if len(ts.Shape) == 0 || len(arg.Shape) == 0 {
log.Fatal("lnn: Cannot perform dot product on scalar values.")
return newTensor
}
if ts.Shape[1] != arg.Shape[0] {
log.Fatal("lnn: Number of columns of first matrix must be equal to number of rows in second.")
return newTensor
}
newTensor.Shape = []int{ts.Shape[0], arg.Shape[1]}
matrix := ts.Values.([][]float64)
matrixArg := arg.Values.([][]float64)
values := make([][]float64, ts.Shape[0])
for i := 0; i < newTensor.Shape[0]; i++ {
r := make([]float64, newTensor.Shape[1])
for j := 0; j < newTensor.Shape[1]; j++ {
var sum float64
for k := 0; k < ts.Shape[1]; k++ {
sum += matrix[i][k] * matrixArg[k][j]
}
r[j] = sum
}
values[i] = r
}
newTensor.Values = values
return newTensor
}
// Transpose.
func (ts TensorObject) Transpose() TensorObject {
var newTensor TensorObject
switch len(ts.Shape) {
case 1:
newTensor.Values = ts.Values
case 2:
newTensor.Shape = []int{ts.Shape[1], ts.Shape[0]}
values := make([][]float64, newTensor.Shape[0])
matrix := ts.Values.([][]float64)
// Matrix or vector.
for i := 0; i < newTensor.Shape[0]; i++ {
r := make([]float64, newTensor.Shape[1])
for j := 0; j < newTensor.Shape[1]; j++ {
r[j] = matrix[j][i]
}
values[i] = r
}
newTensor.Values = values
}
return newTensor
}
// Map function.
func (ts TensorObject) Map(callback func(value float64) float64) TensorObject {
var newTensor = ts.Copy()
switch len(ts.Shape) {
case 0:
value := ts.Values.(float64)
value = callback(value)
newTensor.Values = value
case 2:
values := ts.Values.([][]float64)
for i := 0; i < newTensor.Shape[0]; i++ {
for j := 0; j < newTensor.Shape[1]; j++ {
values[i][j] = callback(values[i][j])
}
}
newTensor.Values = values
}
return newTensor
}
// Add all.
func (ts TensorObject) Sum() float64 {
var sum float64
switch len(ts.Shape) {
case 0:
return ts.Values.(float64)
case 2:
values := ts.Values.([][]float64)
for i := 0; i < ts.Shape[0]; i++ {
for j := 0; j < ts.Shape[1]; j++ {
sum += values[i][j]
}
}
return sum
}
return sum
}
// Values of a matrix tensor.
func (ts TensorObject) Value() [][]float64 {
switch len(ts.Shape) {
case 1:
return [][]float64{{ts.Values.(float64)}}
case 2:
return ts.Values.([][]float64)
}
return [][]float64{}
}
// Col extend.
func (ts TensorObject) ColExtend(scale int) TensorObject {
var newTensor TensorObject
// Check shape.
switch len(ts.Shape) {
case 0:
return ts
case 2:
newTensor.Shape = []int{ts.Shape[0], ts.Shape[1] * scale}
matrix := make([][]float64, ts.Shape[0])
values := ts.Values.([][]float64)
for i := 0; i < ts.Shape[0]; i++ {
r := make([]float64, newTensor.Shape[1])
for j := 0; j < newTensor.Shape[1]; j++ {
r[j] = values[i][j/scale]
}
matrix[i] = r
}
newTensor.Values = matrix
}
return newTensor
}
// Add columns.
func (ts TensorObject) AddCols() TensorObject {
var newTensor TensorObject
if len(ts.Shape) == 0 {
// Scalar.
return ts
} else if len(ts.Shape) == 2 {
newTensor.Shape = []int{ts.Shape[0], 1}
values := make([][]float64, newTensor.Shape[0])
matrix := ts.Values.([][]float64)
for i := 0; i < ts.Shape[0]; i++ {
sum := 0.0
for j := 0; j < ts.Shape[1]; j++ {
sum += matrix[i][j]
}
values[i] = []float64{sum}
}
newTensor.Values = values
}
return newTensor
}
// Make batches.
func (ts TensorObject) MakeBatches(size int) []TensorObject {
var newTensor []TensorObject
if len(ts.Shape) == 0 {
// Scalar.
return []TensorObject{ts.Copy()}
} else if len(ts.Shape) == 2 {
// Matrix.
totalBatches := ts.Shape[1] / size
if totalBatches*size != ts.Shape[1] {
totalBatches += 1
}
matrix := ts.Values.([][]float64)
initial := size
for t := 0; t < totalBatches; t++ {
initial = t * size
limit := initial + size
if limit > ts.Shape[1] {
limit = ts.Shape[1]
}
var nts TensorObject = TensorObject{Shape: []int{ts.Shape[0], limit - initial}}
values := [][]float64{}
for i := 0; i < ts.Shape[0]; i++ {
r := make([]float64, limit-initial)
var c = 0
for j := initial; j < limit; j++ {
r[c] = matrix[i][j]
c++
}
values = append(values, r)
}
nts.Values = values
newTensor = append(newTensor, nts)
}
} else {
return []TensorObject{ts}
}
return newTensor
}
// Export function.
func Tensor(shape []int, args ...float64) TensorObject {
// Default values.
var newTensor TensorObject
if len(shape) == 0 {
newTensor.Shape = shape
} else if len(shape) == 1 {
newTensor.Shape = []int{shape[0], 1}
} else {
newTensor.Shape = []int{shape[0], shape[1]}
}
var min float64 = 0
var max float64 = 0
// Assign arguments.
for k, arg := range args {
if k == 0 {
// Minimum.
min = arg
} else if k == 1 {
// Maximum.
max = arg
}
}
// Check shape.
switch len(newTensor.Shape) {
case 0:
newTensor.Values = lmath.Random(min, max)
case 2:
matrix := [][]float64{}
for i := 0; i < newTensor.Shape[0]; i++ {
matrix = append(matrix, []float64{})
for j := 0; j < newTensor.Shape[1]; j++ {
matrix[i] = append(matrix[i], lmath.Random(min, max))
}
}
newTensor.Values = matrix
}
return newTensor
}
// ToTensor function convert given value to tensor.
func ToTensor(value interface{}) TensorObject {
switch v := value.(type) {
case int:
return TensorObject{Values: float64(v)}
case float64:
return TensorObject{Values: v}
case []int:
var newTensor = TensorObject{Shape: []int{len(v), 1}}
newTensor.Values = toMatrix(sliceToF64(v))
return newTensor
case []float64:
var newTensor = TensorObject{Shape: []int{len(v), 1}}
newTensor.Values = toMatrix(v)
return newTensor
case [][]int:
var newTensor = TensorObject{Shape: []int{len(v), len(v[0])}}
newTensor.Values = slice2dToF64(v)
return newTensor
case [][]float64:
var newTensor = TensorObject{Shape: []int{len(v), len(v[0])}}
newTensor.Values = v
return newTensor
}
return TensorObject{}
}
// Element wise operation.
func elementWise(ts TensorObject, arg TensorObject, operation int) TensorObject {
// Create result tensor.
if len(ts.Shape) == 0 && len(arg.Shape) == 0 {
return scalarOperations(ts, arg, operation)
} else if len(ts.Shape) == 0 && len(arg.Shape) == 2 {
return elementWiseWithMatrix(arg, ts, operation)
}
return elementWiseWithMatrix(ts, arg, operation)
}
// Element wise with matrix.
func elementWiseWithMatrix(ts TensorObject, arg TensorObject, operation int) TensorObject {
newTensor := ts.Copy()
matrix := newTensor.Values.([][]float64)
switch len(arg.Shape) {
case 0:
for i := 0; i < newTensor.Shape[0]; i++ {
for j := 0; j < newTensor.Shape[1]; j++ {
switch operation {
case 0:
matrix[i][j] = matrix[i][j] + arg.Values.(float64)
case 1:
matrix[i][j] = matrix[i][j] - arg.Values.(float64)
case 2:
matrix[i][j] = matrix[i][j] * arg.Values.(float64)
case 3:
matrix[i][j] = matrix[i][j] / arg.Values.(float64)
}
}
}
case 2:
if ts.Shape[0] == arg.Shape[0] && arg.Shape[1] != newTensor.Shape[1] {
if arg.Shape[1] < newTensor.Shape[1] {
arg = arg.ColExtend(ts.Shape[1])
} else {
newTensor = newTensor.ColExtend(arg.Shape[1])
newTensor.Shape[1] = arg.Shape[1]
}
}
matrixArg := arg.Values.([][]float64)
for i := 0; i < arg.Shape[0]; i++ {
for j := 0; j < arg.Shape[1]; j++ {
switch operation {
case 0:
matrix[i][j] = matrix[i][j] + matrixArg[i][j]
case 1:
matrix[i][j] = matrix[i][j] - matrixArg[i][j]
case 2:
matrix[i][j] = matrix[i][j] * matrixArg[i][j]
case 3:
matrix[i][j] = matrix[i][j] / matrixArg[i][j]
}
}
}
}
return newTensor
}
// Scalar values.
func scalarOperations(ts TensorObject, arg TensorObject, operation int) TensorObject {
newTensor := ts.Copy()
switch operation {
case 0:
newTensor.Values = newTensor.Values.(float64) + arg.Values.(float64)
case 1:
newTensor.Values = newTensor.Values.(float64) - arg.Values.(float64)
case 2:
newTensor.Values = newTensor.Values.(float64) * arg.Values.(float64)
case 3:
newTensor.Values = newTensor.Values.(float64) / arg.Values.(float64)
}
return newTensor
}
// Convert [] to []float64.
func sliceToF64(val interface{}) []float64 {
var newSlice []float64
switch v := val.(type) {
case []int:
for i := 0; i < len(v); i++ {
newSlice = append(newSlice, float64(v[i]))
}
case []int64:
for i := 0; i < len(v); i++ {
newSlice = append(newSlice, float64(v[i]))
}
case []float32:
for i := 0; i < len(v); i++ {
newSlice = append(newSlice, float64(v[i]))
}
}
return newSlice
}
// Convert [][] to [][]float64.
func slice2dToF64(val interface{}) [][]float64 {
var newSlice [][]float64
switch v := val.(type) {
case [][]int:
for i := 0; i < len(v); i++ {
newSlice = append(newSlice, sliceToF64(v[i]))
}
case [][]int64:
for i := 0; i < len(v); i++ {
newSlice = append(newSlice, sliceToF64(v[i]))
}
case [][]float32:
for i := 0; i < len(v); i++ {
newSlice = append(newSlice, sliceToF64(v[i]))
}
}
return newSlice
}
// Vector to matrix.
func toMatrix(value []float64) [][]float64 {
matrix := [][]float64{}
for i := 0; i < len(value); i++ {
matrix = append(matrix, []float64{})
for j := 0; j < 1; j++ {
matrix[i] = append(matrix[i], value[i])
}
}
return matrix
}