日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > 循环神经网络 >内容正文

循环神经网络

深度学习-循环神经网络(RNN)

發布時間:2025/4/5 循环神经网络 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 深度学习-循环神经网络(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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。