python - 更新 Tensorflow 中的变量切片

标签 python variables tensorflow slice

我有以下代码片段,用 Lua 和 torch 编写,这是一个自定义边缘检测算法:

 xGrad1[{{},{},{},{1,width-1}}] = input:narrow(4,2,width-1) - input:narrow(4,1,width-1)
 yGrad1[{{},{},{1,height-1},{}}] = input:narrow(3,2,height-1) - input:narrow(3,1,height-1)
 xGrad2[{{},{},{},{2,width}}] = input:narrow(4,2,width-1) - input:narrow(4,1,width-1)
 yGrad2[{{},{},{2,height},{}}] = input:narrow(3,2,height-1) - input:narrow(3,1,height-1)

 local xGrad = (torch.abs(self.xGrad1) + torch.abs(self.xGrad2))/2
 local yGrad = (torch.abs(self.yGrad1) + torch.abs(self.yGrad2))/2
 output = torch.sum(xGrad,2)+torch.sum(yGrad,2)

如您所见,代表图像宽度和高度的 xGrad 和 yGrad 张量的最后两个维度仅部分更新,例如在 xGrad2 中,只有第 2 列到 width-1。

现在我想用 Tensorflow 和 Python 达到相同的结果。我不确定我的一般方法是否正确,但我已将所有 4 个梯度张量初始化为变量,并预先用零填充它们。现在我正在努力处理这些部分分配。我尝试使用 Variable.assign 但没有运气。

目前,这是我的代码:

input = tf.image.decode_png(tf.read_file(f), 3)
input = tf.cast(input, tf.float32)

height = tf.shape(input)[0]
width = tf.shape(input)[1]

xGrad1 = tf.Variable(tf.zeros(tf.shape(input)), validate_shape=False)
yGrad1 = tf.Variable(tf.zeros(tf.shape(input)), validate_shape=False)
xGrad2 = tf.Variable(tf.zeros(tf.shape(input)), validate_shape=False)
yGrad2 = tf.Variable(tf.zeros(tf.shape(input)), validate_shape=False)

xGrad1[:, :width-2].assign(input[:,1:width-2] - input[:,:width-2])
yGrad1[:height-2].assign(input[1:height-2] - input[:height-2])
xGrad2[:, 1:width-1].assign(input[:,1:width-2] - input[:,:width-2])
yGrad2 [1, height-1].assign(input[1:height-2] - input[:height-2])

xGrad = (tf.abs(xGrad1) + tf.abs(xGrad2)) / 2
yGrad = (tf.abs(yGrad1) + tf.abs(yGrad2)) / 2

output = tf.reduce_sum(xGrad,axis=2) + tf.reduce_sum(yGrad,axis=2)

将列表索引从 Lua 转换为 Python 后,直接输出作为 4 个分配命令的参数的计算时,我得到了不错的结果,但在输出 xGrad1 等的内容时,只有黑色图像。

我假设存在形状不兼容的问题,但我已将 validate_shapes 切换为 False,因为我不知道 session 创建时输入的形状,因为输入图像是在 session 启动后加载的。如果有人对此也有想法,请随时回答我,但现在我只是问如何仅部分分配变量张量。

最佳答案

如果你想进行切片作业,你必须遵循这样的操作,

with tf.control_dependencies([xGrad1[:, :width-2].assign(input[:,1:width-2] - input[:,:width-2]), yGrad1[:height-2].assign(input[1:height-2] - input[:height-2]),xGrad2[:, 1:width-1].assign(input[:,1:width-2] - input[:,:width-2]),yGrad2 [1, height-1].assign(input[1:height-2] - input[:height-2])]): # should give the list of slice assignment here
 xGrad = (tf.abs(xGrad1) + tf.abs(xGrad2)) / 2
 yGrad = (tf.abs(yGrad1) + tf.abs(yGrad2)) / 2

output = tf.reduce_sum(xGrad,axis=2) + tf.reduce_sum(yGrad,axis=2)

sess = tf.Session()
with sess.as_default():
    sess.run(tf.global_variables_initializer())
    output= output.eval()

这里是 Tensorflow 中切片赋值的一个很好的解释, https://stackoverflow.com/a/43139565/6531137

希望这有帮助。

关于python - 更新 Tensorflow 中的变量切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47450903/

相关文章:

Python-将全局变量分配给函数返回需要函数是全局的吗?

python - Pandas DataFrame 列分配 ValueError : Wrong number of items passed

templates - 从 YAML 管道到模板文件的 Azure Pipeline 动态参数

Python 回溯字符串长度 n 来自字母表 {a,b,c} 与 #a=#b

jquery - 在变量前添加前缀 "period",jQuery

python - Python 中的 Keras : LSTM Dimensions

python - Out of Memory 在 for 循环中训练顺序模型;以前的解决方案不起作用

tensorflow - 如何从 TensorFlow 数据集中提取数据/标签

python - 使用 Nipy 在 python 中下采样 mri T1 图像

python - scrapy 蜘蛛中的多重继承