这将起作用:

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

虽然这样:
tf.keras.layers.Concatenate()((features['a'], features['b']))

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

那是预期的吗?如果是这样-为什么我通过什么顺序很重要?

谢谢,
扎克

编辑(添加代码示例):
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'

最佳答案

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

对concanate类的注释说明它需要一个列表。
此类调用K.backend的连接函数
https://github.com/keras-team/keras/blob/master/keras/backend/tensorflow_backend.py#L2041

这也说明它需要一个列表。

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

还声明需要张量列表。为什么?我不知道。在此函数中,实际上会检查张量(称为“值”的变量)是否为列表或元组。但是在途中仍然会出现错误。

关于tensorflow - tf.keras.layers.Concatenate()使用列表,但在张量元组上失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53410419/

10-12 03:47
查看更多