Tensorflow basics : Matrix operations


Matrix multiplication is probably is mostly used operation in machine learning, becase all images, sounds, etc are represented in matrixes.

There there are 2 types of multiplication:

Element-wise multiplication : tf.multiply

Element-wise multiplication in TensorFlow is performed using two tensors with identical shapes. This is because the operation multiplies elements in corresponding positions in the two tensors. An example of an element-wise multiplication, denoted by the symbol, is shown below:

Screenshot 2020-02-13 at 12.05.49.png

import tensorflow as tf

A1 = tf.constant([1, 2, 3, 4])
B1 = tf.constant([3, 4, 5, 5])
C1 = tf.multiply(A1, B1)

# C1 = <tf.Tensor: id=2, shape=(4,), dtype=int32, numpy=array([ 3,  8, 15, 20], dtype=int32)>

Matrix multiplication : tf.matmul

import tensorflow as tf

A1 = tf.constant([[2, 24], [2, 26], [2, 57]])
B1 = tf.constant([[1000], [150]])
C1 = tf.matmul(A1, B1)

# C1 = <tf.Tensor: id=5, shape=(3, 1), dtype=int32, numpy=
array([[ 5600],
       [ 5900],
       [10550]], dtype=int32)>

Matrix add : tf.add

Note, that matrixes should be exactly the same shape

import tensorflow as tf

A1 = tf.constant([1,2,3])
B1 = tf.constant([1,2,3])
C1 = tf.add(A1, B1)

# C1 = <tf.Tensor: id=20, shape=(3,), dtype=int32, numpy=array([2, 4, 6], dtype=int32)>

Matrix sum by dimension : tf.reduce_sum()

This operator just sum all elements of the matrix or specific row or column

import tensorflow as tf

A1 = tf.Variable([[1,2,3],[3,2,1], [3,3,3]])
B2 = tf.reduce_sum(A1)
# B2 <tf.Tensor: id=34, shape=(), dtype=int32, numpy=21>

B3 = tf.reduce_sum(A1, 0)
#B3 <tf.Tensor: id=37, shape=(3,), dtype=int32, numpy=array([7, 7, 7], dtype=int32)>

B4 = tf.reduce_sum(A1, 1)
#B4 <tf.Tensor: id=46, shape=(3,), dtype=int32, numpy=array([6, 6, 9], dtype=int32)>




Reference: Source link

Sr. SDET M Mehedi Zaman

Currently working as Sr. SDET at Robi Axiata Limited, a subsidiary of Axiata Group. As a Senior SDET: - Played a key role in introducing Agile Scrum methodology and implementing CI/CD pipeline to ensure quality & timely delivery. - Trained colleagues on emerging technologies, e.g. Apache Spark, Big Data, Hadoop, Internet of Things, Cloud Computing, AR, Video Streaming Services Technology, Blockchain, Data Science- Developed a test automation framework for Android and iOS apps - Developed an e2e web automation framework with Pytest - Performed penetration testing of enterprise solutions to ensure security and high availability using Kali, Burp Suite etc. - Learned Gauntlet security testing automation framework and shared the lesson learned in a knowledge sharing session

Leave a Reply

Your email address will not be published. Required fields are marked *