问题描述
我已成功使用 PCA 和以下页面上的“Sphereize data”选项:https://projector.tensorflow.org/
我想知道如何使用 TensorFlow API 在本地运行相同的计算。我找到了 PCA documentation in the API documentation,但我不确定 API 中是否也可以使用球形数据?
解决方法
“sphereize data”选项通过将每个点按质心移动并制作单位规范来规范化数据。
这是 code used in Tensorboard(在打字稿中):
normalize() {
// Compute the centroid of all data points.
let centroid = vector.centroid(this.points,(a) => a.vector);
if (centroid == null) {
throw Error('centroid should not be null');
}
// Shift all points by the centroid and make them unit norm.
for (let id = 0; id < this.points.length; ++id) {
let dataPoint = this.points[id];
dataPoint.vector = vector.sub(dataPoint.vector,centroid);
if (vector.norm2(dataPoint.vector) > 0) {
// If we take the unit norm of a vector of all 0s,we get a vector of
// all NaNs. We prevent that with a guard.
vector.unit(dataPoint.vector);
}
}
}
您可以使用以下 python 函数重现该规范化:
def sphereize_data(x):
"""
x is a 2D Tensor of shape :(num_vectors,dim_vectors)
"""
centroids = tf.reduce_mean(x,axis=0,keepdims=True)
return tf.math.div_no_nan((x - centroids),tf.norm(x - centroids,keepdims=True))