首页 > 解决方案 > 将函数映射到 TensorFlow 中的部分张量

问题描述

在 TensorFlow 架构中,如何将函数仅应用于张量中的某些元素?例如,在层的最终输出中,一些变量表示我想通过 sigmoid 运行的像素密度,而张量开头的一些变量表示无界的连续值,我不想应用激活完全发挥作用。

这是我正在尝试的:

    output_activation=tf.nn.sigmoid
    x = tf.layers.dense(z, hidden_size, activation=hidden_activation)
    x = tf.layers.dense(x, hidden_size, activation=hidden_activation)
    _logits = tf.layers.dense(x, output_size, activation=None)
    # Apply sigmoid only to image variables
    logits = tf.concat(_logits[:functional_inputs], tf.map_fn(output_activation, _logits[functional_inputs:]), 0)

这给出了以下值错误:

ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("build_decoder/map/TensorArrayStack/TensorArrayGatherV3:0", shape=(?, 288755), dtype=float32)'

这是最好的方法吗?

标签: pythontensorflow

解决方案


推荐阅读