【发布时间】:2015-09-29 23:06:58
【问题描述】:
我正在尝试完成以下功能,但我遇到了索引问题,导致“ValueError:操作数无法与形状(0,9)(5)一起广播”。
我认为我的错误可能来自我尝试从 ssd_difference[] 调用值的方式,但我不完全确定。
另外,根据下面给出的提示,我将如何使用 convolve2d?我知道 numpy 有一个功能,但我不知道我需要投入什么才能让它工作。
附加信息:binomialFilter5() 返回代表二项式过滤器的 dtype float 的 5x1 numpy 数组。我还假设“weights[]”是 ssd_difference[] 值。
def transitionDifference(ssd_difference):
""" 计算帧之间的转换成本,将动态纳入 帐户。
说明:
1.遍历ssd差异的行列,忽略
前两个值和后两个值。
1a。对于 i, j 处的每个值,乘以长度的二项式滤波器
五(稍后在代码中实现)由两个开始的权重
之前的帧直到之后的两帧,然后取这些的总和
产品。
i.e. Your weights for frame i are:
[weight[i - 2, j - 2],
weight[i - 1, j - 1],
weight[i, j],
weight[i + 1, j + 1],
weight[i + 2, j + 2]]
乘以每个 i, j 的二项式过滤器权重得到 你的输出。
可能需要一点理解才能了解我们为什么会这样 计算这个,简单的解释是从 第 4 帧到第 5 帧,我们称之为 ch(4, 5),我们将这个权重设为:
ch(4, 5) = ch(2, 3) + ch(3, 4) + ch(4, 5) + ch(5, 6) + ch(6, 7)
这说明了以前的变化和未来的权重 考虑当前帧时发生变化。
当然,我们通过二项式过滤器对所有这些总和进行加权,所以 权重 ch(4, 5) 仍然是最重要的,但是 希望这能让您更好地理解。
Args:
ssd_difference (numpy.ndarray): A difference matrix as produced by your
ssd function.
Returns:
output (numpy.ndarray): A difference matrix that takes preceding and
following frames into account. The output
difference matrix should have the same dtype as
the input, but be 4 rows and columns smaller,
corresponding to only the frames that have valid
dynamics.
Hint: There is an efficient way to do this with 2d convolution. Think about
the coordinates you are using as you consider the preceding and
following frame pairings.
"""
output = np.zeros((ssd_difference.shape[0] - 4,
ssd_difference.shape[1] - 4), dtype=ssd_difference.dtype)
# WRITE YOUR CODE HERE.
for i in range(len(ssd_difference)):
for j in range(len(ssd_difference)):
if i == 0:
if j > 1:
output[i,j] = np.sum( ssd_difference[i-2:i+2]*binomialFilter5())
elif i == ssd_difference.shape[0] - 1:
if j < ssd_difference.shape[1] - 2:
output[i,j] = np.sum( ssd_difference[i-2:i+2]*binomialFilter5())
else:
output[i,j] = np.sum( ssd_difference[i-2:i+2]*binomialFilter5())
# END OF FUNCTION.
return output
【问题讨论】:
-
向我们展示更多
ValueError,特别是标有---->的行。关注具有shapes (0,9) (5)的操作数,以及试图组合它们的operation是什么。 -
对不起,该行是 output[i,j] = np.sum( ssd_difference[i-2:i+2]*binomialFilter5()) 我知道我不能将一个矩阵相乘大小不同,但我想不出任何其他方式来实现该功能的目的(至少根据说明)