tensorflow - 如何在 TensorFlow 中使用带有 PCA 的 'sphereize data' 选项

标签 tensorflow pca dimensionality-reduction projector

我已在以下页面上成功地将 PCA 与“Sphereize 数据”选项结合使用:https://projector.tensorflow.org/

我想知道如何使用 TensorFlow API 在本地运行相同的计算。我找到了 PCA documentation in the API documentation ,但我不确定是否也可以在 API 的某处对数据进行球形化?

最佳答案

“sphereize data”选项通过将每个点移动质心并使单位规范化来规范化数据。

这是 code used in Tensorboard ( typescript ):

  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))

关于tensorflow - 如何在 TensorFlow 中使用带有 PCA 的 'sphereize data' 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68304309/

相关文章:

machine-learning - 我们什么时候应该使用主成分分析?

python - Keras 的 MSE 损失函数显示与 Tensorflow 的 MSE 指标不同的输出?

r - PCA空间和 'feature-space'发散中的质心距离计算

r - 使用 ggplot2 自定义纯素 PCA 图

r - Factoextra - 更改椭圆和变量的线宽

python - LDA 忽略 n_components?

performance - Tensorflow:高效的多项式采样(Theano x50 更快?)

python - 加载 TensorFlow 后派生一个 python 进程

python - tensorflow 如何使用 tf.map_fn 批量处理 (?, 40,30,128) 和 (?,40,30) 的两个输入张量形状,?是我的批量大小

matlab - 通过 PCA 对 2D 图像进行降维