官术网_书友最值得收藏!

Slicing tensors 

A common thing to do with a tensor is to slice a portion of it. A simple example could be choosing the first five elements of a one-dimensional tensor; let's call the tensor sales. We use a simple notation, sales[:slice_index] where slice_index represents the index where you want to slice the tensor:

sales = torch.FloatTensor([1000.0,323.2,333.4,444.5,1000.0,323.2,333.4,444.5])

sales[:5]
1000.0000
323.2000
333.4000
444.5000
1000.0000
[torch.FloatTensor of size 5]

sales[:-5]
1000.0000
323.2000
333.4000
[torch.FloatTensor of size 3]

Let's do more interesting things with our panda image, such as see what the panda image looks like when only one channel is chosen and see how to select the face of the panda.

Here, we select only one channel from the panda image:

plt.imshow(panda_tensor[:,:,0].numpy())
#0 represents the first channel of RGB

 The output is as follows:

Now, lets crop the image. Say we want to build a face detector for pandas and we need just the face of a panda for that. We crop the tensor image such that it contains only the panda's face:

plt.imshow(panda_tensor[25:175,60:130,0].numpy())

 The output is as follows: 

Another common example would be where you need to pick a specific element of a tensor:

#torch.eye(shape) produces an diagonal matrix with 1 as it diagonal #elements.
sales = torch.eye(3,3)
sales[0,1]

Output- 0.00.0

We will revisit image data in Chapter 5Deep Learning for Computer Vision, when we discuss using CNNs to build image classifiers.

Most of the PyTorch tensor operations are very similar to NumPy operations .
主站蜘蛛池模板: 怀来县| 鞍山市| 内江市| 乌拉特前旗| 云梦县| 南陵县| 铜陵市| 灵山县| 瑞安市| 岫岩| 磐石市| 卓资县| 蚌埠市| 尼勒克县| 兴山县| 阳春市| 孙吴县| 栖霞市| 合水县| 定襄县| 平果县| 白水县| 扎鲁特旗| 溧水县| 盐山县| 宜宾市| 苍梧县| 万载县| 信阳市| 英德市| 吉安市| 随州市| 江门市| 革吉县| 无极县| 寿宁县| 兴国县| 遂溪县| 卓资县| 讷河市| 徐州市|