Multiply Matrix By Vector Tensorflow
If youre familiar with numpy tensors are kind of like nparrays.
Multiply matrix by vector tensorflow. X tfconstant 10 00 00 10 y tfmultiplyx 20 sess tfsession print sessruny 20 00 00 20. Result tfmula b v sessrunresult printv. Here is code that will generate two matrices of dimensions 30000020000 and multiply them. Given two vectors x x1xn and y y1yn the dot product is dot xy x1 y1.
Schematic of the computations of the matrices on line 8 in the code example above the non linear transform arctan is omitted. You can never update the contents of a tensor only create a new one. Tensors are the core datastructure of tensorflowjs they are a generalization of vectors and matrices to potentially higher dimensions. The first matrix will be a tensorflow tensor shaped 3x3 with min values of 1 max values of 10 and the data type will be int32.
You can multiply a matrix or any other tensor by a scalar using the element wise tfmultiply operation which implicitly broadcasts its arguments to match sizes. In this video were going to multiply two matrices by using tfmatmul operation. Here expresses a matrix composed of example vectors raised to the consecutive powers. Or in a more compact form.
All tensors are immutable like python numbers and strings. Import tensorflow as tf import numpy as np tensors are multi dimensional arrays with a uniform type called a dtypeyou can see all supported dtypes at tfdtypesdtype. Tensors creation we have utility functions for common cases like scalar 1d 2d 3d and 4d tensors as well a number of functions to initialize tensors in ways useful for machine learning. The addition of the bias b is broadcasted on all samples in the batch.
By concatenating those two tensors you will only use one matrix multiplication. However the result is a matrix and i am after a scalar. Xn yn i know that it is possible to achieve this by first broadcasting the vectors x and y to a 2 d tensor and then using tfmatmul. If we organize all into a vector of examples we can express the formula above using vector matrix multiplication.