【tensorflow】tensor连接操作concat

最近在写concatenates的时候,Tensorflow一直报错。

前方提示:注意tensorflow版本号
下面错误是tf 1.3.0的错误

按照别人blog的写法,但一直出错:
http://blog.csdn.net/mao_xiao_feng/article/details/53366163

code:

t1 = [[1,2,3],[4,5,6]]
t2 = [[7,8,9],[10,11,12]]
with tf.Session() as sess:
    print(sess.run(tf.concat(0,[t1,t2])))

这里写图片描述

到我这,却是这样的
这里写图片描述

Traceback (most recent call last):
  File "<pyshell#14>", line 2, in <module>
    print(sess.run(tf.concat(0,[t1,t2])))
  File "D:\python\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1062, in concat
    ).assert_is_compatible_with(tensor_shape.scalar())
  File "D:\python\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 737, in assert_is_compatible_with
    raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (2, 2, 3) and () are incompatible

百思不得其解的时候,去看了一眼源码:
我去! tensorflow在1.3的时候把concat重做了,换了输入方式。。。。

当然上面代码在之前版本应该是行的通的。我们来看一眼源码:

def concat(values, axis, name="concat"):
  """Concatenates tensors along one dimension.

  Concatenates the list of tensors `values` along dimension `axis`.  If
  `values[i].shape = [D0, D1, ... Daxis(i), ...Dn]`, the concatenated
  result has shape

      [D0, D1, ... Raxis, ...Dn]

  where

      Raxis = sum(Daxis(i))

  That is, the data from the input tensors is joined along the `axis`
  dimension.

  The number of dimensions of the input tensors must match, and all dimensions
  except `axis` must be equal.

  For example:

  ```python
  t1 = [[1, 2, 3], [4, 5, 6]]
  t2 = [[7, 8, 9], [10, 11, 12]]
  tf.concat([t1, t2], 0) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
  tf.concat([t1, t2], 1) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]

  # tensor t3 with shape [2, 3]
  # tensor t4 with shape [2, 3]
  tf.shape(tf.concat([t3, t4], 0)) ==> [4, 3]
  tf.shape(tf.concat([t3, t4], 1)) ==> [2, 6]
  ```

  Note: If you are concatenating along a new axis consider using stack.
  E.g.

  ```python
  tf.concat([tf.expand_dims(t, axis) for t in tensors], axis)
  ```

  can be rewritten as

  ```python
  tf.stack(tensors, axis=axis)
  ```

  Args:
    values: A list of `Tensor` objects or a single `Tensor`.
    axis: 0-D `int32` `Tensor`.  Dimension along which to concatenate.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` resulting from concatenation of the input tensors.
  """
  if not isinstance(values, (list, tuple)):
    values = [values]
  # TODO(mrry): Change to return values?
  if len(values) == 1:  # Degenerate case of one tensor.
    # Make a throwaway call to convert_to_tensor to make sure
    # that axis is of the correct type, and make sure that
    # the returned tensor is a scalar.
    # TODO(keveman): Implement a standalone type and shape checker.
    with ops.name_scope(name) as scope:
      ops.convert_to_tensor(axis,
                            name="concat_dim",
                            dtype=dtypes.int32).get_shape(
                            ).assert_is_compatible_with(tensor_shape.scalar())
      return identity(values[0], name=scope)
  return gen_array_ops._concat_v2(values=values,
                                  axis=axis,
                                  name=name)

把维度和value位置给我换了啊,我的心里满是我屮艸芔茻

tensorflow1.3下, concat用法,官方也给出了例子:

  t1 = [[1, 2, 3], [4, 5, 6]]
  t2 = [[7, 8, 9], [10, 11, 12]]
  tf.concat([t1, t2], 0) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
  tf.concat([t1, t2], 1) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值