Our terms of service are changing. Learn more.

Publication

Deep Learning

Visual Representation of Matrix and Vector Operations and implementation in NumPy, Torch, and Tensor

Last Updated on August 24, 2020 by Editorial Team

Author(s): Balakrishnakumar V

Deep Learning

Visual Representation of Matrix and Vector Operations and implementations in NumPy, Torch, and TensorFlow.

Implementing rudimentary to advanced operations on deep learning’s fundamental units.

Excerpts

I am accustomed to creating new deep learning architectures for different problems, but which framework (Keras, Pytorch, TensorFlow) to choose is often harder.

Since there’s an uncertainty in it, it’s good to know the fundamental operations on those framework’s fundamental units (NumPy, Torch, Tensor).

In this post, I have performed a handful of the same operations across the 3 frameworks, also tried my hands on visualization for most of them.

This is a beginner-friendly post, so let’s get started.

1. Installation

https://medium.com/media/45dde5571ae3734614a88bca3e9f99a7/href

2. Version Check

https://medium.com/media/4cde2d95a5907cdb2eb868b07fac348f/href

3. Array Initialization ~ 1-D, 2-D, 3-D

Scalar and 1-D Array

Scalar, 1-D, 2-D arrays

Numpy Implementation:

https://medium.com/media/2586325b0ab84277f7bf1ee4dff50d96/href

TensorFlow Implementation:

https://medium.com/media/56d73c939edbdd7d0e14e1d72091b037/href

Torch Implementation:

https://medium.com/media/6f85a23af9421c6d8e9ee6e170d68dbf/href

2-D Vector Array

2-D Array

Numpy Implementation:

https://medium.com/media/9cda361f39d8c1ac58810a036cbf9661/href

TensorFlow Implementation:

https://medium.com/media/da6b0af097dfd5f8bd599b481d9c4404/href

Torch Implementation:

https://medium.com/media/fd989fa1695bcdc501cf4cf2d9c08488/href

4. Generating Data

Zeros and Ones
Diagonal & Same element fill

Numpy Implementation:

https://medium.com/media/423b55d11195cb13bbc4862a896bda43/href

TensorFlow Implementation:

https://medium.com/media/d9a0bb775e154dd307226f7cf6eb845a/href

Torch Implementation:

https://medium.com/media/fcf64cdc5ceef3afd01685c585c78bde/href

Draw random samples from the Normal distribution

Normal Dist’n Bell Curve
Samples were drawn from Normal Dist’n

Numpy Implementation:

https://medium.com/media/c86791e32d2e70a40e3d5feedc0d2f6e/href

TensorFlow Implementation:

https://medium.com/media/e21706a5210223d90907ec70f446b518/href

Torch Implementation:

https://medium.com/media/6bf66553a070b6d54aa6bb87dbc3998c/href

Draw samples from the uniform distribution

Uniform Dist’n Curve
Samples were drawn from Uniform Dist’n

Numpy Implementation:

https://medium.com/media/1e44d2824b10cd7b8fb6fd0cf1a67ebe/href

TensorFlow Implementation:

https://medium.com/media/284dee4387ac82ff8065fe351eb5c04c/href

Torch Implementation:

https://medium.com/media/7a88f706f3e0798e8eb1efb82f1a844f/href

6. Vector Arrangements

Numpy Implementation:

https://medium.com/media/9bf4d1da6b9f8de757c89cdb41ef64fd/href

TensorFlow Implementation:

https://medium.com/media/4b74d9fc4e9cc24a413ec00ccf5916be/href

Torch Implementation:

https://medium.com/media/06c3c61ae046ffa22f0739c8bcb42d01/href

7. Data-Type — Conversions

uint8/16/32/64 ← → float8/16/32/64

Numpy Implementation:

https://medium.com/media/980c211d8a23d852feb9e94f79d4900f/href

TensorFlow Implementation:

https://medium.com/media/e804d8f1050b8c35f55c265714549dd4/href

Torch Implementation:

https://medium.com/media/7a845bcdfacb6fa81d9b82809c2de775/href

8. Math Operations

Sum and Subtract operations
multiply and divide operations

Numpy Implementation:

https://medium.com/media/eba9064d4724ca43b7a5751348245cdc/href

TensorFlow Implementation:

https://medium.com/media/1d75df5212b37b19c0b33e3cff5735f1/href

Torch Implementation:

https://medium.com/media/2eaf4d782ff594aa0a1c05d2f09584bf/href

9. Dot Product

Dot Product

Numpy Implementation:

https://medium.com/media/229abb9b4d6b5d3ecaf8980468286dab/href

TensorFlow Implementation:

https://medium.com/media/6d4642108b886b49292afb65b1420dfc/href

Torch Implementation:

https://medium.com/media/e25cd91f035cd53f30171dbab198c408/href

10. Matrix Multiplication

Matrix Multiplication

Numpy Implementation:

https://medium.com/media/756f796c692625cd4e4f317b7c01ffb2/href

TensorFlow Implementation:

https://medium.com/media/1a34c0b1ddc451ce8b58519325b7d25b/href

Torch Implementation:

https://medium.com/media/e8aab406fa07a73f72d72671e2c04f78/href

11. Indexing and Slicing (2-D)

Indexing and Slicing

Numpy Implementation:

https://medium.com/media/ce07a06a281f34ac32d4ddd2ddde4260/href

TensorFlow Implementation:

https://medium.com/media/c192f141a43237a24246ced3a5e3582d/href

Torch Implementation:

https://medium.com/media/d69b03464fe8827f2002e90581c8aa33/href

12. Indexing and Slicing (2-D — Matrix)

Matrix Slicing

Numpy Implementation:

https://medium.com/media/a512d356764d72ac21ccd086887e7a28/href

TensorFlow Implementation:

https://medium.com/media/9ccb4a88add1d6b73e582e63df0a7e75/href

Torch Implementation:

https://medium.com/media/22a7f25edd7ea0d797630f52e0c6552e/href

13. Reshaping and Transpose axes

Reshape & Transpose

Numpy Implementation:

https://medium.com/media/67c3e5cc59c2d9af3147d49125dd2fae/href

TensorFlow Implementation:

https://medium.com/media/a2bb2414b590eeaff00552dd26a5e1ba/href

Torch Implementation:

https://medium.com/media/6cf038dcdd99b23ff0918f9dd883f9db/href

14. Concatenation

Matrix Concatenation

Numpy Implementation:

https://medium.com/media/d0df66ae2ad4f357e71477f0bd9c1b30/href

TensorFlow Implementation:

https://medium.com/media/f3c57129a8ee77bcaf274e9a45d11709/href

Torch Implementation:

https://medium.com/media/2aff05639626ee78d0fc7fcf37eb8e64/href

15. Summing across axes

Axes — Sum

Numpy Implementation:

https://medium.com/media/3cd1a7a6267a7e69d5be9547c282977a/href

TensorFlow Implementation:

https://medium.com/media/da12f85b8fbf03e4b3dab64e9df28bad/href

Torch Implementation:

https://medium.com/media/52bfa85022a73fffad05e87552512ac4/href

16. Mean across axes

Numpy Implementation:

https://medium.com/media/008539e13e18856c69f2aacd7855f73a/href

TensorFlow Implementation:

https://medium.com/media/2fc79dfc139f24e51b889db02c03bcf2/href

Torch Implementation:

https://medium.com/media/31e2b6d9f259186ffa0046f9941e3d6c/href

17. Dimension Expansion & moving axes.

Concat and move axes

Numpy Implementation:

https://medium.com/media/55cdce957285c88401b3a8beb0b80d40/href

TensorFlow Implementation:

https://medium.com/media/a4cc7e4a8b7067b3fb4ff0b085684df9/href

Torch Implementation:

https://medium.com/media/ae1e1b912150efa208ec95f662cef2e2/href

18. Max (Min) and Argmax :

Max for axis=0
Max for axis=1

Numpy Implementation:

https://medium.com/media/95b135930f90176fc92525c3889885f5/href

TensorFlow Implementation:

https://medium.com/media/2cb0c8bd445f43b969d07ddcb4f9c3ea/href

Torch Implementation:

https://medium.com/media/4a5bdaa3b6f244153098f6957f2c918b/href

19. Slicing and Indexing (3-D Matrix)

3×3 Matrix and its indices
Upper-Left & Lower-Right
Middle Elements & Inverse Middle Element

Numpy Implementation:

https://medium.com/media/f7b62b0ba718d8abd4cf321d58b0bc95/href

TensorFlow Implementation:

https://medium.com/media/7e1f28beb6adacc8bf0f08cac2aea31c/href

Torch Implementation:

https://medium.com/media/8a3088da5bdeb1b90a0b2cc016346dce/href

Due to visualization constraints, I skipped the operations on the higher dimension parts.

I hope I was able to provide some visual understanding to some of the fundamental operations along with your choice of deep learning framework and I will add more detailed operations shortly.

Check out the Notebook to find the above codes curated here → Colab.

Until then, see you next time.

Article By:

BALAKRISHNAKUMAR V

Co-Founder — DeepScopy (An AI-Based Medical Imaging Startup)

Connect with meLinkedIn, GitHub, TwitterMedium

https://deepscopy.com/

`

Visit us DeepScopy

Connect with usTwitter, LinkedIn, Medium


Visual Representation of Matrix and Vector Operations and implementation in NumPy, Torch, and Tensor was originally published in Towards AI — Multidisciplinary Science Journal on Medium, where people are continuing the conversation by highlighting and responding to this story.

Published via Towards AI

Feedback ↓