-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtensors.py
40 lines (35 loc) · 1.17 KB
/
tensors.py
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
def copyTensor(original, dim):
"""========================================
Make a deep copy of a tensor of arbitrary (number of) dimensions.
Usage example, to deep-copy a two-dimensional matrix:
>>> p = [ [1, 2], [3, 4] ]
>>> deep = copyTensor(p, 2)
It can also make a shallow copy, if you pass a number of dimensions less than the actual one:
>>> shallow = copyTensor(p, 1)
========================================"""
copy = []
if dim == 1:
for element in original:
copy.append(element)
elif dim > 1:
for list in original:
copy.append(copyTensor(list, dim - 1))
return copy
def initTensor(value, *lengths):
"""========================================
Initialize uniformly a tensor of arbitrary (number of) dimensions.
Usage example:
>>> print initTensor(0, 3, 4)
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>> print initTensor(0, 2, 3, 4)
[[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]
========================================"""
list = []
dim = len(lengths)
if dim == 1:
for i in range(lengths[0]):
list.append(value)
elif dim > 1:
for i in range(lengths[0]):
list.append(initTensor(value, *lengths[1:]))
return list