Question: i need add some wat to increse performnce for this code please : import pandas as pd from keras.models import Sequential from keras.layers import Dense

i need add some wat to increse performnce for this code please : import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.preprocessing.text import Tokenizer
from sklearn.preprocessing import LabelEncoder
from keras.utils import to_categorical
from sklearn.metrics import f1_score
import numpy as np
# Load training data
df_train = pd.read_csv("Last_Complete_Data_8224.csv")
# Define your Keras model
sentiment_model = Sequential()
sentiment_model.add(Dense(512, input_shape=(6000,), activation='relu'))
sentiment_model.add(Dense(256, activation='relu'))
sentiment_model.add(Dense(128, activation='relu'))
sentiment_model.add(Dense(64, activation='relu'))
sentiment_model.add(Dense(32, activation='relu'))
sentiment_model.add(Dense(2, activation='sigmoid'))
# Compile the model
sentiment_model.compile(loss='categorical_crossentropy', optimizer='Adam', metrics=['accuracy'])
# Tokenize text data
vocab_size =6000
tokenizer = Tokenizer(num_words=vocab_size)
tokenizer.fit_on_texts(df_train['Pre_Text'])
train_reviews_tokenized = pd.DataFrame(tokenizer.texts_to_matrix(df_train['Pre_Text']))
# Encode the label variable
label_encoder = LabelEncoder()
integer_sentiment = label_encoder.fit_transform(df_train['sentiment'])
encoded_y_train = to_categorical(integer_sentiment)
# Train the model
for epoch in range(10):
history = sentiment_model.fit(train_reviews_tokenized, encoded_y_train, epochs=1, validation_split=0.2, verbose=1)
# Calculate training accuracy
train_accuracy = history.history['accuracy'][0]
# Predict classes for validation set
val_predictions = sentiment_model.predict(train_reviews_tokenized)
val_predicted_classes = np.argmax(val_predictions, axis=1)
# Calculate F1-score on validation set
val_f1= f1_score(integer_sentiment, val_predicted_classes, average='weighted')
print("Epoch {}: Training Accuracy ={:.4f}, Validation F1-score ={:.4f}".format(epoch+1, train_accuracy, val_f1))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!