Articles on Trending Technologies

Technical articles with clear explanations and examples

How to resize a tensor in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 7K+ Views

To resize a PyTorch tensor, we use the .view() method. We can increase or decrease the dimension of the tensor, but we have to make sure that the total number of elements in a tensor must match before and after the resize. Steps Import the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it. Create a PyTorch tensor and print it. Resize the above-created tensor using .view() and assign the value to a variable. .view() does not resize the ...

Read More

How to join tensors in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 35K+ Views

PyTorch provides two main methods to join tensors: torch.cat() and torch.stack(). The key difference is that torch.cat() concatenates tensors along an existing dimension, while torch.stack() creates a new dimension for joining. Key Differences torch.cat() concatenates tensors along an existing dimension without changing the number of dimensions. torch.stack() stacks tensors along a new dimension, increasing the tensor dimensionality by one. Using torch.cat() with 1D Tensors Let's start by concatenating 1D tensors ? import torch # Create 1D tensors t1 = torch.tensor([1, 2, 3, 4]) t2 = torch.tensor([0, 3, 4, 1]) t3 = ...

Read More

How to access the metadata of a tensor in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 918 Views

In PyTorch, tensor metadata includes essential information like size, shape, data type, and device location. The most commonly accessed metadata are the tensor's dimensions and total number of elements. Key Metadata Properties PyTorch tensors provide several ways to access metadata: .size() − Returns the dimensions as a torch.Size object .shape − Returns the same dimensions as .size() torch.numel() − Returns the total number of elements .dtype − Returns the data type .device − Returns the device (CPU/GPU) Example 1: 2D Tensor Metadata import torch # Create a 4x3 tensor T = ...

Read More

How to convert a NumPy ndarray to a PyTorch Tensor and vice versa?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 34K+ Views

A PyTorch tensor is like numpy.ndarray. The difference between these two is that a tensor utilizes the GPUs to accelerate numeric computation. We convert a numpy.ndarray to a PyTorch tensor using the function torch.from_numpy(). And a tensor is converted to numpy.ndarray using the .numpy() method. Steps Import the required libraries. Here, the required libraries are torch and numpy. Create a numpy.ndarray or a PyTorch tensor. Convert the numpy.ndarray to a PyTorch tensor using torch.from_numpy() function or convert the PyTorch tensor to numpy.ndarray using the .numpy() method. ...

Read More

How to access and modify the values of a Tensor in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 10K+ Views

We use Indexing and Slicing to access the values of a tensor. Indexing is used to access the value of a single element of the tensor, whereas Slicing is used to access the values of a sequence of elements. We use the assignment operator to modify the values of a tensor. Assigning new value/s using the assignment operator will modify the tensor with new value/s. Steps Import the required libraries. Here, the required library is torch. Define a PyTorch tensor. Access the value of a single element ...

Read More

How to convert an image to a PyTorch Tensor?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 16K+ Views

PyTorch tensors are n-dimensional arrays that can leverage GPU acceleration for faster computations. Converting images to tensors is essential for deep learning tasks in PyTorch, as it allows the framework to process image data efficiently on both CPU and GPU. To convert an image to a PyTorch tensor, we use transforms.ToTensor() which automatically handles scaling pixel values from [0, 255] to [0, 1] and changes the dimension order from HxWxC (Height x Width x Channels) to CxHxW (Channels x Height x Width). Method 1: Converting PIL Images The most common approach is using PIL (Python Imaging Library) ...

Read More

How to insert a temporary text in a tkinter Entry widget?

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 26-Mar-2026 24K+ Views

To insert a temporary text (placeholder) in a tkinter Entry widget, we can bind the event to clear the placeholder text when the user clicks on the entry field. This creates a user-friendly interface with helpful hints. Basic Implementation Here's how to create a basic placeholder text that disappears when the user focuses on the entry widget ? import tkinter as tk # Create the main window root = tk.Tk() root.geometry("400x200") root.title("Temporary Text Example") def clear_placeholder(event): """Clear placeholder text when entry gets focus""" if entry.get() ...

Read More

How to show the status of CAPS Lock Key in tkinter?

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 26-Mar-2026 1K+ Views

We can use the and bindings to check if the CAPS Lock Key is ON or off. In the following example, we will create two user-defined functions "caps_lock_on()" and "caps_lock_off()" which will capture the event of Lock-KeyPress and Lock-KeyRelease and display the status on the screen. Example Here's a complete example that shows the CAPS Lock status using event bindings ? # Import required libraries from tkinter import * # Create an instance of tkinter frame win = Tk() # Define the geometry of the window win.geometry("700x250") win.title("CAPS Lock Status") def ...

Read More

How to call a function using the OptionMenu widget in Tkinter?

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 26-Mar-2026 5K+ Views

The OptionMenu widget in Tkinter allows users to select from a dropdown list of options. You can call a function when an option is selected by using the command parameter and a StringVar object to track the selected value. Basic Setup First, create a StringVar object to manage the selected value and define a callback function ? from tkinter import * # Create main window root = Tk() root.geometry("400x300") root.title("OptionMenu Example") # Create StringVar to store selected value selected_city = StringVar() selected_city.set("Select a City") # Define callback function def on_city_select(value): ...

Read More

How to get the longitude and latitude of a city using Python?

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 26-Mar-2026 33K+ Views

To get the longitude and latitude of a city, we will use the geopy module. geopy uses third-party geocoders and other data sources to locate the coordinates of addresses, cities, countries, etc. First of all, make sure the geopy module is installed − pip install geopy Using Nominatim Geocoder In the following example, we will use the Nominatim geocoder to find the longitude and latitude of the city "Hyderabad" ? Steps Import Nominatim geocoder from geopy module. ...

Read More
Showing 1–10 of 61,303 articles
« Prev 1 2 3 4 5 6131 Next »
Advertisements