prompt_projectapp / model.h5
deepugaur's picture
Create model.h5
8b4479c verified
raw
history blame
1.14 kB
# Example model training script
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense, Dropout
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
import numpy as np
import pickle
# Sample dataset
texts = ["This is valid", "This is malicious", "Valid text", "Malicious text"]
labels = [0, 1, 0, 1] # 0: Valid, 1: Malicious
# Tokenization
tokenizer = Tokenizer(num_words=1000)
tokenizer.fit_on_texts(texts)
sequences = tokenizer.texts_to_sequences(texts)
padded_sequences = pad_sequences(sequences, maxlen=50)
# Save the tokenizer
with open("tokenizer.pkl", "wb") as f:
pickle.dump(tokenizer, f)
# Model architecture
model = Sequential([
Embedding(input_dim=1000, output_dim=64, input_length=50),
LSTM(64, return_sequences=False),
Dropout(0.5),
Dense(1, activation="sigmoid")
])
# Compile and train the model
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
model.fit(padded_sequences, np.array(labels), epochs=10)
# Save the model
model.save("model.h5")