DL_5
DL_5
: 5
Name: Gayatri Rajendra Jagadale
Roll No.:2447062
Batch: D
Problem Statement –
Perform Sentiment Analysis in the network graph using RNN.
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import imdb from tensorflow.keras.models import Seq
from tensorflow.keras.layers import Embedding, SimpleRNN, Dense from tensorflow.ke
import pad_sequences
D:\Users\shrey\anaconda3\lib\site-packages\keras\src\layers\core\ embedding.py:90:
`input_length` is deprecated.
Just remove it. warnings.warn(
Model: "sequential"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━
━━━━━━━━━━━━━━━━━┓
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━
━━━━━━━━━━━━━━━━━┩
│ embedding (Embedding) │ ? │
0 (unbuilt) │
├──────────────────────────────────────┼─────────────────────────────┼
─────────────────┤
│ simple_rnn (SimpleRNN) │ ? │
0 (unbuilt) │
├──────────────────────────────────────┼─────────────────────────────┼
─────────────────┤
│ dense (Dense) │ ? │
0 (unbuilt) │
└──────────────────────────────────────┴─────────────────────────────┴
─────────────────┘
<keras.src.callbacks.history.History at 0x1888798e100>
B. Predict Sentiments on Custom Samplessample_reviews = x_test[:10]
preds = (model.predict(sample_reviews) > 0.5).astype("int").flatten()
# Draw network
pos = nx.spring_layout(G, seed=42)
colors = ['green' if G.nodes[n]['sentiment'] == 'positive' else 'red'
for n in G.nodes()]
labels = nx.get_node_attributes(G, 'label')
plt.figure(figsize=(10, 7))
nx.draw(G, pos, ax=plt.gca(), with_labels=True, node_color=colors,
labels=labels,
font_size=8, node_size=1500)
plt.title("Sentiment Network Graph")
plt.show()