sathvik-dl 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sathvik_dl-0.1.0/PKG-INFO +5 -0
- sathvik_dl-0.1.0/dl.py +318 -0
- sathvik_dl-0.1.0/pyproject.toml +12 -0
- sathvik_dl-0.1.0/sathvik_dl.egg-info/PKG-INFO +5 -0
- sathvik_dl-0.1.0/sathvik_dl.egg-info/SOURCES.txt +6 -0
- sathvik_dl-0.1.0/sathvik_dl.egg-info/dependency_links.txt +1 -0
- sathvik_dl-0.1.0/sathvik_dl.egg-info/top_level.txt +1 -0
- sathvik_dl-0.1.0/setup.cfg +4 -0
sathvik_dl-0.1.0/dl.py
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
codes = {}
|
|
2
|
+
|
|
3
|
+
codes[5] = r'''
|
|
4
|
+
import tensorflow as tf
|
|
5
|
+
from tensorflow.keras.models import Sequential
|
|
6
|
+
from tensorflow.keras.layers import Embedding, GlobalAveragePooling1D, Dense
|
|
7
|
+
|
|
8
|
+
# 1. Load IMDB dataset (Movie Reviews)
|
|
9
|
+
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.imdb.load_data(
|
|
10
|
+
num_words=10000
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
# 2. Make all reviews the same length
|
|
14
|
+
x_train = tf.keras.preprocessing.sequence.pad_sequences(
|
|
15
|
+
x_train, maxlen=200
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
x_test = tf.keras.preprocessing.sequence.pad_sequences(
|
|
19
|
+
x_test, maxlen=200
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
# 3. Create deep learning model
|
|
23
|
+
model = Sequential([
|
|
24
|
+
Embedding(10000, 32),
|
|
25
|
+
GlobalAveragePooling1D(),
|
|
26
|
+
Dense(64, activation='relu'),
|
|
27
|
+
Dense(1, activation='sigmoid')
|
|
28
|
+
])
|
|
29
|
+
|
|
30
|
+
# 4. Compile model
|
|
31
|
+
model.compile(
|
|
32
|
+
optimizer='adam',
|
|
33
|
+
loss='binary_crossentropy',
|
|
34
|
+
metrics=['accuracy']
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
# 5. Train model
|
|
38
|
+
model.fit(
|
|
39
|
+
x_train,
|
|
40
|
+
y_train,
|
|
41
|
+
epochs=5,
|
|
42
|
+
batch_size=32,
|
|
43
|
+
validation_split=0.2
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
# 6. Test model
|
|
47
|
+
loss, accuracy = model.evaluate(x_test, y_test)
|
|
48
|
+
print("Test Accuracy:", accuracy)
|
|
49
|
+
|
|
50
|
+
# 7. Predict first 5 reviews
|
|
51
|
+
predictions = model.predict(x_test[:5])
|
|
52
|
+
|
|
53
|
+
print("\nReview Classification:")
|
|
54
|
+
|
|
55
|
+
for i in range(5):
|
|
56
|
+
if predictions[i][0] >= 0.5:
|
|
57
|
+
result = "Positive"
|
|
58
|
+
else:
|
|
59
|
+
result = "Negative"
|
|
60
|
+
|
|
61
|
+
print("Review", i + 1, ":", result)
|
|
62
|
+
print("Probability:", predictions[i][0])
|
|
63
|
+
|
|
64
|
+
'''
|
|
65
|
+
|
|
66
|
+
codes[6] = r'''
|
|
67
|
+
import numpy as np
|
|
68
|
+
import matplotlib.pyplot as plt
|
|
69
|
+
import pandas as pd
|
|
70
|
+
from sklearn.preprocessing import MinMaxScaler
|
|
71
|
+
from tensorflow.keras.models import Sequential
|
|
72
|
+
from tensorflow.keras.layers import LSTM, Dense
|
|
73
|
+
|
|
74
|
+
# 1. Generate synthetic time series data
|
|
75
|
+
time = np.arange(0, 200, 0.1)
|
|
76
|
+
data = np.sin(time) + 0.1 * np.random.randn(len(time))
|
|
77
|
+
|
|
78
|
+
plt.plot(time, data)
|
|
79
|
+
plt.title("Synthetic Time Series Data")
|
|
80
|
+
plt.show()
|
|
81
|
+
|
|
82
|
+
# 2. Normalize the data
|
|
83
|
+
scaler = MinMaxScaler(feature_range=(0, 1))
|
|
84
|
+
data_scaled = scaler.fit_transform(data.reshape(-1, 1))
|
|
85
|
+
|
|
86
|
+
# 3. Create sequences
|
|
87
|
+
def create_sequences(dataset, seq_length):
|
|
88
|
+
X, y = [], []
|
|
89
|
+
for i in range(len(dataset) - seq_length):
|
|
90
|
+
X.append(dataset[i:i + seq_length, 0])
|
|
91
|
+
y.append(dataset[i + seq_length, 0])
|
|
92
|
+
return np.array(X), np.array(y)
|
|
93
|
+
|
|
94
|
+
SEQ_LEN = 20
|
|
95
|
+
X, y = create_sequences(data_scaled, SEQ_LEN)
|
|
96
|
+
|
|
97
|
+
# 4. Reshape for LSTM (samples, timesteps, features)
|
|
98
|
+
X = X.reshape((X.shape[0], X.shape[1], 1))
|
|
99
|
+
|
|
100
|
+
# 5. Train-test split
|
|
101
|
+
split = int(len(X) * 0.8)
|
|
102
|
+
X_train, X_test = X[:split], X[split:]
|
|
103
|
+
y_train, y_test = y[:split], y[split:]
|
|
104
|
+
|
|
105
|
+
# 6. Build LSTM model
|
|
106
|
+
model = Sequential([
|
|
107
|
+
LSTM(50, activation='relu', input_shape=(SEQ_LEN, 1)),
|
|
108
|
+
Dense(1)
|
|
109
|
+
])
|
|
110
|
+
|
|
111
|
+
model.compile(optimizer='adam', loss='mse')
|
|
112
|
+
|
|
113
|
+
# 7. Train model
|
|
114
|
+
history = model.fit(
|
|
115
|
+
X_train,
|
|
116
|
+
y_train,
|
|
117
|
+
epochs=10,
|
|
118
|
+
batch_size=32,
|
|
119
|
+
validation_data=(X_test, y_test),
|
|
120
|
+
verbose=1
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
# 8. Plot training loss
|
|
124
|
+
plt.plot(history.history['loss'], label='Train Loss')
|
|
125
|
+
plt.plot(history.history['val_loss'], label='Validation Loss')
|
|
126
|
+
plt.legend()
|
|
127
|
+
plt.show()
|
|
128
|
+
|
|
129
|
+
# 9. Forecast
|
|
130
|
+
predictions = model.predict(X_test)
|
|
131
|
+
predictions = scaler.inverse_transform(predictions.reshape(-1, 1))
|
|
132
|
+
y_test_actual = scaler.inverse_transform(y_test.reshape(-1, 1))
|
|
133
|
+
|
|
134
|
+
# 10. Plot predictions vs actual
|
|
135
|
+
plt.plot(y_test_actual, label='Actual')
|
|
136
|
+
plt.plot(predictions, label='Predicted')
|
|
137
|
+
plt.legend()
|
|
138
|
+
plt.show()
|
|
139
|
+
'''
|
|
140
|
+
|
|
141
|
+
codes[7] = r'''
|
|
142
|
+
|
|
143
|
+
import tensorflow as tf
|
|
144
|
+
import matplotlib.pyplot as plt
|
|
145
|
+
import numpy as np
|
|
146
|
+
|
|
147
|
+
# 1. Load CIFAR-10 dataset
|
|
148
|
+
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
|
|
149
|
+
|
|
150
|
+
# Convert labels to 1D
|
|
151
|
+
y_train = y_train.flatten()
|
|
152
|
+
y_test = y_test.flatten()
|
|
153
|
+
|
|
154
|
+
# 2. Resize images
|
|
155
|
+
x_train = tf.image.resize(x_train, (96, 96))
|
|
156
|
+
x_test = tf.image.resize(x_test, (96, 96))
|
|
157
|
+
|
|
158
|
+
# 3. Load pre-trained MobileNetV2
|
|
159
|
+
base_model = tf.keras.applications.MobileNetV2(
|
|
160
|
+
input_shape=(96, 96, 3),
|
|
161
|
+
include_top=False,
|
|
162
|
+
weights="imagenet"
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
# Freeze pre-trained layers
|
|
166
|
+
base_model.trainable = False
|
|
167
|
+
|
|
168
|
+
# 4. Create model
|
|
169
|
+
model = tf.keras.Sequential([
|
|
170
|
+
base_model,
|
|
171
|
+
tf.keras.layers.GlobalAveragePooling2D(),
|
|
172
|
+
tf.keras.layers.Dense(64, activation="relu"),
|
|
173
|
+
tf.keras.layers.Dense(10, activation="softmax")
|
|
174
|
+
])
|
|
175
|
+
|
|
176
|
+
# 5. Compile model
|
|
177
|
+
model.compile(
|
|
178
|
+
optimizer="adam",
|
|
179
|
+
loss="sparse_categorical_crossentropy",
|
|
180
|
+
metrics=["accuracy"]
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
# 6. Train model
|
|
184
|
+
history = model.fit(
|
|
185
|
+
x_train,
|
|
186
|
+
y_train,
|
|
187
|
+
epochs=5,
|
|
188
|
+
batch_size=32,
|
|
189
|
+
validation_split=0.2
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
# 7. Test model
|
|
193
|
+
loss, accuracy = model.evaluate(x_test, y_test)
|
|
194
|
+
print("Test Accuracy:", accuracy * 100, "%")
|
|
195
|
+
|
|
196
|
+
# 8. Predict one image
|
|
197
|
+
prediction = model.predict(x_test[:1])
|
|
198
|
+
|
|
199
|
+
classes = [
|
|
200
|
+
"airplane", "automobile", "bird", "cat", "deer",
|
|
201
|
+
"dog", "frog", "horse", "ship", "truck"
|
|
202
|
+
]
|
|
203
|
+
|
|
204
|
+
predicted_class = np.argmax(prediction[0])
|
|
205
|
+
|
|
206
|
+
print("Actual:", classes[y_test[0]])
|
|
207
|
+
print("Predicted:", classes[predicted_class])
|
|
208
|
+
|
|
209
|
+
# 9. Print training and validation accuracy
|
|
210
|
+
train_accuracy = history.history["accuracy"][-1]
|
|
211
|
+
val_accuracy = history.history["val_accuracy"][-1]
|
|
212
|
+
|
|
213
|
+
print("Training Accuracy:", train_accuracy * 100, "%")
|
|
214
|
+
print("Validation Accuracy:", val_accuracy * 100, "%")
|
|
215
|
+
|
|
216
|
+
# Test accuracy
|
|
217
|
+
loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
|
|
218
|
+
print("Test Accuracy:", test_accuracy * 100, "%")
|
|
219
|
+
|
|
220
|
+
# 10. Accuracy graph
|
|
221
|
+
plt.figure(figsize=(8, 5))
|
|
222
|
+
|
|
223
|
+
plt.plot(
|
|
224
|
+
history.history["accuracy"],
|
|
225
|
+
label="Training Accuracy"
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
plt.plot(
|
|
229
|
+
history.history["val_accuracy"],
|
|
230
|
+
label="Validation Accuracy"
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
plt.xlabel("Epoch")
|
|
234
|
+
plt.ylabel("Accuracy")
|
|
235
|
+
plt.title("Training and Validation Accuracy")
|
|
236
|
+
plt.legend()
|
|
237
|
+
plt.grid()
|
|
238
|
+
plt.show()
|
|
239
|
+
|
|
240
|
+
'''
|
|
241
|
+
|
|
242
|
+
codes[8] = r'''
|
|
243
|
+
from sklearn.model_selection import train_test_split
|
|
244
|
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
|
245
|
+
from sklearn.linear_model import LogisticRegression
|
|
246
|
+
from sklearn.metrics import accuracy_score
|
|
247
|
+
|
|
248
|
+
# Dataset
|
|
249
|
+
reviews = [
|
|
250
|
+
"I loved this movie, it was fantastic",
|
|
251
|
+
"The product is excellent and very useful",
|
|
252
|
+
"Amazing experience, I really enjoyed it",
|
|
253
|
+
"The service was wonderful",
|
|
254
|
+
"Very happy with my purchase",
|
|
255
|
+
"This product is terrible",
|
|
256
|
+
"I hated this movie",
|
|
257
|
+
"Very disappointing experience",
|
|
258
|
+
"The product broke after one day",
|
|
259
|
+
"Poor quality and bad service"
|
|
260
|
+
]
|
|
261
|
+
|
|
262
|
+
# Labels: 1 = Positive, 0 = Negative
|
|
263
|
+
labels = [
|
|
264
|
+
1, 1, 1, 1, 1,
|
|
265
|
+
0, 0, 0, 0, 0
|
|
266
|
+
]
|
|
267
|
+
|
|
268
|
+
# Split data into training and testing sets
|
|
269
|
+
X_train, X_test, y_train, y_test = train_test_split(
|
|
270
|
+
reviews,
|
|
271
|
+
labels,
|
|
272
|
+
test_size=0.2,
|
|
273
|
+
random_state=42,
|
|
274
|
+
stratify=labels
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
# Convert text into TF-IDF features
|
|
278
|
+
vectorizer = TfidfVectorizer()
|
|
279
|
+
|
|
280
|
+
X_train_tfidf = vectorizer.fit_transform(X_train)
|
|
281
|
+
X_test_tfidf = vectorizer.transform(X_test)
|
|
282
|
+
|
|
283
|
+
# Train the model
|
|
284
|
+
model = LogisticRegression()
|
|
285
|
+
model.fit(X_train_tfidf, y_train)
|
|
286
|
+
|
|
287
|
+
# Predict test data
|
|
288
|
+
y_pred = model.predict(X_test_tfidf)
|
|
289
|
+
|
|
290
|
+
# Test accuracy
|
|
291
|
+
print("Test Accuracy:", accuracy_score(y_test, y_pred))
|
|
292
|
+
|
|
293
|
+
# Test new reviews
|
|
294
|
+
new_reviews = [
|
|
295
|
+
"The movie was amazing and I loved it",
|
|
296
|
+
"The product was awful and disappointing",
|
|
297
|
+
"I am very happy with this purchase"
|
|
298
|
+
]
|
|
299
|
+
|
|
300
|
+
new_reviews_tfidf = vectorizer.transform(new_reviews)
|
|
301
|
+
predictions = model.predict(new_reviews_tfidf)
|
|
302
|
+
|
|
303
|
+
# Display results
|
|
304
|
+
for review, prediction in zip(new_reviews, predictions):
|
|
305
|
+
sentiment = "Positive" if prediction == 1 else "Negative"
|
|
306
|
+
print("Review:", review)
|
|
307
|
+
print("Sentiment:", sentiment)
|
|
308
|
+
|
|
309
|
+
'''
|
|
310
|
+
|
|
311
|
+
def show(n):
|
|
312
|
+
print(codes[n])
|
|
313
|
+
|
|
314
|
+
def run(n):
|
|
315
|
+
exec(codes[n], globals())
|
|
316
|
+
|
|
317
|
+
def list_codes():
|
|
318
|
+
print("Available codes:", *codes.keys())
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "sathvik-dl"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "My deep learning codes"
|
|
9
|
+
requires-python = ">=3.8"
|
|
10
|
+
|
|
11
|
+
[tool.setuptools]
|
|
12
|
+
py-modules = ["dl"]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
dl
|