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

Basic building blocks

Such as TensorFlow, PyTorch represents data in tensor form. Torch tensors are defined as standard data types, such as torch.FloatTensor() , torch.charTensor(), and torch.intTensor(). As mentioned, operations in PyTorch are highly Pythonic. To repeat the exact same multiplication operation that we performed in preceding TensorFlow: 

import torch 
x = torch.IntTensor([4])
y = torch.IntTensor([5])
product = x * y

As a result of it native Python feel, PyTorch allows for easy interaction between standard numpy arrays and PyTorch tensors. It's easy to switch back and forth between the two:

import torch 
import numpy as np

## Create a numpy array
numpy_array = np.random.randn(20,20)

##Convert the numpy array to a pytorch tensor
pytorch_tensor = torch.from_numpy(numpy_array)

## Convert it back to Numpy
numpy_again = pytorch_tensor.numpy()

PyTorch tensors can easily be indexed and sliced in a Pythonic way as well. For instance, let's say that we want to access a particular value of a tensor in PyTorch; we can easily do that with NumPy-like indexing: 

tensor = torch.FloatTensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

## print the third element of the 2nd row of the tensor
print(tensor[1][2])

Likewise, we can easily manipulate the contents of the tensor in a NumPy-like manner:

## replace the second value of the first tensor
tensor[0][1] = 1
print(tensor)

Like TensorFlow, PyTorch runs on the concept of variables, which are values that are intended to change and be updated during training processes. To create a variable in PyTorch, we wrap a tensor with the Variable function:

from torch.autograd import Variable
tensor_two = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
variable = Variable(tensor_two)

If you'd like to gain access to the tensor that's inside of the variable wrapper, you can call .data on the variable:

variable.data

## Returns:
tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

While PyTorch is relatively new and still developing, its Python-like structure is leading to fast adoption rates in the community. 

主站蜘蛛池模板: 夹江县| 南溪县| 雷波县| 上栗县| 贵港市| 乌拉特前旗| 惠水县| 绩溪县| 利津县| 营山县| 敦煌市| 互助| 满洲里市| 右玉县| 凤城市| 武穴市| 肥城市| 南漳县| 华容县| 崇义县| 海林市| 垣曲县| 唐海县| 南宫市| 子洲县| 灌阳县| 胶南市| 南康市| 文昌市| 紫金县| 天门市| 德化县| 宁夏| 台安县| 准格尔旗| 任丘市| 扶沟县| 阜康市| 吉安县| 柯坪县| 普兰店市|