Close Menu
    Trending
    • Business Owners Can Finally Replace a Subtle Cost That Really Adds Up
    • I Won $10,000 in a Machine Learning Competition — Here’s My Complete Strategy
    • When AIs bargain, a less advanced agent could cost you
    • Do You Really Need GraphRAG? — AI Innovations and Insights 50 | by Florian June | AI Exploration Journey | Jun, 2025
    • What Is ‘Doom Spending’ and Which Generation Falls for It?
    • Grad-CAM from Scratch with PyTorch Hooks
    • Categorical Data Encoding: The Secret Sauce Behind Better Machine Learning Models | by Pradeep Jaiswal | Jun, 2025
    • Who Is Alexandr Wang, the Founder of Scale AI Joining Meta?
    Finance StarGate
    • Home
    • Artificial Intelligence
    • AI Technology
    • Data Science
    • Machine Learning
    • Finance
    • Passive Income
    Finance StarGate
    Home»Machine Learning»My Beginer notes to Python & PyTorch I | by AnneStructo | May, 2025
    Machine Learning

    My Beginer notes to Python & PyTorch I | by AnneStructo | May, 2025

    FinanceStarGateBy FinanceStarGateMay 2, 2025No Comments4 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    Qs: Why do we want a Tensor ? What does imply by a “Tensor” ?
    Reply: It’s the information construction that performs the facility function in deep studying. Merely, it’s the 0D, 1D, 2D, 3D or mutli dimensional array of numbers {that a} machine studying library use to retailer and compute the information.

    Qs: What sort of Tensors can be found ?
    Ans: 1D, 2D, 3D and Mutli dimensional tensors can be found.

    1. Making a Easy Tensor
    import torch
    print(torch.tensor([4,5,6]))

    [ ] That is used to checklist the tensor components.
    Above code will merely print a 1D Tensor(merely a vector or a listing type of) of [4, 5, 6]

    1.1 Importing Required Library

    With the Use of this ‘torch’ Library, torch library so you may create and manipulate tensors.

    Qs: What’s the function behind importing the Library ?
    Reply: It’s like a prepared made device. So, with this command you will merely borrow that device briefly. It already comprise some features, lessons,.. So, we don’t want to begin from scratch. On this case, we’re importing ‘torch’ library, So, we get the entry to carry out pre outlined operation like creating, manipulating modifying tensors. This library is helpful in constructing deep studying fashions shortly.
    Additionally, it offers automated differentiation. i.e. It would routinely calculate the gradients , we don’t have to manually carry out calculus stuff like chain rule, derivatives, and so on..

    1.2 Assigning the properties for the Tensor

    import torch
    my_tensor = torch.tensor([[4,5,6],[7,8,9]], dtype= torch.float32,
    gadget='cpu', requires_grad= True )
    print(my_tensor)

    Qs: Why can we use dtype=torch.float32 ?
    Ans: In right here dtype=torch.float32 means, every quantity within the tensor needs to be a 32-bit floating level quantity .
    i.e. Want to jot down 4.0 as a substitute of 4

    Qs: What else can we write as a substitute of torch.float32 ?
    Ans: Not solely float, we will additionally use int, uint, bool as nicely
    integer varieties (int8, int16, int32, int64, uint8)
    float varieties (float16, float32, float64)
    Boolean (bool)

    torch.tensor([1.0, 2.0], dtype=torch.float32)    # 32-bit float (default for many ML duties)
    torch.tensor([1.0, 2.0], dtype=torch.float64) # 64-bit float (double precision)
    torch.tensor([1.0, 2.0], dtype=torch.float16) # 16-bit float (much less reminiscence, sooner on GPU)

    torch.tensor([1, 2, 3], dtype=torch.int64) # 64-bit integer (additionally referred to as lengthy)
    torch.tensor([1, 2, 3], dtype=torch.int32) # 32-bit integer (additionally referred to as int)
    torch.tensor([1, 2, 3], dtype=torch.int16) # 16-bit integer (brief)
    torch.tensor([1, 2, 3], dtype=torch.int8) # 8-bit signed integer
    torch.tensor([1, 2, 3], dtype=torch.uint8) # 8-bit unsigned integer (solely constructive)

    torch.tensor([True, False], dtype=torch.bool) # Boolean tensor (True/False)

    From above, if we iterate on cpu, float 16, int 16 will not be appropriate

    Qs: Why can we use gadget=’cpu’ ?
    Ans: This can retailer the Tensor in CP, If our PC or laptop computer has NVIDIA Graphic card it is going to place the tensor on ‘GPU’. If there are a number of graphic playing cards for a pc, we have to select saying on which we’re going to place the Tensor. In any other case the system will routinely select. The above code could be additional improved by asking the system to get the enter for gadget

    import torch
    gadget= 'cuda' if torch.cuda.is_available() else 'cpu'
    my_tensor = torch.tensor([[4,5,6],[7,8,9]], dtype= torch.float32,
    gadget=gadget, requires_grad= True )
    print(my_tensor)

    In right here each double quotes(” “) and single quotes(’ ’) work the identical for strings in python.

    Qs: Why can we use requires_grad=True ?
    Ans: In right here, we’re asking the pytorch to recollect what we’re going to do that with this Tensor in Future (Protecting monitor of the operations) . So, it is going to routinely calculate the gradients later & that is essential in coaching the neural networks.

    1.3 Printing the Tensor properties

    import torch
    gadget= 'cuda' if torch.cuda.is_available() else 'cpu'
    my_tensor = torch.tensor([[4,5,6],[7,8,9]], dtype= torch.float32,
    gadget=gadget, requires_grad= True )

    print(my_tensor)
    print(my_tensor.dtype)
    print(my_tensor.gadget)
    print(my_tensor.form)
    print(my_tensor.requires_grad)

    The beneath desk exhibits the which means for every line within the code.

    ```
    | Enter | What It Does | Output | Notes |
    |--------------------------------|----------------------------|--------------------------------------------------|---------------------------------------|
    | print(my_tensor) | Shows the tensor | tensor([[4., 5., 6.], | Reveals values and whether or not |
    | | | [7., 8., 9.]], requires_grad=True) | gradient monitoring is enabled |
    | print(my_tensor.dtype) | Knowledge sort of components | torch.float32 | 32-bit floating level numbers |
    | print(my_tensor.gadget) | System it’s saved on | cpu | Tensor is on the CPU |
    | print(my_tensor.form) | Tensor measurement | torch.Measurement([2, 3]) | 2 rows × 3 columns |
    | print(my_tensor.requires_grad) | Is gradient monitoring on? | True | Used for backpropagation |
    ```

    Above code could be additional improved, to print with the output together with a transparent labeled descriptions.

    import torch
    gadget= 'cuda' if torch.cuda.is_available() else 'cpu'
    my_tensor = torch.tensor([[4,5,6],[7,8,9]], dtype= torch.float32,
    gadget=gadget, requires_grad= True )

    print("my Tensor= ",my_tensor)
    print("Tensor sort= ",my_tensor.dtype)
    print("Tensor to be at ",my_tensor.gadget)
    print("Tensor Dimension= ",my_tensor.form)
    print("If my tensor requires a gradient ",my_tensor.requires_grad)



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleWhat A Recession Is Like For Early Retirees: The Good and Bad
    Next Article Step-by-Step Guide to Build and Deploy an LLM-Powered Chat with Memory in Streamlit
    FinanceStarGate

    Related Posts

    Machine Learning

    Do You Really Need GraphRAG? — AI Innovations and Insights 50 | by Florian June | AI Exploration Journey | Jun, 2025

    June 17, 2025
    Machine Learning

    Categorical Data Encoding: The Secret Sauce Behind Better Machine Learning Models | by Pradeep Jaiswal | Jun, 2025

    June 17, 2025
    Machine Learning

    How Netflix Uses Data to Hook You | by Vikash Singh | Jun, 2025

    June 17, 2025
    Add A Comment

    Comments are closed.

    Top Posts

    Artificial intelligence enhances air mobility planning | MIT News

    April 25, 2025

    From Code to Creativity: Building Multimodal AI Apps with Gemini and Imagen | by Hiralkotwani | May, 2025

    May 15, 2025

    Stop Risking Your Expensive MacBook on Trips. Get This $378 Version Instead.

    March 25, 2025

    Why Manual Data Entry Is Killing Estate Planning Productivity

    April 7, 2025

    Hidden risks for Canadians planning to downsize their retirement

    June 9, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Data Science
    • Finance
    • Machine Learning
    • Passive Income
    Most Popular

    NVIDIA to Manufacture AI Supercomputers in U.S.

    April 14, 2025

    Meta CEO Mark Zuckerberg Wants You to Make AI Friends

    May 8, 2025

    The Model Context Protocol (MCP) : Game-Changer or Vendor Lock-in Trap? | by Jalaj Agrawal | Jun, 2025

    June 2, 2025
    Our Picks

    These Are the Top 10 Franchises Under $25,000 in 2025

    May 21, 2025

    CEOs Get Paid Too Much, According to Pretty Much Everyone in the World | by Bhajan Bishnoi | Feb, 2025

    February 12, 2025

    Taxpayers get clarity on charitable donation deadline

    February 4, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Data Science
    • Finance
    • Machine Learning
    • Passive Income
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2025 Financestargate.com All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.