用numpy操作实现conv1d [英] Implementing conv1d with numpy operations

查看:343
本文介绍了用numpy操作实现conv1d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用numpy操作实现tensorflow的conv1d,暂时不考虑跨步和填充。我以为先前的问题,但今天意识到,当处理大于1的内核时,我仍然没有得到正确的答案。

I am trying to implement tensorflow's conv1d using numpy operations, ignoring strides and padding for now. I thought I understood it after my previous question but realized today that I was still not getting the right answer when dealing with kernels wider than 1.

所以现在我试图将tflearn用作模板,因为它可以为我计算内核形状。现在,我知道可以将卷积计算为矩阵乘法,因此我尝试相应地使用内核矩阵,但是我没有得到与tflearn相同的答案。检查源代码是非常不透明的,因为它只是调用tensorflow的专用编译实现。

So now I am trying to use tflearn as a template because it computes the kernel shape for me. Now that I understand that the convolution can be computed as a matrix multiplication I am attempting to use the kernel matrix accordingly, but I am not getting the same answer as tflearn. Examining the source code is quite opaque because it just calls out to tensorflow's dedicated compiled implementations.

到目前为止,这就是我得到的:

Here's what I've got so far:

inp = np.arange(10).reshape(1,10,1).astype(np.float32)
filters = 2
width = 3
z = tflearn.conv_1d(inp, filters, width, strides=1, padding='same', bias=False)
s = tf.Session()
s.run(tf.global_variables_initializer())
z1, w = s.run([z, z.W])
print('tflearn output shape', z1.shape)
print('tflearn kernel shape', w.shape)
print('numpy matmul shape', (inp @ w).shape)

这表明tflearn内核将宽度作为附加尺寸插入到开头:

This indicates that the tflearn kernel puts the width as an extra dimension inserted at the beginning:

tflearn output shape (1, 10, 2)
tflearn kernel shape (3, 1, 1, 2)
numpy matmul shape (3, 1, 10, 2)

因此,我得到的结果具有额外的 3 维度。很好,那么我如何正确地减小它以获得与tensorflow相同的答案?我尝试对这个维度求和,但这是不正确的:

Accordingly the result I get has that extra 3 dimension. Fine, so how do I correctly reduce it to get the same answer as tensorflow? I tried summing this dimension, but it is not correct:

print('tflearn output:')
print(z1)

print('numpy output:')
print(np.sum(inp @ w, axis=0))

给出,

tflearn output:
[[[-0.02252221  0.24712706]
  [ 0.49539018  1.0828717 ]
  [ 0.0315876   2.0945265 ]
  [-0.43221498  3.1061814 ]
  [-0.89601755  4.117836  ]
  [-1.3598201   5.129491  ]
  [-1.823623    6.141146  ]
  [-2.2874253   7.152801  ]
  [-2.7512276   8.164455  ]
  [-2.989808    6.7048397 ]]]
numpy output:
[[[ 0.          0.        ]
  [-0.46380258  1.0116549 ]
  [-0.92760515  2.0233097 ]
  [-1.3914077   3.0349646 ]
  [-1.8552103   4.0466194 ]
  [-2.319013    5.0582743 ]
  [-2.7828155   6.069929  ]
  [-3.2466178   7.0815845 ]
  [-3.7104206   8.093239  ]
  [-4.174223    9.104893  ]]]

其中重新明显不同。 zW 当然已经被初始化为随机值,所以这些数字也是随机的,但是我正在寻找使它们等于 z1的numpy计算。 ,因为它们执行相同的内核。显然,它并不像 inp @ w 那样简单。

which are clearly different. z.W has of course been initialized to random values so these numbers are random too, but I am looking for the numpy calculation that would make them equal to z1, since they are executing the same kernel. Clearly it is not as simple as inp @ w.

谢谢。

推荐答案

对不起,经过一番思考,我已经回答了我自己的问题……这是滑动窗口操作的来源,我试图在上一个问题中引入:

Okay sorry, I have answered my own question after some thought... THIS is where the sliding window operation comes in that I was trying to introduce in my previous question:

y = (inp @ w)
y[0,:,:-2,:] + y[1,:,1:-1,:] + y[2,:,2:,:]

gives,

array([[[ 0.49539018,  1.0828717 ],
        [ 0.0315876 ,  2.0945265 ],
        [-0.43221498,  3.1061814 ],
        [-0.89601755,  4.117836  ],
        [-1.3598201 ,  5.129491  ],
        [-1.823623  ,  6.141146  ],
        [-2.2874253 ,  7.152801  ],
        [-2.7512276 ,  8.164455  ]]], dtype=float32)

等于 z1 忽略了第一行和最后一行,完全是我期望的三点卷积。

which is equal to z1 ignoring the first and last rows, exactly what I'd expect from a 3-point convolution.

编辑:但是我有义务如果有人可以提出一种更简洁/有效的方式来表示滑动窗口。.我从我先前的问题出发,认为即使在矩阵乘法中也可以考虑滑动窗口,所以不幸的是必须明确地编写索引逻辑。

but I would be much obliged if someone could propose a more succinct / efficient way to express the sliding window.. I thought from my previous question that even the sliding window could be taken into account in the matrix multiplication, so it's unfortunate to need to write the indexing logic explicitly.

这篇关于用numpy操作实现conv1d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆