<jupyter_start><jupyter_text><jupyter_code>import numpy as np
from tensorflow.keras.preprocessing.text import one_hot
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Embedding 
reviews = ['nice food',
           'amazing restaurant',
           'too good',
           'just loved it!',
           'will go again',
           'horrible food',
           'never go there',
           'poor service',
           'poor quality',
           'needs improvement']
sentiment = np.array([1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
vocab_size = 30
encoded_reviews = [one_hot(d, vocab_size) for d in reviews]
encoded_reviews
max_length = 3
padded_reviews = pad_sequences(encoded_reviews, maxlen=max_length, padding='post')
padded_reviews
embeded_vactor_size = 4

model = Sequential()
model.add(Embedding(vocab_size, embeded_vactor_size, input_length=max_length, name='embedding'))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
X = padded_reviews
y = sentiment
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.summary()
model.fit(X, y, epochs=50, verbose=0)
loss, accuracy = model.evaluate(X, y)
accuracy
weight = model.get_layer('embedding').get_weights()[0]
len(weight)
weight[4]
weight[15]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11866759195003469, 'fraction_numerical': 0.015267175572519083, 'mean_word_length': 4.768, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '663274', 'license': 'Apache-2.0', 'n_tokens_mistral': 499, 'n_tokens_neox': 473, 'n_words': 113, 'path': 'simple_word_embedding.ipynb', 'repo_name': 'nahin333/DL-practice-codes', 'size': '1441'}