tensorflow 将 2d 张量内的块设置为常数值 [英] tensorflow set block within 2d tensor to constant value

查看:28
本文介绍了tensorflow 将 2d 张量内的块设置为常数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在尝试做的一个最小的例子:

<前><代码>将 numpy 导入为 np将张量流导入为 tf地图 = tf.placeholder(tf.float32)xmin = tf.placeholder(tf.int32)xmax = tf.placeholder(tf.int32)ymin = tf.placeholder(tf.int32)ymax = tf.placeholder(tf.int32)post_operation_map = 2.0 * 地图 + 1.0post_operation_map[ymin:ymax, xmin:xmax] = np.naninit = tf.global_variables_initializer()使用 tf.Session() 作为 sess:feed = {map:np.random.rand(200,200),最小:20,最大:40,ymin:20,最大:40}sess.run(post_operation_map,feed_dict=feed)

代码失败并出现以下错误:TypeError: 'Tensor' 对象不支持项目分配.

可以对代码做出一些假设:

  • 事先不知道地图的形状.
  • xmin, xmax, ymin, ymax 的范围将始终适合 map.shape 的边界.

我该如何解决这个问题?我猜我需要使用 tf.assign,但我不知道如何使用.

解决方案

这应该可以解决问题:

将 numpy 导入为 np将张量流导入为 tf地图 = tf.placeholder(tf.float32)xmin = tf.placeholder(tf.int32)xmax = tf.placeholder(tf.int32)ymin = tf.placeholder(tf.int32)ymax = tf.placeholder(tf.int32)post_operation_map = 2.0 * 地图 + 1.0# 用nan填充块形状 = tf.shape(post_operation_map)dtype = post_operation_map.dtype形状_x,形状_y = 形状[0],形状[1]x_range = tf.range(shape_x)[:, tf.newaxis]y_range = tf.range(shape_y)[tf.newaxis, :]掩码 = (xmin <= x_range) &(x_range 

输出:

[[ 2.50152206 1.01042879 2.88725328 1.27295971 2.99401283 1.84210801][ 2.98338175 2.26357031 南南 南 2.68635511][ 1.00461781 2.00605297 南南南 2.16447353][ 2.15073347 1.64699006 南南 南 1.97648919][ 1.7709868 1.65353572 1.6698066 2.26957846 2.75840473 1.23831809][ 1.51848006 1.45277226 1.46150732 1.08112144 2.87904882 2.62266874][ 1.86656547 1.5177052 1.36731267 2.70582867 1.57994771 2.48001719][ 1.89354372 2.88848639 1.49879098 1.36527407 1.47415829 2.95422626]]

Here's a minimal example of what I'm trying to do:


import numpy as np
import tensorflow as tf

map = tf.placeholder(tf.float32)
xmin = tf.placeholder(tf.int32)
xmax = tf.placeholder(tf.int32)
ymin = tf.placeholder(tf.int32)
ymax = tf.placeholder(tf.int32)

post_operation_map = 2.0 * map + 1.0
post_operation_map[ymin:ymax, xmin:xmax] = np.nan
init = tf.global_variables_initializer()

with tf.Session() as sess:
    feed = {map:np.random.rand(200,200),
            xmin:20,
            xmax:40,
            ymin:20,
            ymax:40}
    sess.run(post_operation_map, feed_dict=feed)


The code fails with the following error: TypeError: 'Tensor' object does not support item assignment.

There are some assumptions that can be made about the code:

  • The shape of map is not known in advance.
  • The ranges of xmin, xmax, ymin, ymax will always fit inside the bounds of map.shape.

How can I get around this? I'm guessing I need to use tf.assign, but I don't know how.

解决方案

This should do the trick:

import numpy as np
import tensorflow as tf

map = tf.placeholder(tf.float32)
xmin = tf.placeholder(tf.int32)
xmax = tf.placeholder(tf.int32)
ymin = tf.placeholder(tf.int32)
ymax = tf.placeholder(tf.int32)

post_operation_map = 2.0 * map + 1.0

# Fill block with nan
shape = tf.shape(post_operation_map)
dtype = post_operation_map.dtype
shape_x, shape_y = shape[0], shape[1]
x_range = tf.range(shape_x)[:, tf.newaxis]
y_range = tf.range(shape_y)[tf.newaxis, :]
mask = (xmin <= x_range) & (x_range < xmax) & (ymin <= y_range) & (y_range < ymax)
post_operation_map = tf.where(
    mask, tf.fill(shape, tf.constant(np.nan, dtype)), post_operation_map)

with tf.Session() as sess:
    feed = {map:np.random.rand(8, 6),
            xmin: 1,
            xmax: 4,
            ymin: 2,
            ymax: 5}
    print(sess.run(post_operation_map, feed_dict=feed))

Output:

[[ 2.50152206  1.01042879  2.88725328  1.27295971  2.99401283  1.84210801]
 [ 2.98338175  2.26357031         nan         nan         nan  2.68635511]
 [ 1.00461781  2.00605297         nan         nan         nan  2.16447353]
 [ 2.15073347  1.64699006         nan         nan         nan  1.97648919]
 [ 1.7709868   1.65353572  1.6698066   2.26957846  2.75840473  1.23831809]
 [ 1.51848006  1.45277226  1.46150732  1.08112144  2.87904882  2.62266874]
 [ 1.86656547  1.5177052   1.36731267  2.70582867  1.57994771  2.48001719]
 [ 1.89354372  2.88848639  1.49879098  1.36527407  1.47415829  2.95422626]]

这篇关于tensorflow 将 2d 张量内的块设置为常数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆