深度学习常用函数

1 numpy 常用函数

import numpy as np

np.repeat

data 为 numpy 数组 size 为扩充的大小 axis 为扩充的维度

data 数组的 第2维度 数据扩充为 size 大小, 如果 data.shape = (100,1), 扩充后结果为 (100,size), 以第2维数据作为填充值。

data.repeat(size, axis=1)  # 扩展数据

np.asarray

将列表等数据转换为 numpy 数组

data = np.asarray(data)

np.swapaxes

更换 data 数组的 0 - 1 维度

np.swapaxes(data, 0, 1)

np.expand_dims

扩展 data 的维度, axis 代表要扩维度的轴

 np.expand_dims(data, axis=3)     # 扩展维度

np.squeeze(axis=None)

去除 numpy 数组 轴 = 1 的对应维度

np.squeeze(axis=1)

np.concatenate((a1,…,a3), axis=None)

拼接 numpy 数组,可以拼接多个数组(需要保证维度一致)

numpy.concatenate((a1,a2,a3), axis=0)

np.repeat()

用于重复数组中的元素

params: a - numpy 数组, repeats - 重复次数(int), axis - 维度

numpy.repeat(a, repeats, axis=None)

使用 numpy 实现像 list append 操作一样的拼接效果

list 拼接然后转为 numpy 是最简单的方法, 但是numpy的直接拼接在某些场景也是必须的。

numpy实现

import numpy as np


if __name__ == '__main__':
    array = np.empty(4)		# 赋值空数组
    array = np.expand_dims(array, axis=0)		# 扩展维度
    for i in range(4):
        temp = np.asarray([i, i+1, i+2, i+3])
        temp = np.expand_dims(temp, axis=0)  # 扩展维度
        array = np.concatenate((array, temp), axis=0)  # 拼接
    array = array[1:]
    print(array)
    
>>>
[[0. 1. 2. 3.]
 [1. 2. 3. 4.]
 [2. 3. 4. 5.]
 [3. 4. 5. 6.]]

list实现

import numpy as np


if __name__ == '__main__':
    array = []
    for i in range(4):
        temp = [i, i+1, i+2, i+3]
        array.append(temp)
    array = np.asarray(array)
    print(array)
>>>
[[0 1 2 3]
 [1 2 3 4]
 [2 3 4 5]
 [3 4 5 6]]

2 torch 常用函数

torch.cuda.is_available

测试 GPU 是否可用

torch.cuda.is_available()

tensor.to

将数据转换到设备上进行运算

device= 'cpu' | 'cuda'

trainX.to(device)

torch.tensor

将 numpy 数据转换为 tensor 格式

torch.tensor(test_segments)

tensor.detach().numpy()

将 tensor 数据转换为 numpy 格式

test_predict.detach().numpy()

torch.unsqueeze(temp, dim=0)

增加 tensor 数组的维度

temp = torch.unsqueeze(temp, dim=0)

torch.squeeze()

缩减 tensor 数组的维度, 自动缩减 维度 = 1

temp = torch.squeeze()

torch.cat()

拼接多个 tensor 数组

results = torch.cat((results, temp), dim=0)

使用 torch 实现拼接

这个主要用在损失函数上, 自定义损失函数必须保证 tensor 格式,否则无法在张量图上做反向传播,则损失函数无效

import torch

if __name__ == '__main__':
    results = torch.empty(4)
    results = torch.unsqueeze(results, dim=0)
    for i in range(4):
        temp = torch.from_numpy(np.asarray([i, i+1, i+2, i+3]))
        temp = torch.unsqueeze(temp, dim=0)
        results = torch.cat((results, temp), dim=0)
    # 删除之前的未初始化的内容
    results = results[1:, :]
    print(results)
>>>
tensor([[0., 1., 2., 3.],
        [1., 2., 3., 4.],
        [2., 3., 4., 5.],
        [3., 4., 5., 6.]])

3 数据处理 API

train_test_split

from sklearn.model_selection import train_test_split

将数据集根据划分比例划分, 默认乱序。

test_size = 小数 | 整数 小数代表划分比例, 整数代表测试集个数

shuffle = True | False 开启或关闭乱序

X_train, X_test, y_train, y_test = train_test_split(self.train_data, self.train_label,test_size=1,random_state=None, shuffle=False)  	# 开启随机

metrics.r2_score()

from sklearn import metrics

对预测结果的置信度进行评价

metrics.r2_score(labels, prediction)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值