मल्टी-क्लास क्लासिफिकेशन मॉडल

Keras के साथ भाषा मॉडलिंग के लिए Recurrent Neural Networks (RNNs)

David Cecchini

Data Scientist

Sentiment क्लासिफिकेशन मॉडल की समीक्षा

# Build and compile the model
model = Sequential()

model.add(Embedding(10000, 128))
model.add(LSTM(128, dropout=0.2))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
Keras के साथ भाषा मॉडलिंग के लिए Recurrent Neural Networks (RNNs)

मॉडल आर्किटेक्चर

यही आर्किटेक्चर इस्तेमाल कर सकते हैं

# Build the model
model = Sequential()
model.add(Embedding(10000, 128))
model.add(LSTM(128, dropout=0.2))

# Output layer में `num_classes` यूनिट हैं और `softmax` यूज़ होता है model.add(Dense(num_classes, activation="softmax"))
# मॉडल को कॉम्पाइल करें model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) ...
Keras के साथ भाषा मॉडलिंग के लिए Recurrent Neural Networks (RNNs)

20 News Group डेटासेट

20 News Groups डेटासेट

  • sklearn.datasets import fetch_20newsgroups पर उपलब्ध
# डेटा लोड करने का फंक्शन इम्पोर्ट करें
from sklearn.datasets import fetch_20newsgroups

# ट्रेन और टेस्ट सेट डाउनलोड करें news_train = fetch_20newsgroups(subset='train')
news_test = fetch_20newsgroups(subset='test')
Keras के साथ भाषा मॉडलिंग के लिए Recurrent Neural Networks (RNNs)

20 News Group डेटासेट

डेटा में ये एट्रिब्यूट्स हैं:

  • news_train.DESCR: डोक्यूमेंटेशन.
  • news_train.data: टेक्स्ट डेटा.
  • news_train.filenames: डिस्क पर फाइल पाथ.
  • news_train.target: क्लास के न्यूमेरिकल इंडेक्स.
  • news_train.target_names: क्लास के यूनिक नाम.
Keras के साथ भाषा मॉडलिंग के लिए Recurrent Neural Networks (RNNs)

टेक्स्ट डेटा का प्री-प्रोसेस

# Import modules
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.utils import to_categorical

# टोकनाइज़र बनाकर फिट करें tokenizer = Tokenizer() tokenizer.fit_on_texts(news_train.data)
# (X, Y) वैरिएबल बनाएँ X_train = tokenizer.texts_to_sequences(news_train.data) X_train = pad_sequences(X_train, maxlen=400) Y_train = to_categorical(news_train.target)
Keras के साथ भाषा मॉडलिंग के लिए Recurrent Neural Networks (RNNs)

डेटा पर ट्रेनिंग

ट्रेनिंग डेटा पर मॉडल ट्रेन करें

# Train the model
model.fit(X_train, Y_train, 
          batch_size=64, epochs=100)

# टेस्ट डेटा पर इवैल्यूएट करें
model.evaluate(X_test, Y_test)
Keras के साथ भाषा मॉडलिंग के लिए Recurrent Neural Networks (RNNs)

अभ्यास करते हैं!

Keras के साथ भाषा मॉडलिंग के लिए Recurrent Neural Networks (RNNs)

Preparing Video For Download...