pythonCopy code
# Importing necessary libraries
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, Flatten, Dense
# Sample data (usually it would be more complex and larger)
sentences = [
"I love machine learning",
"I love coding in Python",
"I enjoy learning new things"
]
# Processing data: In real-world tasks, you would use more sophisticated preprocessing
words = set(word for sentence in sentences for word in sentence.split())
word_to_index = {word: index for index, word in enumerate(words)}
# Parameters
vocab_size = len(words) # Total unique words in the dataset
embedding_dim = 5 # The dimension of word embeddings
max_length = 5 # Maximum length of sentences/sequences
# Creating a Sequential model
model = Sequential([
Embedding(input_dim=vocab_size, output_dim=embedding_dim, input_length=max_length), # Embedding layer
Flatten(), # Flattening the 3D tensor output from the Embedding layer
Dense(16, activation='relu'), # Dense layer with 16 neurons and ReLU activation function
Dense(1, activation='sigmoid') # Output layer with 1 neuron and sigmoid activation function (binary classification)
])
# Compiling the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Displaying the model summary
model.summary()
# In real-world tasks, you would fit the model with data by using the model.fit() function