当我运行以下代码
from tensorflow import keras
import numpy as np
x = np.ones((1,2,1))
model = keras.models.Sequential()
model.add(keras.layers.GRU(
units = 1, activation='tanh', recurrent_activation='sigmoid',
use_bias=True, kernel_initializer='ones',
recurrent_initializer='ones',bias_initializer='zeros', return_sequences = True))
model.predict(x)
我得到输出=>Array ([[0.20482421], [0.34675306]]], dtype=float32)
当我手工操作时,我得到0.55
假设没有偏差且所有权重都设置为1
hidden_(t-1) = 0
update_gate = sigmoid(1x1 + 1x0) = 0.73
relance_gate = sigmoid(1x1 + 1x0) = 0.73
candidate_h(t) = tanh(1 × (0 × 0.73) + 1 × 1) = tanh(1) = 0.76
h(t) = 0.73*0.76 + (1 - 0.73)x0 = 0.55
那么第一个输出值不应该是0.55吗?
您似乎错误地将最后一行的等式替换为隐藏状态。
sigmoid(1 * 1 + 1 * 0) = 0.73105857863, tanh(1 * 1 + 1 * 0) = 0.761594155956
t H<子>子>= Z<子>t子>⊙H<子>t - 1子>+ (1 - Z<子>t子>)⊙H<一口>~一口><子>t子>
H,<子>t - 1子>= 0,这导致H<子>t子>= (1 - Z<子>t子>)⊙H<一口>~一口><子>t子>
根据我得到的GRU公式,h(t) = 0.73105857863 * 0 + (1 - 0.73105857863) x 0.761594155956 = 0.20482421480989209117972
匹配输出0.20482421
。
对于下一个时间步,
R<子>t子>=乙状结肠(1 * 1 + 1 * 0.20482421)=0.769381871687
Z<子>t子>=乙状结肠(1 * 1 + 1 * 0.20482421)=0.769381871687
H<一口>~一口><子>t子>=双曲正切(1 * 1 + 0.769381871687 * 0.20482421 * 1)=0.8202522791
Ht= 0.769381871687 * 0.20482421 + (1 - 0.769381871687) * 0.8202522791 =0.346753079407
与0.34675306
的最终输出相匹配。
,
https://d2l.ai/chapter_recurrent-modern/gru.html隐藏状态https://pytorch.org/docs/stable/generated/torch.nn.GRU.html