欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pytorch中.new()的作用詳解

 更新時間:2020年02月18日 10:48:08   作者:悲戀花丶無心之人  
今天小編就為大家分享一篇Pytorch中.new()的作用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

一、作用

創(chuàng)建一個新的Tensor,該Tensor的typedevice都和原有Tensor一致,且無內(nèi)容。

二、使用方法

如果隨機定義一個大小的Tensor,則新的Tensor有兩種創(chuàng)建方法,如下:

inputs = torch.randn(m, n)
 
new_inputs = inputs.new()
new_inputs = torch.Tensor.new(inputs)

三、具體代碼

import torch
 
rectangle_height = 1
rectangle_width = 4
inputs = torch.randn(rectangle_height, rectangle_width)
for i in range(rectangle_height):
  for j in range(rectangle_width):
    inputs[i][j] = (i + 1) * (j + 1)
print("inputs:", inputs)
new_inputs = inputs.new()
print("new_inputs:", new_inputs)
# Constructs a new tensor of the same data type as self tensor.
print(new_inputs.type(), inputs.type())
print('')
 
inputs = inputs.squeeze(dim=0)
print("inputs:", inputs)
# new_inputs = inputs.new()
new_inputs = torch.Tensor.new(inputs)
print("new_inputs:", new_inputs)
# Constructs a new tensor of the same data type as self tensor.
print(new_inputs.type(), inputs.type())
if torch.cuda.is_available():
  device = torch.device("cuda")
  inputs, new_inputs = inputs.to(device), new_inputs.to(device)
  print(inputs.device, new_inputs.device)

結(jié)果如下:

可以看到不論inputs是多少維的,新建的new_inputstypedevice都與inputs保持一致

inputs: tensor([[1., 2., 3., 4.]])
new_inputs: tensor([])
torch.FloatTensor torch.FloatTensor
 
inputs: tensor([1., 2., 3., 4.])
new_inputs: tensor([])
torch.FloatTensor torch.FloatTensor
cuda:0 cuda:0

四、實際應(yīng)用(添加噪聲)

可以對Tensor添加噪聲,添加如下代碼即可實現(xiàn):

noise = inputs.data.new(inputs.size()).normal_(0,0.01)
print(noise)

結(jié)果如下:

tensor([ 0.0062, 0.0137, -0.0209, 0.0072], device='cuda:0')

以上這篇Pytorch中.new()的作用詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python 在OpenCV里實現(xiàn)仿射變換—坐標變換效果

    Python 在OpenCV里實現(xiàn)仿射變換—坐標變換效果

    這篇文章主要介紹了Python 在OpenCV里實現(xiàn)仿射變換—坐標變換效果,本文通過一個例子給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • 詳解python如何根據(jù)參數(shù)不同調(diào)用不同的類和方法

    詳解python如何根據(jù)參數(shù)不同調(diào)用不同的類和方法

    這篇文章主要為大家詳細介紹了在python中如何根據(jù)參數(shù)不同調(diào)用不同的類和方法,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-03-03
  • 如何使用Python?VTK高亮顯示actor

    如何使用Python?VTK高亮顯示actor

    這篇文章主要介紹了如何使用Python?VTK高亮顯示actor,通過Python-VTK在同一個窗口中,高亮顯示選中的actor。本例子中的代碼,當窗口中的圓球actor被選中時,會變成紅色,并且會顯示actor三遍面片邊緣信息,下文相關(guān)內(nèi)容需要的小伙伴可以參考一下
    2022-04-04
  • 深入了解Python中frozenset的功能與用法

    深入了解Python中frozenset的功能與用法

    在Python中,frozenset是一種與set類似的數(shù)據(jù)類型,但具有不可變性,本文將詳細介紹frozenset的作用、用法以及與set的區(qū)別,感興趣的可以了解下
    2024-01-01
  • Python+tkinter實現(xiàn)制作文章搜索軟件

    Python+tkinter實現(xiàn)制作文章搜索軟件

    無聊的時候做了一個搜索文章的軟件,有沒有更加的方便快捷不知道,好玩就行了。軟件是利用Python和tkinter實現(xiàn)的,感興趣的可以嘗試一下
    2022-10-10
  • Tensorflow2.1 完成權(quán)重或模型的保存和加載

    Tensorflow2.1 完成權(quán)重或模型的保存和加載

    這篇文章主要為大家介紹了Tensorflow2.1 完成權(quán)重或模型的保存和加載,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • python游戲?qū)崙?zhàn)項目之智能五子棋

    python游戲?qū)崙?zhàn)項目之智能五子棋

    下五子棋嗎?信不信我讓你幾步你也贏不了?本篇為你帶來用python編寫的五子棋小游戲,文中給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值
    2021-09-09
  • python自動結(jié)束mysql慢查詢會話的實例代碼

    python自動結(jié)束mysql慢查詢會話的實例代碼

    這篇文章主要介紹了python自動結(jié)束mysql慢查詢會話,主要涉及到了mysql慢查詢會話查詢,定時任務(wù)的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2019-10-10
  • Python sqrt()函數(shù)用法說明

    Python sqrt()函數(shù)用法說明

    這篇文章主要介紹了Python sqrt()函數(shù)用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python機器學習應(yīng)用之決策樹分類實例詳解

    Python機器學習應(yīng)用之決策樹分類實例詳解

    決策樹(Decision?Tree)是在已知各種情況發(fā)生概率的基礎(chǔ)上,通過構(gòu)成決策樹來求取凈現(xiàn)值的期望值大于等于零的概率,評價項目風險,判斷其可行性的決策分析方法,是直觀運用概率分析的一種圖解法
    2022-01-01

最新評論