私はtensorflowでいくつかの深いニューラルネットワークを実装しようとしています。しかし、私はすでに最初のステップで問題があります。tensorflow:畳み込みによる奇妙な結果がtheanoに比べて(フリップしていないが)
私は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
テンソルフローでの畳み込み演算のレイアウトはtheanoとは少し異なります。これは入力が少し違って見える理由です。 しかし、Theanoのストライドはデフォルトで(1,1,1,1)と有効なコンボリューションもデフォルトであるため、これはまったく同じ入力でなければなりません。
さらに、tensorflowはカーネルを反転させません(相互相関を実装します)。
なぜ同じ結果が得られないのかご存じですか?事前に
おかげで、
ローマ