pytorch中代码看不懂_Pytorch学习笔记(一)---- 基础语法

书上内容太多太杂,看完容易忘记,特此记录方便日后查看,所有基础语法以代码形式呈现,代码和注释均来源与书本和案例的整理。

# -*- coding: utf-8 -*-

# All codes and comments from <>

# Code url : https://github.com/zhouzhoujack/pytorch-book

# lesson_1 : Basic code syntax of PT(Pytorch)

import torch as t

import numpy as np

# 1.The version of torch

print(t.__version__)# version is 1.0.1

# 2.The defination of tensor

x = t.Tensor(5, 3) # 构建 5x3 矩阵,只是分配了空间,未初始化

x = t.Tensor([[1,2],[3,4]])

# 3.Initialize tensor by [0,1] uniform distribution

x = t.rand(5, 3)

print(x, x.size()[0]) # torch.Size 是tuple对象的子类,因此它支持tuple的所有操作,如x.size()[0]

# 4.Addition

x = t.rand(5, 3)

y = t.rand(5, 3)

print("first method: ", x + y)

print("second method: ", t.add(x,y))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值