torch.tensor是torch.tensor與torch.empty的一種混合。
當傳入資料時,torch.tensor使用全域性預設的dtype——floattensor,而torch.tensor從資料中推斷資料型別。
import torch
t1=torch.
tensor(2
,3)t2=torch.
tensor([
[1,2
,3],
[4,5
,6]]
)t3=torch.
tensor([
[1.,2.
,3.],
[1.,2.
,3.]]
('the value of t1:{},the dtype of t1:{}'
.format
(t1,t1.
type()
('the value of t2:{},the dtype of t2:{}'
.format
(t2,t2.
type()
('the value of t3:{},the dtype of t3:{}'
.format
(t3,t3.
type()
))
此外,torch.tensor(1)返回固定值1,而torch.tensor(1)返回大小為1的張量,並隨機初始化值。the value of t1:
tensor([
[0.,0.
,0.],
[0.,0.
,0.]]
),the dtype of t1:torch.floattensor
the value of t2:
tensor([
[1,2
,3],
[4,5
,6]]
),the dtype of t2:torch.longtensor
the value of t3:
tensor([
[1.,2.
,3.],
[1.,2.
,3.]]
),the dtype of t3:torch.floattensor
Pytorch學習 1 pytorch簡介
pytorch簡介 1 pytorch簡介 1.1 pytorch的大概 pytorch不是簡單的封裝 lua torch 提供python介面,而是對當下tensor之上的模組進行重構,並增加了最先進的自動求導系統,成為當下最流行的動態圖框架。pytorch是乙個基於torch的python開源機...
Pytorch學習之旅 2 線性回歸實戰
回歸分析中,只包括乙個自變數和乙個因變數,且二者的關係可用一條直線近似表示,這種回歸分析稱為一元線性回歸分析,即 y w x b m od el y w x bmodel y w x b model y w x b採用最小平方法求解一元線性模型最優引數 w b。m se 1m i 1m ype rd...
Pytorch學習之旅 4 邏輯回歸實戰
資料處理 建立模型 選擇損失函式 選擇優化器 迭代訓練import torch import torch.nn as nn import matplotlib.pyplot as plt import numpy as np torch.manual seed 10 step 1 5 生成資料 sa...