【问题标题】:How to replace certain values in Tensorflow tensor with the values of the other tensor?如何用另一个张量的值替换 Tensorflow 张量中的某些值?
【发布时间】:2017-09-22 18:27:52
【问题描述】:

我有一个大小为 (64, 2, 82, 1) 的 Tensorflow 张量 A,我想将其 (:, :, 80:82, :) 部分替换为张量 B 的相应部分(也为 ( 64、2、82、1) 大小)。

我该怎么做?

P.S.:准确地说,我的意思是在 numpy 中看起来像这样的操作:

A[:, :, 80:82, :] = B[:, :, 80:82, :]

【问题讨论】:

标签: python tensorflow


【解决方案1】:

以下代码可能会帮助您了解一些想法,

a = tf.constant([[11,0,13,14],
                 [21,22,23,0]])
condition = tf.equal(a, 0)
case_true = tf.reshape(tf.multiply(tf.ones([8], tf.int32), -9999), [2, 4])
case_false = a
a_m = tf.where(condition, case_true, case_false)
sess = tf.Session()
sess.run(a_m)

这里我正在访问张量的单个元素!

【讨论】:

  • 我正在尝试在我的激活函数中使用您的代码,如下所示:` def ScoreActivationFromSigmoid(x, target_min=1, target_max=9) : condition = K.tf.logical_and(K.tf.更少(x,1),K.tf.greater(x,-1))case_true = K.tf.reshape(K.tf.multiply(tf.ones([x.shape[1] * x.shape[2 ]]), 0.0), x.shape) case_false = a changed_x = K.tf.where(condition, case_true, case_false) 激活_x = K.sigmoid(changed_x) 分数 = 激活_x * (target_max - target_min) + target_min 返回分数`但它没有找到正确的形状,我应该如何正确重塑?
【解决方案2】:

tf.assign 应该可以工作:(未测试)

 tf.assign(A[:, :, 80:82, :], B[:, :, 80:82, :])

【讨论】:

  • 不幸的是它没有:a = tf.constant([[1,2,3], [4,5,6]])b = tf.constant([[7,8,9], [10,11,12]])tf.assign(a[0,:], b[0, :]) 给出以下错误:TypeError: assign() got an unexpected keyword argument 'name'
  • 您不能将 assign 与常量一起使用。
猜你喜欢
  • 2017-06-21
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-01
  • 2020-06-11
  • 1970-01-01
相关资源
最近更新 更多