首页 > 解决方案 > Python Tensorflow tf.concat 在连接 2 个张量时引发奇怪的错误

问题描述

这是我想将 2 个张量(张量,array_tensor)连接到“new_tensor”中的代码,但是我返回了这个错误。

如果它有助于我在 Tensorflow 1.14.1-dev20190318 for gpu 上运行

    tensor = None
    array_tensor = None
    dummy_tensor = tf.convert_to_tensor(0.0,dtype=tf.float32)
    dummy_tensor_2 = tf.convert_to_tensor(0.0,dtype=tf.float32)
    dataset = tf.data.Dataset.from_tensors(tensors=(dummy_tensor,dummy_tensor_2))
    for row in float_validation:
        print(row[0])
        tensor = tf.convert_to_tensor(row[0], dtype=tf.float32)
        array_tensor = tf.convert_to_tensor(row[1],dtype=tf.float32)
        new_tensor = tf.concat([tensor,array_tensor],axis=2)
        # dataset = dataset.concatenate(tf.data.Dataset.from_tensors(tensors=(tensor,array_tensor)))
        dataset = dataset.concatenate(tf.data.Dataset.from_tensors(tensors=(new_tensor)))
  File "D:\School\Last_Year\thesis_actual\thesis\thesis_prototype\test.py", line 126, in <module>
    new_tensor = tf.concat([tensor,array_tensor],axis=2)
  File "D:\School\Last_Year\thesis_actual\thesis\thesis_prototype\venv\lib\site-packages\tensorflow\python\util\dispatch.py", line 180, in wrapper
    return target(*args, **kwargs)
  File "D:\School\Last_Year\thesis_actual\thesis\thesis_prototype\venv\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1271, in concat
    return gen_array_ops.concat_v2(values=values, axis=axis, name=name)
  File "D:\School\Last_Year\thesis_actual\thesis\thesis_prototype\venv\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 1378, in concat_v2
    _six.raise_from(_core._status_to_exception(e.code, message), None)
  File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: ConcatOp : Expected concatenating dimensions in the range [0, 0), but got 2 [Op:ConcatV2] name: concat```

标签: pythontensorflowmachine-learning

解决方案


推荐阅读