Quantcast
Channel: AI Beta - Unity Discussions
Viewing all articles
Browse latest Browse all 109

How to change the shape of a tensor?

$
0
0
import numpy as np

# Create a 3-channel 4x4 image data
image_data = np.array([[[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]]], dtype=np.uint8)

# CHW arrangement
print("CHW arrangement:")
print(image_data)

# HWC arrangement
image_data_hwc = np.transpose(image_data, (1, 2, 0))
print("\nHWC arrangement:")
print(image_data_hwc)

# Flatten CHW-arranged data
flat_data_chw = image_data.flatten()
print("Flattened data (CHW arrangement):")
print(flat_data_chw)

# Flatten HWC-arranged data
flat_data_hwc = image_data_hwc.flatten()
print("\nFlattened data (HWC arrangement):")
print(flat_data_hwc)

I want to implement such a function, but I searched and couldn’t find such an API.

I have tried the following code

// Create a 2 × 2 × 2 tensor
TensorShape shape = new TensorShape(2, 2, 2);
int[] data = new int[] { 1, 2, 3, 4, 10, 20, 30, 40 };
TensorInt tensor = new TensorInt(shape, data);

// Create a copy of the tensor with the new shape 2 × 1 × 4
TensorShape newShape = new TensorShape(2, 1, 4);
Tensor reshapedTensor = tensor.ShallowReshape(newShape);

But it doesn’t do what I want

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 109

Trending Articles