L.S
L.S

Reputation: 13

Concatenating tensorflow variables into one tensor

I am trying to concatenate two tensorflow variables that were initially tensors in my program into one tensor to be used as labelling input into an LSTM. I am having issues doing this due to the data type of these variables. Is it possible to concatenate data types of this format in tensorflow, and if not, is there any way around it?

The reason I am taking this approach is so that I can replace the text categorical data I have for each file(file names) with my numerical data breaking down the document(that I have processed using TF-IDF).

Here is a printout of some of my data format that is stored in a python dictionary (data_chart) that I am using to replace the y tensor:

{'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, ...,

Here is a printout of the y tensor (I only have two elements right now, but I plan on adding more later, the name of the elements directly correlate to the names of the data stored in the python dictionary above):

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

Here is the section of the code where I try to iterate over each file name and replace it with this data by converting the data to TF variables.

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

That section of code is giving me this error:


---------------------------------------------------------------------------
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

Does anyone know how I can concatenate my data in this format, or any alternatives that I should try. Thank you in advance, and please let me know if there is any more of my code that would be helpful to see.

Upvotes: 1

Views: 1053

Answers (1)

Anchal Gupta
Anchal Gupta

Reputation: 337

Your question is not clear to me. However, your piece of code producing error is not correct. You can see, you are trying to concatenate 1 tensor of dtype tf.float64 & other of None value on axis 0. For concatenating on 0 axis, as your point tensor is of rank 2, thereby, second should be also of rank 2 while you are passing only None value.

Either make, existing data = tf.zeros((1,))

Upvotes: 1

Related Questions