You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
No gradient defined for operation 'ExtractImagePatches' #2921
I tried to use tf.extract_image_patches to implement a locally layer as #2605 said. However an error occurred:
LookupError: No gradient defined for operation 'local/ExtractImagePatches' (op type: ExtractImagePatches)
I checked that tf.space_to_depth has the gradient definition but tf.extract_image_patches does not. And it seems not easy for me to implement the gradient. Any advice? @martinwicke@benoitsteiner@mrry
My code is rather messy and very far from a minimal example, but here it is:
def make_x_hat(self, x):
L, H = self.L, self.H
#put two cols of zeros
# TODO sort out messy tranpose
padded = tf.pad(tf.transpose(x), [[0, 0], [1, 1]], 'CONSTANT')
#make 4d and swap to make indexing work more nicely
x4 = tf.expand_dims(tf.expand_dims(tf.transpose(padded), 0), 3)
#rectangular patches of 2 (cols) by L+1 rows
ksizes = [1, 2, L + 1, 1]
strides = [1, ]*4
rates = [1, ]*4
patches = tf.extract_image_patches(x4, 'VALID', ksizes, strides, rates)
#first layer: [0, :, 0, -L:]
#middle layers [0, :, 0, 1:-1]
#final layer [0, :, 4, 1:L+1]
raw = tf.squeeze(tf.transpose(patches, (0, 2, 1, 3)))
ret = []
ret.append(raw[:, 0, L+1:2*L+1])
for h in range(1, H):
ret.append(raw[:, h, 1:(2*L+1)])
ret.append(raw[:, H, 1:L+1])
return ret
I was intending to use extract_image_patches to avoid a loop over h (I was going to combine the middle layers to a single array, if it had worked), but the performance gain is so minimal I just used this instead:
def make_x_hat(self, x):
x = tf.transpose(x)
N, L, H = self.N, self.L, self.H
n = N + L
ret = []
a = []
for l in range(L):
a.append(x[l:n-L+l, 0])
ret.append(tf.transpose(tf.pack(a)))
for h in range(1, H):
a = []
b = []
for l in range(L):
a.append(x[l:n-L+l, h])
b.append(x[l+1:n-L+l+1, h-1])
A = tf.transpose(tf.pack(a))
B = tf.transpose(tf.pack(b))
ret.append(tf.concat(1, (B, A)))
a = []
for l in range(L):
a.append(x[l+1:n-L+l+1, H-1])
ret.append(tf.transpose(tf.pack(a)))
return ret
Activity
sherrym commentedon Jun 17, 2016
@jyshee go for it if you need it.
jyshee commentedon Jun 21, 2016
I tried to implement the gradient of tf.extract_image_patches using tf.reshape and tf.pad Ops as:
hughsalimbeni commentedon Jun 27, 2016
I've hit this problem also. A quick test of the @jyshee solution fails in my example in the reshape line due to a mismatched number of elements
jyshee commentedon Jun 28, 2016
@hughsalimbeni could you show more details about your error when you test the codes above?
hughsalimbeni commentedon Jun 28, 2016
My code is rather messy and very far from a minimal example, but here it is:
I was intending to use extract_image_patches to avoid a loop over h (I was going to combine the middle layers to a single array, if it had worked), but the performance gain is so minimal I just used this instead:
which works fine.
nikhilmishra000 commentedon Aug 4, 2016
I have an implementation of this gradient -- should I create a pull request?
martinwicke commentedon Aug 5, 2016
Yes please, that would be great!