title: Can't convert Operation 'compute_qmap_grid/map/while/PrintV2' to Tensor
body: <p>I am trying to use tf.map_fn function to compute a series slices of a tensor with code below.</p>
  
  <pre><code>qab = tf.map_fn(
          fill_qab_slice,
          tf.range(n_var),
          dtype=tf.float64,
          parallel_iterations=multiprocessing.cpu_count())
  </code></pre>
  
  <p>where fill_qab_slice is a function to compute a slice with some given parameters.</p>
  
  <pre><code>def fill_qab_slice(idx):
    return _fill_qab_slice(idx, z1, z2, var_aa, corr_ab, nonlin_fn)
  </code></pre>
  
  <p>It returns a function like below.</p>
  
  <pre><code>def _fill_qab_slice(idx, z1, z2, var_aa, corr_ab, nonlin_fn):
    """Helper method used for parallel computation for full qab."""
    log_weights_ab_unnorm = -(z1**2 + z2**2 - 2 * z1 * z2 * corr_ab) / (
        2 * var_aa[idx] * (1 - corr_ab**2))
    log_weights_ab = log_weights_ab_unnorm - tf.reduce_logsumexp(
        log_weights_ab_unnorm, axis=[0, 1], keep_dims=True)
    weights_ab = tf.exp(log_weights_ab)
  
    qab_slice = tf.reduce_sum(
        nonlin_fn(z1) * nonlin_fn(z2) * weights_ab, axis=[0, 1])
    qab_slice = tf.print(qab_slice, [idx], "Generating slice: ")
    return qab_slice
  </code></pre>
  
  <p>In some servers, I got an error below. However, the code can run successfully in some other servers. So I believe the code should have no bug. But what may cause the error?</p>
  
  <pre><code>Traceback (most recent call last):
    File "/Users/empramsesii/Code/Gaussian/nngp/run_experiments.py", line 232, in &lt;module&gt;
      tf.app.run(main)
    File "/Users/empramsesii/Library/Python/3.6/lib/python/site-packages/tensorflow/python/platform/app.py", line 125, in run
      _sys.exit(main(argv))
    File "/Users/empramsesii/Code/Gaussian/nngp/run_experiments.py", line 228, in main
      run_nngp_eval(hparams, FLAGS.experiment_dir)
    File "/Users/empramsesii/Code/Gaussian/nngp/run_experiments.py", line 150, in run_nngp_eval
      use_fixed_point_norm=FLAGS.use_fixed_point_norm)
    File "/Users/empramsesii/Code/Gaussian/nngp/nngp.py", line 86, in __init__
      self.qab_grid) = self.get_grid(n_gauss, n_var, n_corr, max_var, max_gauss)
    File "/Users/empramsesii/Code/Gaussian/nngp/nngp.py", line 119, in get_grid
      max_var=max_var, max_gauss=max_gauss)
    File "/Users/empramsesii/Code/Gaussian/nngp/nngp.py", line 390, in _compute_qmap_grid
      parallel_iterations=multiprocessing.cpu_count())
    File "/Users/empramsesii/Library/Python/3.6/lib/python/site-packages/tensorflow/python/ops/functional_ops.py", line 499, in map_fn
      maximum_iterations=n)
    File "/Users/empramsesii/Library/Python/3.6/lib/python/site-packages/tensorflow/python/ops/control_flow_ops.py", line 3310, in while_loop
      return_same_structure)
    File "/Users/empramsesii/Library/Python/3.6/lib/python/site-packages/tensorflow/python/ops/control_flow_ops.py", line 3021, in BuildLoop
      pred, body, original_loop_vars, loop_vars, shape_invariants)
    File "/Users/empramsesii/Library/Python/3.6/lib/python/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2956, in _BuildLoop
      body_result = body(*packed_vars_for_body)
    File "/Users/empramsesii/Library/Python/3.6/lib/python/site-packages/tensorflow/python/ops/control_flow_ops.py", line 3279, in &lt;lambda&gt;
      body = lambda i, lv: (i + 1, orig_body(*lv))
    File "/Users/empramsesii/Library/Python/3.6/lib/python/site-packages/tensorflow/python/ops/functional_ops.py", line 491, in compute
      tas = [ta.write(i, value) for (ta, value) in zip(tas, flat_fn_values)]
    File "/Users/empramsesii/Library/Python/3.6/lib/python/site-packages/tensorflow/python/ops/functional_ops.py", line 491, in &lt;listcomp&gt;
      tas = [ta.write(i, value) for (ta, value) in zip(tas, flat_fn_values)]
    File "/Users/empramsesii/Library/Python/3.6/lib/python/site-packages/tensorflow/python/util/tf_should_use.py", line 189, in wrapped
      return _add_should_use_warning(fn(*args, **kwargs))
    File "/Users/empramsesii/Library/Python/3.6/lib/python/site-packages/tensorflow/python/ops/tensor_array_ops.py", line 842, in write
      return self._implementation.write(index, value, name=name)
    File "/Users/empramsesii/Library/Python/3.6/lib/python/site-packages/tensorflow/python/util/tf_should_use.py", line 189, in wrapped
      return _add_should_use_warning(fn(*args, **kwargs))
    File "/Users/empramsesii/Library/Python/3.6/lib/python/site-packages/tensorflow/python/ops/tensor_array_ops.py", line 267, in write
      value = ops.convert_to_tensor(value, name="value")
    File "/Users/empramsesii/Library/Python/3.6/lib/python/site-packages/tensorflow/python/framework/ops.py", line 1059, in convert_to_tensor
      as_ref=False)
    File "/Users/empramsesii/Library/Python/3.6/lib/python/site-packages/tensorflow/python/framework/ops.py", line 1155, in internal_convert_to_tensor
      ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
    File "/Users/empramsesii/Library/Python/3.6/lib/python/site-packages/tensorflow/python/framework/ops.py", line 6185, in _operation_conversion_error
      name, as_ref))
  TypeError: Can't convert Operation 'compute_qmap_grid/map/while/PrintV2' to Tensor (target dtype=None, name='value', as_ref=False)
  </code></pre>
  
  <p>Addition
  For all servers, I use python 3.6.7 and Tensorflow 1.12 without GPU.</p>