G. Debailly
G. Debailly

Reputation: 1331

Scalar multiplication of tensors

In TensorFlow Core for Python there is an operation called tf.math.scalar_mul.

I would like to scale tensors in TensorFlow.js. By trying for instance a * 0.1 I get an error message (at least from Typescript):The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.ts(2362).

Is scaling tensors applicable without making them arrays, scale elementwise then transform back to tensors?

Upvotes: 3

Views: 896

Answers (2)

edkeveked
edkeveked

Reputation: 18401

Though, tf.scalar can be used, one can also use direclty tensor.mul(number) like the following

tf.tensor([1, 2, 3, 4]).mul(5).print(); // [5, 10, 15, 20]

Upvotes: 3

G. Debailly
G. Debailly

Reputation: 1331

I found the answer in the API documentation. For multiplying a tensor a with 5, just a.mul(tf.scalar(5)).

Upvotes: 0

Related Questions