Reputation: 13
The in and result should be look like result
in = (0, 1, 2, 3, 4, 5, 6, 7, 8)
tensor = (2, 3, 2)
result:
(((0, 1), (2, 3), (4, 5)), ((6, 7), (8, 0), (0, 0)))
Upvotes: 1
Views: 145
Reputation: 120
Try:
def multiply (arr):
result = 1
for x in arr:
result *= x
return result
def reshape(shape, arr):
result = arr
newSize = multiply(shape)
if (len(arr) < newSize):
while (len(arr) < newSize):
result.append(0)
else:
result= result[:newSize]
for s in shape[::-1]:
result = [ result[i:i+s] for i in range(0,len(result),s)]
return result
data = [0, 1, 2, 3, 4, 5, 6, 7, 8]
shape = [2, 3, 2]
print(reshape(shape, data))
data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
shape = [6, 2]
print(reshape(shape, data))
Upvotes: 1