Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

python - Tensorflow: How to modify the value in tensor

Since I need to write some preprocesses for the data before using Tensorflow to train models, some modifications on the tensor is needed. However, I have no idea about how to modify the values in tensor like the way using numpy.

The best way of doing so is that it is able to modify tensor directly. Yet, it seems not possible in the current version of Tensorflow. An alternative way is changing tensor to ndarray for the process, and then use tf.convert_to_tensor to change back.

The key is how to change tensor to ndarray.
1) tf.contrib.util.make_ndarray(tensor): https://www.tensorflow.org/versions/r0.8/api_docs/python/contrib.util.html#make_ndarray
It seems the easiest way as per the document, yet I cannot find this function in the current version of the Tensorflow. Second, the input of it is TensorProto rather than tensor.
2) Use a.eval() to copy a to another ndarray
Yet, it works only at using tf.InteractiveSession() in notebook.

A simple case with codes shows below. The purpose of this code is making that the tfc has the same output as npc after the process.

HINT
You should treat that tfc and npc are independent to each other. This meets the situation that at first the retrieved training data is in tensor format with tf.placeholder().


Source code

import numpy as np
import tensorflow as tf
tf.InteractiveSession()

tfc = tf.constant([[1.,2.],[3.,4.]])
npc = np.array([[1.,2.],[3.,4.]])
row = np.array([[.1,.2]])
print('tfc:
', tfc.eval())
print('npc:
', npc)
for i in range(2):
    for j in range(2):
        npc[i,j] += row[0,j]

print('modified tfc:
', tfc.eval())
print('modified npc:
', npc)

Output:

tfc:
[[ 1. 2.]
[ 3. 4.]]
npc:
[[ 1. 2.]
[ 3. 4.]]
modified tfc:
[[ 1. 2.]
[ 3. 4.]]
modified npc:
[[ 1.1 2.2]
[ 3.1 4.2]]

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use assign and eval (or sess.run) the assign:

import numpy as np
import tensorflow as tf

npc = np.array([[1.,2.],[3.,4.]])
tfc = tf.Variable(npc) # Use variable 

row = np.array([[.1,.2]])

with tf.Session() as sess:   
    tf.initialize_all_variables().run() # need to initialize all variables

    print('tfc:
', tfc.eval())
    print('npc:
', npc)
    for i in range(2):
        for j in range(2):
            npc[i,j] += row[0,j]
    tfc.assign(npc).eval() # assign_sub/assign_add is also available.
    print('modified tfc:
', tfc.eval())
    print('modified npc:
', npc)

It outputs:

tfc:
 [[ 1.  2.]
 [ 3.  4.]]
npc:
 [[ 1.  2.]
 [ 3.  4.]]
modified tfc:
 [[ 1.1  2.2]
 [ 3.1  4.2]]
modified npc:
 [[ 1.1  2.2]
 [ 3.1  4.2]]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...