2017-03-15 7 views
0

나는 tensorflow가있는 약간의 신경망을 구현하려고합니다. 그러나 나는 첫 번째 단계에서 이미 문제가있다.tensorflow : theano에 비해 convolution에서의 이상한 결과 (비록 뒤집히지 않고)

내가 입력 할 때 다음과 같은 theano.tensor.nnet.conv2d을 사용하여, 나는 예상 된 결과 얻을 : 나는 tf.nn.conv2d에 presumingly 같은 일을 수행 할 때

import theano.tensor as T 
import theano 
import numpy as np 
# Theano expects input of shape (batch_size, channels, height, width) 
# and filters of shape (out_channel, in_channel, height, width) 
x = T.tensor4() 
w = T.tensor4() 
c = T.nnet.conv2d(x, w, filter_flip=False) 
f = theano.function([x, w], [c], allow_input_downcast=True) 
base = np.array([[1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1]]).T 
i = base[np.newaxis, np.newaxis, :, :] 
print f(i, i) # -> results in 3 as expected because np.sum(i*i) = 3 

그러나, 내 결과가 다릅니다

import tensorflow as tf 
import numpy as np 
# TF expects input of (batch_size, height, width, channels) 
# and filters of shape (height, width, in_channel, out_channel) 
x = tf.placeholder(tf.float32, shape=(1, 4, 3, 1), name="input") 
w = tf.placeholder(tf.float32, shape=(4, 3, 1, 1), name="weights") 
c = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='VALID') 
with tf.Session() as sess: 
    base = np.array([[1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1]]).T 
    i = base[np.newaxis, :, :, np.newaxis] 
    weights = base[:, :, np.newaxis, np.newaxis] 
    res = sess.run(c, feed_dict={x: i, w: weights}) 
    print res # -> results in -5.31794233e+37 

을 tensorflow에서 컨볼 루션 연산의 레이아웃은 theano와 약간 다르므로 입력이 약간 다르게 보입니다. 그러나 Theano의 strides는 기본적으로 (1,1,1,1)이고 유효한 convolution이 기본값이기 때문에 이것은 똑같은 입력이어야합니다.

또한 tensorflow는 커널을 뒤집지 않습니다 (교차 상관 구현).

동일한 결과가 표시되지 않는 이유는 무엇입니까? 사전에

덕분에,

답변

0

좋아 로마, 나는 그것을 나 자신을 이해하지 않기 때문에 정말 아니다에도 불구하고, 해결책을 발견했다. 먼저, 해결하려고하는 작업에 대해 TheanoTensorflow은 다른 회선을 사용하는 것으로 보입니다. 현재 작업은 "1.5 D 컨볼 루션 (convolution)"으로, 입력 (여기서는 DNA 시퀀스)을 통해 한 방향으로 만 커널을 움직이는 것을 의미합니다.

Theano에서 나는 커널과 동일한 양의 행을 가진 conv2d 연산으로이를 해결했으며 정상적으로 작동했습니다.

(아마도 정확하게) Tensorflow (아마 정확하게)은 그 행을 채널로 해석하여 conv1d를 사용하기를 원합니다.

그래서, 다음은 작동하지만 처음에하지 않았다한다 : 나는 버전 1.0.1에 Tensorflow를 업데이트 할 때까지

import tensorflow as tf 
import numpy as np 

# TF expects input of (batch_size, height, width, channels) 
# and filters of shape (height, width, in_channel, out_channel) 
x = tf.placeholder(tf.float32, shape=(1, 4, 3, 1), name="input") 
w = tf.placeholder(tf.float32, shape=(4, 3, 1, 1), name="weights") 

x_star = tf.reshape(x, [1, 4, 3]) 
w_star = tf.reshape(w, [4, 3, 1]) 
c = tf.nn.conv1d(x_star, w_star, stride=1, padding='VALID') 
with tf.Session() as sess: 
    base = np.array([[1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1]]).T 
    i = base[np.newaxis, :, :, np.newaxis] 
    weights = base[:, :, np.newaxis, np.newaxis] 
    res = sess.run(c, feed_dict={x: i, w: weights}) 
    print res # -> produces 3 after updating tensorflow 

이 코드는 NaN를 생산하고 그 이후는 예상 출력을 생성합니다.

요약하면, 제 문제는 2D 컨볼 루션 대신 1D 컨벌루션을 사용하여 부분적으로 해결되었지만 여전히 프레임 워크의 업데이트가 필요했습니다. 두 번째 부분에서는 처음부터 잘못된 행동을 일으킨 원인을 전혀 모릅니다.

편집 : 원래 질문에 게시 된 코드가 올바르게 작동합니다. 그래서 다른 행동은 TF의 오래된 (어쩌면 손상된) 버전에서만 나온 것입니다.