无法将运算'compute_qmap_grid / map / while / PrintV2'转换为Tensor

时间:2018-12-11 05:24:44

标签: python python-3.x tensorflow

我正在尝试使用tf.map_fn函数通过以下代码计算张量的一系列切片。

qab = tf.map_fn(
        fill_qab_slice,
        tf.range(n_var),
        dtype=tf.float64,
        parallel_iterations=multiprocessing.cpu_count())

其中fill_qab_slice是用于计算具有某些给定参数的切片的函数。

def fill_qab_slice(idx):
  return _fill_qab_slice(idx, z1, z2, var_aa, corr_ab, nonlin_fn)

它返回如下函数。

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

在某些服务器中,下面出现错误。但是,该代码可以在其他一些服务器上成功运行。因此,我认为代码应该没有错误。但是什么原因可能导致错误?

Traceback (most recent call last):
  File "/Users/empramsesii/Code/Gaussian/nngp/run_experiments.py", line 232, in <module>
    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 <lambda>
    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 <listcomp>
    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)

加法 对于所有服务器,我使用不带GPU的python 3.6.7和Tensorflow 1.12。

0 个答案:

没有答案