< Combine vectors >
tensorflow 의 경우 일반적인 방법으로
a = 1-a 를 사용하게 되면 tensorflow는 assignment를 허가 하지 않는다고 에러가 난다. 즉 session을 통해 실행 시킨 뒤 값을 추출한 뒤에는 값 할당이 가능하지만 graph로 만들어놓은 상태에서 아직 session을 하기 이전에는 안된다.
def 에서 열심히 graph를 만들다가 결합해야 할 경우 (joint, merging) 어찌해야 하는가?
예를 들어서 랜덤 변수 두개 (aaa, bbb) 를 만들어서 설명해 보면
print(sess.run(aaa))
[-0.99799103 -2.55633688 -0.25172943 -1.19995308 0.35475734]
print(sess.run(bbb))
[ 1.74303401 0.98615628 1.72890472 2.35131192 0.72014081]
각각 값이 5개 들어있는 vector 이다. aaa를 x축 값, bbb를 y축 값이라고 하고 나는 (x,y) 를 가지고 싶어 한다면 두개의 vector를 나란히 붙여야 한다.
(tensorflow API r1.0 이하 버전 기준, 현재는 바뀜)
흔히 쓰이는 concat 의 경우
ccc = tf.concat(0,[aaa, bbb])
print(sess.run(ccc))
[-0.99799103 -2.55633688 -0.25172943 -1.19995308 0.35475734 1.74303401
0.98615628 1.72890472 2.35131192 0.72014081]
두개를 axis=0을 기준으로 붙이면 무난히 붙는다. 하지만 나는 (x,y)로 2열로 붙이고 싶으므로
ccc = tf.concat(1,[aaa, bbb])
이렇게 axis=1로 주었더니 아래와 같이 에러가 난다.
ValueError: Shape must be at least rank 2 but is rank 1 for 'concat' (op: 'Concat') with input shapes: [], [5], [5].
tensorflow 홈페이지를 가보니(https://www.tensorflow.org/versions/r0.11/api_docs/python/array_ops/slicing_and_joining#tile)
친절하게
Note: If you are concatenating along a new axis consider using pack. E.g.
tf.concat(axis, [tf.expand_dims(t, axis) for t in tensors])
can be rewritten as
tf.pack(tensors, axis=axis)
이렇게 설명되어 있다.
따라서 pack을 이용하여서 붙여봤더니
ddd = tf.pack([aaa, bbb], axis=1)
print(sess.run(ddd))
[[-0.99799103 1.74303401]
[-2.55633688 0.98615628]
[-0.25172943 1.72890472]
[-1.19995308 2.35131192]
[ 0.35475734 0.72014081]]
원했던 (x,y) 로 잘 붙었다.
하지만, tensor가 1-D 형태의 vector 이며 차원의 갯수가 같을땐 pack으로 가능하지만,
만약 aaa = tensor(16, 500) 이고 bbb = tensor(16, 360) 일경우에 같은 방법으로 pack 을 사용하면
ValueError: Dimension 0 in both shapes must be equal, but are 500 and 360 for 'concat' ValueError: Dimension 1 in both shapes must be equal, but are 500 and 360
From merging shape 0 with other shapes. for 'pack_1' (op: 'Pack') with input shapes: [16,500], [16,360].
ValueError: Dimension 1 in both shapes must be equal, but are 500 and 360
From merging shape 0 with other shapes. for 'pack_2' (op: 'Pack') with input shapes: [16,500], [16,360].
dimension을 0으로 주든 1로 주든 둘다 크기문제를 말한다. 같은 shape이여야 딱 붙이는 개념인듯.
따라서 이 경우에는 tf.concat(1, [aaa, bbb]) 로 붙여주면 뒤에 이어서 바로 붙는다.
ccc = tf.concat(1, [aaa, bbb])
: tensor(16, 860)