我有一个 B (批量大小)乘以 F (特征计数) mask 张量spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/football2020db
spring.datasource.username = root
spring.datasource.password = password
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
,喜欢对输入M
应用(逐元素乘法)。
...事实是,我的x
的原始特征列已转换为非恒定宽度的嵌入,因此其总尺寸为 B 用 E (嵌入式尺寸)。
我的代码草稿如下:
x
我的问题是:我在这里缺少任何明显的优化吗?这是for循环的正确方法吗?还是有一些更直接的方法来实现这一目标?
我可以控制嵌入过程,因此可以存储任何有用的表示形式,例如不受# Given something like:
M = torch.Tensor([[0.2, 0.8], [0.5, 0.5], [0.6, 0.4]]) # B=3, F=2
x = torch.Tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 0], [11, 12, 13, 14, 15]]) # E=5
feature_sizes = [2, 3] # (Feature 0 embedded to 2 cols, feature 1 to 3)
# In forward() pass:
components = []
for ix, size in enumerate(feature_sizes):
components.append(M[:, ix].view(-1, 1).expand(-1, size))
M_x = torch.cat(components, dim=1)
# Now M_x is (B, E) and can be mapped with x
> M_x = torch.Tensor([
> [0.2, 0.4, 2.4, 3.6, 4],
> [3, 3.5, 4, 4.5, 0],
> [6.6, 7.2, 5.2, 5.6, 6],
> ])
个整数列表的约束。
答案 0 :(得分:0)
Du,我忘了:索引操作可以做到这一点!
鉴于上述情况(但我将使用更复杂的feature_sizes
进行更清晰地展示),我们可以使用类似以下内容的索引张量进行预计算:
# Given that:
feature_sizes = [1, 3, 1, 2]
# Produce nested list e.g. [[0], [1, 1, 1], [2], [3, 3]]:
ixs_per_feature = [[ix] * size for ix, size in enumerate(feature_sizes)]
# Flatten out into a vector e.g. [0, 1, 1, 1, 2, 3, 3]:
mask_ixs = torch.LongTensor(
[item for sublist in ixs_per_feature for item in sublist]
)
# Now can directly produce M_x by indexing M:
M_x = M[:, mask_ixs]
通过使用此方法而不是for循环,我得到了适度的加速。