我想實現一些深度神經網絡張量流。但是,我已經在第一步中遇到了問題。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不會翻轉內核(實現互相關)。
你有什麼想法,爲什麼這不會給出相同的結果?
由於提前,
羅馬