Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tf.keras.layers.Concatenate() works with a list but fails on a tuple of tensors

This will work:

tf.keras.layers.Concatenate()([features['a'], features['b']])

While this:

tf.keras.layers.Concatenate()((features['a'], features['b']))

Results in:

TypeError: int() argument must be a string or a number, not 'TensorShapeV1'

Is that expected? If so - why does it matter what sequence do I pass?

Thanks, Zach

EDIT (adding a code example):

import pandas as pd
import numpy as np

data = {
    'a': [1.0, 2.0, 3.0],
    'b': [0.1, 0.3, 0.2],
}

with tf.Session() as sess: 
  ds = tf.data.Dataset.from_tensor_slices(data)
  ds = ds.batch(1)

  it = ds.make_one_shot_iterator()
  features = it.get_next()

  concat = tf.keras.layers.Concatenate()((features['a'], features['b']))


  try:
    while True:
      print(sess.run(concat))
  except tf.errors.OutOfRangeError:
    pass


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-135-0e1a45017941> in <module>()
      6   features = it.get_next()
      7 
----> 8   concat = tf.keras.layers.Concatenate()((features['a'], features['b']))
      9 
     10 

google3/third_party/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
    751           # the user has manually overwritten the build method do we need to
    752           # build it.
--> 753           self.build(input_shapes)
    754         # We must set self.built since user defined build functions are not
    755         # constrained to set self.built.

google3/third_party/tensorflow/python/keras/utils/tf_utils.py in wrapper(instance, input_shape)
    148             tuple(tensor_shape.TensorShape(x).as_list()) for x in input_shape]
    149       else:
--> 150         input_shape = tuple(tensor_shape.TensorShape(input_shape).as_list())
    151     output_shape = fn(instance, input_shape)
    152     if output_shape is not None:

google3/third_party/tensorflow/python/framework/tensor_shape.py in __init__(self, dims)
    688       else:
    689         # Got a list of dimensions
--> 690         self._dims = [as_dimension(d) for d in dims_iter]
    691 
    692   @property

google3/third_party/tensorflow/python/framework/tensor_shape.py in as_dimension(value)
    630     return value
    631   else:
--> 632     return Dimension(value)
    633 
    634 

google3/third_party/tensorflow/python/framework/tensor_shape.py in __init__(self, value)
    183       raise TypeError("Cannot convert %s to Dimension" % value)
    184     else:
--> 185       self._value = int(value)
    186       if (not isinstance(value, compat.bytes_or_text_types) and
    187           self._value != value):

TypeError: int() argument must be a string or a number, not 'TensorShapeV1'
like image 875
Zach Moshe Avatar asked Nov 21 '18 10:11

Zach Moshe


People also ask

How do you concatenate tensors in Keras?

To concatenate an arbitrary number of tensors, simply calculate the size of each minus the last axis (multiply all the axes before last to get size), find the largest tensor m, then upsample or repeat each tensor x by ceiling(m. size / x. size).

What does concatenate layer do in Keras?

Concatenate class Layer that concatenates a list of inputs. It takes as input a list of tensors, all of the same shape except for the concatenation axis, and returns a single tensor that is the concatenation of all inputs.

How do you concatenate two tensors in Python?

We can join two or more tensors using torch.cat() and torch. stack(). torch.cat() is used to concatenate two or more tensors, whereas torch. stack() is used to stack the tensors.

How do you concatenate layers?

Create and Connect Concatenation LayerName the concatenation layer 'concat' . Create two ReLU layers and connect them to the concatenation layer. The concatenation layer concatenates the outputs from the ReLU layers.


1 Answers

https://github.com/keras-team/keras/blob/master/keras/layers/merge.py#L329

comment on the concanate class states it requires a list. this class calls K.backend's concatenate function https://github.com/keras-team/keras/blob/master/keras/backend/tensorflow_backend.py#L2041

which also states it requires a list.

in tensorflow https://github.com/tensorflow/tensorflow/blob/r1.12/tensorflow/python/ops/array_ops.py#L1034

also states it requires a list of tensors. Why? I don't know. in this function the tensors (variable called "values") actually gets checked if its a list or tuple. but somewhere along the way you still get an error.

like image 147
Mete Han Kahraman Avatar answered Oct 16 '22 11:10

Mete Han Kahraman