【问题标题】:Tensorflow: How to modify the value in tensorTensorflow:如何修改张量中的值
【发布时间】:2016-09-01 11:48:47
【问题描述】:

由于我需要在使用 Tensorflow 训练模型之前为数据编写一些预处理程序,因此需要对 tensor 进行一些修改。但是,我不知道如何像使用numpy 那样修改tensor 中的值。

最好的办法是直接修改tensor。然而,在当前版本的 Tensorflow 中似乎是不可能的。另一种方法是将进程的tensor 更改为ndarray,然后使用tf.convert_to_tensor 更改回来。

关键是如何将tensor改为ndarray
1)tf.contrib.util.make_ndarray(tensor): https://www.tensorflow.org/versions/r0.8/api_docs/python/contrib.util.html#make_ndarray
根据文档,这似乎是最简单的方法,但我在当前版本的 Tensorflow 中找不到此功能。其次,它的输入是TensorProto而不是tensor
2) 使用a.eval()a 复制到另一个ndarray
然而,它仅适用于在笔记本中使用tf.InteractiveSession()

下面是一个带有代码的简单案例。此代码的目的是使tfc 在处理后具有与npc 相同的输出。

提示
您应该认为tfcnpc 是相互独立的。这满足了最初检索到的训练数据为tensor格式和tf.placeholder()的情况。


源代码

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)

输出:

tfc:
[[ 1. 2.]
[ 3. 4.]]
全国人大:
[[ 1. 2.]
[ 3. 4.]]
修改过的 tfc:
[[ 1. 2.]
[ 3. 4.]]
修改后的NPC:
[[ 1.1 2.2]
[3.1 4.2]]

【问题讨论】:

    标签: python numpy tensorflow


    【解决方案1】:

    使用assign和eval(或sess.run)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)
    

    它输出:

    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]]
    

    【讨论】:

    • 谢谢!我认为您的想法是假设 tfc 在开始时与 npc 共享相同的值,以便您可以首先处理 npc 然后将 assign 处理到 tfc 以获得相同的输出。然而事实并非如此。在实际情况下,您拥有的唯一数据是tfc,建议您需要将npc 视为开头不存在。所以关键应该是如何处理tfc中的数据。
    • @user3030046 取决于操作。如果它们是简单的 add/sub,请使用 assign_sub/assign_add。其他,我们还有很多其他的方法。你想更新 tf.Tensor 中的元素吗?如果你给我一个用例,我会看看我能做什么。
    • 我的情况是我正在尝试实现 CBOW(here 中的最后一个单元格。它需要对所有附近跳过的向量求和以进行预测。具体来说,它就像使用在 10×3 矩阵上逐行求和并输出 10×1 向量。此卷积运算将在 10×100 矩阵上重复,它将产生相同大小的输出矩阵。你有有什么想法吗?
    • @user3030046 你能用一些示例代码添加另一个问题吗?最好理解你的问题并回答它。谢谢!
    【解决方案2】:

    我为此苦苦挣扎了一段时间。给出的答案会将assign 操作添加到图形中(因此如果您随后保存检查点,则会不必要地增加.meta 的大小)。更好的解决方案是使用tf.keras.backend.set_value。可以通过以下方式使用原始张量流来模拟:

        for x, value in zip(tf.global_variables(), values_npfmt):
          if hasattr(x, '_assign_placeholder'):
            assign_placeholder = x._assign_placeholder
            assign_op = x._assign_op
          else:
            assign_placeholder = array_ops.placeholder(tf_dtype, shape=value.shape)
            assign_op = x.assign(assign_placeholder)
            x._assign_placeholder = assign_placeholder
            x._assign_op = assign_op
          get_session().run(assign_op, feed_dict={assign_placeholder: value})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多