【问题标题】:how to calculate component of array while using tensorlflow如何在使用张量流时计算数组的分量
【发布时间】:2019-03-21 11:50:56
【问题描述】:

从我的网络中,我可以在网络中间得到一个 shape(21, 3) 的张量。 在将张量提供给下一个网络之前,我需要计算它。 例如,我需要用张量中的特定值减去每个分量。 如果
tensor[10, 0] = x, tensor[10, 1] = y, tensor[10, 2] = z tensor[:, 0] = tensor[:, 0] - x tensor[:, 1] = tensor[:, 1] - y tensor[:, 2] = tensor[:, 2] - z的值

还需要像这样规范化它

tensor[10, 0] - tensor[0, 0] = a, tensor[10, 1] - tensor[0, 1] = b, tensor[10, 2] - tensor[0, 2] = c tensor = tensor / sqrt(a^2 + b^2 + c^2)

有什么方法可以计算张量并将其再次提供给网络的其余部分?

【问题讨论】:

    标签: python arrays tensorflow keras


    【解决方案1】:

    您可以计算一个形状为 [1,3] 的张量,其值为 [tensor[10,0], tensor[10,1], tensor[10,2]],如下所示:(还有其他方法)

    s = tf.concat([tf.reshape(tensor[10,0], shape=[1,1]), tf.reshape(tensor[10,1], shape=[1,1]), tf.reshape(tensor[10,2], shape=[1,1])], axis=1)
    

    现在您可以从tensor 中减去s

    tensor2 = tensor - s
    

    您将使用广播将值更新为tensor[:, 0] = tensor[:, 0] - x 等。

    对于归一化,你可以直接计算如下:

    tensor3 = tensor2 / tf.sqrt(tf.square(tensor[10,0] - tensor[0,0]) + tf.square(tensor[10,1] - tensor[0,1]) + tf.square(tensor[10,2] - tensor[0,2]))
    

    【讨论】:

    • 感谢您的回答!但是你知道同样的方法但是如果我有批量张量 (32, 21, 3) 怎么办?
    • 我用'for'状态解决了这个问题!如果有更好的方法,我很乐意告诉我
    猜你喜欢
    • 2017-01-19
    • 1970-01-01
    • 2021-06-14
    • 2019-07-13
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多