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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…