python - *variable.shape 在 python 中是什么意思
问题描述
我知道"*variable_name"
协助打包和拆包。
但是variable_name.shape 是如何工作的呢?无法想象为什么以“”为前缀时会挤出第二个维度?
print("top_class.shape {}".format(top_class.shape))
top_class.shape torch.Size([64, 1])
print("*top_class.shape {}".format(*top_class.shape))
*top_class.shape 64
解决方案
因为numpy.array
它广泛用于数学相关和图像处理程序,.shape
描述了所有现有维度的数组大小:
>>> import numpy as np
>>> a = np.zeros((3,3,3))
>>> a
array([[[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]]])
>>> a.shape
(3, 3, 3)
>>>
星号将元组“解包”成几个单独的参数,在你的情况下(64,1)
变成64, 1
,所以只有第一个被打印,因为只有一个格式规范。
推荐阅读
- python - 在 groupby 中过滤,仅用于类似的列 python
- java - 我在哪里可以下载 Okta SAML Toolkit for Java Jar
- performance - 事件监听器与休息调用
- javascript - 为什么即使在预加载后浏览器也会多次加载音频文件?
- json - 在golang中使用HTTP Client for Places API,解组JSON和无效字符
- sql - 选择预览查询中不存在的行
- c# - 将 JSON 对象反序列化为嵌套的 C# 对象
- ios - 无法从 API 获取图像以在 Swift 的 UIImageView 中加载
- asp.net - 如何使用 ASP.NET 网站实现无人值守部署,而不是 Web 应用程序?
- android - 从 Android Studio '3.2 Canary 16' 升级到 '3.2 Beta 1' 后数据绑定编译错误