当前位置: 首页>>代码示例>>Python>>正文


Python Tensor.new_empty方法代码示例

本文整理汇总了Python中torch.Tensor.new_empty方法的典型用法代码示例。如果您正苦于以下问题:Python Tensor.new_empty方法的具体用法?Python Tensor.new_empty怎么用?Python Tensor.new_empty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在torch.Tensor的用法示例。


在下文中一共展示了Tensor.new_empty方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: forward

# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import new_empty [as 别名]
    def forward(self,  # pylint: disable=arguments-differ
                inputs: torch.Tensor,
                weight: torch.Tensor,
                bias: torch.Tensor,
                state_accumulator: torch.Tensor,
                memory_accumulator: torch.Tensor,
                dropout_mask: torch.Tensor,
                lengths: torch.Tensor,
                gates: torch.Tensor) -> Tuple[torch.Tensor, None]:
        sequence_length, batch_size, input_size = inputs.size()
        tmp_i = inputs.new_empty(batch_size, 6 * self.hidden_size)
        tmp_h = inputs.new_empty(batch_size, 5 * self.hidden_size)
        is_training = 1 if self.train else 0
        highway_lstm_layer.highway_lstm_forward_cuda(input_size,  # type: ignore # pylint: disable=no-member
                                                     self.hidden_size,
                                                     batch_size,
                                                     self.num_layers,
                                                     sequence_length,
                                                     inputs,
                                                     lengths,
                                                     state_accumulator,
                                                     memory_accumulator,
                                                     tmp_i,
                                                     tmp_h,
                                                     weight,
                                                     bias,
                                                     dropout_mask,
                                                     gates,
                                                     is_training)

        self.save_for_backward(inputs, lengths, weight, bias, state_accumulator,
                               memory_accumulator, dropout_mask, gates)

        # The state_accumulator has shape: (num_layers, sequence_length + 1, batch_size, hidden_size)
        # so for the output, we want the last layer and all but the first timestep, which was the
        # initial state.
        output = state_accumulator[-1, 1:, :, :]
        return output, state_accumulator[:, 1:, :, :]
开发者ID:pyknife,项目名称:allennlp,代码行数:40,代码来源:alternating_highway_lstm.py


注:本文中的torch.Tensor.new_empty方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。