What would be the best way to multiply a matrix by a scalar in TensorFlow? I simply want to scale up the matrix by some scalar value.
Thanks!
You can use tf. math. scalar_mul which will take the scalar value as first parameter and the tensor as the second one. It is not recommended to use numpy operations within complicated operations in case you are using Tensorflow on Keras for models training.
Numpy multiply array by scalar In order to multiply array by scalar in python, you can use np. multiply() method.
You can multiply a matrix (or any other tensor) by a scalar using the element-wise tf.multiply()
operation, which implicitly broadcasts its arguments to match sizes:
x = tf.constant([[1.0, 0.0], [0.0, 1.0]])
y = tf.multiply(x, 2.0)
sess = tf.Session()
print sess.run(y)
# ==> [[2.0, 0.0], [0.0, 2.0]]
very simple:
scalar * matrix
TensorFlow converts that to tf.multiply and broadcasts everything.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With