tf.keras.layers.Concatenate()使用列表,但对张量元组失败

时间:2018-11-21 10:48:00

标签: tensorflow keras

这将起作用:

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'

1 个答案:

答案 0 :(得分:2)

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

关于conantate类的

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

还指出它需要一个列表。

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

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