This guide introduces how to install PyTorch and TensorFlow, along with basic tensor operation examples.
- PyTorch Installation Guide
- Introduction to PyTorch Tensors
- Basic PyTorch Tensor Operations
- TensorFlow Installation Guide
- Introduction to TensorFlow Tensors
- Basic TensorFlow Tensor Operations
PyTorch is a popular deep learning framework that supports both CPU and GPU.
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpuEnsure that CUDA and cuDNN are installed, then run:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118Note:
cu118corresponds to CUDA 11.8. Replace it with your appropriate CUDA version, such ascu117for CUDA 11.7.
Use thenvidia-smicommand to confirm your CUDA version.
Refer to the PyTorch official installation page for the latest instructions:
PyTorch Official Installation Guide
The core data structure in PyTorch is the Tensor, which represents multi-dimensional arrays similar to NumPy arrays but with GPU acceleration.
import torch
# Create a 2x2 tensor
tensor = torch.tensor([[1, 2], [3, 4]])
print("PyTorch Tensor:
", tensor)import torch
tensor1 = torch.tensor([[1, 2], [3, 4]])
tensor2 = torch.tensor([[5, 6], [7, 8]])
# Tensor addition
result = tensor1 + tensor2
print("Tensor Addition Result:
", result)import torch
tensor1 = torch.tensor([[1, 2], [3, 4]])
tensor2 = torch.tensor([[2, 3], [4, 5]])
# Element-wise multiplication
result = tensor1 * tensor2
print("Element-wise Multiplication Result:
", result)import torch
matrix1 = torch.tensor([[1, 2], [3, 4]])
matrix2 = torch.tensor([[5, 6], [7, 8]])
# Matrix multiplication
result = torch.matmul(matrix1, matrix2)
print("Matrix Multiplication Result:
", result)TensorFlow is another popular deep learning framework that supports both CPU and GPU.
pip install tensorflow --upgradeEnsure CUDA and cuDNN are installed, then run:
pip install tensorflow --upgradeNote: TensorFlow 2.0+ automatically detects GPU support when the correct NVIDIA drivers and CUDA toolkit are installed.
Refer to the TensorFlow official installation page for the latest instructions:
TensorFlow Official Installation Guide
The core data structure in TensorFlow is the Tensor, which represents multi-dimensional arrays (similar to matrices).
import tensorflow as tf
# Create a constant tensor
tensor = tf.constant([[1, 2], [3, 4]])
print("TensorFlow Tensor:
", tensor)import tensorflow as tf
tensor1 = tf.constant([[1, 2], [3, 4]])
tensor2 = tf.constant([[5, 6], [7, 8]])
# Tensor addition
result = tf.add(tensor1, tensor2)
print("Tensor Addition Result:
", result)import tensorflow as tf
tensor1 = tf.constant([[1, 2], [3, 4]])
tensor2 = tf.constant([[2, 3], [4, 5]])
# Element-wise multiplication
result = tf.multiply(tensor1, tensor2)
print("Element-wise Multiplication Result:
", result)import tensorflow as tf
matrix1 = tf.constant([[1, 2], [3, 4]])
matrix2 = tf.constant([[5, 6], [7, 8]])
# Matrix multiplication
result = tf.matmul(matrix1, matrix2)
print("Matrix Multiplication Result:
", result)Refer to the following tutorials to practice regression and classification tasks with PyTorch and TensorFlow:
tutorial_03_torch_regression_and_classification.ipynbtutorial_03_tf_regression_and_classification.ipynb
- François Chollet
- Tensorflow.org
- pytorch.org