本文介绍了为什么在Torch7中实现x = deepcopy(x)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
if is_pooling then
for k = 1, #color_codes do
color_content_masks[k] = image.scale(color_content_masks[k], math.ceil(color_content_masks[k]:size(2)/2), math.ceil(color_content_masks[k]:size(1)/2))
color_style_masks[k] = image.scale(color_style_masks[k], math.ceil(color_style_masks[k]:size(2)/2), math.ceil(color_style_masks[k]:size(1)/2))
end
elseif is_conv then
local sap = nn.SpatialAveragePooling(3,3,1,1,1,1):float()
for k = 1, #color_codes do
color_content_masks[k] = sap:forward(color_content_masks[k]:repeatTensor(1,1,1))[1]:clone()
color_style_masks[k] = sap:forward(color_style_masks[k]:repeatTensor(1,1,1))[1]:clone()
end
end
color_content_masks = deepcopy(color_content_masks)
color_style_masks = deepcopy(color_style_masks)
上面的代码是有关深层照片样式转换的火炬计划.您可以在 https://github.com/luanfujun/deep-photo-styletransfer .函数deepcopy()如下所示,与Lua官方网站中推荐的功能相同.
The code above is a torch project about deep photo style transfer. You can find the code in https://github.com/luanfujun/deep-photo-styletransfer . The function deepcopy() is as below which is the same as recommended in Lua official site.
function deepcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[deepcopy(orig_key)] = deepcopy(orig_value)
end
setmetatable(copy, deepcopy(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig
end
return copy
end
您会看到,如果If语句中的color_content_masks和color_style_masks直接更改,那么为什么我们需要实现一个Deepcopy?
As you can see, in the If statement color_content_masks and color_style_masks is change directly, so why do we need to implement a deepcopy??
推荐答案
它看起来像以下情况:
a = createSomeTable()
...
b = a -- now b references same table as a, and modifying b will modify a
...
b = deepcopy(b)
--[[
now we performed deep copy - b references copy of table a
and modifying b will not modify a
--]]
这篇关于为什么在Torch7中实现x = deepcopy(x)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!