Aniket Bote
Aniket Bote

Reputation: 3574

Vectorization using tensors Tensorflow

I have tensor x of shape (10,2)
I have tensor y of shape (10,)
I have tensor y_pred of shape (10,)

I want to compute a tensor dw of shape (2,1) using the formula as follows.

Let dw1 and dw2 be the elements in tensor dw.
dw1 = tf.reduce_sum(x1 * (y - y_pred)) * (-2/n)
where ,
x1 = 1st column of tensor x
n = scalar

dw2 = tf.reduce_sum(x2 * (y - y_pred)) * (-2/n)
where ,
x2 = 2nd column of tensor x
n = scalar

However, tensor x will have a dynamic shape.
If tensor x has a shape (10,3)
dw will have shape of (3,1)

In ML perspective I am calculating gradients of the loss function w.r.t weights.
The loss function is MSE.

I know how to implement this using for loop.
But I don't understand how I should implement it using Vectorization ie. Not using any for loops

Upvotes: 0

Views: 233

Answers (1)

tornikeo
tornikeo

Reputation: 938


Try the plain old matrix multiplication:

x = tf.ones((10,2), dtype=tf.float32)
y = tf.ones((10,1), dtype=tf.float32) * 2
y_pred = tf.ones((10,1), dtype=tf.float32) * 3
n = 3

result = tf.matmul(tf.transpose(x), y-y_pred) / (-n/2)

Explanation:

tf.ones creates a matrix for demonstration purposes. First argument marks it's shape. the x has shape of 10 by 2, to multiply that by a matrix of shape 10 by 1, you need to transpose x.
Then you just do the matrix multiplication that does exactly what you need.

Note that dtype=tf.float32 is necessary, without it you might get a casting error.

Upvotes: 1

Related Questions