深度学习-循环神经网络(RNN)
生活随笔
收集整理的這篇文章主要介紹了
深度学习-循环神经网络(RNN)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
作者: 明天依舊可好
QQ交流群: 807041986
注:關于深度學習的相關問題,若本文未涉及可在下方留言告訴我,我會在文章中進行補充的。
原文鏈接:https://mtyjkh.blog.csdn.net/article/details/111088248
深度學習系列:深度學習(TensorFlow 2)簡單入門
代碼|數據: 微信公眾號(明天依舊可好)中回復:深度學習
導入數據
import pandas as pd import tensorflow as tf import osdf = pd.read_csv("Tweets.csv",usecols=["airline_sentiment","text"]) df # categorical 實際上是計算一個列表型數據中的類別數,即不重復項, # 它返回的是一個CategoricalDtype 類型的對象,相當于在原來數據上附加上類別信息 , # 具體的類別可以通過和對應的序號可以通過 codes 和 categories df.airline_sentiment = pd.Categorical(df.airline_sentiment).codes df建立詞匯表
import tensorflow_datasets as tfds import ostokenizer = tfds.features.text.Tokenizer()vocabulary_set = set() for text in df["text"]:some_tokens = tokenizer.tokenize(text)vocabulary_set.update(some_tokens)vocab_size = len(vocabulary_set) vocab_size ''' 輸出: 18027 '''樣本編碼(測試)
encoder = tfds.features.text.TokenTextEncoder(vocabulary_set) encoded_example = encoder.encode(text) print(encoded_example) ''' text為: '@AmericanAir we have 8 ppl so we need 2 know how many seats are on the next flight. Plz put us on standby for 4 people on the next flight?' 輸出: [12939, 13052, 13579, 11267, 14825, 8674, 13052, 12213, 12082, 12156, 5329, 5401, 10099, 3100, 7974, 7804, 5671, 2947, 9873, 7864, 9704, 7974, 3564, 11759, 15266, 11250, 7974, 7804, 5671, 2947] '''將文本編碼成數字形式
df["encoded_text"] = [encoder.encode(text) for text in df["text"]] df train_x = df["encoded_text"][:10000] train_y = df["airline_sentiment"][:10000] test_x = df["encoded_text"][10000:] test_y = df["airline_sentiment"][10000:]from tensorflow import keras train_x = keras.preprocessing.sequence.pad_sequences(train_x,maxlen=50) test_x = keras.preprocessing.sequence.pad_sequences(test_x,maxlen=50)train_x.shape,train_y.shape,test_x.shape,test_y.shape ''' 輸出: ((10000, 50), (10000,), (4640, 50), (4640,)) '''構建模型
import tensorflow as tfmodel = tf.keras.Sequential([tf.keras.layers.Embedding(vocab_size+1, 64),tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),tf.keras.layers.Dense(64, activation='relu'),tf.keras.layers.Dense(1) ])model.summary() ''' 輸出: Model: "sequential_1" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= embedding_2 (Embedding) (None, None, 64) 1153792 _________________________________________________________________ bidirectional_2 (Bidirection (None, 128) 66048 _________________________________________________________________ dense_4 (Dense) (None, 64) 8256 _________________________________________________________________ dense_5 (Dense) (None, 1) 65 ================================================================= Total params: 1,228,161 Trainable params: 1,228,161 Non-trainable params: 0 _________________________________________________________________ '''激活
model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),optimizer=tf.keras.optimizers.Adam(1e-4),metrics=['accuracy'])訓練模型
history = model.fit(train_x,train_y,epochs=20,batch_size=200,validation_data=(test_x, test_y),verbose=1) ''' 輸出: Epoch 1/20 50/50 [==============================] - 6s 117ms/step - loss: -4.8196 - accuracy: 0.6652 - val_loss: -0.7605 - val_accuracy: 0.7071 ...... Epoch 19/20 50/50 [==============================] - 6s 123ms/step - loss: -37.5176 - accuracy: 0.7586 - val_loss: -9.0619 - val_accuracy: 0.7272 Epoch 20/20 50/50 [==============================] - 6s 120ms/step - loss: -40.0017 - accuracy: 0.7611 - val_loss: -7.7479 - val_accuracy: 0.7248'''總結
以上是生活随笔為你收集整理的深度学习-循环神经网络(RNN)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 欢迎进入 K同学啊 的博客目录(全站式导
- 下一篇: 神经元模型及网络结构