forked from Micky-12/Question_Paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrnn.py
55 lines (43 loc) · 2.17 KB
/
rnn.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import pandas as pd
from sklearn.model_selection import train_test_split
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense
# Load preprocessed data and labels
data = pd.read_csv(r"C:\Users\harsha anand\Desktop\Miniproject\flask\extracted_questions.csv")
data = data.dropna()
# Separate features (X) and target variable (Y)
X = data['Question'] # Assuming 'Question' is the column name for input text
Y = data['Difficulty'] # Assuming 'Difficulty' is the column name for target variable
# Map ordinal categories to numerical values
difficulty_mapping = {"easy": 0, "medium": 1, "hard": 2}
Y = Y.map(difficulty_mapping)
# Splitting data into train and validation sets
X_train, X_val, y_train, y_val = train_test_split(X, Y, test_size=0.2, random_state=42)
# Tokenize the text data
tokenizer = Tokenizer()
tokenizer.fit_on_texts(X_train)
# Convert text data to sequences
X_train_seq = tokenizer.texts_to_sequences(X_train)
X_val_seq = tokenizer.texts_to_sequences(X_val)
# Pad sequences
max_length = 100 # Example value, adjust based on your maximum sequence length
X_train_pad = pad_sequences(X_train_seq, maxlen=max_length, padding='post')
X_val_pad = pad_sequences(X_val_seq, maxlen=max_length, padding='post')
# Define model parameters
vocab_size = len(tokenizer.word_index) + 1
embedding_dim = 100 # Example value, adjust based on your embedding dimension
num_classes = len(difficulty_mapping)
# Define the RNN model
model = Sequential()
model.add(Embedding(input_dim=vocab_size, output_dim=embedding_dim, input_length=max_length))
model.add(LSTM(units=64, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(num_classes, activation='softmax'))
# Compile the model
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Train the model
history = model.fit(X_train_pad, y_train, epochs=10, batch_size=32, validation_data=(X_val_pad, y_val))
# Save the trained model
model.save('question_generation_model.h5')