tensorflow / tensorflow/java

Variables inside a graph are mutable if you fetch them

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

还没有人认领这个 Issue。

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

描述

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): macOS, Oracle Linux 7.
  • TensorFlow installed from (source or binary): binary
  • TensorFlow version (use command below): v0.3.1

Describe the current behavior

This test fails as initial = 3.0f and after = 9.0f.

  public void mutableVariablesTest() {
    try (Graph g = new Graph();
         Session s = new Session(g);
         TFloat32 inputTensor = TFloat32.vectorOf(1.0f,1.0f,1.0f)) {
      Ops tf = Ops.create(g);

      Placeholder<TFloat32> input =
              tf.withName("input").placeholder(TFloat32.class, Placeholder.shape(Shape.of(3)));
      Variable<TFloat32> a = tf.variable(tf.constant(new float[]{1.0f,1.0f,1.0f}));

      ReduceSum<TFloat32> output = tf.withName("output").reduceSum(tf.math.mul(a,input),tf.constant(0));

      Init init = tf.init();

      s.run(init);

      Tensor t = s.runner().feed(input,inputTensor).fetch(output).run().get(0);
      float initial = ((TFloat32)t).getFloat();
      t.close();

      TFloat32 aTensor = (TFloat32) s.runner().fetch(a).run().get(0);
      aTensor.setFloat(3.0f,0);
      aTensor.setFloat(3.0f,1);
      aTensor.setFloat(3.0f,2);
      aTensor.close();

      t = s.runner().feed(input,inputTensor).fetch(output).run().get(0);
      float after = ((TFloat32)t).getFloat();
      t.close();

      assertEquals(initial,after);
    }
  }

It fails when running on CPU on macOS and Linux. I checked on Linux on a GPU and the test passes.

Describe the expected behavior

The test should pass consistently across all backends, as I would expect to get a copy of the weights back rather than something that lets me directly mutate the state of the graph in all cases rather than just on GPU. At the very least it should fail consistently on both CPU and GPU, but I think we should disallow direct mutation of variables outside of a graph.

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 issue 中的 mutableVariablesTest 示例开始,并在其中描述的 CPU 和 GPU 后端上复现该示例。跟踪获取到的 Variable 张量是如何处理的,然后验证获取操作不会意外改变图状态,并且各后端的行为保持一致。

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

评估

技术栈
java
领域
machine-learning
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

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