python - 有没有比使用 Reshape 更好的方法来重复张量内的图层?
问题描述
我想将 [Batch Size, 1] 形状的张量转换为 [Batch Size, Channel Size]。为此,我编写了以下算法:
@tf.function
def fit_channel_layers(input_tensor, target_tensor):
final_channel_layers = tf.shape(target_tensor)[-1]
input_tensor = tf.repeat(input_tensor, repeats=[final_channel_layers])
input_tensor = tf.reshape(input_tensor, [local_batch_size, final_channel_layers])
return input_tensor
input_tensor = tf.convert_to_tensor([[1.],[0.],[0.],[1.],[0.],[0.]])
target_tensor = tf.convert_to_tensor([[1.,1.,1.,1.,1.,1.],
[0.,0.,0.,0.,0.,0.],
[0.,0.,0.,0.,0.,0.],
[1.,1.,1.,1.,1.,1.],
[0.,0.,0.,0.,0.,0.],
[0.,0.,0.,0.,0.,0.]])
fit_channel_layers(input_tensor, target_tensor)
输出合适
array([[1., 1., 1., 1., 1., 1.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[1., 1., 1., 1., 1., 1.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]], dtype=float32)>
但是,我不确定使用 reshape 对性能的影响。我知道使用 numpy 可以使用逗号和乘数运算符轻松扩展 numpy 数组。但是,我想知道是否有比目前用@tf.function 图形代码编写的更好的方法来完成这项任务?
解决方案
推荐阅读
- c++ - VSC - IntelliSense 将布尔运算符标记为错误
- reactjs - 如何在 next.js 应用程序中加载自定义字体?
- javascript - extjs文件放在哪里?
- python - 我想在循环中的python中制作一个列表列表
- c++ - 二维数组遍历编码问题C++/递归函数没有返回正确的值
- php - 在 PHP 中使用 GPS 位置围绕线创建多边形
- extjs - 普通图像 Extjs 上的操作图标
- ios - 使用 Cloud Firestore 时位置是否重要?
- vue.js - 如何在本地将 Material Design Iconic Font 托管到 vue 应用中
- html - 用于在浏览器中打开的组合文件 HTML + CSS