未来应该不太接触NLP,所以就大概看了下视频。主要完成了用numpy构造RNN和LSTM前向,后向不推导了。最后附基于LSTM参加比赛的代码,LSTM时间序列模型比赛利器。
RNN构造:
import numpy as np
import rnn_utils
def rnn_unit(apre, x, wax, waa, wya, ba, by):
a = np.tanh(np.dot(wax, x.T) + np.dot(waa, apre)+ba)
y = rnn_utils.softmax(np.dot(wya, a) + by) # ex/sum(ex)
return a, y
def rnn_init(apre, x, n, y):
np.random.seed(1)
wax = np.random.randn(n, x.shape[2]) # 10*14
waa = np.random.randn(n, apre.shape[0]) # 10*5
wya = np.random.randn(y.shape[2], n) # 10*10
ba = np.zeros((n,x.shape[1]))
by = np.zeros((y.shape[2],x.shape[1]))
param = {
"wax": wax,
"waa": waa,
"wya": wya,
"ba": ba,
"by": by
}
return param
def model(x, y, n): # 假设x维度为8*100*14 8个100*14的数据 y为8*100*10 8个含有10种分类的结果
a0 = np.random.randn(n, x.shape[1]) # 5*100
p = rnn_init(a0, x, n, y)
a1, y1 = rnn_unit(a0, x[0], p["wax"], p["waa"], p["wya"], p["ba"], p["by"])
a2, y2 = rnn_unit(a1, x[1], p["wax"], p["waa"], p["wya"], p["ba"], p["by"])
a3, y3 = rnn_unit(a2, x[2], p["wax"], p["waa"], p["wya"], p["ba"], p["by"])
a4, y4 = rnn_unit(a3, x[3], p["wax"], p["waa"], p["wya"], p["ba"], p["by"])
a5, y5 = rnn_unit(a4, x[4], p["wax"], p["waa"], p["wya"], p["ba"], p["by"])
a6, y6 = rnn_unit(a5, x[5], p["wax"], p["waa"], p["wya"], p["ba"], p["by"])
a7, y7 = rnn_unit(a6, x[6], p["wax"], p["waa"], p["wya"], p["ba"], p["by"])
a8, y8 = rnn_unit(a7, x[7], p["wax"], p["waa"], p["wya"], p["ba"], p["by"])
x = np.random.randn(8,100,14)
y = np.random.randn(8,100,10)
model(x,y,5)
LSTM构造:
import numpy as np
import rnn_utils
def LSTM_unit(x, apre, cpre, p):
wca = p["wca"]
wcx = p["wcx"]
bc = p["bc"]
wua = p["wua"]
wux = p["wux"]
bu = p["bu"]
wfa = p["wfa"]
wfx = p["wfx"]
bf = p["bf"]
woa = p["woa"]
wox = p["wox"]
bo = p["bo"]
wya = p["wya"]
by = p["by"]
c_temp = np.tanh(np.dot(wca, apre) + np.dot(wcx, x.T) + bc)
gateu = rnn_utils.sigmoid(np.dot(wua, apre) + np.dot(wux, x.T) + bu)
gatef = rnn_utils.sigmoid(np.dot(wfa, apre) + np.dot(wfx, x.T) + bf)
gateo = rnn_utils.sigmoid(np.dot(woa, apre) + np.dot(wox, x.T) + bo)
c = gateu * c_temp + gatef * cpre
a = gateo * np.tanh(c)
y_hat = rnn_utils.softmax(np.dot(wya, a) + by) # ex/sum(ex)
return a, c, y_hat
def LSTM_init(a, x, y, n):
wca = np.random.randn(n, a.shape[0])
wcx = np.random.randn(n, x.shape[2])
wua = np.random.randn(n, a.shape[0])
wux = np.random.randn(n, x.shape[2])
wfa = np.random.randn(n, a.shape[0])
wfx = np.random.randn(n, x.shape[2])
woa = np.random.randn(n, a.shape[0])
wox = np.random.randn(n, x.shape[2])
wya = np.random.randn(y.shape[2], n)
bc = np.zeros((n, x.shape[1]))
bu = np.zeros((n, x.shape[1]))
bf = np.zeros((n, x.shape[1]))
bo = np.zeros((n, x.shape[1]))
by = np.zeros((y.shape[2], x.shape[1]))
param = {
"wca": wca,
"wcx": wcx,
"wua": wua,
"wux": wux,
"wfa": wfa,
"wfx": wfx,
"woa": woa,
"wox": wox,
"wya": wya,
"bc": bc,
"bu": bu,
"bf": bf,
"bo": bo,
"by": by
}
return param
def model(x, y, n): # 假设x维度为8*100*14 8个100*14的数据 y为8*100*10 8个含有10种分类的结果
a0 = np.random.randn(n, x.shape[1]) # 1*100
c0 = np.random.randn(n, x.shape[1])
p = LSTM_init(a0, x, y, n)
a1, c1, y_hat1 = LSTM_unit(x[0], a0, c0, p)
print(a1.shape,c1.shape,y_hat1.shape)
a2, c2, y_hat2 = LSTM_unit(x[1], a1, c1, p)
a3, c3, y_hat3 = LSTM_unit(x[2], a2, c2, p)
a4, c4, y_hat4 = LSTM_unit(x[3], a3, c3, p)
a5, c5, y_hat5 = LSTM_unit(x[4], a4, c4, p)
a6, c6, y_hat6 = LSTM_unit(x[5], a5, c5, p)
a7, c7, y_hat7 = LSTM_unit(x[6], a6, c6, p)
a8, c8, y_hat8 = LSTM_unit(x[7], a7, c7, p)
x = np.random.randn(8,100,14)
y = np.random.randn(8,100,10)
model(x,y,5)
没怎么仔细学,只是根据公式打了代码。若有错误,敬请指正。
下面这个是我用keras构建LSTM模型参加比赛代码:根据上个月份出行数据,预测本月是否出行。
比赛还没结束,就不透露太多了。
def LSTM(train_x,train_y, test_x):
timesteps=8
data_dim=10
LSTM_train_x=train_x.loc[:,"2021-11-06home":"2021-11-28other_time"]
LSTM_test_x=test_x.loc[:,"2021-11-06home":"2021-11-28other_time"]
#display(LSTM_train_x)
LSTM_train_x=np.array(LSTM_train_x).reshape((LSTM_train_x.shape[0],timesteps,data_dim))
LSTM_test_x=np.array(LSTM_test_x).reshape((LSTM_test_x.shape[0],timesteps,data_dim))
#display(LSTM_train_x)
#display(LSTM_test_x)
model=ks.Sequential()
model.add(ks.layers.LSTM(4,input_shape=(timesteps,data_dim)))
model.add(ks.layers.Dropout(0.5))
model.add(ks.layers.Dense(1,activation="sigmoid"))
model.compile(loss='binary_crossentropy', optimizer=ks.optimizers.Adam(),
metrics=['accuracy'])
print(model.summary())
history = model.fit( LSTM_train_x, train_y, epochs=300, batch_size=512, verbose=15)
# plot history
train_pred = model.predict(LSTM_train_x)
test_pred = model.predict(LSTM_test_x)
return train_pred, test_pred
然后融合LGB和XGB模型,目前测试集f1 score:0.86
训练验证集f1 score:0.8991