Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -75,58 +75,6 @@ X_test_embeddings = np.array(X_test_embeddings)
|
|
75 |
X_train = np.array(X_train_embeddings)
|
76 |
X_test = np.array(X_test_embeddings)
|
77 |
|
78 |
-
# Sentiment Prediction with RNN Neural Network and Confusion Matrix
|
79 |
-
|
80 |
-
from keras.models import Sequential
|
81 |
-
from keras.layers import Dense, SimpleRNN, Reshape, Dropout
|
82 |
-
from keras.optimizers import Adam
|
83 |
-
from keras.callbacks import LearningRateScheduler
|
84 |
-
from sklearn.metrics import confusion_matrix
|
85 |
-
import seaborn as sns
|
86 |
-
import matplotlib.pyplot as plt
|
87 |
-
import numpy as np
|
88 |
-
from keras.models import Sequential
|
89 |
-
from keras.layers import Embedding, SpatialDropout1D, LSTM, Dense
|
90 |
-
|
91 |
-
max_features = 10000 # Adjust this based on your vocabulary size
|
92 |
-
embed_dim = 128 # Adjust this based on the desired dimension of the embedding space
|
93 |
-
lstm_out = 64 # Adjust this based on the number of LSTM units
|
94 |
-
|
95 |
-
model = Sequential()
|
96 |
-
model.add(Embedding(max_features, embed_dim, input_length=X_train.shape[1]))
|
97 |
-
model.add(SpatialDropout1D(0.4))
|
98 |
-
model.add(LSTM(lstm_out, dropout=0.2, recurrent_dropout=0.2))
|
99 |
-
model.add(Dense(3, activation='softmax')) # Adjust the number of units based on your task
|
100 |
-
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
|
101 |
-
print(model.summary())
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
# Use a learning rate scheduler
|
106 |
-
def lr_schedule(epoch):
|
107 |
-
return 0.0001 * 0.9 ** epoch
|
108 |
-
|
109 |
-
opt = Adam(learning_rate=0.0001)
|
110 |
-
lr_scheduler = LearningRateScheduler(lr_schedule)
|
111 |
-
|
112 |
-
# Compile the model
|
113 |
-
model.compile(optimizer=opt, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
114 |
-
|
115 |
-
# Print model summary to check the architecture
|
116 |
-
model.summary()
|
117 |
-
|
118 |
-
# Train the model with the learning rate scheduler
|
119 |
-
model.fit(X_train_embeddings, y_train, epochs=1, batch_size=32, validation_split=0.1, callbacks=[lr_scheduler])
|
120 |
-
|
121 |
-
# Evaluate the model on the test set
|
122 |
-
accuracy = model.evaluate(X_test_embeddings, y_test)[1]
|
123 |
-
print(f"Accuracy: {accuracy * 100:.2f}%")
|
124 |
-
|
125 |
-
# Predictions on the test set
|
126 |
-
y_pred_probabilities = model.predict(X_test_embeddings)
|
127 |
-
y_pred = np.argmax(y_pred_probabilities, axis=1)
|
128 |
-
|
129 |
-
|
130 |
# # Sentiment Prediction with RNN Neural Network and Confusion Matrix
|
131 |
|
132 |
# from keras.models import Sequential
|
@@ -137,15 +85,22 @@ y_pred = np.argmax(y_pred_probabilities, axis=1)
|
|
137 |
# import seaborn as sns
|
138 |
# import matplotlib.pyplot as plt
|
139 |
# import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
-
# # Build a neural network model with RNN
|
142 |
# model = Sequential()
|
143 |
-
# model.add(
|
144 |
-
# model.add(
|
145 |
-
# model.add(
|
146 |
-
# model.add(Dense(
|
147 |
-
# model.
|
148 |
-
# model.
|
|
|
|
|
149 |
|
150 |
# # Use a learning rate scheduler
|
151 |
# def lr_schedule(epoch):
|
@@ -153,7 +108,7 @@ y_pred = np.argmax(y_pred_probabilities, axis=1)
|
|
153 |
|
154 |
# opt = Adam(learning_rate=0.0001)
|
155 |
# lr_scheduler = LearningRateScheduler(lr_schedule)
|
156 |
-
|
157 |
# # Compile the model
|
158 |
# model.compile(optimizer=opt, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
159 |
|
@@ -161,15 +116,60 @@ y_pred = np.argmax(y_pred_probabilities, axis=1)
|
|
161 |
# model.summary()
|
162 |
|
163 |
# # Train the model with the learning rate scheduler
|
164 |
-
# model.fit(X_train_embeddings, y_train, epochs=
|
165 |
-
|
166 |
# # Evaluate the model on the test set
|
167 |
# accuracy = model.evaluate(X_test_embeddings, y_test)[1]
|
|
|
168 |
|
169 |
# # Predictions on the test set
|
170 |
# y_pred_probabilities = model.predict(X_test_embeddings)
|
171 |
# y_pred = np.argmax(y_pred_probabilities, axis=1)
|
172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
language = st.slider('Enter the language:')
|
174 |
user_text = st.slider('Enter the text:')
|
175 |
|
|
|
75 |
X_train = np.array(X_train_embeddings)
|
76 |
X_test = np.array(X_test_embeddings)
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
# # Sentiment Prediction with RNN Neural Network and Confusion Matrix
|
79 |
|
80 |
# from keras.models import Sequential
|
|
|
85 |
# import seaborn as sns
|
86 |
# import matplotlib.pyplot as plt
|
87 |
# import numpy as np
|
88 |
+
# from keras.models import Sequential
|
89 |
+
# from keras.layers import Embedding, SpatialDropout1D, LSTM, Dense
|
90 |
+
|
91 |
+
# max_features = 10000 # Adjust this based on your vocabulary size
|
92 |
+
# embed_dim = 128 # Adjust this based on the desired dimension of the embedding space
|
93 |
+
# lstm_out = 64 # Adjust this based on the number of LSTM units
|
94 |
|
|
|
95 |
# model = Sequential()
|
96 |
+
# model.add(Embedding(max_features, embed_dim, input_length=X_train.shape[1]))
|
97 |
+
# model.add(SpatialDropout1D(0.4))
|
98 |
+
# model.add(LSTM(lstm_out, dropout=0.2, recurrent_dropout=0.2))
|
99 |
+
# model.add(Dense(3, activation='softmax')) # Adjust the number of units based on your task
|
100 |
+
# model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
|
101 |
+
# print(model.summary())
|
102 |
+
|
103 |
+
|
104 |
|
105 |
# # Use a learning rate scheduler
|
106 |
# def lr_schedule(epoch):
|
|
|
108 |
|
109 |
# opt = Adam(learning_rate=0.0001)
|
110 |
# lr_scheduler = LearningRateScheduler(lr_schedule)
|
111 |
+
|
112 |
# # Compile the model
|
113 |
# model.compile(optimizer=opt, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
114 |
|
|
|
116 |
# model.summary()
|
117 |
|
118 |
# # Train the model with the learning rate scheduler
|
119 |
+
# model.fit(X_train_embeddings, y_train, epochs=1, batch_size=32, validation_split=0.1, callbacks=[lr_scheduler])
|
120 |
+
|
121 |
# # Evaluate the model on the test set
|
122 |
# accuracy = model.evaluate(X_test_embeddings, y_test)[1]
|
123 |
+
# print(f"Accuracy: {accuracy * 100:.2f}%")
|
124 |
|
125 |
# # Predictions on the test set
|
126 |
# y_pred_probabilities = model.predict(X_test_embeddings)
|
127 |
# y_pred = np.argmax(y_pred_probabilities, axis=1)
|
128 |
|
129 |
+
|
130 |
+
# Sentiment Prediction with RNN Neural Network and Confusion Matrix
|
131 |
+
|
132 |
+
from keras.models import Sequential
|
133 |
+
from keras.layers import Dense, SimpleRNN, Reshape, Dropout
|
134 |
+
from keras.optimizers import Adam
|
135 |
+
from keras.callbacks import LearningRateScheduler
|
136 |
+
from sklearn.metrics import confusion_matrix
|
137 |
+
import seaborn as sns
|
138 |
+
import matplotlib.pyplot as plt
|
139 |
+
import numpy as np
|
140 |
+
|
141 |
+
# Build a neural network model with RNN
|
142 |
+
model = Sequential()
|
143 |
+
model.add(Dense(256, input_shape=(1024,), activation='tanh'))
|
144 |
+
model.add(Reshape((1, 256)))
|
145 |
+
model.add(SimpleRNN(128, activation='relu'))
|
146 |
+
model.add(Dense(64, activation='relu'))
|
147 |
+
model.add(Dropout(0.5)) # Adding dropout for regularization
|
148 |
+
model.add(Dense(3, activation='softmax'))
|
149 |
+
|
150 |
+
# Use a learning rate scheduler
|
151 |
+
def lr_schedule(epoch):
|
152 |
+
return 0.0001 * 0.9 ** epoch
|
153 |
+
|
154 |
+
opt = Adam(learning_rate=0.0001)
|
155 |
+
lr_scheduler = LearningRateScheduler(lr_schedule)
|
156 |
+
#
|
157 |
+
# Compile the model
|
158 |
+
model.compile(optimizer=opt, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
159 |
+
|
160 |
+
# Print model summary to check the architecture
|
161 |
+
model.summary()
|
162 |
+
|
163 |
+
# Train the model with the learning rate scheduler
|
164 |
+
model.fit(X_train_embeddings, y_train, epochs=30, batch_size=32, validation_split=0.1, callbacks=[lr_scheduler])
|
165 |
+
|
166 |
+
# Evaluate the model on the test set
|
167 |
+
accuracy = model.evaluate(X_test_embeddings, y_test)[1]
|
168 |
+
|
169 |
+
# Predictions on the test set
|
170 |
+
y_pred_probabilities = model.predict(X_test_embeddings)
|
171 |
+
y_pred = np.argmax(y_pred_probabilities, axis=1)
|
172 |
+
|
173 |
language = st.slider('Enter the language:')
|
174 |
user_text = st.slider('Enter the text:')
|
175 |
|