一. torch.cat()函数解析
1. 函数说明
1.1 官网:torch.cat(),函数定义及参数说明如下图所示:

1.2 函数功能
函数将两个张量(tensor)按指定维度拼接在一起,注意:除拼接维数dim数值可不同外其余维数数值需相同,方能对齐,如下面例子所示。torch.cat()函数不会新增维度,而torch.stack()函数会新增一个维度,相同的是两个都是对张量进行拼接
; 2. 代码举例
2.1 输入两个二维张量(dim=0):dim=0对行进行拼接
a = torch.randn(2,3)
b = torch.randn(3,3)
c = torch.cat((a,b),dim=0)
a,b,c
输出结果如下:
(tensor([[-0.90, -0.37, 1.96],
[-2.65, -0.60, 0.05]]),
tensor([[ 1.30, 0.24, 0.27],
[-1.99, -1.09, 1.67],
[-1.62, 1.54, -0.14]]),
tensor([[-0.90, -0.37, 1.96],
[-2.65, -0.60, 0.05],
[ 1.30, 0.24, 0.27],
[-1.99, -1.09, 1.67],
[-1.62, 1.54, -0.14]]))
2.2 输入两个二维张量(dim=1): dim=1对列进行拼接
a = torch.randn(2,3)
b = torch.randn(2,4)
c = torch.cat((a,b),dim=1)
a,b,c
输出结果如下:
(tensor([[-0.55, -0.84, -1.60],
[ 0.39, -0.96, 1.02]]),
tensor([[-0.83, -0.09, 0.05, 0.17],
[ 0.28, -0.74, -0.27, -0.85]]),
tensor([[-0.55, -0.84, -1.60, -0.83, -0.09, 0.05, 0.17],
[ 0.39, -0.96, 1.02, 0.28, -0.74, -0.27, -0.85]]))
2.3 输入两个三维张量:dim=0 对通道进行拼接
a = torch.randn(2,3,4)
b = torch.randn(1,3,4)
c = torch.cat((a,b),dim=0)
a,b,c
输出结果如下:
(tensor([[[ 0.51, -0.72, -0.02, 0.76],
[ 0.72, 1.01, 0.39, -0.13],
[ 0.37, -0.63, -2.69, 0.74]],
[[ 0.72, -0.31, -0.27, 0.10],
[ 1.66, -0.06, 1.91, -0.66],
[ 0.34, -0.23, -0.18, -1.22]]]),
tensor([[[ 0.94, 0.77, -0.41, -1.20],
[-0.23, -1.03, -0.25, 1.67],
[-1.00, -0.68, -0.35, -0.50]]]),
tensor([[[ 0.51, -0.72, -0.02, 0.76],
[ 0.72, 1.01, 0.39, -0.13],
[ 0.37, -0.63, -2.69, 0.74]],
[[ 0.72, -0.31, -0.27, 0.10],
[ 1.66, -0.06, 1.91, -0.66],
[ 0.34, -0.23, -0.18, -1.22]],
[[ 0.94, 0.77, -0.41, -1.20],
[-0.23, -1.03, -0.25, 1.67],
[-1.00, -0.68, -0.35, -0.50]]]))
2.4 输入两个三维张量:dim=1对行进行拼接
a = torch.randn(2,3,4)
b = torch.randn(2,4,4)
c = torch.cat((a,b),dim=1)
a,b,c
输出结果如下:
(tensor([[[-0.86, 0.00, -1.26, 1.20],
[-0.46, -1.08, -0.82, 2.03],
[-0.89, 0.43, 1.92, 0.49]],
[[ 0.24, -0.02, 0.32, 0.97],
[ 0.33, -1.34, 0.76, -1.55],
[ 0.38, 1.45, 0.27, -0.64]]]),
tensor([[[ 0.82, 0.85, -0.30, -0.58],
[-0.09, 0.40, 0.02, 0.75],
[-0.70, 0.67, -0.88, -0.50],
[-0.62, -1.65, -1.10, -1.39]],
[[-0.85, -1.61, -0.35, -0.56],
[ 0.00, 1.40, 0.41, 0.39],
[-0.01, 0.04, 0.80, 0.41],
[-1.21, -0.64, 1.14, 1.64]]]),
tensor([[[-0.86, 0.00, -1.26, 1.20],
[-0.46, -1.08, -0.82, 2.03],
[-0.89, 0.43, 1.92, 0.49],
[ 0.82, 0.85, -0.30, -0.58],
[-0.09, 0.40, 0.02, 0.75],
[-0.70, 0.67, -0.88, -0.50],
[-0.62, -1.65, -1.10, -1.39]],
[[ 0.24, -0.02, 0.32, 0.97],
[ 0.33, -1.34, 0.76, -1.55],
[ 0.38, 1.45, 0.27, -0.64],
[-0.85, -1.61, -0.35, -0.56],
[ 0.00, 1.40, 0.41, 0.39],
[-0.01, 0.04, 0.80, 0.41],
[-1.21, -0.64, 1.14, 1.64]]]))
2.5 输入两个三维张量:dim=2对列进行拼接
a = torch.randn(2,3,4)
b = torch.randn(2,3,5)
c = torch.cat((a,b),dim=2)
a,b,c
输出结果如下:
(tensor([[[ 0.13, -0.02, 0.13, -0.25],
[ 1.42, -0.22, -0.87, 0.27],
[-0.07, 1.04, -0.06, 0.91]],
[[ 0.88, -1.46, 0.04, 0.35],
[ 1.36, 0.64, 0.75, 0.39],
[ 0.36, 1.13, 0.83, 0.56]]]),
tensor([[[-0.47, -2.30, -0.49, -1.02, 1.74],
[ 0.71, 0.89, 0.80, -0.05, -1.35],
[-0.40, 0.26, -0.78, -1.50, -0.92]],
[[-0.77, -0.01, 1.23, 0.70, -0.66],
[ 0.28, -0.18, -0.91, 2.23, 1.14],
[-1.93, -0.17, 0.15, 0.40, 0.32]]]),
tensor([[[ 0.13, -0.02, 0.13, -0.25, -0.47, -2.30, -0.49, -1.02, 1.74],
[ 1.42, -0.22, -0.87, 0.27, 0.71, 0.89, 0.80, -0.05, -1.35],
[-0.07, 1.04, -0.06, 0.91, -0.40, 0.26, -0.78, -1.50, -0.92]],
[[ 0.88, -1.46, 0.04, 0.35, -0.77, -0.01, 1.23, 0.70, -0.66],
[ 1.36, 0.64, 0.75, 0.39, 0.28, -0.18, -0.91, 2.23, 1.14],
[ 0.36, 1.13, 0.83, 0.56, -1.93, -0.17, 0.15, 0.40, 0.32]]]))
Original: https://blog.csdn.net/flyingluohaipeng/article/details/125038212
Author: cv_lhp
Title: Pytorch中torch.cat()函数解析
相关阅读
Title: [python]从环境变量和配置文件中获取配置参数
python学习笔记之从环境变量和配置文件中获取配置参数
前言
从环境变量和配置文件中获取配置参数,相关库:
python-dotenv
:第三方库,需要使用pip安装configparser
:标准库
示例代码
- test.ini
[mysql]
host = "192.168.0.10"
port = 3306
user = "root"
password = "123456"
[postgresql]
host = "192.168.0.11"
port = 5432
user = "postgres"
password = "123456"
- demo.py
from configparser import ConfigParser, NoSectionError, NoOptionError
from dotenv import load_dotenv
import os
# 如果存在环境变量的文件,则加载配置到环境变量
if os.path.exists("settings.env"):
load_dotenv("settings.env")
os_env = os.environ
def read_config(filename: str) -> ConfigParser:
"""
从文件中读取配置信息
Parameters
----------
filename : str, 配置文件
"""
# 实例化对象
config = ConfigParser()
if not os.path.exists(filename):
raise FileNotFoundError(f"配置文件 {filename} 不存在")
config.read(filename, encoding="utf-8")
return config
def get_config(config: ConfigParser, section: str, key: str):
"""
根据指定section和key获取value
Parameters
----------
config: ConfigParser(), 配置实例对象
section: str, 配置文件中的区域
key: str, 配置的参数名
"""
# 优先从环境变量中获取配置参数, 没有的话再从配置文件中获取
value = os_env.get(key, "")
if not value:
try:
value = config.get(section, key)
except (NoOptionError, NoSectionError):
# 没有的话就返回None
value = None
return value
if __name__ == '__main__':
config = read_config("test.ini")
print(get_config(config, "mysql", "host"))
Original: https://www.cnblogs.com/XY-Heruo/p/16539682.html
Author: 花酒锄作田
Title: [python]从环境变量和配置文件中获取配置参数
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/282633/
转载文章受原作者版权保护。转载请注明原作者出处!