[英](Conv1D) Tensorflow and Jax Resulting Different Outputs for The Same Input
I am trying to use conv1d functions to make a transposed convlotion repectively at jax and tensorflow. I read the documentation of both of jax and tensorflow for the con1d_transposed operation but they are resulting with different outputs for the same input.我正在尝试使用 conv1d 函数分别在 jax 和 tensorflow 处进行转置转换。我阅读了 jax 和 tensorflow 的文档以了解 con1d_transposed 操作,但它们对相同的输入产生不同的输出。
I can not find out what the problem is.我不知道是什么问题。 And I don't know which one produces the correct results.
而且我不知道哪一个会产生正确的结果。 Help me please.
请帮帮我。
My Jax Implementation (Jax Code)我的 Jax 实现(Jax 代码)
x = np.asarray([[[1, 2, 3, 4, -5], [1, 2, 3, 4, 5]]], dtype=np.float32).transpose((0, 2, 1))
filters = np.array([[[1, 0, -1], [-1, 0, 1]],
[[1, 1, 1], [-1, -1, -1]]],
dtype=np.float32).transpose((2, 1, 0))
kernel_rot = np.rot90(np.rot90(filters))
print(f"x strides: {x.strides}\nfilters strides: {kernel_rot.strides}\nx shape: {x.shape}\nfilters shape: {filters.shape}\nx: \n{x}\nfilters: \n{filters}\n")
dn1 = lax.conv_dimension_numbers(x.shape, filters.shape,('NWC', 'WIO', 'NWC'))
print(dn1)
res = lax.conv_general_dilated(x,kernel_rot,(1,),'SAME',(1,),(1,),dn1)
res = np.asarray(res)
print(f"result strides: {res.strides}\nresult shape: {res.shape}\nresult: \n{res}\n")
My TensorFlow Implementation (TensorFlow Code)我的TensorFlow实现(TensorFlow代码)
x = np.asarray([[[1, 2, 3, 4, -5], [1, 2, 3, 4, 5]]], dtype=np.float32).transpose((0, 2, 1))
filters = np.array([[[1, 0, -1], [-1, 0, 1]],
[[1, 1, 1], [-1, -1, -1]]],
dtype=np.float32).transpose((2, 1, 0))
print(f"x strides: {x.strides}\nfilters strides: {filters.strides}\nx shape: {x.shape}\nfilters shape: {filters.shape}\nx: \n{x}\nfilters: \n{filters}\n")
res = tf.nn.conv1d_transpose(x, filters, output_shape = x.shape, strides = (1, 1, 1), padding = 'SAME', data_format='NWC', dilations=1)
res = np.asarray(res)
print(f"result strides: {res.strides}\nresult shape: {res.shape}\nresult: \n{res}\n")
Output from the Jax来自 Jax 的 Output
result strides: (40, 8, 4)
result shape: (1, 5, 2)
result:
[[[ 0. 0.]
[ 0. 0.]
[ 0. 0.]
[10. 10.]
[ 0. 10.]]]
Output from the TensorFlow Output 从 TensorFlow
result strides: (40, 8, 4)
result shape: (1, 5, 2)
result:
[[[ 5. -5.]
[ 8. -8.]
[ 11. -11.]
[ 4. -4.]
[ 5. -5.]]]
Function conv1d_transpose
expects filters in shape [filter_width, output_channels, in_channels]
. Function
conv1d_transpose
需要形状为[filter_width, output_channels, in_channels]
的过滤器。 If filters
in snippet above were transposed to satisfy this shape, then for jax to return correct results, while computing dn1
parameter should be WOI
( W idth - O utput_channels - I nput_channels) and not WIO
( W idth - I nput_channels - O utput_channels).如果将上面代码段中的
filters
转置以满足此形状,那么为了让 jax 返回正确的结果,同时计算dn1
参数应该是WOI
( W idth - O utput_channels - I nput_channels)而不是WIO
( W idth - I nput_channels - O utput_channels) . After that:之后:
result.strides = (40, 8, 4)
result.shape = (1, 5, 2)
result:
[[[ -5., 5.],
[ -8., 8.],
[-11., 11.],
[ -4., 4.],
[ -5., 5.]]]
Results not same as with tensorflow, but kernels for jax were flipped, so actually that was expected.结果与 tensorflow 不同,但是 jax 的内核被翻转了,所以这实际上是预期的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.