[torch]Getting the Output of a Layer

本文通过实例演示了如何使用Torch库构建一个包含FastLSTM层的序列模型,并详细展示了模型内部各层输出的状态,有助于理解序列模型的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://groups.google.com/forum/#!topic/torch7/R9DAdx95aqc

introduction

require 'cutorch'
require 'cunn'
require 'rnn'
require 'os'

tensor1 = torch.zeros(5,10)
tensor1[3]=torch.rand(1,10)
print(tensor1)
input = {tensor1,torch.rand(5,10),torch.rand(5,10),torch.rand(5,10),torch.rand(5,10),torch.rand(5,10)}
net = nn.Sequencer(
   nn.Sequential()
      :add(nn.MaskZero(nn.FastLSTM(10,3),1))
      :add(nn.MaskZero(nn.Linear(3,4),1))
      :add(nn.MaskZero(nn.LogSoftMax(),1))
)

output = net:forward(input)
local m = net.modules
--[[
print("net")
print(net)
print("m")
print(m)
--]]
for i = 1, #input do
        print(output[i])
        print(m[1].sharedClones[i].modules[1].output)
end

print("net")
print(net)
print("m")
print(m)

output

net 
nn.Sequencer @ nn.Recursor @ nn.Sequential {
  [input -> (1) -> (2) -> (3) -> output]
  (1): nn.MaskZero @ nn.FastLSTM(10 -> 3)
  (2): nn.MaskZero @ nn.Linear(3 -> 4)
  (3): nn.MaskZero @ nn.LogSoftMax
}
m   
{
  1 : 
    {
      sharedClones : 
        {
          1 : 
            {
              gradInput : DoubleTensor - empty
              modules : 
                {
                  1 : {...}
                  2 : {...}
                  3 : {...}
                }
              _type : "torch.DoubleTensor"
              output : DoubleTensor - size: 5x4
            }
          2 : 
            {
              gradInput : DoubleTensor - empty
              modules : 
                {
                  1 : {...}
                  2 : {...}
                  3 : {...}
                }
              _type : "torch.DoubleTensor"
              output : DoubleTensor - size: 5x4
            }
          3 : 
            {
              gradInput : DoubleTensor - empty
              modules : 
                {
                  1 : {...}
                  2 : {...}
                  3 : {...}
                }
              _type : "torch.DoubleTensor"
              output : DoubleTensor - size: 5x4
            }
          4 : 
            {
              gradInput : DoubleTensor - empty
              modules : 
                {
                  1 : {...}
                  2 : {...}
                  3 : {...}
                }
              _type : "torch.DoubleTensor"
              output : DoubleTensor - size: 5x4
            }
          5 : 
            {
              gradInput : DoubleTensor - empty
              modules : 
                {
                  1 : {...}
                  2 : {...}
                  3 : {...}
                }
              _type : "torch.DoubleTensor"
              output : DoubleTensor - size: 5x4
            }
          6 : 
            {
              gradInput : DoubleTensor - empty
              modules : 
                {
                  1 : {...}
                  2 : {...}
                  3 : {...}
                }
              _type : "torch.DoubleTensor"
              output : DoubleTensor - size: 5x4
            }
        }
      step : 7
      outputs : 
        {
          1 : DoubleTensor - size: 5x4
          2 : DoubleTensor - size: 5x4
          3 : DoubleTensor - size: 5x4
          4 : DoubleTensor - size: 5x4
          5 : DoubleTensor - size: 5x4
          6 : DoubleTensor - size: 5x4
        }
      output : DoubleTensor - size: 5x4
      gradInput : DoubleTensor - empty
      modules : 
        {
          1 : 
            {
              gradInput : DoubleTensor - empty
              modules : 
                {
                  1 : {...}
                  2 : {...}
                  3 : {...}
                }
              _type : "torch.DoubleTensor"
              output : DoubleTensor - size: 5x4
            }
        }
      _gradOutputs : {...}
      rho : 6
      recurrentModule : 
        {
          gradInput : DoubleTensor - empty
          modules : 
            {
              1 : 
                {
                  output : DoubleTensor - size: 5x3
                  gradInput : DoubleTensor - empty
                  nInputDim : 1
                  batchmode : true
                  zeroMask : ByteTensor - size: 5x1
                  _type : "torch.DoubleTensor"
                  _zeroMask : DoubleTensor - size: 5x1
                  module : {...}
                  modules : {...}
                }
              2 : 
                {
                  output : DoubleTensor - size: 5x4
                  gradInput : DoubleTensor - empty
                  nInputDim : 1
                  batchmode : true
                  zeroMask : ByteTensor - size: 5x1
                  _type : "torch.DoubleTensor"
                  _zeroMask : DoubleTensor - size: 5x1
                  module : {...}
                  modules : {...}
                }
              3 : 
                {
                  output : DoubleTensor - size: 5x4
                  gradInput : DoubleTensor - empty
                  nInputDim : 1
                  batchmode : true
                  zeroMask : ByteTensor - size: 5x1
                  _type : "torch.DoubleTensor"
                  _zeroMask : DoubleTensor - size: 5x1
                  module : {...}
                  modules : {...}
                }
            }
          _type : "torch.DoubleTensor"
          output : DoubleTensor - size: 5x4
        }
      nSharedClone : 6
      _type : "torch.DoubleTensor"
      gradInputs : {...}
      module : 
        {
          gradInput : DoubleTensor - empty
          modules : 
            {
              1 : 
                {
                  output : DoubleTensor - size: 5x3
                  gradInput : DoubleTensor - empty
                  nInputDim : 1
                  batchmode : true
                  zeroMask : ByteTensor - size: 5x1
                  _type : "torch.DoubleTensor"
                  _zeroMask : DoubleTensor - size: 5x1
                  module : {...}
                  modules : {...}
                }
              2 : 
                {
                  output : DoubleTensor - size: 5x4
                  gradInput : DoubleTensor - empty
                  nInputDim : 1
                  batchmode : true
                  zeroMask : ByteTensor - size: 5x1
                  _type : "torch.DoubleTensor"
                  _zeroMask : DoubleTensor - size: 5x1
                  module : {...}
                  modules : {...}
                }
              3 : 
                {
                  output : DoubleTensor - size: 5x4
                  gradInput : DoubleTensor - empty
                  nInputDim : 1
                  batchmode : true
                  zeroMask : ByteTensor - size: 5x1
                  _type : "torch.DoubleTensor"
                  _zeroMask : DoubleTensor - size: 5x1
                  module : {...}
                  modules : {...}
                }
            }
          _type : "torch.DoubleTensor"
          output : DoubleTensor - size: 5x4
        }
      rmInSharedClones : true
    }
}

可以看出m是一个table类型的变量. 所以看看想要它输出什么就能输出什么.
例如:

tensor1 = torch.zeros(5,10)
tensor1[3]=torch.rand(1,10)
print(tensor1)
input = {tensor1,torch.rand(5,10),torch.rand(5,10),torch.rand(5,10),torch.rand(5,10),torch.rand(5,10)}
net = nn.Sequencer(
   nn.Sequential()
      :add(nn.MaskZero(nn.FastLSTM(10,3),1))
--      :add(nn.MaskZero(nn.Linear(3,4),1))
--      :add(nn.MaskZero(nn.LogSoftMax(),1))
)

output = net:forward(input)
local m = net.modules
--[[
print("net")
print(net)
print("m")
print(m)
--]]
for i = 1, #input do
        print(output[i])
        print(m[1].sharedClones[i].modules[1].output)
end)

test

require 'cutorch'
require 'cunn'
require 'rnn'
require 'os'
--[[
net = nn.Sequencer(
   nn.Sequential()
      :add(nn.MaskZero(nn.FastLSTM(10,6),1))
      :add(nn.MaskZero(nn.Linear(6,4),1))
      :add(nn.MaskZero(nn.LogSoftMax(),1))
)
parameters, gradParameters = net:getParameters()
lightModel = net:clone('weight','bias','running_mean','running_std')
torch.save('model.t7',lightModel)
--]]

net=torch.load("model.t7")

--[[
tensor1 = torch.zeros(5,10)
tensor1[3]=torch.Tensor{3,4,5,6,7,8,23,2,12,90}
tensor2 = torch.ones(5,10)
tensor2[{{1,2},{}}]=torch.Tensor{ {1,3,4,5,6,0,3,2,56,2}, {5,3,2,5,7,3,45,78,235,10}}
tensor2[4]=torch.ones(1,10):fill(3.2)
tensor2[5]=torch.zeros(1,10)
input = {tensor1,tensor2}
--]]
--net=torch.load("/work1/t2g-shinoda2011/15M54105/trecvid/torch-lstm3/batch5_epoch5_hiddensize256_cw1/model_100ex_batch5_unit256_epoch70")
--[[
array = {}
tensor1  = torch.zeros(5,10)
tensor1[3]=torch.rand(1,10)
tensor2 = torch.rand(5,10)
tensor3 = torch.rand(5,10)
tensor4 = torch.rand(5,10)
tensor1=tensor1:cuda()
tensor2=tensor2:cuda()
tensor3=tensor3:cuda()
tensor4=tensor4:cuda()
table.insert(array, tensor1)
table.insert(array, tensor2)
table.insert(array, tensor3)
table.insert(array, tensor4)
file = torch.DiskFile('input.asc', 'w')
file:writeObject(array)
file:close()
os.exit()
--]]
net:cuda()
file = torch.DiskFile('input.asc', 'r')
input = file:readObject()
print(input)
local m = net.modules
output = net:forward(input)
--[[
print("net")
print(net)
print("m")
print(m)
--]]
model = (nn.MaskZero(nn.LogSoftMax(),1)):cuda()
for seqj = 1, #input do
    print(seqj)
    res = m[1].sharedClones[seqj].modules[2].output
    out1=output[seqj]
    out2=model:forward(res)
    print(out1-out2)

end

test.lua得到的tep = m[1].sharedClones[seqj].modules[2].output[i]是取了net的第二层的输出.
out1与out2值相等.说明确实tep是第二层的输出.

Parameters for big model inference torch_dtype (str or torch.dtype, optional) — Override the default torch.dtype and load the model under a specific dtype. The different options are: torch.float16 or torch.bfloat16 or torch.float: load in a specified dtype, ignoring the model’s config.torch_dtype if one exists. If not specified the model will get loaded in torch.float (fp32). "auto" - A torch_dtype entry in the config.json file of the model will be attempted to be used. If this entry isn’t found then next check the dtype of the first weight in the checkpoint that’s of a floating point type and use that as dtype. This will load the model using the dtype it was saved in at the end of the training. It can’t be used as an indicator of how the model was trained. Since it could be trained in one of half precision dtypes, but saved in fp32. A string that is a valid torch.dtype. E.g. “float32” loads the model in torch.float32, “float16” loads in torch.float16 etc. For some models the dtype they were trained in is unknown - you may try to check the model’s paper or reach out to the authors and ask them to add this information to the model’s card and to insert the torch_dtype entry in config.json on the hub. device_map (str or dict[str, Union[int, str, torch.device]] or int or torch.device, optional) — A map that specifies where each submodule should go. It doesn’t need to be refined to each parameter/buffer name, once a given module name is inside, every submodule of it will be sent to the same device. If we only pass the device (e.g., "cpu", "cuda:1", "mps", or a GPU ordinal rank like 1) on which the model will be allocated, the device map will map the entire model to this device. Passing device_map = 0 means put the whole model on GPU 0. To have Accelerate compute the most optimized device_map automatically, set device_map="auto". For more information about each option see designing a device map. max_memory (Dict, optional) — A dictionary device identifier to maximum memory if using device_map. Will default to the maximum memory available for each GPU and the available CPU RAM if unset. tp_plan (str, optional) — A torch tensor parallel plan, see here. Currently, it only accepts tp_plan="auto" to use predefined plan based on the model. Note that if you use it, you should launch your script accordingly with torchrun [args] script.py. This will be much faster than using a device_map, but has limitations. tp_size (str, optional) — A torch tensor parallel degree. If not provided would default to world size. device_mesh (torch.distributed.DeviceMesh, optional) — A torch device mesh. If not provided would default to world size. Used only for tensor parallel for now. offload_folder (str or os.PathLike, optional) — If the device_map contains any value "disk", the folder where we will offload weights. offload_state_dict (bool, optional) — If True, will temporarily offload the CPU state dict to the hard drive to avoid getting out of CPU RAM if the weight of the CPU state dict + the biggest shard of the checkpoint does not fit. Defaults to True when there is some disk offload. offload_buffers (bool, optional) — Whether or not to offload the buffers with the model parameters. quantization_config (Union[QuantizationConfigMixin,Dict], optional) — A dictionary of configuration parameters or a QuantizationConfigMixin object for quantization (e.g bitsandbytes, gptq). There may be other quantization-related kwargs, including load_in_4bit and load_in_8bit, which are parsed by QuantizationConfigParser. Supported only for bitsandbytes quantizations and not preferred. consider inserting all such arguments into quantization_config instead. subfolder (str, optional, defaults to "") — In case the relevant files are located inside a subfolder of the model repo on huggingface.co, you can specify the folder name here. variant (str, optional) — If specified load weights from variant filename, e.g. pytorch_model..bin. variant is ignored when using from_tf or from_flax. use_safetensors (bool, optional, defaults to None) — Whether or not to use safetensors checkpoints. Defaults to None. If not specified and safetensors is not installed, it will be set to False. weights_only (bool, optional, defaults to True) — Indicates whether unpickler should be restricted to loading only tensors, primitive types, dictionaries and any types added via torch.serialization.add_safe_globals(). When set to False, we can load wrapper tensor subclass weights. key_mapping (`dict[str, str], optional) — A potential mapping of the weight names if using a model on the Hub which is compatible to a Transformers architecture, but was not converted accordingly. kwargs (remaining dictionary of keyword arguments, optional) — Can be used to update the configuration object (after it being loaded) and initiate the model (e.g., output_attentions=True). Behaves differently depending on whether a config is provided or automatically loaded: If a configuration is provided with config, **kwargs will be directly passed to the underlying model’s __init__ method (we assume all relevant updates to the configuration have already been done) If a configuration is not provided, kwargs will be first passed to the configuration class initialization function (from_pretrained()). Each key of kwargs that corresponds to a configuration attribute will be used to override said attribute with the supplied kwargs value. Remaining keys that do not correspond to any configuration attribute will be passed to the underlying model’s __init__ function. 详细解释一下上面这个
最新发布
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值