# Deep Learning Array Broadcasting

# – one-dimensional array adding to n-dimensional array

## “Broadcasting” in Numpy

# "broadcasting" in numpy # Note: the following code is running on Python3 import numpy as np a = np.array([[0,1,2,3,4], [5,6,7,8,9]]) # two-dimensional array b = np.ones(5) # one-dimensional array, b= [ 1. 1. 1. 1. 1.] print ('a=\n', a) print ('b=', b) # b= [ 1. 1. 1. 1. 1.] print ('a + b =\n', a+b) # b will add to each line of a, this is called "broadcasting" # a= # [[0 1 2 3 4] # [5 6 7 8 9]] # b= [ 1. 1. 1. 1. 1.] # # a + b = # [[ 1. 2. 3. 4. 5.] # [ 6. 7. 8. 9. 10.]]

## “Broadcasting” in Tensorflow

# "broadcasting" in deep learning (Tensorflow) # Note: the following code is running on Python3 import tensorflow as tf a = tf.constant([[0,1,2,3,4], [5,6,7,8,9]]) # two-dimensional array b = tf.ones(5, tf.int32) # one-dimensional array, b= [1 1 1 1 1] with tf.Session() as sess: print ('a=\n', sess.run(a)) print ('b=', sess.run(b)) # b= [1 1 1 1 1] print ('a + b =\n', sess.run(a+b)) # b will add to each line of a, this is called "broadcasting" # a= # [[0 1 2 3 4] # [5 6 7 8 9]] # b= [1 1 1 1 1] # # a + b = # [[ 1 2 3 4 5] # [ 6 7 8 9 10]]

## Leave a Reply

## Your email is safe with us.