tensorflow / tensorflow/java

How to update the weight about BlockLSTM ?

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

还没有人认领这个 Issue。

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

描述

HI :

now I use BlockLSTM for build lstm layer ,but I don't know how to update the lstm weight parameters , if need use blockLSTMGrad or something to do ,the coda is paste here:

object LstmExample {

  def initializeTruncatedNormalTensor(shape: Operand[TInt32], scope: Scope): TFloat32 = {
    TruncatedNormal.seed(1000L)
    //        TruncatedNormal<TFloat32> truncatedNormal = TruncatedNormal.create(scope, shape, TFloat32.DTYPE);
    //        DataType<TFloat32> DTYPE = DataType.create("FLOAT", 1, 4, TFloat32Impl::mapTensor);
    //        DataType DTYPE = DataType.valueOf("FLOAT");
    val truncatedNormal: TruncatedNormal[TFloat32] = TruncatedNormal.create(scope, shape, classOf[TFloat32])
    return truncatedNormal.asTensor
  }
  private def getWeightMatrix(shape: Operand[TInt32], scope: Scope) = { //        Tensor<TFloat32> tensorWeight = TensorValues.initializeTruncatedNormalTensor(shape, scope);
    val tensorWeight = TensorValues.initializeTruncatedNormalTensor(shape, scope)
    Constant.create(scope, tensorWeight)
  }

  def printTensor(tensor: Operand[TFloat32],name:String): Unit ={
    val data: Array[Float] = TensorResources.extractFloats(tensor.asTensor())
    println(s"data:${name},  ${data.mkString(" | ")}")
  }
  def main(args: Array[String]): Unit = {
    val libraryPath = System.getProperty("java.library.path")
    System.out.println(libraryPath)
    implicit val session = TestSession.createTestSession(TestSession.Mode.EAGER) // EagerSession.create()
    implicit val tf = session.getTF // Ops.create(session).withName("test")
    implicit val scope = tf.scope()
    //    val session = EagerSession.create
    //    val tf = Ops.create(session)
    //        Scope scope = new Scope(session);
    //    val scope = session.baseScope()
    val rawInputSequence = Array(Array(Array(0.1f, 0.2f)), Array(Array(0.3f, 0.4f))) //shape (timelen, batch_size, num_inputs).
    val inputSequence = tf.constant(rawInputSequence)
    val inputSize = 2
    val cellSize =  5
    val maximumTimeLength = 2
    val cellShape = Array(1, cellSize)
    val cellDims = Constant.vectorOf(scope, cellShape)
    val seqLenMax = tf.array(maximumTimeLength)
    //        Operand<TFloat32> initialCellState = Zeros.create(scope, cellDims, TFloat32.DTYPE);
    //        Operand<TFloat32> initialHiddenState = Zeros.create(scope, cellDims, TFloat32.DTYPE);
    val initialCellState = Zeros.create(scope, cellDims, classOf[TFloat32])
    val initialHiddenState = Zeros.create(scope, cellDims, classOf[TFloat32])
    val weightShape = Array(inputSize + cellSize, cellSize * 4)
    val weightMatrixDims = Constant.vectorOf(scope, weightShape)
    val weightMatrix = getWeightMatrix(weightMatrixDims, scope)
    //    session.print(weightMatrix)
    val weightGatesShape = Array(cellSize)
    val weightGatesDims = Constant.vectorOf(scope, weightGatesShape)
    val weightInputGate = getWeightMatrix(weightGatesDims, scope)
    printTensor(weightInputGate,"weightInputGate")
    val weightForgetGate = getWeightMatrix(weightGatesDims, scope)
    printTensor(weightForgetGate,"weightForgetGate")
    val weightOutputGate = getWeightMatrix(weightGatesDims, scope)
    printTensor(weightOutputGate ,"weightOutputGate")
    val biasShape = Array(cellSize * 4)
    val biasDim = Constant.vectorOf(scope, biasShape)
    //classOf[TFloat32]
    //        Operand<TFloat32> bias = Zeros.create(scope, biasDim, TFloat32.DTYPE);
    val bias = Zeros.create(scope, biasDim, classOf[TFloat32])
    val blockLSTM = BlockLSTM.create(scope, tf.dtypes.cast(seqLenMax, classOf[TInt64]), inputSequence, initialCellState, initialHiddenState, weightMatrix, weightInputGate, weightForgetGate, weightOutputGate, bias)

//    session.print(blockLSTM.i)
//    println("&&&cs")
//    session.print(blockLSTM.cs)
//    println("&&&f")
//    session.print(blockLSTM.f)
//    println("&&&o")
//    session.print(blockLSTM.o)
//    println("&&&ci")
//    session.print(blockLSTM.ci)
//    println("&&&co")
//    session.print(blockLSTM.co)
//    println("&&&h")
//    session.print(blockLSTM.h)

thanks for your help

贡献指南

打开贡献指南

从这里开始

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

调研方向

从提供的 Scala 示例和 BlockLSTM.create 调用开始,然后检查 Java API 中是否有 BlockLSTMGrad 或其他梯度运算。 在 TensorFlow Java 环境中运行该示例,并确定公开的 API 是否支持更新所显示的权重张量。支持的权重更新工作流已记录,或 API 限制已明确说明,即表示完成。

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

评估

技术栈
java, scala, tensorflow
领域
machine-learning
Issue 类型
文档
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
20/100

把新 issue 发到你的邮箱

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