import numpy as np
def softmax(x):
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum(axis=0)
def rnn_cell_forward(xt, a_prev, parameters):
Wax = parameters["Wax"]
Waa = parameters["Waa"]
Wya = parameters["Wya"]
ba = parameters["ba"]
by = parameters["by"]
a_next = np.tanh(np.dot(Wax,xt)+np.dot(Waa,a_prev)+ba)
yt_pred = softmax(np.dot(Wya,a_next)+by)
cache = (a_next, a_prev, xt, parameters)
return a_next, yt_pred, cache
#测试
np.random.seed(1)
xt = np.random.randn(3,10)
a_prev = np.random.randn(5,10)
Waa = np.random.randn(5,5)
Wax = np.random.randn(5,3)
Wya = np.random.randn(2,5)
ba = np.random.randn(5,1)
by = np.random.randn(2,1)
parameters = {"Waa": Waa, "Wax": Wax, "Wya": Wya, "ba": ba, "by": by}
a_next, yt_pred, cache = rnn_cell_forward(xt, a_prev, parameters)
print("a_next[4] = ", a_next[4])
print("a_next.shape = ", a_next.shape)
print("yt_pred[1] =", yt_pred[1])
print("yt_pred.shape = ", yt_pred.shape)
样本数量 :10
xt:3*10 神经元个数:3 a_prev、a_next : 5*10 神经元个数:5 yt: 2*10 神经元个数:2
一个神经元一个偏置项 ba: 5*1 by: 2*1
Waa : 5*5 a_prev a_next 分别表示 a_prev 的第1,2,3,4,5个神经元到 a_next的第1个神经元的权重.
Wax : 5*3 xt a_next 分别表示 xt 的第 1,2,3 个神经元到a_next的第1个神经元的权重。
Wya:2*5 a_next yt_pred 分别表示 a_next的第1,2,3,4,5个神经元到 yt_pred 的第1个神经元的权重.
运行结果如下:
a_next[4] = [ 0.59584544 0.18141802 0.61311866 0.99808218 0.85016201 0.99980978
-0.18887155 0.99815551 0.6531151 0.82872037]
a_next.shape = (5, 10)
yt_pred[1] = [0.9888161 0.01682021 0.21140899 0.36817467 0.98988387 0.88945212
0.36920224 0.9966312 0.9982559 0.17746526]
yt_pred.shape = (2, 10)