鍓嶈█
澶у濂斤紝鎴戞槸闃垮厜銆?/p>
鏈笓鏍忔暣鐞嗕簡銆奝yTorch娣卞害瀛︿範椤圭洰瀹炴垬100渚嬨€嬶紝鍐呭寘鍚簡鍚勭涓嶅悓鐨勬繁搴﹀涔犻」鐩紝鍖呭惈椤圭洰鍘熺悊浠ュ強婧愮爜锛屾瘡涓€涓」鐩疄渚嬮兘闄勫甫鏈夊畬鏁寸殑浠g爜+鏁版嵁闆嗐€?/p>
姝e湪鏇存柊涓瓇 鉁?/p>
馃毃 鎴戠殑椤圭洰鐜锛?/p>
- 骞冲彴锛歐indows10
- 璇█鐜锛歱ython3.7
- 缂栬瘧鍣細PyCharm
- PyTorch鐗堟湰锛?.8.1
馃挜 椤圭洰涓撴爮锛氥€怭yTorch娣卞害瀛︿範椤圭洰瀹炴垬100渚嬨€?/p>
涓€銆佸熀浜嶭STM瀹炵幇鏄ヨ仈涓婅仈瀵逛笅鑱斻€?/h2>
銆婂ぉ瀵瑰湴锛岄洦瀵归锛屽悜閲忓鍔犵彮锛熻繖涓剳娲炴竻濂囩殑瀵硅仈AI锛屽ぇ瀹堕兘鐜╃柉浜嗐€嬶紝鐪嬪埌鐜板湪鐨勭敓鎴愭妧鏈凡缁忕倝鐏函闈掞紝鍋氬嚭鐨勬晠浜嬬湡鍋囬毦杈ㄣ€傛宸ф渶杩戝涔犱簡LSTM妯″瀷锛岄€氳繃鍙傝€冭澶氭枃鏈敓鎴愮殑鐩稿叧椤圭洰锛屽啓浜嗚繖涓嚜鍔ㄥ鏄ヨ仈鐨勯」鐩€?/p>
浜屻€佹暟鎹泦浠嬬粛
鑳屾櫙鎻忚堪
璇ユ暟鎹泦鍖呭惈浜嗚秴杩?0涓囧壇鐨勪腑鏂囧鑱旀暟鎹€?/p>
鏁版嵁璇存槑
鏁版嵁闆嗗寘鍚?涓枃浠讹細
- train_in.txt: 瀵硅仈鐨勪笂鑱斻€傛瘡琛岄兘鏄竴涓緭鍏ワ紝姣忎釜璇嶉兘鐢ㄧ┖鏍奸殧寮€銆?璁粌闆嗙殑杈撳叆)
- train_out.txt:瀵硅仈鐨勪笅鑱斻€傛瘡琛岄兘鏄竴涓緭鍑恒€傚搴旂潃 train_in.txt鐨勬瘡涓€琛屻€傛瘡涓瘝閮界敤绌烘牸闅斿紑銆?(璁粌闆嗙殑杈撳嚭)
- test_in.txt : 瀵硅仈鐨勪笂鑱斻€傛瘡琛岄兘鏄竴涓緭鍏ワ紝姣忎釜璇嶉兘鐢ㄧ┖鏍奸殧寮€銆?娴嬭瘯闆嗙殑杈撳叆)
- test_out.txt : 瀵硅仈鐨勪笅鑱斻€傛瘡琛岄兘鏄竴涓緭鍑恒€傚搴旂潃 test_in.txt鐨勬瘡涓€琛屻€傛瘡涓瘝閮界敤绌烘牸闅斿紑銆?(娴嬭瘯闆嗙殑杈撳嚭)
- vocabs: 璇嶆眹鏂囦欢锛屽畠灏嗙敤浜庡湪seq2seq妯″紡涓嬭繘琛岃缁冦€?/li>
鏁版嵁鏉ユ簮
https://www.heywhale.com/mw/dataset/5c46e6f42d8ef5002b736d6d/content
寰堟劅璋㈣繖浣嶅崥涓荤殑璐$尞銆?/p>
涓夈€佺綉缁滅粨鏋?/h2>
椤圭洰涓娇鐢ㄧ殑妯″瀷鏄疞STM锛屽湪妯″瀷涓垜浠畾涔変簡涓変釜缁勪欢锛屽垎鍒槸embedding灞?/code>锛?code>lstm灞?/code>鍜?code>鍏ㄨ繛鎺ュ眰
銆?/p>
- Embedding灞傦細灏嗘瘡涓瘝鐢熸垚瀵瑰簲鐨?code>宓屽叆鍚戦噺锛屽氨鏄埄鐢ㄤ竴涓繛缁瀷鍚戦噺鏉ヨ〃绀烘瘡涓瘝
- Lstm灞傦細鎻愬彇璇彞涓殑璇箟淇℃伅
- Linear灞傦細灏嗙粨鏋滄槧灏勬垚vocab_size澶у皬鐢ㄤ簬澶氬垎绫伙紝鍗虫瘡涓瓧鐨勬鐜?/li>
娉ㄦ剰锛氬湪LSTM缃戠粶涓繑鍥炵殑鍊间负姣忎竴涓椂闂寸墖
鐨勮緭鍑猴紝鑰屼笉鏄皢鏈€鍚庝竴涓猳utput鍏ㄩ儴杈撳嚭
# 瀹氫箟缃戠粶缁撴瀯
class LSTM(nn.Module):
def __init__(self, vocab_size, embedding_dim, hidden_dim, num_layers):
super(LSTM, self).__init__()
self.hidden_dim = hidden_dim
self.embeddings = nn.Embedding(vocab_size + 1, embedding_dim)
self.lstm = nn.LSTM(embedding_dim, hidden_dim, num_layers)
self.linear = nn.Linear(hidden_dim, vocab_size)
def forward(self, x):
time_step, batch_size = x.size() # 124, 16
embeds = self.embeddings(x)
output, (h_n, c_n) = self.lstm(embeds)
output = self.linear(output.reshape(time_step * batch_size, -1))
# 瑕佽繑鍥炴墍鏈夋椂闂寸偣鐨勬暟鎹紝姣忎釜鏃堕棿鐐瑰搴斾竴涓瓧锛屼篃灏辨槸vocab_size缁村害鐨勫悜閲?
return output
鍥涖€侀娴嬩笅鑱斿嚱鏁?/h2>
棣栧厛灏嗘垜浠渶瑕侀娴嬬殑涓婅仈浼犲叆鍑芥暟锛岀劧鍚庡皢杩欎釜涓婅仈鏄犲皠鎴愮浉搴旂殑搴忓彿锛屽舰鎴愭暟鍊煎悜閲忥紝鐒跺悗灏嗗叾杞垚tensor锛岀劧鍚庡姞杞芥ā鍨嬶紝鐒跺悗灏嗗叾閫佸叆妯″瀷锛岃幏寰椾笅鑱斿搴旂殑杈撳嚭锛岀劧鍚庡皢杈撳嚭鐨勫悜閲忚繘琛宎rgmax鑾峰彇棰勬祴鍑烘鐜囨渶澶х殑瀛楋紝鐒跺悗灏嗗叾鏄犲皠鎴愭眽瀛楀舰鎴愬彜璇椼€?/p>
def couplet_match(s):
# 灏嗗瓧绗︿覆杞负鏁板€?
x = [word2idx[word] for word in s]
# 灏嗘暟鍊煎悜閲忚浆涓簍ensor
x = torch.from_numpy(np.array(x).reshape(-1, 1))
# 鍔犺浇妯″瀷
model_path = './best_model.pkl'
model = LSTM(vocab_size=vocab_size, hidden_dim=hidden_dim,
embedding_dim=embedding_dim, num_layers=num_layers)
model.load_state_dict(torch.load(model_path, 'cpu'))
y = model(x)
y = y.argmax(axis=1)
r = ''.join([idx2word[idx.item()] for idx in y])
print('涓婅仈锛?s锛屼笅鑱旓細%s' % (s, r))
瀹屾暣婧愮爜
銆怭yTorch娣卞害瀛︿範椤圭洰瀹炴垬100渚嬨€戔€斺€?鍩轰簬LSTM瀹炵幇鏄ヨ仈涓婅仈瀵逛笅鑱?| 绗?4渚媉Bi 8 Bo鐨勫崥瀹?CSDN鍗氬
銆婂ぉ瀵瑰湴锛岄洦瀵归锛屽悜閲忓鍔犵彮锛熻繖涓剳娲炴竻濂囩殑瀵硅仈AI锛屽ぇ瀹堕兘鐜╃柉浜嗐€嬶紝鐪嬪埌鐜板湪鐨勭敓鎴愭妧鏈凡缁忕倝鐏函闈掞紝鍋氬嚭鐨勬晠浜嬬湡鍋囬毦杈ㄣ€傛宸ф渶杩戝涔犱簡LSTM妯″瀷锛岄€氳繃鍙傝€冭澶氭枃鏈敓鎴愮殑鐩稿叧椤圭洰锛屽啓浜嗚繖涓嚜鍔ㄥ鏄ヨ仈鐨勯」鐩€?/p>
浜屻€佹暟鎹泦浠嬬粛
鑳屾櫙鎻忚堪
璇ユ暟鎹泦鍖呭惈浜嗚秴杩?0涓囧壇鐨勪腑鏂囧鑱旀暟鎹€?/p>
鏁版嵁璇存槑
鏁版嵁闆嗗寘鍚?涓枃浠讹細
- train_in.txt: 瀵硅仈鐨勪笂鑱斻€傛瘡琛岄兘鏄竴涓緭鍏ワ紝姣忎釜璇嶉兘鐢ㄧ┖鏍奸殧寮€銆?璁粌闆嗙殑杈撳叆)
- train_out.txt:瀵硅仈鐨勪笅鑱斻€傛瘡琛岄兘鏄竴涓緭鍑恒€傚搴旂潃 train_in.txt鐨勬瘡涓€琛屻€傛瘡涓瘝閮界敤绌烘牸闅斿紑銆?(璁粌闆嗙殑杈撳嚭)
- test_in.txt : 瀵硅仈鐨勪笂鑱斻€傛瘡琛岄兘鏄竴涓緭鍏ワ紝姣忎釜璇嶉兘鐢ㄧ┖鏍奸殧寮€銆?娴嬭瘯闆嗙殑杈撳叆)
- test_out.txt : 瀵硅仈鐨勪笅鑱斻€傛瘡琛岄兘鏄竴涓緭鍑恒€傚搴旂潃 test_in.txt鐨勬瘡涓€琛屻€傛瘡涓瘝閮界敤绌烘牸闅斿紑銆?(娴嬭瘯闆嗙殑杈撳嚭)
- vocabs: 璇嶆眹鏂囦欢锛屽畠灏嗙敤浜庡湪seq2seq妯″紡涓嬭繘琛岃缁冦€?/li>
鏁版嵁鏉ユ簮
https://www.heywhale.com/mw/dataset/5c46e6f42d8ef5002b736d6d/content
寰堟劅璋㈣繖浣嶅崥涓荤殑璐$尞銆?/p>
涓夈€佺綉缁滅粨鏋?/h2>
椤圭洰涓娇鐢ㄧ殑妯″瀷鏄疞STM锛屽湪妯″瀷涓垜浠畾涔変簡涓変釜缁勪欢锛屽垎鍒槸embedding灞?/code>锛?code>lstm灞?/code>鍜?code>鍏ㄨ繛鎺ュ眰
銆?/p>
- Embedding灞傦細灏嗘瘡涓瘝鐢熸垚瀵瑰簲鐨?code>宓屽叆鍚戦噺锛屽氨鏄埄鐢ㄤ竴涓繛缁瀷鍚戦噺鏉ヨ〃绀烘瘡涓瘝
- Lstm灞傦細鎻愬彇璇彞涓殑璇箟淇℃伅
- Linear灞傦細灏嗙粨鏋滄槧灏勬垚vocab_size澶у皬鐢ㄤ簬澶氬垎绫伙紝鍗虫瘡涓瓧鐨勬鐜?/li>
娉ㄦ剰锛氬湪LSTM缃戠粶涓繑鍥炵殑鍊间负姣忎竴涓椂闂寸墖
鐨勮緭鍑猴紝鑰屼笉鏄皢鏈€鍚庝竴涓猳utput鍏ㄩ儴杈撳嚭
# 瀹氫箟缃戠粶缁撴瀯
class LSTM(nn.Module):
def __init__(self, vocab_size, embedding_dim, hidden_dim, num_layers):
super(LSTM, self).__init__()
self.hidden_dim = hidden_dim
self.embeddings = nn.Embedding(vocab_size + 1, embedding_dim)
self.lstm = nn.LSTM(embedding_dim, hidden_dim, num_layers)
self.linear = nn.Linear(hidden_dim, vocab_size)
def forward(self, x):
time_step, batch_size = x.size() # 124, 16
embeds = self.embeddings(x)
output, (h_n, c_n) = self.lstm(embeds)
output = self.linear(output.reshape(time_step * batch_size, -1))
# 瑕佽繑鍥炴墍鏈夋椂闂寸偣鐨勬暟鎹紝姣忎釜鏃堕棿鐐瑰搴斾竴涓瓧锛屼篃灏辨槸vocab_size缁村害鐨勫悜閲?
return output
鍥涖€侀娴嬩笅鑱斿嚱鏁?/h2>
棣栧厛灏嗘垜浠渶瑕侀娴嬬殑涓婅仈浼犲叆鍑芥暟锛岀劧鍚庡皢杩欎釜涓婅仈鏄犲皠鎴愮浉搴旂殑搴忓彿锛屽舰鎴愭暟鍊煎悜閲忥紝鐒跺悗灏嗗叾杞垚tensor锛岀劧鍚庡姞杞芥ā鍨嬶紝鐒跺悗灏嗗叾閫佸叆妯″瀷锛岃幏寰椾笅鑱斿搴旂殑杈撳嚭锛岀劧鍚庡皢杈撳嚭鐨勫悜閲忚繘琛宎rgmax鑾峰彇棰勬祴鍑烘鐜囨渶澶х殑瀛楋紝鐒跺悗灏嗗叾鏄犲皠鎴愭眽瀛楀舰鎴愬彜璇椼€?/p>
def couplet_match(s):
# 灏嗗瓧绗︿覆杞负鏁板€?
x = [word2idx[word] for word in s]
# 灏嗘暟鍊煎悜閲忚浆涓簍ensor
x = torch.from_numpy(np.array(x).reshape(-1, 1))
# 鍔犺浇妯″瀷
model_path = './best_model.pkl'
model = LSTM(vocab_size=vocab_size, hidden_dim=hidden_dim,
embedding_dim=embedding_dim, num_layers=num_layers)
model.load_state_dict(torch.load(model_path, 'cpu'))
y = model(x)
y = y.argmax(axis=1)
r = ''.join([idx2word[idx.item()] for idx in y])
print('涓婅仈锛?s锛屼笅鑱旓細%s' % (s, r))
瀹屾暣婧愮爜
銆怭yTorch娣卞害瀛︿範椤圭洰瀹炴垬100渚嬨€戔€斺€?鍩轰簬LSTM瀹炵幇鏄ヨ仈涓婅仈瀵逛笅鑱?| 绗?4渚媉Bi 8 Bo鐨勫崥瀹?CSDN鍗氬
椤圭洰涓娇鐢ㄧ殑妯″瀷鏄疞STM锛屽湪妯″瀷涓垜浠畾涔変簡涓変釜缁勪欢锛屽垎鍒槸embedding灞?/code>锛?code>lstm灞?/code>鍜?code>鍏ㄨ繛鎺ュ眰
銆?/p>
- Embedding灞傦細灏嗘瘡涓瘝鐢熸垚瀵瑰簲鐨?code>宓屽叆鍚戦噺锛屽氨鏄埄鐢ㄤ竴涓繛缁瀷鍚戦噺鏉ヨ〃绀烘瘡涓瘝
- Lstm灞傦細鎻愬彇璇彞涓殑璇箟淇℃伅
- Linear灞傦細灏嗙粨鏋滄槧灏勬垚vocab_size澶у皬鐢ㄤ簬澶氬垎绫伙紝鍗虫瘡涓瓧鐨勬鐜?/li>
娉ㄦ剰锛氬湪LSTM缃戠粶涓繑鍥炵殑鍊间负姣忎竴涓椂闂寸墖
鐨勮緭鍑猴紝鑰屼笉鏄皢鏈€鍚庝竴涓猳utput鍏ㄩ儴杈撳嚭
# 瀹氫箟缃戠粶缁撴瀯
class LSTM(nn.Module):
def __init__(self, vocab_size, embedding_dim, hidden_dim, num_layers):
super(LSTM, self).__init__()
self.hidden_dim = hidden_dim
self.embeddings = nn.Embedding(vocab_size + 1, embedding_dim)
self.lstm = nn.LSTM(embedding_dim, hidden_dim, num_layers)
self.linear = nn.Linear(hidden_dim, vocab_size)
def forward(self, x):
time_step, batch_size = x.size() # 124, 16
embeds = self.embeddings(x)
output, (h_n, c_n) = self.lstm(embeds)
output = self.linear(output.reshape(time_step * batch_size, -1))
# 瑕佽繑鍥炴墍鏈夋椂闂寸偣鐨勬暟鎹紝姣忎釜鏃堕棿鐐瑰搴斾竴涓瓧锛屼篃灏辨槸vocab_size缁村害鐨勫悜閲?
return output
鍥涖€侀娴嬩笅鑱斿嚱鏁?/h2>
棣栧厛灏嗘垜浠渶瑕侀娴嬬殑涓婅仈浼犲叆鍑芥暟锛岀劧鍚庡皢杩欎釜涓婅仈鏄犲皠鎴愮浉搴旂殑搴忓彿锛屽舰鎴愭暟鍊煎悜閲忥紝鐒跺悗灏嗗叾杞垚tensor锛岀劧鍚庡姞杞芥ā鍨嬶紝鐒跺悗灏嗗叾閫佸叆妯″瀷锛岃幏寰椾笅鑱斿搴旂殑杈撳嚭锛岀劧鍚庡皢杈撳嚭鐨勫悜閲忚繘琛宎rgmax鑾峰彇棰勬祴鍑烘鐜囨渶澶х殑瀛楋紝鐒跺悗灏嗗叾鏄犲皠鎴愭眽瀛楀舰鎴愬彜璇椼€?/p>
def couplet_match(s):
# 灏嗗瓧绗︿覆杞负鏁板€?
x = [word2idx[word] for word in s]
# 灏嗘暟鍊煎悜閲忚浆涓簍ensor
x = torch.from_numpy(np.array(x).reshape(-1, 1))
# 鍔犺浇妯″瀷
model_path = './best_model.pkl'
model = LSTM(vocab_size=vocab_size, hidden_dim=hidden_dim,
embedding_dim=embedding_dim, num_layers=num_layers)
model.load_state_dict(torch.load(model_path, 'cpu'))
y = model(x)
y = y.argmax(axis=1)
r = ''.join([idx2word[idx.item()] for idx in y])
print('涓婅仈锛?s锛屼笅鑱旓細%s' % (s, r))
瀹屾暣婧愮爜
銆怭yTorch娣卞害瀛︿範椤圭洰瀹炴垬100渚嬨€戔€斺€?鍩轰簬LSTM瀹炵幇鏄ヨ仈涓婅仈瀵逛笅鑱?| 绗?4渚媉Bi 8 Bo鐨勫崥瀹?CSDN鍗氬