简体   繁体   中英

Does the tensorflow.nn.conv1d has a gradient?

I tried to use get_gradient_function() on tensorflow.nn.conv1d like this:

import tensorflow as tf
from tensorflow.python.framework.ops import get_gradient_function

d = tf.constant([1, 0, 2, 3, 0, 1, 1], dtype=tf.float32, name='d')
k = tf.constant([2, 1, 3], dtype=tf.float32, name='k')

data = tf.reshape(d, [1, int(d.shape[0]), 1], name='data')
kernel = tf.reshape(k, [int(k.shape[0]), 1, 1], name='kernel')

conv = tf.nn.conv1d(data, kernel, 1, 'SAME', name='conv')

with tf.Session() as sess:
    print (sess.run(conv))

op = tf.get_default_graph().get_operation_by_name('conv')
print(get_gradient_function(op))

I am getting the following error at the second to last line.

KeyError: "The name 'conv' refers to an Operation not in the graph."

It seems that there are NO 'conv' in graph, you can print all operation by tf.get_default_graph().get_operaions() shown as below

d
k
data/shape
data
kernel/shape
kernel
conv/ExpandDims/dim
conv/ExpandDims
conv/ExpandDims_1/dim
conv/ExpandDims_1
conv/Conv2D
conv/Squeeze

And conv.op.name print conv/Squeeze . so the name=conv just give outter name.

In this way, op = tf.get_default_graph().get_operation_by_name('conv/Squeeze') will work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM