Python Articles

Page 513 of 852

How to perform element-wise addition on tensors in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Nov-2021 9K+ Views

We can use torch.add() to perform element-wise addition on tensors in PyTorch. It adds the corresponding elements of the tensors. We can add a scalar or tensor to another tensor. We can add tensors with same or different dimensions. The dimension of the final tensor will be same as the dimension of the higher dimension tensor.StepsImport the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it.Define two or more PyTorch tensors and print them. If you want to add a scalar quantity, define it.Add two or more tensors ...

Read More

How to resize a tensor in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Nov-2021 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.StepsImport 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 original tensor; it only gives a view with the new size, as its ...

Read More

How to access the metadata of a tensor in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Nov-2021 902 Views

We access the size (or shape) of a tensor and the number of elements in the tensor as the metadata of the tensor. To access the size of a tensor, we use the .size() method and the shape of a tensor is accessed using .shape.Both .size() and .shape produce the same result. We use the torch.numel() function to find the total number of elements in the tensor.StepsImport the required library. Here, the required library is torch. Make sure that you have installed torch.Define a PyTorch tensor.Find the metadata of the tensor. Use .size() and .shape to access the size and ...

Read More

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

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Nov-2021 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, whereasSlicing 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.StepsImport the required libraries. Here, the required library is torch.Define a PyTorch tensor.Access the value of a single element at particular index using indexing or access the values of sequence of elements using slicing.Modify the accessed values with new ...

Read More

How to convert an image to a PyTorch Tensor?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Nov-2021 15K+ Views

A PyTorch tensor is an n-dimensional array (matrix) containing elements of a single data type. A tensor is like a numpy array. The difference between numpy arrays and PyTorch tensors is that the tensors utilize the GPUs to accelerate the numeric computations. For the accelerated computations, the images are converted to the tensors.To convert an image to a PyTorch tensor, we can take the following steps −StepsImport the required libraries. The required libraries are torch, torchvision, Pillow.Read the image. The image must be either a PIL image or a numpy.ndarray (HxWxC) in the range [0, 255]. Here H, W, and ...

Read More

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

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 26-Oct-2021 24K+ Views

To insert a temporary text in a tkinter Entry widget, we will bind the event with the Entry widget and call a user-defined function to delete the text inside the Entry widget.Steps −Import the tkinter library and create an instance of tkinter frame.Set the size of the frame using geometry method.Create a user-defined method "temp_text()" to capture the event and delete the temporary text inside the Entry widget.Create an Entry widget inside the Root window and set its properties such as background color, width, and border width.Use the insert() method of the Entry widget to insert a string ...

Read More

How to update a Button widget in Tkinter?

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 26-Oct-2021 3K+ Views

We can update a Button widget in Tkinter in various ways, for example, we can change its size, change its background color, or remove its border, etc. In the following example, we will create three Button widgets and each of the buttons, upon clicking, will call a different function to update their features.Example# Import the required library from tkinter import * from tkinter import ttk # Create an instance of tkinter frame win = Tk() # Define geometry of the window win.geometry("700x300") # Function to Increase the Size of the Button def Button_Size(): button1.configure(font=('Calibri ...

Read More

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

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 26-Oct-2021 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 print the status on the screen.Example# Import required libraries from tkinter import * from tkinter import ttk # Create an instance of tkinter frame win = Tk() # Define the geometry of the window win.geometry("700x250") win.title("CAPS Lock Status") def caps_lock_on(e): label_caps.config(text="CAPS Lock is ON") def caps_lock_off(e): label_caps.config(text="CAPS ...

Read More

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

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 26-Oct-2021 5K+ Views

Let's take an example and see how to call a function using OptionMenu widget in Tkinter. In the example, we will use a StringVar object and call its get() method. A StringVar object in Tkinter can help manage the value of a widget.We will create an OptionMenu widget and fill it with a list of strings. When the user selects an option, it will invoke a function which in turn will print the selected option as a label.Steps −Import the tkinter library and create an instance of tkinter frame.Set the size of the frame using geometry method.Create a set of ...

Read More

How to use a StringVar object in an Entry widget in Tkinter?

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 26-Oct-2021 6K+ Views

A StringVar object in Tkinter can help manage the value of a widget such as an Entry widget or a Label widget. You can assign a StringVar object to the textvariable of a widget. For example, data = ['Car', 'Bus', 'Truck', 'Bike', 'Airplane'] var = StringVar(win) my_spinbox = Spinbox(win, values=data, textvariable=var)Here, we created a list of strings followed by a StringVar object "var". Next, we assigned var to the textvariable of a Spinbox widget. To get the current value of the Spinbox, you can use var.get().ExampleThe following example demonstrates how you can use a StringVar object in an ...

Read More
Showing 5121–5130 of 8,519 articles
« Prev 1 511 512 513 514 515 852 Next »
Advertisements