simusid
simusid

Reputation: 1904

Calculate Standard Deviation in TensorflowJS?

I'm writing my first TFJS demo app. I have a well trained cats/dogs binary classifier that I've converted using the converter. I can load the model and pass images from an iphone or android camera for classification. Unfortunately, the results are terrible. My model was trained using a keras ImageDataGenerator with samplewise_center and samplewise_std_normalization both set to true. I'm now pretty sure I need to do the same normalization in my app.

I see tf.math.reduce_mean() and reduce_std() in the full API. In the JS API I see ts.mean() but no ts.std(). Do I need to calculate std from scratch?

Upvotes: 4

Views: 1050

Answers (1)

edkeveked
edkeveked

Reputation: 18401

There is not yet an operator to compute the standard deviation in the js api. It can either be computed from scratch or use tf.moments to get the variance and then compute the standard deviation usinng the square root.

const a = tf.tensor1d([1, 2, 3]);

tf.moments(a).variance.print()
tf.moments(a).variance.sqrt().print()

Upvotes: 5

Related Questions