Category: Python

  • In Pytorch what is nn.Embedding for and how is it different from One Hot Encding for representing categorical data

    In Pytorch what is nn.Embedding for and how is it different from One Hot Encding for representing categorical data

    In PyTorch, nn.Embedding is a class that provides a simple lookup table that maps integers (usually representing discrete items like words, tokens, or categories) to continuous vectors. It is primarily used for working with categorical data in deep learning models, particularly in natural language processing tasks. nn.Embedding is often used to convert discrete tokens (e.g., […]

  • What is the PyTorch permute() function for?

    What is the PyTorch permute() function for?

    In PyTorch, the permute() function is used to rearrange the dimensions of a tensor according to a specified order. This can be useful in various deep learning scenarios, such as when you need to change the dimension order of your input data to match the expected input format of a model. The function takes a […]

  • Pairwise Squared Euclidean Distance Loss function used in “Taming Transformers for high resolution images” paper explained

    Pairwise Squared Euclidean Distance Loss function used in “Taming Transformers for high resolution images” paper explained

    The code snippet using PyTorch library below is found in the Taming Transformers paper: This code snippet is performing a vectorized calculation to compute pairwise squared Euclidean distances between two sets of vectors: z_flattened and the rows of self.embedding.weight. Let’s break down the code: Now let’s analyze the calculations: Finally, the code adds the three […]

  • What is tensor.detach() used for in PyTorch?

    What is tensor.detach() used for in PyTorch?

    Let’s take a closer look at the detach() function in PyTorch, which plays a helpful role when working with Tensors. The detach() function creates a new Tensor that shares the same data as the original one but without the attached computation history. This essentially separates the new Tensor from the computation graph, making it independent […]

  • What does the tensor.view() function do in PyTorch and how is it different from permute?

    What does the tensor.view() function do in PyTorch and how is it different from permute?

    In PyTorch, the view() function is a tensor operation used to reshape a tensor without changing its underlying data. It allows you to change the dimensions of a tensor to fit your desired shape while preserving the original data and maintaining the same number of elements. This is especially useful when you need to change […]

  • Visualizing a neural network in 3D with Python, Blender and Tensorflow

    Visualizing a neural network in 3D with Python, Blender and Tensorflow

    I have started recently a new project in which I am trying to visualize a neural network in 3D with Python, Blender and Tensorflow. This is a very interesting challenge and a great way to learn things that I didn’t know I needed to know. For example how to get access to the different layers […]

  • Google Dataflow Secure Quickstart with Python

    Google Dataflow Secure Quickstart with Python

    In this step-by-step guide I will share the additional steps that I followed to enable Google Dataflow with the least permissions needed, with Google KMS Encryption enabled, and all the necessary service accounts, that one would be expected to employ in a secure production deployment. I will base this guide on the already existing GCP […]

  • How to resize an image with Tensorflow tf.image.resize()

    How to resize an image with Tensorflow tf.image.resize()

    When dealing with training data, you often need to resize images to a fixed size, according to the architecture of the machine learning model that you want to use. In Tensorflow we can use tf.image.resize() to resize images to different resolutions. The method controls the different algorithms that we can use for resizing. You can […]

  • Python Exercise for Beginners – Implementing One Hot Encoding

    Python Exercise for Beginners – Implementing One Hot Encoding

    In today’s Python exercise we will create a one-hot encoding algorithm to convert categorical data to a numerical format. The reason why we need to do this is that machine learning algorithms only can deal with numbers, not strings. What is One Hot Encoding? One-hot encoding is a way of representing categorical information in binary […]

  • Python Selenium Webdriver.implicitly_wait(seconds)

    Python Selenium Webdriver.implicitly_wait(seconds)

    This method configures the Python Selenium web driver to wait up to N seconds before it gives up searching for an element on a page. By default, implicitly_wait is set to 0, which means that if an element is not immediately available, it will fail immediately if, upon loading the page, it doesn’t find the […]