File size: 4,613 Bytes
00dd7f8 7253e8a 00dd7f8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
import streamlit as st
import numpy as np
import torch
from torch import nn
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
# Set page config
st.set_page_config(page_title='Advanced NLP with Deep Learning', layout='wide')
# Title with styled emoji
st.markdown('<h1 style="color:#4CAF50; text-align:center;">π€ Advanced NLP with Deep Learning π</h1>', unsafe_allow_html=True)
# Section 1: Word Embeddings
st.markdown('<h2 style="color:#FF5733">π 1. Word Embeddings</h2>', unsafe_allow_html=True)
st.subheader('π Definition:')
st.write("""
Word embeddings are dense vector representations of words where similar words have similar representations. They are essential for text-based deep learning models.
- **πΉ Word2Vec (Skip-gram & CBOW)**: Learns word representations based on context.
- **πΉ GloVe (Global Vectors)**: Uses word co-occurrence statistics to learn embeddings.
- **πΉ FastText**: Handles subword information, helping with out-of-vocabulary words.
""")
# Word2Vec Example
st.subheader('π§© Word2Vec Example:')
sentence = st.text_area("Enter a sentence to visualize Word2Vec embeddings", "NLP is amazing and very useful.")
if st.button('π¨ Visualize Word2Vec'):
words = sentence.split()
embeddings = {word: np.random.rand(1, 50) for word in words}
st.write("**Word2Vec Embeddings (Random Example):**")
for word, emb in embeddings.items():
st.write(f"{word}: {emb.flatten()[:5]}...")
# Section 2: Sequence Models
st.markdown('<h2 style="color:#3E7FCB">π 2. Sequence Models</h2>', unsafe_allow_html=True)
st.subheader('π Definition:')
st.write("""
Sequence models process sequential data like sentences and play a key role in NLP tasks like translation, summarization, and sentiment analysis.
- **πΉ RNN (Recurrent Neural Networks)**: Maintains memory of previous words.
- **πΉ LSTM (Long Short-Term Memory)**: Handles long-range dependencies.
- **πΉ GRU (Gated Recurrent Units)**: A simplified LSTM version.
""")
# RNN Example
st.subheader('π οΈ RNN Example (PyTorch):')
if st.button('π₯οΈ Show RNN Model Architecture'):
class SimpleRNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(SimpleRNN, self).__init__()
self.rnn = nn.RNN(input_size, hidden_size, batch_first=True)
self.fc = nn.Linear(hidden_size, output_size)
def forward(self, x):
out, _ = self.rnn(x)
out = self.fc(out[:, -1, :])
return out
rnn_model = SimpleRNN(input_size=10, hidden_size=20, output_size=1)
st.write("**RNN Architecture:**")
st.write(rnn_model)
# Section 3: Attention Mechanisms
st.markdown('<h2 style="color:#E67E22">π 3. Attention Mechanisms</h2>', unsafe_allow_html=True)
st.subheader('π Definition:')
st.write("""
Attention mechanisms allow models to focus on key parts of an input sequence, improving performance on tasks that require long-range dependencies.
- **πΉ Self-attention**: Assigns importance to different words.
- **πΉ Seq2Seq Models**: Encoder-decoder models used for translation.
- **πΉ Transformer**: Parallel processing for high efficiency.
""")
# Transformer Example
st.subheader('π οΈ Transformer Example (Simplified):')
if st.button('π₯οΈ Show Transformer Architecture'):
input_layer = keras.Input(shape=(None, 512))
attention_output = layers.MultiHeadAttention(num_heads=8, key_dim=512)(input_layer, input_layer)
pooled_output = layers.GlobalAveragePooling1D()(attention_output)
dense1 = layers.Dense(256, activation="relu")(pooled_output)
output_layer = layers.Dense(1)(dense1)
transformer_model = keras.Model(inputs=input_layer, outputs=output_layer)
st.write("**Transformer Architecture (Fixed Version):**")
st.write(transformer_model)
# Section 4: Key Attention Components
st.markdown('<h2 style="color:#9C27B0">π 4. Attention Components</h2>', unsafe_allow_html=True)
st.subheader('π Self-attention:')
st.write("""
Each word in a sequence attends to all other words and assigns an importance weight, capturing long-range dependencies.
""")
st.subheader('π Seq2Seq:')
st.write("""
Used for translation, where an encoder processes input and a decoder generates output.
""")
st.subheader('β‘ Transformer:')
st.write("""
Revolutionized NLP by using self-attention in both encoder and decoder while processing all tokens in parallel.
""")
st.markdown('<h3 style="color:#4CAF50; text-align:center;">β¨ Thanks for Exploring NLP! β¨</h3>', unsafe_allow_html=True)
|