Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:\n', tfc.eval())
print('npc:\n', npc)
for i in range(2):
    for j in range(2):
        npc[i,j] += row[0,j]

print('modified tfc:\n', tfc.eval())
print('modified npc:\n', 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]]

like image 643
user3030046 Avatar asked May 06 '16 11:05

user3030046


1 Answers

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:\n', tfc.eval())
    print('npc:\n', 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:\n', tfc.eval())
    print('modified npc:\n', 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]]
like image 125
Sung Kim Avatar answered Sep 18 '22 09:09

Sung Kim