将张量流变量连接成一个张量

时间:2020-04-16 15:28:30

标签: python python-3.x tensorflow

我试图将两个tensorflow变量(它们最初是程序中的张量)连接到一个张量中,以用作LSTM的标签输入。由于这些变量的数据类型,我在执行此操作时遇到问题。可以在tensorflow中连接这种格式的数据类型吗?如果没有,有没有解决的办法?

我采用这种方法的原因是,我可以用分解文档的数字数据(我使用TF-IDF处理过的数值数据)替换每个文件(文件名)的文本分类数据。

这是我的某些数据格式的打印输出,该数据存储在我用来替换y张量的python字典(data_chart)中:

{'ACAM2000': (<tf.Tensor: shape=(36522,), dtype=float64, numpy=
array([2.96672401e-05, 1.16349841e-05, 7.28487958e-05, ...,
'Flublok': (<tf.Tensor: shape=(36522,), dtype=float64, numpy=
array([4.71040407e-05, 1.84733990e-05, 1.15665381e-04, ...,
                  nan,            nan,            nan])>, <tf.Tensor: shape=(), dtype=string, numpy=b'Flublok'>), 'Flucelvax': (<tf.Tensor: shape=(36522,), dtype=float64, numpy=
array([4.43845883e-05, 1.74068763e-05, 1.08987684e-04, ...,

这是y张量的打印输出(我现在只有两个元素,但是我打算稍后再添加,这些元素的名称与上面python字典中存储的数据的名称直接相关):< / p>

tf.Tensor(
[[b'Gardasil']
 [b'mmr']], shape=(2, 1), dtype=string)

在这部分代码中,我尝试遍历每个文件名,然后通过将数据转换为TF变量将其替换为该数据。

existing_data = None
for element in y:
    variableA = tf.Variable(element, name="variableA")
    for k in data_chart.keys():
        while variableA == k:
            point = data_chart.get(k)
            existing_data = tf.concat([point, existing_data], 0) 
            print(point)
            break

那段代码给了我这个错误:


---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-18-46e6c7720921> in <module>()
      7             #print(variableA)
      8             point = data_chart.get(k)
----> 9             existing_data = tf.concat([point, existing_data], 0)
     10             print(point)
     11             #tf.strings.join( [existing_data, point], separator='', name=None)

/home/lily/.local/lib/python3.6/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
    178     """Call target, and fall back on dispatchers if there is a TypeError."""
    179     try:
--> 180       return target(*args, **kwargs)
    181     except (TypeError, ValueError):
    182       # Note: convert_to_eager_tensor currently raises a ValueError, not a

/home/lily/.local/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py in concat(values, axis, name)
   1596           dtype=dtypes.int32).get_shape().assert_has_rank(0)
   1597       return identity(values[0], name=name)
-> 1598   return gen_array_ops.concat_v2(values=values, axis=axis, name=name)
   1599 
   1600 

/home/lily/.local/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py in concat_v2(values, axis, name)
   1175       try:
   1176         return concat_v2_eager_fallback(
-> 1177             values, axis, name=name, ctx=_ctx)
   1178       except _core._SymbolicException:
   1179         pass  # Add nodes to the TensorFlow graph.

/home/lily/.local/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py in concat_v2_eager_fallback(values, axis, name, ctx)
   1207         "'concat_v2' Op, not %r." % values)
   1208   _attr_N = len(values)
-> 1209   _attr_T, values = _execute.args_to_matching_eager(list(values), ctx)
   1210   _attr_Tidx, (axis,) = _execute.args_to_matching_eager([axis], ctx, _dtypes.int32)
   1211   _inputs_flat = list(values) + [axis]

/home/lily/.local/lib/python3.6/site-packages/tensorflow/python/eager/execute.py in args_to_matching_eager(l, ctx, default_dtype)
    261       ret.append(
    262           ops.convert_to_tensor(
--> 263               t, dtype, preferred_dtype=default_dtype, ctx=ctx))
    264       if dtype is None:
    265         dtype = ret[-1].dtype

/home/lily/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
   1339 
   1340     if ret is None:
-> 1341       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
   1342 
   1343     if ret is NotImplemented:

/home/lily/.local/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py in _autopacking_conversion_function(v, dtype, name, as_ref)
   1447   elif dtype != inferred_dtype:
   1448     v = nest.map_structure(_cast_nested_seqs_to_dtype(dtype), v)
-> 1449   return _autopacking_helper(v, dtype, name or "packed")
   1450 
   1451 

/home/lily/.local/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py in _autopacking_helper(list_or_tuple, dtype, name)
   1353     # checking.
   1354     if all(ops.is_dense_tensor_like(elem) for elem in list_or_tuple):
-> 1355       return gen_array_ops.pack(list_or_tuple, name=name)
   1356   must_pack = False
   1357   converted_elems = []

/home/lily/.local/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py in pack(values, axis, name)
   6335         pass  # Add nodes to the TensorFlow graph.
   6336     except _core._NotOkStatusException as e:
-> 6337       _ops.raise_from_not_ok_status(e, name)
   6338   # Add nodes to the TensorFlow graph.
   6339   if not isinstance(values, (list, tuple)):

/home/lily/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)
   6624   message = e.message + (" name: " + name if name is not None else "")
   6625   # pylint: disable=protected-access
-> 6626   six.raise_from(core._status_to_exception(e.code, message), None)
   6627   # pylint: enable=protected-access
   6628 

/home/lily/.local/lib/python3.6/site-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: cannot compute Pack as input #1(zero-based) was expected to be a double tensor but is a string tensor [Op:Pack] name: packed

有人知道我如何以这种格式连接我的数据,或者我应该尝试的任何替代方法。 预先谢谢您,如果有更多我的代码对您有所帮助,请告诉我。

1 个答案:

答案 0 :(得分:1)

我不清楚您的问题。 但是,您的代码产生错误是不正确的。 您会看到,您正在尝试将dtype tf.float64的1个张量与第0轴上的None值进行串联。对于在0轴上进行串联,由于您的点张量为2级,因此,第二个也应为2级,而您只传递了None值。

任何一种制造方式,现有数据= tf.zeros((1,))