0% found this document useful (0 votes)
41 views

Pytorch Exercise

Uploaded by

nandinimukati07
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Pytorch Exercise

Uploaded by

nandinimukati07
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PyTorch Exercise

Explore following functions of PyTorch with example.

1. Tensor Creation

● torch.tensor() - This function creates a tensor from a Python list or NumPy array.

● torch.zeros() - This function creates a tensor filled with zeros.

● torch.ones() - This function creates a tensor filled with ones.

● torch.arange() - This function creates a tensor with values from start to end with a
specified

● torch.eye() - This function creates a tensor with ones on the diagonal and zeros
elsewhere.

● torch.empty() - This function creates an uninitialized tensor.

● torch.full() - This function creates a tensor filled with a specified value.

● torch.rand() - This function creates a tensor with random values from a uniform
distribution.

● torch.randint() - This function creates a tensor with random integer values from a
specified range.

2. Tensor Operations

● Arithmetic operations:

1. torch.add()- Adds two tensors element-wise.

2. torch.sub() - Subtracts the second tensor from the first element-wise.

3. torch.mul() - Multiplies two tensors element-wise.

4. torch.div() - Divides the first tensor by the second element-wise.

● Matrix operations:

1. torch.matmul() - Performs matrix multiplication on two tensors.

● Reduction operations:
1. torch.sum() - Calculates the sum of all elements in a tensor.

2. torch.mean() - Calculates the mean of all elements in a tensor.

3. torch.std() - Calculates the standard deviation of all elements in a tensor.

4. torch.var() - Calculates the variance of all elements in a tensor.

5. torch.min() - Returns the minimum value in a tensor.

6. torch.max() - Returns the maximum value in a tensor.

● Comparison operations:

1. torch.eq() - Compares two tensors element-wise for equality.

2. torch.ne()- Compares two tensors element-wise for inequality.

3. torch.gt() - Compares two tensors element-wise for greater than.

4. torch.ge()- Compares two tensors element-wise for greater than or equal.


Greater Than or Equal

5. torch.lt()- Compares two tensors element-wise for less than.

6. torch.le()- Compares two tensors element-wise for less than or equal.

● Reshaping operations:

1. torch.view() - Changes the shape of a tensor without copying its data.

2. torch.reshape()- Changes the shape of a tensor by copying its data.

3. torch.transpose()- Transposes the rows and columns of a tensor.

4. torch.flatten()- Flattens a tensor into a one-dimensional tensor.

● Concatenation and splitting:

1. torch.cat() - Concatenates multiple tensors along a specified dimension.

2. torch.split()- Splits a tensor into multiple tensors along a specified dimension.

3. Neural Network Modules

● Layers:
1. torch.nn.Linear()- This function is used to create a fully connected linear layer.
It takes in the number of input features and output features as arguments. This
layer performs a matrix multiplication followed by an optional bias addition.

2. orch.nn.Conv2d() - This function is used to create a 2D convolutional layer. It


takes in the number of input channels, output channels, kernel size, and stride
as arguments. This layer applies a sliding window of the specified kernel size
to the input data, performing a dot product at each position to generate the
output.

3. torch.nn.Conv1d() - This function is used to create a 1D convolutional layer. It


takes in the number of input channels, output channels, kernel size, and stride
as arguments. This layer applies a sliding window of the specified kernel size
to the input data, performing a dot product at each position to generate the
output.

4. torch.nn.Conv3d() - This function is used to create a 3D convolutional layer. It


takes in the number of input channels, output channels, kernel size, and stride
as arguments. This layer applies a sliding window of the specified kernel size
to the input data, performing a dot product at each position to generate the
output.

5. torch.nn.RNN() - This function is used to create a Recurrent Neural Network


(RNN) layer. It takes in the number of input features, hidden size, and number
of layers as arguments. This layer processes the input sequence sequentially,
using the output from the previous time step to generate the next output.

6. torch.nn.ReLU() - This function is used to create a Rectified Linear Unit


(ReLU) activation function layer. It takes no arguments and applies the ReLU
function to the input data, which maps all negative values to 0 and all positive
values to the same value.

7. torch.nn.Sigmoid() - This function is used to create a sigmoid activation


function layer. It takes no arguments and applies the sigmoid function to the
input data, which maps all values to a range between 0 and 1.
8. torch.nn.Tanh() - This function is used to create a hyperbolic tangent (tanh)
activation function layer. It takes no arguments and applies the tanh function
to the input data, which maps all values to a range between -1 and 1.

● Loss functions:

1. torch.nn.MSELoss() - This function is used to create a Mean Squared Error


(MSE) loss function. It takes no arguments and calculates the average squared
difference between the predicted and actual values.

2. torch.nn.CrossEntropyLoss() - This function is used to create a Cross-Entropy


loss function. It takes no arguments and calculates the cross-entropy between
the predicted and actual values.

3. torch.nn.NLLLoss() - This function is used to create a Negative Log-


Likelihood (NLL) loss function. It takes no arguments and calculates the
negative log-likelihood of the predicted values given the actual values.

4. torch.nn.BCELoss() - This function is used to create a Binary Cross-Entropy


(BCE) loss function. It takes no arguments and calculates the binary cross-
entropy between the predicted and actual binary values.

5. torch.nn.BCEWithLogitsLoss() - This function is used to create a Binary


Cross-Entropy with Logits loss function. It takes no arguments and calculates
the binary cross-entropy between the predicted and actual binary values, using
the logits as the input.

● Optimizers:

1. torch.optim.SGD() - This function is used to create a Stochastic Gradient


Descent (SGD) optimizer. It takes in the model parameters and learning rate as
arguments and updates the model parameters based on the gradient of the loss
function.

2. torch.optim.Adam() - This function is used to create an Adam optimizer. It


takes in the model parameters and learning rate as arguments and updates the
model parameters based on the gradient of the loss function using the Adam
algorithm.

● Utility modules:
1. torch.nn.Module() - This function is used to create a neural network module. It
is the base class for all neural network modules in PyTorch and provides the
basic functionality for defining and training neural networks.

2. torch.nn.Sequential() - This function is used to create a sequential neural


network module. It takes in a list of layers as arguments and applies them
sequentially to the input data.

3. torch.nn.DataParallel() - This function is used to create a data parallel neural


network module. It takes in a model and device as arguments and allows the
model to be trained in parallel across multiple devices.

You might also like