-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_sentiment_analysis_model.py
36 lines (30 loc) · 1.17 KB
/
train_sentiment_analysis_model.py
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
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense, Dropout
import json
# Example dataset (replace with your actual data loading code)
texts = ["I love this movie!", "This book is terrible.", ...]
labels = [1, 0, ...] # 1 for positive sentiment, 0 for negative
# Tokenize texts
tokenizer = Tokenizer(num_words=10000)
tokenizer.fit_on_texts(texts)
sequences = tokenizer.texts_to_sequences(texts)
padded_sequences = pad_sequences(sequences, maxlen=100)
# Define model architecture
model = Sequential([
Embedding(10000, 64, input_length=100),
LSTM(128),
Dense(64, activation='relu'),
Dropout(0.5),
Dense(1, activation='sigmoid')
])
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Train the model
model.fit(padded_sequences, labels, epochs=10, validation_split=0.2)
# Save model and tokenizer
model.save('model.h5')
tokenizer_json = tokenizer.to_json()
with open('tokenizer.json', 'w', encoding='utf-8') as f:
f.write(tokenizer_json)