简体   繁体   中英

How to change the value of a tensor which is not a tf.Variable in TensorFlow?

I know that there is a tf.assign function in TensorFlow, but this function is mainly aimed at mutable tensor ( tf.Variable ). How to modify the value of the tensor? For example, the following code,

import numpy as np
import tensorflow as tf

X = tf.placeholder(tf.float32, shape=[None, 32, 32, 3])

conv1 = tf.layers.conv2d(X, filters=64, kernel_size=(3, 3), padding='same',name='conv1')
relu1 = tf.nn.relu(conv1)

conv2 = tf.layers.conv2d(relu1, filters=64, kernel_size=(3, 3), padding='same',name='conv2')
relu2 = tf.nn.relu(conv2)

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

tensor = sess.graph.get_tensor_by_name(u'conv2/Conv2D:0')
feature_map = tf.reduce_mean(tensor[:,:,:,24])

image = np.random.uniform(size=(1,32,32,3))
sess.run([feature_map], feed_dict={X: image})

How to modify the value of feature_map and do not affect its derivation?

More specifically, when I change the value of feature_map , it does not affect its derivation process. For example, y = a^2 , y'= 2a , I just need to change a = 1 to a = 2 .

Other_op = tf.gradients(feature_map, X)

Different feature_map would achieve the different values, but it does not destroy the graph structures of operation.

That's not possible. A tensor is the output of tf.Operation . From documentation :

A Tensor is a symbolic handle to one of the outputs of an Operation . It does not hold the values of that operation's output, but instead provides a means of computing those values in a TensorFlow tf.Session .

So you can't change its value independently.

In your example feature_map doesn't have a value as it's an operation. Therefore you can't change it's value as such. What you can do, is pass another value in as part of the feed_dict parameter of session.run .

So for example if your feature_map is followed by an operation like this:

other_op = tf.gradient(feature_map, X)

Then you can change the value passed in to that op ( gradient in this case) via feed_dict like so:

session.run(other_op, feed_dict={feature_map: <new value>})

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