Jovian
⭐️
Sign In
In [8]:
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot([2,5,6],[6,3,1])
Out[8]:
[<matplotlib.lines.Line2D at 0x1e432ec7390>]
Notebook Image
In [13]:
plt.plot([2,4,6,8,10],[6,3,8,2,9],label='l')
plt.xlabel('x')
plt.ylabel('y')
plt.title('title')
plt.legend()
plt.text(5,5,'text')
plt.show()
Notebook Image
In [4]:
import torch
t2=torch.tensor([[0,1,2],[3,4,5]])
print(t2)
print('数据={}'.format(t2))
print(t2.reshape(3,2))
print(t2+1)
print('大小={}'.format(t2.size()))
print('维度={}'.format(t2.dim()))
print('元素个数={}'.format(t2.numel()))
tensor([[0, 1, 2], [3, 4, 5]]) 数据=tensor([[0, 1, 2], [3, 4, 5]]) tensor([[0, 1], [2, 3], [4, 5]]) tensor([[1, 2, 3], [4, 5, 6]]) 大小=torch.Size([2, 3]) 维度=2 元素个数=6
In [14]:
t1=torch.empty(2)
t2=torch.zeros(2,2)
t3=torch.ones(2,2,2)
t4=torch.full((2,2,2,2),3.)
print(t1,"\n",t2,"\n",t3,"\n",t4) 
tensor([0., 0.]) tensor([[0., 0.], [0., 0.]]) tensor([[[1., 1.], [1., 1.]], [[1., 1.], [1., 1.]]]) tensor([[[[3., 3.], [3., 3.]], [[3., 3.], [3., 3.]]], [[[3., 3.], [3., 3.]], [[3., 3.], [3., 3.]]]])
In [4]:
import torch
print("1=",torch.arange(0,4,step=1))
print("2=",torch.linspace(0,5,steps=4))
print("3=",torch.logspace(0,3,steps=4))
1= tensor([0, 1, 2, 3]) 2= tensor([0.0000, 1.6667, 3.3333, 5.0000]) 3= tensor([ 1., 10., 100., 1000.])
In [30]:
probs=torch.full((3,4),0.6)
print(probs)
print(torch.bernoulli(probs))#大小为(3,4)的0~1张量
weights=torch.tensor([[1,100],[100,1],[1,1]],dtype=torch.float32)#权重
print(torch.multinomial(weights,1))
print(torch.randperm(5,dtype=torch.float32))
print(torch.randint(low=0,high=4,size=(3,4)))
print(torch.randint_like(torch.ones(3,4),low=0,high=4))#默认情况下元素类型为torch.float32
tensor([[0.6000, 0.6000, 0.6000, 0.6000], [0.6000, 0.6000, 0.6000, 0.6000], [0.6000, 0.6000, 0.6000, 0.6000]]) tensor([[1., 0., 1., 1.], [0., 0., 0., 0.], [0., 0., 1., 1.]]) tensor([[1], [0], [1]]) tensor([0., 2., 3., 1., 4.]) tensor([[1, 0, 2, 1], [2, 2, 0, 3], [1, 2, 3, 2]]) tensor([[2., 3., 2., 0.], [2., 1., 1., 1.], [1., 1., 2., 1.]])
In [50]:
t=torch.arange(12).reshape(3,4)
print(t)
t234=torch.arange(24).view(2,3,4)
t2=torch.reciprocal(t234.float())#reciprocal 不能使用在long型
print(t2)
print(torch.pow(torch.arange(1,4),torch.arange(3)))
print(torch.mul(torch.ones(2,2),torch.ones(2,2)))#点积
print(torch.mm(torch.ones(2,2),torch.ones(2,2)))#叉乘
print(torch.ones(2,2)*torch.ones(2,2))
tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) tensor([[[ inf, 1.0000, 0.5000, 0.3333], [0.2500, 0.2000, 0.1667, 0.1429], [0.1250, 0.1111, 0.1000, 0.0909]], [[0.0833, 0.0769, 0.0714, 0.0667], [0.0625, 0.0588, 0.0556, 0.0526], [0.0500, 0.0476, 0.0455, 0.0435]]]) tensor([1, 2, 9]) tensor([[1., 1.], [1., 1.]]) tensor([[2., 2.], [2., 2.]]) tensor([[1., 1.], [1., 1.]])
In [65]:
t=torch.arange(5)
print('各元素方差={}'.format(t.float().var()))
print('各元素积={}'.format(t.prod()))
print('各元素最大值={}'.format(t.max()))
print('中位数={}'.format(t.median()))
print('第2大的数值={}'.format(t.kthvalue(2)))
print(t.cumsum(dim=0))
各元素方差=2.5 各元素积=0 各元素最大值=4 中位数=2 第2大的数值=torch.return_types.kthvalue( values=tensor(1), indices=tensor(1)) tensor([ 0, 1, 3, 6, 10])
In [68]:
#蒙特卡洛算法计算圆周率
import torch
import math
num_sample=10000000
sample=torch.rand(num_sample,2)
dist=sample.norm(p=2,dim=1)
ratio=(dist<1).float().mean()
pi=ratio*4
print('pi={}'.format(pi))
print(math.pi)
pi=3.141758918762207 3.141592653589793
In [ ]:
import jovian
jovian.commit()
[jovian] Saving notebook..
[jovian] Creating a new notebook on https://jvn.io [jovian] Please enter your API key (from https://jvn.io ): ········
[jovian] Error: The current API key is invalid or expired.
[jovian] Please enter your API key (from https://jvn.io ):