first commit
This commit is contained in:
37
e1_preliminaries_ndarray.py
Normal file
37
e1_preliminaries_ndarray.py
Normal file
@ -0,0 +1,37 @@
|
||||
import torch
|
||||
|
||||
|
||||
def main():
|
||||
print('===== Chapter 1 =====')
|
||||
chapter_1()
|
||||
|
||||
|
||||
def chapter_1():
|
||||
x = torch.arange(12)
|
||||
print(f'x: {x}')
|
||||
print(f'x.shape: {x.shape}')
|
||||
print(f'x.numel(): {x.numel()}')
|
||||
xs = x.reshape(3, 4)
|
||||
print(f'x.reshape: {xs}')
|
||||
xs = x.reshape(-1, 4)
|
||||
print(f'x.reshape auto 1: {xs}')
|
||||
xs = x.reshape(3, -1)
|
||||
print(f'x.reshape auto 2: {xs}')
|
||||
|
||||
zeros = torch.zeros((2, 3, 4))
|
||||
print(f'zeros: {zeros}')
|
||||
ones = torch.ones((2, 3, 4))
|
||||
print(f'ones: {ones}')
|
||||
|
||||
randoms = torch.randn(3, 4)
|
||||
print(f'randn: {randoms}')
|
||||
|
||||
manual = torch.tensor([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
|
||||
print(f'manual: {manual}')
|
||||
# 看起来reshape第一个是行数
|
||||
manual = torch.tensor([2, 1, 4, 3, 1, 2, 3, 4, 4, 3, 2, 1]).reshape(3, -1)
|
||||
print(f'manual: {manual}')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user