新手:RNNs中W和U的概念理解
Newbie: Conceptual Understanding of W and U in RNNs
首先Post,所以请对我放宽心:)请post任何关于我的提问和论坛技巧的评论,我们将不胜感激!
我正在尝试了解构成 RNN 的矩阵大小和操作。我将介绍我已经了解的内容,希望我们都在同一页面上。 (或者你可以 TL;DR 到底部的问题)
X_Sets 是一个二维数组,它有一些正弦波值,Y_sets 是一个一维数组,它保存每个记录序列中的下一个正弦波值。这里的目标是准确预测正弦波的下一个值。
初始值:
learning_rate = 0.0001
nepoch = 25
T = 50 # sequence length
hidden_dim = 100
output_dim = 1
U = np.random.uniform(0, 1, (hidden_dim, T))
W = np.random.uniform(0, 1, (hidden_dim, hidden_dim))
V = np.random.uniform(0, 1, (output_dim, hidden_dim))
这是我目前正在使用的代码片段,它是前向传播函数的一部分。评论中的解释。
for i in range(Y_Sets.shape[0]):
#select the first record from both data sets and print out the sizes for all to see
x, y = X_Sets[i], Y_Sets[i]
print(Y_Sets.shape) #(100, 1)
print(X_Sets.shape) #(100, 50, 1)
print(x.shape) #(50, 1)
print(y.shape) #(1,)
#clear the prev_s values as the computed hidden values will be different for each record.
prev_s = np.zeros((hidden_dim, 1))
#loop for one record.
for t in range(T):
#new input array is 0'd every loop
new_input = np.zeros(x.shape)
#we only fill the array in the t'th position, everything else is 0
new_input[t] = x[t]
#See Question
mulu = np.dot(U, new_input)
#Same issue here
mulw = np.dot(W, prev_s) #why is W a 2D matrix?
add = mulw + mulu
s = sigmoid(add)
mulv = np.dot(V, s)
prev_s = s
问题:
我知道有 100 个隐藏层,每个隐藏层都有自己的 U,所以将每个单独的 x[t] 乘以 U 的列是有意义的。但是 - 在下一轮循环中,t将是 2,x[2] 将在第二列中,将由另一组 100 Us 进行点积。
现在 - 我被引导相信 RNN 的全部意义在于它们是有效的,因为 U、V 和 W 在整个序列上是恒定的,而在这里我们可以看到它们在序列上是不同的。
为什么?
编辑:这是我正在关注的指南:https://www.analyticsvidhya.com/blog/2019/01/fundamentals-deep-learning-recurrent-neural-networks-scratch-python/
我认为你错了。首先,只有一个隐藏层,有 100 个节点。其次,U 在每个时间步之后都没有变化,从代码片段看 U 是固定的,在看到整个序列后它可能会发生变化。 V 和 W 也一样。我在这里没有看到更新方程式。
首先Post,所以请对我放宽心:)请post任何关于我的提问和论坛技巧的评论,我们将不胜感激!
我正在尝试了解构成 RNN 的矩阵大小和操作。我将介绍我已经了解的内容,希望我们都在同一页面上。 (或者你可以 TL;DR 到底部的问题)
X_Sets 是一个二维数组,它有一些正弦波值,Y_sets 是一个一维数组,它保存每个记录序列中的下一个正弦波值。这里的目标是准确预测正弦波的下一个值。
初始值:
learning_rate = 0.0001
nepoch = 25
T = 50 # sequence length
hidden_dim = 100
output_dim = 1
U = np.random.uniform(0, 1, (hidden_dim, T))
W = np.random.uniform(0, 1, (hidden_dim, hidden_dim))
V = np.random.uniform(0, 1, (output_dim, hidden_dim))
这是我目前正在使用的代码片段,它是前向传播函数的一部分。评论中的解释。
for i in range(Y_Sets.shape[0]):
#select the first record from both data sets and print out the sizes for all to see
x, y = X_Sets[i], Y_Sets[i]
print(Y_Sets.shape) #(100, 1)
print(X_Sets.shape) #(100, 50, 1)
print(x.shape) #(50, 1)
print(y.shape) #(1,)
#clear the prev_s values as the computed hidden values will be different for each record.
prev_s = np.zeros((hidden_dim, 1))
#loop for one record.
for t in range(T):
#new input array is 0'd every loop
new_input = np.zeros(x.shape)
#we only fill the array in the t'th position, everything else is 0
new_input[t] = x[t]
#See Question
mulu = np.dot(U, new_input)
#Same issue here
mulw = np.dot(W, prev_s) #why is W a 2D matrix?
add = mulw + mulu
s = sigmoid(add)
mulv = np.dot(V, s)
prev_s = s
问题:
我知道有 100 个隐藏层,每个隐藏层都有自己的 U,所以将每个单独的 x[t] 乘以 U 的列是有意义的。但是 - 在下一轮循环中,t将是 2,x[2] 将在第二列中,将由另一组 100 Us 进行点积。
现在 - 我被引导相信 RNN 的全部意义在于它们是有效的,因为 U、V 和 W 在整个序列上是恒定的,而在这里我们可以看到它们在序列上是不同的。 为什么?
编辑:这是我正在关注的指南:https://www.analyticsvidhya.com/blog/2019/01/fundamentals-deep-learning-recurrent-neural-networks-scratch-python/
我认为你错了。首先,只有一个隐藏层,有 100 个节点。其次,U 在每个时间步之后都没有变化,从代码片段看 U 是固定的,在看到整个序列后它可能会发生变化。 V 和 W 也一样。我在这里没有看到更新方程式。