-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tensor.h
108 lines (94 loc) · 4.34 KB
/
tensor.h
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
/**********************************************************************************/
/* Copyright (c) 2023 Mark Seminatore */
/* All rights reserved. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining a copy */
/* of this software and associated documentation files(the "Software"), to deal */
/* in the Software without restriction, including without limitation the rights */
/* to use, copy, modify, merge, publish, distribute, sublicense, and / or sell */
/* copies of the Software, and to permit persons to whom the Software is */
/* furnished to do so, subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be included in all */
/* copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/**********************************************************************************/
#pragma once
#ifndef __TENSOR_H
#define __TENSOR_H
#include <stdlib.h>
#include <stdint.h>
#include "ann_config.h"
//----------------------------------
// Note: change to double if desired
//----------------------------------
typedef float real;
#ifndef max
# define max(a, b) (((a) > (b)) ? (a) : (b))
#endif
//------------------------------
// tensor structure
//------------------------------
typedef struct
{
int rows, cols;
int stride; // not currently used
real *values;
int rank; // not currently used
} Tensor, *PTensor;
//------------------------------
// flags for matvec operation
//------------------------------
typedef enum TENSOR_TRANSPOSE {
Tensor_NoTranspose,
Tensor_Transpose
} TENSOR_TRANSPOSE;
//------------------------------
// function decls
//------------------------------
// creation/destruction
PTensor tensor_create(int rows, int cols);
PTensor tensor_create_from_array(int rows, int cols, real *vals);
void tensor_set_from_array(PTensor t, int rows, int cols, real *array);
void tensor_free(PTensor t);
PTensor tensor_ones(int rows, int cols);
PTensor tensor_zeros(int rows, int cols);
PTensor tensor_create_random_uniform(int rows, int cols, real min, real max);
PTensor tensor_onehot(PTensor t, int classes);
PTensor tensor_copy(PTensor t);
// math ops
PTensor tensor_add_scalar(PTensor t, real val);
PTensor tensor_add(PTensor a, PTensor b);
PTensor tensor_sub(PTensor a, PTensor b);
PTensor tensor_mul_scalar(PTensor t, real val);
PTensor tensor_mul(PTensor a, PTensor b);
PTensor tensor_div(PTensor a, PTensor b);
PTensor tensor_matvec(TENSOR_TRANSPOSE trans, real alpha, PTensor mtx, real beta, PTensor v, PTensor dest);
PTensor tensor_square(PTensor t);
PTensor tensor_exp(PTensor t);
PTensor tensor_argmax(PTensor t);
PTensor tensor_max(PTensor t);
real tensor_sum(PTensor t);
PTensor tensor_axpy(real alpha, PTensor x, PTensor y);
PTensor tensor_gemm(real alpha, PTensor A, PTensor B, real beta, PTensor C);
PTensor tensor_axpby(real alpha, PTensor x, real beta, PTensor y);
PTensor tensor_outer(real alpha, PTensor a, PTensor b, PTensor dest);
PTensor tensor_heaviside(PTensor a);
// manipulation
real tensor_get_element(PTensor t, int row, int col);
void tensor_set_element(PTensor t, int row, int col, real val);
PTensor tensor_slice_rows(PTensor t, int rows);
PTensor tensor_slice_cols(PTensor t, int cols);
void tensor_fill(PTensor t, real val);
void tensor_random_uniform(PTensor t, real min, real max);
// IO
void tensor_print(PTensor t);
int tensor_save_to_file(PTensor t, const char *filename);
#endif