尝试使Internet代码适应我拥有的其他模式/代码(Tensorflow 1.15.0)
2个主要区别:
我要修改的互联网代码部分是:
model = inception.Inception()
resized_image = model.resized_image
y_logits = model.y_logits
with model.graph.as_default():
pl_cls_target = tf.placeholder(dtype=tf.int32)
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y_logits, labels=[pl_cls_target])
gradient = tf.gradients(loss, resized_image)
我成功 :(其中“ lastLayer”是模型的最后一层,具有100个输出值)
resized_image = lastLayer.graph.get_tensor_by_name('Placeholder_1:0')
logits = lastLayer
with lastLayer.graph.as_default():
pl_cls_target = tf.placeholder(dtype=tf.float32)
loss = tf.reduce_mean(tf.squared_difference(logits, pl_cls_target))
gradient = tf.gradients(loss, resized_image)
现在,我想从另一个具有2048个值的中间层计算梯度。
通过以下方式观看图表:
op = session.graph.get_operations()
[m.values() for m in op]
所有带有2048的“行”都是:
(<tf.Tensor 'random_uniform_14/RandomUniform:0' shape=(100, 2048) dtype=float32>,)
(<tf.Tensor 'random_uniform_14/mul:0' shape=(100, 2048) dtype=float32>,)
(<tf.Tensor 'random_uniform_14:0' shape=(100, 2048) dtype=float32>,)
(<tf.Tensor 'layer_3/fully_connected/weights:0' shape=(100, 2048) dtype=float32_ref>,)
(<tf.Tensor 'layer_3/fully_connected/weights/Assign:0' shape=(100, 2048) dtype=float32_ref>,)
(<tf.Tensor 'layer_3/fully_connected/weights/read:0' shape=(100, 2048) dtype=float32>,)
(<tf.Tensor 'Const_14:0' shape=(2048,) dtype=float32>,)
(<tf.Tensor 'layer_3/fully_connected/biases:0' shape=(2048,) dtype=float32_ref>,)
(<tf.Tensor 'layer_3/fully_connected/biases/Assign:0' shape=(2048,) dtype=float32_ref>,)
(<tf.Tensor 'layer_3/fully_connected/biases/read:0' shape=(2048,) dtype=float32>,)
(<tf.Tensor 'l2_normalize/Square:0' shape=(100, 2048) dtype=float32>,)
(<tf.Tensor shape=(1, 2048) dtype=float32>,)
(<tf.Tensor 'l2_normalize/Maximum:0' shape=(1, 2048) dtype=float32>,)
(<tf.Tensor 'l2_normalize/Rsqrt:0' shape=(1, 2048) dtype=float32>,)
(<tf.Tensor 'l2_normalize:0' shape=(100, 2048) dtype=float32>,)
(<tf.Tensor 'save/Assign_25:0' shape=(2048,) dtype=float32_ref>,)
(<tf.Tensor 'save/Assign_26:0' shape=(100, 2048) dtype=float32_ref>,)
我尝试仅更改调用其中某些层的“ logits”变量。 例如:
resized_image = lastLayer.graph.get_tensor_by_name('Placeholder_1:0')
logits = lastLayer.graph.get_tensor_by_name('l2_normalize/Rsqrt:0')
with lastLayer.graph.as_default():
pl_cls_target = tf.placeholder(dtype=tf.float32)
loss = tf.reduce_mean(tf.squared_difference(logits, pl_cls_target))
gradient = tf.gradients(loss, resized_image)
但未成功(渐变始终为无)。
有什么主意吗?