如何在 TensorFlow 中使用 PCA 的 'sphereize data' 选项
How to use the 'sphereize data' option with PCA in TensorFlow
我在以下页面上成功地将 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, axis=0, keepdims=True))
我在以下页面上成功地将 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, axis=0, keepdims=True))