首页 > 解决方案 > keras中conv1d中的矩阵乘法

问题描述

我试图了解 conv1d 的工作原理并尝试手动计算。下面是我写的代码

import tensorflow as tf

tf.random.set_seed(42)

# The inputs are 2-length vectors with 4 timesteps, and the batch size is 1
input_shape = (1, 4, 2)
x = tf.random.normal(input_shape)

# there are going to be 5 filters with kernel size is 3
conv_layer = tf.keras.layers.Conv1D(5, 3, activation='relu',input_shape=input_shape[1:])
y = conv_layer(x)
print('input vector', x)
print('output vector', y)
print('weights', conv_layer.weights[0])

这些给出以下输出。

input vector tf.Tensor(
[[[ 0.3274685 -0.8426258]
  [ 0.3194337 -1.4075519]
  [-2.3880599 -1.0392479]
  [-0.5573232  0.539707 ]]], shape=(1, 4, 2), dtype=float32)
output vector tf.Tensor(
[[[0.         0.7420608  0.         0.         0.        ]
  [0.         0.         1.2456826  0.04663306 0.        ]]], shape=(1, 2, 5), dtype=float32)
weights <tf.Variable 'conv1d_10/kernel:0' shape=(3, 2, 5) dtype=float32, numpy=
array([[[ 0.2008642 , -0.0165928 ,  0.46075237, -0.26492321,
          0.247114  ],
        [ 0.4196731 ,  0.47759396,  0.26654935, -0.16115183,
          0.05044025]],

       [[-0.25485608,  0.21096879, -0.4066371 ,  0.03724921,
          0.22973436],
        [ 0.40091085, -0.17139468, -0.34874785, -0.06216273,
          0.42850113]],

       [[-0.38695297, -0.40390682,  0.0806585 ,  0.47221655,
          0.44756472],
        [ 0.10378796,  0.11860859,  0.34301662,  0.35566896,
          0.41862184]]], dtype=float32)>

为了理解0.7420608输出向量中的值是如何存在的,这里是我所做的计算。但值不匹配。

((0.3274685 * -0.0165928  + -0.8426258 * 0.47759396) + (0.3194337 * 0.21096879 + -1.4075519 * -0.17139468) + (-2.3880599 * -0.40390682 + 0.539707 * 0.11860859)) / 3

上述计算的输出是0.30977946772426507。我犯了什么错误?

标签: pythontensorflowkerasconv-neural-network

解决方案


推荐阅读