【问题标题】:Tensorflow 2.0 , replace 0 values in a tensor with 1sTensorflow 2.0,用 1s 替换张量中的 0 值
【发布时间】:2019-08-23 16:00:14
【问题描述】:

所以我正在尝试使用 tensorflow 实现居中和缩放,我需要将张量中的 values==0 替换为 1.0。

我知道用 numpy x_std[x_std == 0.0] = 1.0 做到这一点,但无法找出在 tensorflow 2.0 上做到这一点的最佳方法。

代码是:

def _center_scale_xy(X, Y, scale=True):
    """ Center X, Y and scale if the scale parameter==True
    Returns
    -------
        X, Y, x_mean, y_mean, x_std, y_std
    """
    # center
    x_mean = tf.reduce_mean(X,axis=0)
    X -= x_mean
    y_mean = tf.reduce_mean(Y,axis=0)
    Y -= y_mean
    # scale
    if scale:
        x_std = tf.math.reduce_std(X,axis=0)
        #x_std[x_std == 0.0] = 1.0 #This one I need to implement with tensors
        X /= x_std
        y_std = tf.math.reduce_std(Y,axis=0)
        y_std[y_std == 0.0] = 1.0
        Y /= y_std
    else:
        x_std = np.ones(X.shape[1])
        y_std = np.ones(Y.shape[1])
    return X, Y, x_mean, y_mean, x_std, y_std

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    像这样使用tf.where:

    x_std = tf.where(tf.equal(x_std, 0), tf.ones_like(x_std), x_std)
    

    【讨论】:

      猜你喜欢
      • 2017-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多