Tensorflow 2.0,将张量中的0值替换为1s

海梅(Jaime Ferrando Huertas)

所以我试图用张量流实现居中和缩放,我需要用1.0替换张量中的value == 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
Jdehesa

tf.where像这样使用

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

根据列将多个无标题列中的值替换为0、1、2

Tensorflow:将张量中的所有值替换为最小值或最大值

MNIST Tensorflow:如何将形式[i]的张量转换为形式[... 0,0,0,1,0,0 ...]的张量,其中1在第i个位置?

如何在TensorFlow 2中获得Keras张量的值?

将Tensorflow 1.5转换为Tensorflow 2

使用1d张量将Tensorflow索引为2d张量

Tensorflow JS将1d张量转换为2d张量构造为对角线正方形矩阵

TensorFlow:将GRUCell权重从compat.v1转换为tensorflow 2

Tensorflow,如何将2D张量(矩阵)与1D向量中的相应元素相乘

为什么TensorFlow 2比TensorFlow 1慢得多?

如何在Tensorflow中更新2D张量的子集?

TensorFlow - 3D 张量,从 2D 张量和步幅 1 中收集每个第 N 个张量

替换Tensorflow v2的占位符

将tensorflow 1.xx模型加载到tensorflow 2.xx中

Tensorflow 2:将3D张量编码为2D张量

Tensorflow 1和2语法差异

在Tensorflow 2中导入WAV文件

如何在TensorFlow 2中写入TensorBoard

Tensorflow 2中的执行急切

在tensorflow 2中加载ModelCheckpoint

在TensorFlow中实施im2col

tensorflow v2中的变量分配

Tensorflow 2 中的 Model.fit() 输出

在 Tensorflow 2 中定义自己的 pad

Tensorflow替换张量中接近零的值

在tensorflow python中替换值或创建张量掩码

ValueError:无法将 NumPy 数组转换为张量(不支持的对象类型 int)。Tensorflow 1.15 运行。Tensorflow 2.x 中断

将张量中的所有索引替换为1s

如何在 TensorFlow2 中写出这个方程?(4x+2 = 0)