tensorflow / tensorflow/java

Exception in thread "main" org.tensorflow.exceptions.TensorFlowException: No gradient defined for op: SelectV2.

未关闭
#437 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Java
星标
928
派生
227
PR 合并指标
30 天内没有已合并 PR

描述

env :
tensorflow-java : 0.5.0-SNAPSHOT
scala :2.12.11
java : jdk 15
Hi :
when I defined a tf Variable named embeddings ,and want to do some operation with it, invoke the callone method

def clipByNormTF[T <: TNumber](someNums: Operand[T], clipNormBias: Operand[T], axis: Operand[TInt32], tf: Ops): Output[TFloat32] = {
    try {

      val l2sum = tf.reduceSum(tf.math.square(tf.dtypes.cast(someNums, classOf[TFloat64])), axis, ReduceSum.keepDims(true))
      val pred = tf.math.greater(l2sum, tf.constant(0.0))
      println("Here will crash !!! please ")
      val l2sumSafe = tf.select(pred, l2sum, tf.onesLike(l2sum))
      val l2norm = tf.select(pred, tf.math.sqrt(l2sumSafe), l2sum)

      val intermediate = tf.math.mul(tf.dtypes.cast(someNums, classOf[TFloat64]), tf.dtypes.cast(clipNormBias, classOf[TFloat64]))
      val valuesClip = tf.identity(tf.math.divNoNan(intermediate, tf.math.maximum(l2norm, tf.dtypes.cast(clipNormBias, classOf[TFloat64]))))
      if (someNums.shape().isCompatibleWith(intermediate.shape())) {
        println(s"match the shape ${someNums.shape().get(0)} ${someNums.shape().get(1)}  || ${intermediate.shape().get(0)} ${intermediate.shape().get(1)}")

        tf.dtypes.cast(valuesClip, classOf[TFloat32]).asOutput()
      } else {
        println(s"can not match the shape ${someNums.shape().get(0)} ${someNums.shape().get(1)}  || ${intermediate.shape().get(0)} ${intermediate.shape().get(1)}")
        throw new Exception("bad shape")
      }
    }

  def callOne(tf: Ops, input: Operand[T]): Operand[TFloat32] = {
    println(s"Embedding Layer ${this.layerName} input compute shape ${input.shape().asArray().mkString("|")}")
    val inputZ = tf.dtypes.cast(input, classOf[TInt32])
    val gatherResult = tf.gather(this.embeddings, inputZ, tf.constant(0))
    val gr = tf.dtypes.cast(gatherResult, classOf[TFloat32])
    val result = clipByNormTF(gr, tf.constant(this.maxNormBias), tf.constant(Array(1, 0)), tf)
    println(s"Embedding Layer call result shape  ${this.layerName}  compute output shape ${result.shape().asArray().mkString("|")}")
    result
  }


meet to error console

Exception in thread "main" org.tensorflow.exceptions.TensorFlowException: No gradient defined for op: SelectV2. Please see https://www.tensorflow.org/code/tensorflow/cc/gradients/README.md for instructions on how to add C++ gradients.
	at org.tensorflow.internal.c_api.AbstractTF_Status.throwExceptionIfNotOK(AbstractTF_Status.java:101)
	at org.tensorflow.Graph.addGradients(Graph.java:1152)
	at org.tensorflow.Graph.addGradients(Graph.java:703)
	at org.tensorflow.op.core.Gradients.create(Gradients.java:104)
	at org.tensorflow.op.core.Gradients.create(Gradients.java:125)
	at org.tensorflow.op.Ops.gradients(Ops.java:2814)

I do not know why , feel appreciate help me ,thanks

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从提供的 clipByNormTF 和 callOne 代码以及经过 Graph.addGradients、Gradients.create 和 Ops.gradients 的堆栈跟踪开始。使用 tensorflow-java 0.5.0-SNAPSHOT、Scala 2.12.11 和 JDK 15 重现 SelectV2 失败;验证梯度行为或适当的修复后即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
java, scala
领域
machine-learning
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
需要澄清
新手友好度
30/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。