Yield Keyword in Ruby Programming

Mukul Latiyan
Updated on 25-Jan-2022 10:33:05

502 Views

There are often cases where we would want to execute a normal expression multiple times inside a method but without having to repeat the same expression again and again. With the yield keyword, we can do the same.We can also pass arguments to the yield keyword and get values in return as well. Now let's explore some examples to see how the yield keyword works in Ruby.Example 1Consider the code shown below where we are declaring a normal yield keyword twice inside a method and then calling it.def tuts    puts "In the tuts method"    # using yield keyword ... Read More

True, False, and Nil in Ruby Programming

Mukul Latiyan
Updated on 25-Jan-2022 10:29:39

341 Views

We know that everything in Ruby is treated as an object, and so the true, false and nil as well. They are built-in types that Ruby provides to do different conditional checks and more. In this article, we will explore different examples of the true, false and nil data types and how to use them.True, False in RubyLet's start with a very simple example where we will check if two variables are equal or not.Example 1Consider the code shown belowfirst = 10 second = 10 if first == second    # If Condition is true    puts "True! First ... Read More

Static Members in Ruby Programming

Mukul Latiyan
Updated on 25-Jan-2022 10:25:26

3K+ Views

Static Members in Ruby are declared with the help of the class. Since Ruby doesn't provide a reserved keyword such as static, when we make use of the class variable, then we create a static variable and then we can declare a method of that class in which the static variable is defined as a static method as well.In Ruby, there are two implementations for the static keyword −Static variableStatic methodIn this article, we will explore both these implementations where first, we will explore a code example of how to declare a static variable and then we will see how ... Read More

Adjust Sharpness of an Image in PyTorch

Shahid Akhtar Khan
Updated on 25-Jan-2022 08:58:11

2K+ Views

To adjust the sharpness of an image, we apply adjust_sharpness(). It's one of the functional transforms provided by the torchvision.transforms module. adjust_sharpness() transformation accepts both PIL and tensor images.A tensor image is a PyTorch tensor with shape [C, H, W], where C is number of channels, H is image height, and W is image width. This transform also accepts a batch of tensor images. If the image is neither a PIL image nor tensor image, then we first convert it to a tensor image and then apply the adjust_sharpness(). The sharpness should be any non-negative number.Syntaxtorchvision.transforms.functional.adjust_sharpness(img, sharpness_factor)Parametersimg – Image of ... Read More

Construct Complex Tensor with Real and Imaginary Parts in PyTorch

Shahid Akhtar Khan
Updated on 25-Jan-2022 08:51:23

2K+ Views

With given real and imaginary parts, we can construct a complex number in PyTorch using torch.complex() method. The real and imaginary parts must be float or double. Both the real and imaginary parts must be of the same type. If the real part is float, then the imaginary must also be float.If the inputs are torch.float32, then the constructed complex tensor must be torch.complex64.If the inputs are torch.float64, then the complex tensor must be torch.complex128.Syntaxtorch.complex(real, imag)Parametersreal and imag − Real and imaginary parts of the complex tensor. Both must be of the same dtype, float or double only.StepsWe could use the ... Read More

Find Start and Ending Index of an Element in an Unsorted Array in C++

sudhir sharma
Updated on 25-Jan-2022 08:48:09

936 Views

In this problem, we are given an array aar[] of n integer values which are not sorted and an integer val. Our task is to find the start and ending index of an element in an unsorted array.For the occurrence of the element in the array, we will return, "Starting index and ending index " if it is found in the array twice or more."Single index " if it is found in the array once."Element not present " if it is not present in the array.Let's take an example to understand the problem, Example 1Input : arr[] = {2, 1, ... Read More

Define a Simple Convolutional Neural Network in PyTorch

Shahid Akhtar Khan
Updated on 25-Jan-2022 08:45:27

356 Views

To define a simple convolutional neural network (CNN), we could use the following steps −StepsFirst we import the important libraries and packages. We try to implement a simple CNN in PyTorch. In all the following examples, the required Python library is torch. Make sure you have already installed it.import torch import torch.nn as nn import torch.nn.functional as FOur next step is to build a simple CNN model. Here, we use the nn package to implement our model. For this, we define a class MyNet and pass nn.Module as the parameter.class MyNet(nn.Module):We need to create two functions inside the class to ... Read More

Define a Simple Artificial Neural Network in PyTorch

Shahid Akhtar Khan
Updated on 25-Jan-2022 08:39:11

571 Views

To define a simple artificial neural network (ANN), we could use the following steps −StepsFirst we import the important libraries and packages. We try to implement a simple ANN in PyTorch. In all the following examples, the required Python library is torch. Make sure you have already installed it.import torch import torch.nn as nnOur next step is to build a simple ANN model. Here, we use the nn package to implement our model. For this, we define a class MyNetwork and pass nn.Module as the parameter.class MyNetwork(nn.Module):We need to create two functions inside the class to get our model ready. ... Read More

Load a Computer Vision Dataset in PyTorch

Shahid Akhtar Khan
Updated on 25-Jan-2022 08:33:23

786 Views

There are many datasets available in Pytorch related to computer vision tasks. The torch.utils.data.Dataset provides different types of datasets. The torchvision.datasets is a subclass of torch.utils.data.Dataset and has many datasets related to images and videos. PyTorch also provides us a torch.utils.data.DataLoader which is used to load multiple samples from a dataset.StepsWe could use the following steps to load computer vision datasets −Import the required libraries. In all the following examples, the required Python libraries are torch, Matplotlib, and torchvision. Make sure you have already installed them.import torch import torchvision from torchvision import datasets from torchvision.transforms import ToTensor import matplotlib.pyplot as ... Read More

Find Square Root Under Modulo p Using Shanks-Tonelli Algorithm in C++

sudhir sharma
Updated on 25-Jan-2022 08:28:01

598 Views

In this problem, we are given two values n and a prime number p. Our task is to find Square Root under Modulo p.Let's take an example to understand the problem, Input : n = 4, p = 11 Output : 9Solution ApproachHere, we will be using Tonelli-Shanks Algorithm.Tonelli-Shanks Algorithm is used in modular arithmetic to solve for a value x in congruence of the form x2 = n (mod p).The algorithm to find square root modulo using shank's Tonelli Algorithm −Step 1 − Find the value of $(n^{((p-1)/2)})(mod\:p)$, if its value is p -1, then modular square root is ... Read More

Advertisements