torch.mean()

mean()函数的参数:dim=0,按列求平均值,返回的形状是(1,列数);dim=1,按行求平均值,返回的形状是(行数,1),默认不设置dim的时候,返回的是所有元素的平均值。

x=torch.arange(12).view(4,3)
'''
注意:在这里使用的时候转一下类型,否则会报RuntimeError: Can only calculate the mean of floating types. Got Long instead.的错误。
查看了一下x元素类型是torch.int64,根据提示添加一句x=x.float()转为tensor.float32就行
'''
x=x.float()
x_mean=torch.mean(x)
x_mean0=torch.mean(x,dim=0,keepdim=True)
x_mean1=torch.mean(x,dim=1,keepdim=True)
print('x:')
print(x)
print('x_mean0:')
print(x_mean0)
print('x_mean1:')
print(x_mean1)
print('x_mean:')
print(x_mean)

查看了一下x元素类型是torch.int64,根据提示添加一句x=x.float()转为tensor.float32就行
输出结果:

x:
tensor([[ 0.,  1.,  2.],
        [ 3.,  4.,  5.],
        [ 6.,  7.,  8.],
        [ 9., 10., 11.]])
x_mean0:
tensor([[4.5000, 5.5000, 6.5000]])
x_mean1:
tensor([[ 1.],
        [ 4.],
        [ 7.],
        [10.]])
x_mean:
tensor(5.5000)

torch.mean().mean()

x=torch.arange(24).view(4,3,2)
x=x.float()
x_mean=torch.mean(x)
print(x)
print(x.mean())
print(x.mean(dim=0,keepdim=True).mean(dim=1,keepdim=True).mean(dim=2,keepdim=True))
print(x.mean(dim=1,keepdim=True).mean(dim=2,keepdim=True))

输出:

tensor([[[ 0.,  1.],
         [ 2.,  3.],
         [ 4.,  5.]],

        [[ 6.,  7.],
         [ 8.,  9.],
         [10., 11.]],

        [[12., 13.],
         [14., 15.],
         [16., 17.]],

        [[18., 19.],
         [20., 21.],
         [22., 23.]]])
tensor(11.5000)
tensor([[[11.5000]]])
tensor([[[ 2.5000]],

        [[ 8.5000]],

        [[14.5000]],

        [[20.5000]]])

torch.mean()和torch.mean(dim=0).mean(dim=1)的区别

以二维为例:torch.mean()返回的是一个标量,而torch.mean(dim=0).mean(dim=1)返回的是一个1行1列的张量,虽然数值相同

x=torch.arange(12).view(4,3)
x=x.float()
x_mean=torch.mean(x)
print(x_mean)
y= x.mean(dim=0, keepdim=True).mean(dim=1, keepdim=True)
print(y)

输出:

tensor(5.5000)
tensor([[5.5000]])
UTF-8和GBK是两种不同的字符编码方式,UTF-8是一种变长编码,而GBK是一种固定长度编码。在C语言中,可以通过使用相关库函数来实现UTF-8和GBK的转换。 首先,要将UTF-8编码的字符转换为GBK编码的字符,可以使用iconv函数。iconv函数是一个在C语言中进行字符集转换的库函数,它可以将一个字符由一种编码方式转换为另一种编码方式。首先,需要先使用iconv_open函数来打开一个转换句柄,将源字符串的编码方式指定为UTF-8,目标字符串的编码方式指定为GBK。然后,使用iconv函数将源字符串转换为目标字符串。 示例代码如下: ```c #include <iconv.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *source = "UTF-8编码的字符串"; char *target = malloc(sizeof(char) * (strlen(source) + 1)); // 打开转换句柄 iconv_t conv = iconv_open("GBK", "UTF-8"); if (conv == (iconv_t)(-1)) { perror("转换句柄打开失败"); exit(1); } // 进行转换 size_t inBytes = strlen(source); size_t outBytes = strlen(source) + 1; size_t ret = iconv(conv, &source, &inBytes, &target, &outBytes); if (ret == (size_t)(-1)) { perror("转换失败"); exit(1); } // 关闭转换句柄 iconv_close(conv); printf("GBK编码的字符串:%s\n", target); free(target); return 0; } ``` 同样地,要将GBK编码的字符转换为UTF-8编码的字符,只需要将上述示例代码中的源编码和目标编码对调即可。 以上就是在C语言中如何进行UTF-8和GBK编码的转换的简要说明。通过使用iconv函数,我们可以在C语言中轻松实现这两种编码方式之间的转换。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值