机器学习_pytorch_神经网络基础

numpy 和 torch 对比

  1. numpy、torch的相互转换
import torch
import numpy as np
np_data = np.arange(6).reshape((2,3))
torch_data = torch.from_numpy(np_data)
tensor2array = torch_data.numpy()

print(np_data)
print(torch_data)
print(tensor2array)

运行结果:

[[0 1 2]
 [3 4 5]]
tensor([[0, 1, 2],
        [3, 4, 5]], dtype=torch.int32)
[[0 1 2]
 [3 4 5]]
  1. 数学运算
# abs 绝对值计算
data = [-1, -2, 1, 2]
tensor = torch.FloatTensor(data)  # 转换成32位浮点 tensor
print(
    '\nabs',
    '\nnumpy: ', np.abs(data),          # [1 2 1 2]
    '\ntorch: ', torch.abs(tensor)      # [1 2 1 2]
)

# sin   三角函数 sin
print(
    '\nsin',
    '\nnumpy: ', np.sin(data),      # [-0.84147098 -0.90929743  0.84147098  0.90929743]
    '\ntorch: ', torch.sin(tensor)  # [-0.8415 -0.9093  0.8415  0.9093]
)

# mean  均值
print(
    '\nmean',
    '\nnumpy: ', np.mean(data),         # 0.0
    '\ntorch: ', torch.mean(tensor)     # 0.0
)
  1. 矩阵运算
# matrix multiplication 矩阵点乘
data = [[1,2], [3,4]]
tensor = torch.FloatTensor(data)  # 转换成32位浮点 tensor
# correct method
print(
    '\nmatrix multiplication (matmul)',
    '\nnumpy: ', np.matmul(data, data),     # [[7, 10], [15, 22]] 或者用 data.dot(data)
    '\ntorch: ', torch.mm(tensor, tensor)   # [[7, 10], [15, 22]] 在torch中dot是限于一维数据,会将数据展平之后再运算
)

变量 Variable

  1. 变量Variable的定义
from torch.autograd import Variable
tensor = torch.FloatTensor([[1,2],[3,4]])
variable = Variable(tensor, requires_grad=True)
  1. 变量Variable与tensor的区别

    tensor 不能计算梯度,而Variable可以

v_out = torch.mean(variable*variable)
v_out.backward()
print(variable.grad)
  1. 获取Variable里的数据
print(variable)     #  Variable 形式
"""
Variable containing:
 1  2
 3  4
[torch.FloatTensor of size 2x2]
"""

print(variable.data)    # tensor 形式
"""
 1  2
 3  4
[torch.FloatTensor of size 2x2]
"""

print(variable.data.numpy())    # numpy 形式
"""
[[ 1.  2.]
 [ 3.  4.]]
"""

激励函数

import torch
import torch.nn.functional as F     # 激励函数都在这
from torch.autograd import Variable

x = torch.tensor([[1,2],[3,4]]).numpy()
# relu函数
y_relu = F.relu(x)
# sigmoid函数
y_sigmoid = F.sigmoid(x)
# tanh函数
y_tanh = F.tanh(x)
# softplus函数
y_softplus = F.softplus(x)
# softmax函数
y_softmax = F.softmax(x)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值