First, Pytorch introduction
1, the descriptionPytorch is Torch in Python (Torch is a neural network using the Lua language) and TensorFlow comparison Pytorch established neural network is dynamic TensorFlow is a highly industrial of static graph TensorFlow , its underlying code is hard to read. Pytorch good so a little, if you dive into the API, you can at least see TensorFlow more than see the bottom of a little pytorch.
2. Installation PytorchOfficial website: http://pytorch.org/into the official website after you can select the corresponding installation options currently only support Linux and MacOS version (2017-05-06) to perform the following corresponding installation command
Installation Pytorch will install two modules one is torch, a torchvision, torch is the main module, used to build a neural network, Torchvision is a secondary module, there is a database, there are some trained neural network waiting for you to use directly, such as (Vgg, Alexnet, ResNet). The above ubuntu14 under the python2.7 installation is not a problem, in the python3.5 under CentOS6.5 installation may error installation python3.5 configuration:
1 2 3 |
./configure--enable-shared \--prefix=/usr/local/python3. 5 \ ldflags= "-wl,--rpath=/usr/local/lib" |
Then run Python may report loading shared Libraries:libpython3.5m.so.1.0:cannot open Shared object file:no such file or directory error, A copy of the libpython3.5m.so.1.0 to the/usr/lib64 directory
1 |
cp/home/python/python-3.5. 3/libpython3. 5m. So. 1.0/usr/lib64 |
Ii. Basic Knowledge
1, and numpy similarities
(1) Data conversionImporting packages: Import Torch convert numpy data to torch data
1 2 |
Np_data = Np.arange (6). Reshape ((2, 3)) Torch_data = Torch.from_numpy (np_data) |
Converting torch data to numpy data
1 |
Tensor2array = Torch_data.numpy () |
(2) The operation in TorchThe tensor operation in Api:http://pytorch.org/docs/torch.html#math-operations Torch is very similar to the NumPy array operation, such as Np.abs ()--> Torch.abs () Np.sin ()--> torch.sin () matrix multiplication: data = [[1,2], [3,4]] tensor = torch. Floattensor (data) # converted to 32-bit floating-point tensor torch.mm (tensor, tensor)
2. Variable variable
(1) DescriptionVariable is a place where the value of a variable is stored. The value of this change is tensor data.
(2) using
Import Package
1 2 |
Import Torch from Torch.autograd import Variable # torch Variable Module |
Define Tensor:tensor = Torch. Floattensor ([[[1,2],[3,4]]) puts tensor in variable:variable = variable (tensor, requires_grad=true) Requires_grad is the reference not to participate in the error back propagation, or to calculate the gradient print (variable) output, (more than variable containing:, indicating is variable)
1 2 3 4 |
Variable Containing:1 2 3 4 [Torch. Floattensor of size 2x2] |