2017-03-15 68 views
0

我想實現一些深度神經網絡張量流。但是,我已經在第一步中遇到了問題。tensorflow:卷積奇怪的結果與theano相比(不翻轉,雖然)

當我鍵入以下使用theano.tensor.nnet.conv2d,我得到預期的結果:

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 

然而,當我做presumingly同樣的事情在tf.nn.conv2d,我的結果是不同的:

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中的步幅默認爲(1,1,1,1),並且有效卷積也是默認值,所以這應該是完全相同的輸入。此外,tensorflow不會翻轉內核(實現互相關)。

你有什麼想法,爲什麼這不會給出相同的結果?

由於提前,

羅馬

回答

0

好吧,我找到了解決辦法,即使它是不是真的一個,因爲我不明白它自己。 首先,對於我試圖解決的任務來說,TheanoTensorflow使用不同的卷積。 手頭的任務是一個「1.5 D卷積」,這意味着在輸入(這裏是DNA序列)上只向一個方向滑動核心。

Theano,我用conv2d操作解決了這個問題,它的行數與內核數量相同,並且工作正常。

但是,Tensorflow(可能正確)要我使用conv1d,將行解釋爲通道。

因此,下面應該工作,但在剛開始沒:

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,直到我更新Tensorflow到1.0.1版本,從那時起,它產生預期的輸出。總之,我的問題部分通過使用1D卷積而不是2D卷積來解決,但仍然需要更新框架。對於第二部分,我根本不知道可能導致錯誤行爲的原因。

編輯:我張貼在我原來的問題的代碼現在也正常工作。所以不同的行爲只來自一箇舊的(也許是腐敗的)TF版本。