dlopss 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.
- dlopss-0.1.0/LICENSE +21 -0
- dlopss-0.1.0/PKG-INFO +55 -0
- dlopss-0.1.0/README.md +38 -0
- dlopss-0.1.0/pyproject.toml +37 -0
- dlopss-0.1.0/setup.cfg +4 -0
- dlopss-0.1.0/src/dlopss/__init__.py +45 -0
- dlopss-0.1.0/src/dlopss/data/__init__.py +1 -0
- dlopss-0.1.0/src/dlopss/data/a/__init__.py +1 -0
- dlopss-0.1.0/src/dlopss/data/a/p1 +30 -0
- dlopss-0.1.0/src/dlopss/data/a/p2 +81 -0
- dlopss-0.1.0/src/dlopss/data/a/p3 +73 -0
- dlopss-0.1.0/src/dlopss/data/a/p4 +48 -0
- dlopss-0.1.0/src/dlopss/data/a/p5 +107 -0
- dlopss-0.1.0/src/dlopss/data/a/p6 +38 -0
- dlopss-0.1.0/src/dlopss/data/a/p7 +45 -0
- dlopss-0.1.0/src/dlopss/data/a/p8 +57 -0
- dlopss-0.1.0/src/dlopss/data/b/1.ipynb +1 -0
- dlopss-0.1.0/src/dlopss/data/b/1s.ipynb +143 -0
- dlopss-0.1.0/src/dlopss/data/b/2a.ipynb +1 -0
- dlopss-0.1.0/src/dlopss/data/b/2b.ipynb +257 -0
- dlopss-0.1.0/src/dlopss/data/b/3a.ipynb +1 -0
- dlopss-0.1.0/src/dlopss/data/b/3b.ipynb +318 -0
- dlopss-0.1.0/src/dlopss/data/b/4a.ipynb +1 -0
- dlopss-0.1.0/src/dlopss/data/b/4b.ipynb +406 -0
- dlopss-0.1.0/src/dlopss/data/b/5a.ipynb +1 -0
- dlopss-0.1.0/src/dlopss/data/b/5b.ipynb +1 -0
- dlopss-0.1.0/src/dlopss/data/b/6a.ipynb +1 -0
- dlopss-0.1.0/src/dlopss/data/b/6b.ipynb +265 -0
- dlopss-0.1.0/src/dlopss/data/b/7a.ipynb +1 -0
- dlopss-0.1.0/src/dlopss/data/b/7b.ipynb +292 -0
- dlopss-0.1.0/src/dlopss/data/b/8.ipynb +1 -0
- dlopss-0.1.0/src/dlopss/data/b/__init__.py +1 -0
- dlopss-0.1.0/src/dlopss.egg-info/PKG-INFO +55 -0
- dlopss-0.1.0/src/dlopss.egg-info/SOURCES.txt +34 -0
- dlopss-0.1.0/src/dlopss.egg-info/dependency_links.txt +1 -0
- dlopss-0.1.0/src/dlopss.egg-info/top_level.txt +1 -0
dlopss-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 hi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
dlopss-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dlopss
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Deep Learning course data files and notebooks.
|
|
5
|
+
Author-email: hi <hifrombro@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/khete/dlopss
|
|
8
|
+
Keywords: deep learning,notebooks,jupyter,data
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Topic :: Education
|
|
12
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
13
|
+
Requires-Python: >=3.8
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Dynamic: license-file
|
|
17
|
+
|
|
18
|
+
# dlopss
|
|
19
|
+
|
|
20
|
+
Deep Learning course data files and Jupyter notebooks, bundled as an installable package.
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install dlopss
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
import dlopss
|
|
32
|
+
|
|
33
|
+
# Root data directory
|
|
34
|
+
data_dir = dlopss.get_data_path()
|
|
35
|
+
|
|
36
|
+
# data/a — course files
|
|
37
|
+
a_dir = dlopss.get_a_path()
|
|
38
|
+
for f in dlopss.list_a_files():
|
|
39
|
+
print(f.name)
|
|
40
|
+
|
|
41
|
+
# data/b — Jupyter notebooks
|
|
42
|
+
b_dir = dlopss.get_b_path()
|
|
43
|
+
for nb in dlopss.list_notebooks():
|
|
44
|
+
print(nb.name)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Contents
|
|
48
|
+
|
|
49
|
+
**data/a/** — p1 through p8 (course files)
|
|
50
|
+
|
|
51
|
+
**data/b/** — 15 Jupyter notebooks: 1, 1s, 2a, 2b, 3a, 3b, 4a, 4b, 5a, 5b, 6a, 6b, 7a, 7b, 8
|
|
52
|
+
|
|
53
|
+
## License
|
|
54
|
+
|
|
55
|
+
MIT
|
dlopss-0.1.0/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# dlopss
|
|
2
|
+
|
|
3
|
+
Deep Learning course data files and Jupyter notebooks, bundled as an installable package.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install dlopss
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
import dlopss
|
|
15
|
+
|
|
16
|
+
# Root data directory
|
|
17
|
+
data_dir = dlopss.get_data_path()
|
|
18
|
+
|
|
19
|
+
# data/a — course files
|
|
20
|
+
a_dir = dlopss.get_a_path()
|
|
21
|
+
for f in dlopss.list_a_files():
|
|
22
|
+
print(f.name)
|
|
23
|
+
|
|
24
|
+
# data/b — Jupyter notebooks
|
|
25
|
+
b_dir = dlopss.get_b_path()
|
|
26
|
+
for nb in dlopss.list_notebooks():
|
|
27
|
+
print(nb.name)
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Contents
|
|
31
|
+
|
|
32
|
+
**data/a/** — p1 through p8 (course files)
|
|
33
|
+
|
|
34
|
+
**data/b/** — 15 Jupyter notebooks: 1, 1s, 2a, 2b, 3a, 3b, 4a, 4b, 5a, 5b, 6a, 6b, 7a, 7b, 8
|
|
35
|
+
|
|
36
|
+
## License
|
|
37
|
+
|
|
38
|
+
MIT
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "dlopss"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Deep Learning course data files and notebooks."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.8"
|
|
13
|
+
authors = [
|
|
14
|
+
{ name = "hi", email = "hifrombro@gmail.com" }
|
|
15
|
+
]
|
|
16
|
+
keywords = ["deep learning", "notebooks", "jupyter", "data"]
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"Topic :: Education",
|
|
21
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Homepage = "https://github.com/khete/dlopss"
|
|
26
|
+
|
|
27
|
+
[tool.setuptools.packages.find]
|
|
28
|
+
where = ["src"]
|
|
29
|
+
|
|
30
|
+
[tool.setuptools.package-data]
|
|
31
|
+
"dlopss" = [
|
|
32
|
+
"data/__init__.py",
|
|
33
|
+
"data/a/*",
|
|
34
|
+
"data/a/__init__.py",
|
|
35
|
+
"data/b/*.ipynb",
|
|
36
|
+
"data/b/__init__.py",
|
|
37
|
+
]
|
dlopss-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""
|
|
2
|
+
dlopss
|
|
3
|
+
~~~~~~
|
|
4
|
+
Access bundled Deep Learning course data files.
|
|
5
|
+
|
|
6
|
+
Usage
|
|
7
|
+
-----
|
|
8
|
+
>>> import dlopss
|
|
9
|
+
>>> dlopss.get_data_path() # root data directory
|
|
10
|
+
>>> dlopss.get_a_path() # data/a directory
|
|
11
|
+
>>> dlopss.get_b_path() # data/b directory (notebooks)
|
|
12
|
+
>>> dlopss.list_a_files() # all files in a/
|
|
13
|
+
>>> dlopss.list_notebooks() # all .ipynb files in b/
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from importlib.resources import files
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def get_data_path() -> Path:
|
|
21
|
+
"""Return path to the root data/ directory."""
|
|
22
|
+
return Path(str(files("dlopss").joinpath("data")))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def get_a_path() -> Path:
|
|
26
|
+
"""Return path to the data/a/ directory."""
|
|
27
|
+
return get_data_path() / "a"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def get_b_path() -> Path:
|
|
31
|
+
"""Return path to the data/b/ directory (notebooks)."""
|
|
32
|
+
return get_data_path() / "b"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def list_a_files() -> list:
|
|
36
|
+
"""Return sorted list of all files in data/a/."""
|
|
37
|
+
return sorted([f for f in get_a_path().iterdir() if f.is_file() and f.name != "__init__.py"])
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def list_notebooks() -> list:
|
|
41
|
+
"""Return sorted list of all .ipynb files in data/b/."""
|
|
42
|
+
return sorted(get_b_path().glob("*.ipynb"))
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
__all__ = ["get_data_path", "get_a_path", "get_b_path", "list_a_files", "list_notebooks"]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# marks data/ as a package so importlib.resources can locate it
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# marks data/a as a package so importlib.resources can locate it
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import matplotlib.pyplot as plt
|
|
3
|
+
X = np.array([[0,0],[0,1],[1,0],[1,1]])
|
|
4
|
+
y = np.array([0,0,0,1])
|
|
5
|
+
df = pd.read_csv("data.csv")
|
|
6
|
+
X = df.iloc[:,:2].values
|
|
7
|
+
y = df.iloc[:,-1].values
|
|
8
|
+
w = np.zeros(2)
|
|
9
|
+
b,lr = 0,0.1
|
|
10
|
+
for _ in range(20):
|
|
11
|
+
for xi,yi in zip(X,y):
|
|
12
|
+
pred = int(np.dot(w,xi)+b >= 0.5)
|
|
13
|
+
w += lr*(yi-pred)*xi
|
|
14
|
+
b += lr*(yi-pred)
|
|
15
|
+
print("Predicted:",[int(np.dot(w,xi)+b >= 0.5) for xi in X])
|
|
16
|
+
print("Actual:",y)
|
|
17
|
+
x1 = np.linspace(X[:,0].min(),X[:,0].max(),100)
|
|
18
|
+
plt.scatter(X[:,0],X[:,1],c=y)
|
|
19
|
+
if w[1] != 0:
|
|
20
|
+
plt.plot(x1,(0.5-b-w[0]*x1)/w[1])
|
|
21
|
+
plt.show()
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
import numpy as np
|
|
25
|
+
import pandas as pd
|
|
26
|
+
import matplotlib.pyplot as plt
|
|
27
|
+
import tensorflow as tf
|
|
28
|
+
import time
|
|
29
|
+
import datetime
|
|
30
|
+
from tensorflow.keras import regularizers
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import tensorflow as tf
|
|
2
|
+
import matplotlib.pyplot as plt
|
|
3
|
+
import pandas as pd
|
|
4
|
+
import time
|
|
5
|
+
|
|
6
|
+
# MNIST loading
|
|
7
|
+
train = pd.read_csv("mnist_train(1).csv")
|
|
8
|
+
test = pd.read_csv("mnist_test(1).csv")
|
|
9
|
+
|
|
10
|
+
# Fashion-MNIST loading: Replace the above filenames with:
|
|
11
|
+
# train = pd.read_csv("fashion-mnist_train.csv")
|
|
12
|
+
# test = pd.read_csv("fashion-mnist_test.csv")
|
|
13
|
+
|
|
14
|
+
x_train = train.iloc[:,1:].values.astype('float32').reshape(-1,28,28)/255.0
|
|
15
|
+
y_train = train.iloc[:,0].values
|
|
16
|
+
x_test = test.iloc[:,1:].values.astype('float32').reshape(-1,28,28)/255.0
|
|
17
|
+
y_test = test.iloc[:,0].values
|
|
18
|
+
|
|
19
|
+
print("Training:",x_train.shape)
|
|
20
|
+
print("Testing:",x_test.shape)
|
|
21
|
+
|
|
22
|
+
experiments = [
|
|
23
|
+
('relu',128,64),
|
|
24
|
+
('tanh',128,64),
|
|
25
|
+
('relu',64,32)
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
histories = []
|
|
29
|
+
times = []
|
|
30
|
+
|
|
31
|
+
for act,a,b in experiments:
|
|
32
|
+
model = tf.keras.Sequential([
|
|
33
|
+
tf.keras.layers.Flatten(input_shape=(28,28)),
|
|
34
|
+
tf.keras.layers.Dense(a,activation=act),
|
|
35
|
+
tf.keras.layers.Dense(b,activation=act),
|
|
36
|
+
tf.keras.layers.Dense(10,activation='softmax')
|
|
37
|
+
])
|
|
38
|
+
|
|
39
|
+
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
|
|
40
|
+
|
|
41
|
+
start = time.time()
|
|
42
|
+
h = model.fit(x_train,y_train,epochs=2,batch_size=32,validation_split=0.1,verbose=1)
|
|
43
|
+
elapsed = time.time()-start
|
|
44
|
+
|
|
45
|
+
histories.append(h)
|
|
46
|
+
times.append(elapsed)
|
|
47
|
+
|
|
48
|
+
loss,acc = model.evaluate(x_test,y_test,verbose=0)
|
|
49
|
+
print(act,a,b,"Test Accuracy:",acc)
|
|
50
|
+
print("Training Time:",round(elapsed,2),"seconds")
|
|
51
|
+
|
|
52
|
+
labels = ['ReLU 128-64','Tanh 128-64','ReLU 64-32']
|
|
53
|
+
|
|
54
|
+
# Training and Validation Accuracy
|
|
55
|
+
for h,label in zip(histories,labels):
|
|
56
|
+
plt.plot(h.history['accuracy'],label='Train '+label)
|
|
57
|
+
plt.plot(h.history['val_accuracy'],label='Validation '+label)
|
|
58
|
+
plt.title("Training and Validation Accuracy")
|
|
59
|
+
plt.xlabel("Epochs")
|
|
60
|
+
plt.ylabel("Accuracy")
|
|
61
|
+
plt.legend()
|
|
62
|
+
plt.grid()
|
|
63
|
+
plt.show()
|
|
64
|
+
|
|
65
|
+
# Training and Validation Loss
|
|
66
|
+
for h,label in zip(histories,labels):
|
|
67
|
+
plt.plot(h.history['loss'],label='Train '+label)
|
|
68
|
+
plt.plot(h.history['val_loss'],label='Validation '+label)
|
|
69
|
+
plt.title("Training and Validation Loss")
|
|
70
|
+
plt.xlabel("Epochs")
|
|
71
|
+
plt.ylabel("Loss")
|
|
72
|
+
plt.legend()
|
|
73
|
+
plt.grid()
|
|
74
|
+
plt.show()
|
|
75
|
+
|
|
76
|
+
# Training Time Comparison
|
|
77
|
+
plt.bar(labels,times)
|
|
78
|
+
plt.title("Training Time Comparison")
|
|
79
|
+
plt.xlabel("Model")
|
|
80
|
+
plt.ylabel("Time (seconds)")
|
|
81
|
+
plt.show()
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import tensorflow as tf
|
|
2
|
+
import matplotlib.pyplot as plt
|
|
3
|
+
import pandas as pd
|
|
4
|
+
from tensorflow.keras import regularizers
|
|
5
|
+
# Load MNIST dataset
|
|
6
|
+
train = pd.read_csv("mnist_train(1).csv") # Fashion: "fashion-mnist_train.csv"
|
|
7
|
+
test = pd.read_csv("mnist_test(1).csv") # Fashion: "fashion-mnist_test.csv"
|
|
8
|
+
x_train = train.iloc[:,1:].values.astype('float32').reshape(-1,28,28)/255.0
|
|
9
|
+
y_train = train.iloc[:,0].values
|
|
10
|
+
x_test = test.iloc[:,1:].values.astype('float32').reshape(-1,28,28)/255.0
|
|
11
|
+
y_test = test.iloc[:,0].values
|
|
12
|
+
print("Training:",x_train.shape)
|
|
13
|
+
print("Testing:",x_test.shape)
|
|
14
|
+
# Model 1: No Regularization
|
|
15
|
+
m1 = tf.keras.Sequential([
|
|
16
|
+
tf.keras.layers.Flatten(input_shape=(28,28)),
|
|
17
|
+
tf.keras.layers.Dense(128,activation='relu'),
|
|
18
|
+
tf.keras.layers.Dense(10,activation='softmax')
|
|
19
|
+
])
|
|
20
|
+
# Model 2: L2 Regularization
|
|
21
|
+
m2 = tf.keras.Sequential([
|
|
22
|
+
tf.keras.layers.Flatten(input_shape=(28,28)),
|
|
23
|
+
tf.keras.layers.Dense(128,activation='relu',kernel_regularizer=regularizers.l2(0.001)),
|
|
24
|
+
tf.keras.layers.Dense(10,activation='softmax')
|
|
25
|
+
])
|
|
26
|
+
# Model 3: Dropout Regularization
|
|
27
|
+
m3 = tf.keras.Sequential([
|
|
28
|
+
tf.keras.layers.Flatten(input_shape=(28,28)),
|
|
29
|
+
tf.keras.layers.Dense(128,activation='relu'),
|
|
30
|
+
tf.keras.layers.Dropout(0.5),
|
|
31
|
+
tf.keras.layers.Dense(10,activation='softmax')
|
|
32
|
+
])
|
|
33
|
+
histories = []
|
|
34
|
+
for model in [m1,m2,m3]:
|
|
35
|
+
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
|
|
36
|
+
h = model.fit(x_train,y_train,epochs=5,validation_split=0.2,batch_size=32,verbose=0)
|
|
37
|
+
histories.append(h)
|
|
38
|
+
labels = ['No Reg','L2','Dropout']
|
|
39
|
+
# Validation Accuracy Comparison
|
|
40
|
+
for h,label in zip(histories,labels):
|
|
41
|
+
plt.plot(h.history['val_accuracy'],label=label)
|
|
42
|
+
plt.title("Validation Accuracy Comparison")
|
|
43
|
+
plt.xlabel("Epochs")
|
|
44
|
+
plt.ylabel("Validation Accuracy")
|
|
45
|
+
plt.legend()
|
|
46
|
+
plt.grid()
|
|
47
|
+
plt.savefig("practical3_output.png")
|
|
48
|
+
plt.show()
|
|
49
|
+
# Training and Validation Accuracy
|
|
50
|
+
for h,label in zip(histories,labels):
|
|
51
|
+
plt.plot(h.history['accuracy'],label=label+' Train')
|
|
52
|
+
plt.plot(h.history['val_accuracy'],label=label+' Val')
|
|
53
|
+
plt.title("Training and Validation Accuracy")
|
|
54
|
+
plt.xlabel("Epochs")
|
|
55
|
+
plt.ylabel("Accuracy")
|
|
56
|
+
plt.legend()
|
|
57
|
+
plt.grid()
|
|
58
|
+
plt.show()
|
|
59
|
+
# Training and Validation Loss
|
|
60
|
+
for h,label in zip(histories,labels):
|
|
61
|
+
plt.plot(h.history['loss'],label=label+' Train')
|
|
62
|
+
plt.plot(h.history['val_loss'],label=label+' Val')
|
|
63
|
+
plt.title("Training and Validation Loss")
|
|
64
|
+
plt.xlabel("Epochs")
|
|
65
|
+
plt.ylabel("Loss")
|
|
66
|
+
plt.legend()
|
|
67
|
+
plt.grid()
|
|
68
|
+
plt.savefig("practical3_loss.png")
|
|
69
|
+
plt.show()
|
|
70
|
+
# Evaluate all models on test data
|
|
71
|
+
for model,label in zip([m1,m2,m3],labels):
|
|
72
|
+
loss,acc = model.evaluate(x_test,y_test,verbose=0)
|
|
73
|
+
print(label,"Test Accuracy:",acc*100,"%")
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import tensorflow as tf
|
|
2
|
+
import matplotlib.pyplot as plt
|
|
3
|
+
import pandas as pd
|
|
4
|
+
import time
|
|
5
|
+
train = pd.read_csv("mnist_train(1).csv") # Fashion: "fashion-mnist_train.csv"
|
|
6
|
+
test = pd.read_csv("mnist_test(1).csv") # Fashion: "fashion-mnist_test.csv"
|
|
7
|
+
x_train = train.iloc[:,1:].values.astype('float32').reshape(-1,28,28)/255.0
|
|
8
|
+
y_train = train.iloc[:,0].values
|
|
9
|
+
x_test = test.iloc[:,1:].values.astype('float32').reshape(-1,28,28)/255.0
|
|
10
|
+
y_test = test.iloc[:,0].values
|
|
11
|
+
print("Training:",x_train.shape)
|
|
12
|
+
print("Testing:",x_test.shape)
|
|
13
|
+
acts = ['sigmoid','tanh','relu']
|
|
14
|
+
hist,times,grads = {},{},{}
|
|
15
|
+
for act in acts:
|
|
16
|
+
m = tf.keras.Sequential([
|
|
17
|
+
tf.keras.layers.Input((28,28)),
|
|
18
|
+
tf.keras.layers.Flatten(),
|
|
19
|
+
tf.keras.layers.Dense(128,activation=act),
|
|
20
|
+
tf.keras.layers.Dense(64,activation=act),
|
|
21
|
+
tf.keras.layers.Dense(10,activation='softmax')
|
|
22
|
+
])
|
|
23
|
+
m.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
|
|
24
|
+
t = time.time()
|
|
25
|
+
hist[act] = m.fit(x_train,y_train,epochs=5,batch_size=32,validation_split=0.1,verbose=0)
|
|
26
|
+
times[act] = time.time()-t
|
|
27
|
+
print(act,"Accuracy:",m.evaluate(x_test,y_test,verbose=0)[1],"Time:",round(times[act],2))
|
|
28
|
+
with tf.GradientTape() as tape:
|
|
29
|
+
loss = tf.reduce_mean(tf.keras.losses.sparse_categorical_crossentropy(y_train[:32],m(x_train[:32])))
|
|
30
|
+
grads[act] = [tf.norm(g).numpy() for g in tape.gradient(loss,m.trainable_variables) if g is not None]
|
|
31
|
+
for key in ['accuracy','val_accuracy','loss','val_loss']:
|
|
32
|
+
for act in acts:
|
|
33
|
+
plt.plot(hist[act].history[key],label=act)
|
|
34
|
+
plt.title(key.capitalize())
|
|
35
|
+
plt.xlabel("Epochs")
|
|
36
|
+
plt.legend()
|
|
37
|
+
plt.show()
|
|
38
|
+
plt.bar(acts,[times[a] for a in acts])
|
|
39
|
+
plt.title("Training Time")
|
|
40
|
+
plt.ylabel("Seconds")
|
|
41
|
+
plt.show()
|
|
42
|
+
for act in acts:
|
|
43
|
+
plt.plot(grads[act],label=act)
|
|
44
|
+
plt.title("Gradient Flow")
|
|
45
|
+
plt.xlabel("Trainable Variable Index")
|
|
46
|
+
plt.ylabel("Gradient Norm")
|
|
47
|
+
plt.legend()
|
|
48
|
+
plt.show()
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import tensorflow as tf
|
|
2
|
+
import matplotlib.pyplot as plt
|
|
3
|
+
import time
|
|
4
|
+
import pandas as pd # For MNIST CSV loading
|
|
5
|
+
# Load CIFAR-10 dataset
|
|
6
|
+
train_ds = tf.keras.utils.image_dataset_from_directory(
|
|
7
|
+
"dataset/cifar/cifar10/train",
|
|
8
|
+
image_size=(32,32), batch_size=64,
|
|
9
|
+
validation_split=0.1, subset="training", seed=42
|
|
10
|
+
)
|
|
11
|
+
val_ds = tf.keras.utils.image_dataset_from_directory(
|
|
12
|
+
"dataset/cifar/cifar10/train",
|
|
13
|
+
image_size=(32,32), batch_size=64,
|
|
14
|
+
validation_split=0.1, subset="validation", seed=42
|
|
15
|
+
)
|
|
16
|
+
test_ds = tf.keras.utils.image_dataset_from_directory(
|
|
17
|
+
"dataset/cifar/cifar10/test",
|
|
18
|
+
image_size=(32,32), batch_size=64
|
|
19
|
+
)
|
|
20
|
+
print("Classes:",train_ds.class_names)
|
|
21
|
+
# For MNIST: Replace the above CIFAR-10 loading section with:
|
|
22
|
+
# train = pd.read_csv("mnist_train(1).csv")
|
|
23
|
+
# test = pd.read_csv("mnist_test(1).csv")
|
|
24
|
+
# x_train = train.iloc[:,1:].values.astype('float32')/255.0
|
|
25
|
+
# y_train = train.iloc[:,0].values
|
|
26
|
+
# x_test = test.iloc[:,1:].values.astype('float32')/255.0
|
|
27
|
+
# y_test = test.iloc[:,0].values
|
|
28
|
+
# x_train_cnn = x_train.reshape(-1,28,28,1)
|
|
29
|
+
# x_test_cnn = x_test.reshape(-1,28,28,1)
|
|
30
|
+
# Prepare MLP data
|
|
31
|
+
mlp_train = train_ds.map(lambda x,y:(tf.reshape(x/255.0,[-1,3072]),y)).prefetch(1)
|
|
32
|
+
mlp_val = val_ds.map(lambda x,y:(tf.reshape(x/255.0,[-1,3072]),y)).prefetch(1)
|
|
33
|
+
mlp_test = test_ds.map(lambda x,y:(tf.reshape(x/255.0,[-1,3072]),y)).prefetch(1)
|
|
34
|
+
# Prepare CNN data
|
|
35
|
+
cnn_train = train_ds.map(lambda x,y:(x/255.0,y)).prefetch(1)
|
|
36
|
+
cnn_val = val_ds.map(lambda x,y:(x/255.0,y)).prefetch(1)
|
|
37
|
+
cnn_test = test_ds.map(lambda x,y:(x/255.0,y)).prefetch(1)
|
|
38
|
+
# For MNIST: No map() or prefetch() required.
|
|
39
|
+
# Use x_train and y_train directly for MLP.
|
|
40
|
+
# Use x_train_cnn and y_train directly for CNN.
|
|
41
|
+
# Build MLP model
|
|
42
|
+
mlp = tf.keras.Sequential([
|
|
43
|
+
tf.keras.layers.Input((3072,)), # MNIST: Input((784,))
|
|
44
|
+
tf.keras.layers.Dense(256,activation='relu'),
|
|
45
|
+
tf.keras.layers.Dense(10,activation='softmax')
|
|
46
|
+
])
|
|
47
|
+
# Build CNN model with data augmentation
|
|
48
|
+
cnn = tf.keras.Sequential([
|
|
49
|
+
tf.keras.layers.Input((32,32,3)), # MNIST: Input((28,28,1))
|
|
50
|
+
tf.keras.layers.RandomFlip("horizontal"),
|
|
51
|
+
tf.keras.layers.RandomRotation(0.1), # MNIST: RandomRotation(0.05)
|
|
52
|
+
tf.keras.layers.Conv2D(32,(3,3),activation='relu'),
|
|
53
|
+
tf.keras.layers.MaxPooling2D((2,2)),
|
|
54
|
+
tf.keras.layers.Conv2D(64,(3,3),activation='relu'),
|
|
55
|
+
tf.keras.layers.MaxPooling2D((2,2)),
|
|
56
|
+
tf.keras.layers.Flatten(),
|
|
57
|
+
tf.keras.layers.Dense(128,activation='relu'),
|
|
58
|
+
tf.keras.layers.Dense(10,activation='softmax')
|
|
59
|
+
])
|
|
60
|
+
for model in [mlp,cnn]:
|
|
61
|
+
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
|
|
62
|
+
# Train MLP
|
|
63
|
+
start = time.time()
|
|
64
|
+
h1 = mlp.fit(mlp_train,epochs=5,validation_data=mlp_val,verbose=0)
|
|
65
|
+
mlp_time = time.time()-start
|
|
66
|
+
mlp_loss,mlp_acc = mlp.evaluate(mlp_test,verbose=0)
|
|
67
|
+
print("MLP Accuracy:",mlp_acc*100,"%")
|
|
68
|
+
print("MLP Training Time:",round(mlp_time,2),"seconds")
|
|
69
|
+
# For MNIST MLP training:
|
|
70
|
+
# h1 = mlp.fit(x_train,y_train,epochs=5,batch_size=64,validation_split=0.1)
|
|
71
|
+
# mlp_loss,mlp_acc = mlp.evaluate(x_test,y_test,verbose=0)
|
|
72
|
+
# Train CNN
|
|
73
|
+
start = time.time()
|
|
74
|
+
h2 = cnn.fit(cnn_train,epochs=5,validation_data=cnn_val,verbose=0)
|
|
75
|
+
cnn_time = time.time()-start
|
|
76
|
+
cnn_loss,cnn_acc = cnn.evaluate(cnn_test,verbose=0)
|
|
77
|
+
print("CNN Accuracy:",cnn_acc*100,"%")
|
|
78
|
+
print("CNN Training Time:",round(cnn_time,2),"seconds")
|
|
79
|
+
# For MNIST CNN training:
|
|
80
|
+
# h2 = cnn.fit(x_train_cnn,y_train,epochs=5,batch_size=64,validation_split=0.1)
|
|
81
|
+
# cnn_loss,cnn_acc = cnn.evaluate(x_test_cnn,y_test,verbose=0)
|
|
82
|
+
# Compare accuracy
|
|
83
|
+
plt.bar(['MLP','CNN'],[mlp_acc*100,cnn_acc*100])
|
|
84
|
+
plt.title("MLP vs CNN Accuracy")
|
|
85
|
+
plt.ylabel("Accuracy (%)")
|
|
86
|
+
plt.show()
|
|
87
|
+
# Compare training time
|
|
88
|
+
plt.bar(['MLP','CNN'],[mlp_time,cnn_time])
|
|
89
|
+
plt.title("Training Time Comparison")
|
|
90
|
+
plt.ylabel("Time (seconds)")
|
|
91
|
+
plt.show()
|
|
92
|
+
# Plot validation accuracy
|
|
93
|
+
plt.plot(h1.history['val_accuracy'],label='MLP')
|
|
94
|
+
plt.plot(h2.history['val_accuracy'],label='CNN')
|
|
95
|
+
plt.title("Validation Accuracy")
|
|
96
|
+
plt.xlabel("Epochs")
|
|
97
|
+
plt.ylabel("Accuracy")
|
|
98
|
+
plt.legend()
|
|
99
|
+
plt.show()
|
|
100
|
+
# Plot validation loss
|
|
101
|
+
plt.plot(h1.history['val_loss'],label='MLP')
|
|
102
|
+
plt.plot(h2.history['val_loss'],label='CNN')
|
|
103
|
+
plt.title("Validation Loss")
|
|
104
|
+
plt.xlabel("Epochs")
|
|
105
|
+
plt.ylabel("Loss")
|
|
106
|
+
plt.legend()
|
|
107
|
+
plt.show()
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import tensorflow as tf
|
|
2
|
+
import matplotlib.pyplot as plt
|
|
3
|
+
import pandas as pd
|
|
4
|
+
df = pd.read_csv("IMDB Dataset(2).csv")
|
|
5
|
+
df['sentiment'] = df['sentiment'].map({'positive':1,'negative':0})
|
|
6
|
+
train = df.sample(frac=0.8,random_state=42)
|
|
7
|
+
test = df.drop(train.index)
|
|
8
|
+
x_train = train['review'].astype(str).values
|
|
9
|
+
y_train = train['sentiment'].values
|
|
10
|
+
x_test = test['review'].astype(str).values
|
|
11
|
+
y_test = test['sentiment'].values
|
|
12
|
+
tokenizer = tf.keras.preprocessing.text.Tokenizer(num_words=5000)
|
|
13
|
+
tokenizer.fit_on_texts(x_train)
|
|
14
|
+
x_train = tokenizer.texts_to_sequences(x_train)
|
|
15
|
+
x_test = tokenizer.texts_to_sequences(x_test)
|
|
16
|
+
x_train = tf.keras.preprocessing.sequence.pad_sequences(x_train,maxlen=100)
|
|
17
|
+
x_test = tf.keras.preprocessing.sequence.pad_sequences(x_test,maxlen=100)
|
|
18
|
+
model = tf.keras.Sequential([
|
|
19
|
+
tf.keras.layers.Embedding(5000,64),
|
|
20
|
+
tf.keras.layers.SimpleRNN(32), # Changed from LSTM(64) to SimpleRNN(32)
|
|
21
|
+
tf.keras.layers.Dense(1,activation='sigmoid')
|
|
22
|
+
])
|
|
23
|
+
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
|
|
24
|
+
h = model.fit(x_train,y_train,epochs=3,batch_size=64,validation_split=0.1)
|
|
25
|
+
print("Test Accuracy:",model.evaluate(x_test,y_test,verbose=0)[1]*100,"%")
|
|
26
|
+
plt.plot(h.history['accuracy'],label='Train')
|
|
27
|
+
plt.plot(h.history['val_accuracy'],label='Validation')
|
|
28
|
+
plt.title("Simple RNN IMDB Accuracy")
|
|
29
|
+
plt.xlabel("Epochs")
|
|
30
|
+
plt.legend()
|
|
31
|
+
plt.show()
|
|
32
|
+
for i in range(3):
|
|
33
|
+
p = model.predict(x_test[i:i+1],verbose=0)[0][0]
|
|
34
|
+
print("Review",i+1,":","Positive" if p>=0.5 else "Negative",round(p,2))
|
|
35
|
+
review = ["This movie was amazing and I really enjoyed it"]
|
|
36
|
+
seq = tf.keras.preprocessing.sequence.pad_sequences(tokenizer.texts_to_sequences(review),maxlen=100)
|
|
37
|
+
p = model.predict(seq,verbose=0)[0][0]
|
|
38
|
+
print("Prediction:", "Positive" if p>=0.5 else "Negative")
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import tensorflow as tf
|
|
2
|
+
import matplotlib.pyplot as plt
|
|
3
|
+
train_ds = tf.keras.utils.image_dataset_from_directory(
|
|
4
|
+
"dataset/cats_and_dogs_filtered/train",
|
|
5
|
+
image_size=(160,160), batch_size=32
|
|
6
|
+
)
|
|
7
|
+
val_ds = tf.keras.utils.image_dataset_from_directory(
|
|
8
|
+
"dataset/cats_and_dogs_filtered/validation",
|
|
9
|
+
image_size=(160,160), batch_size=32
|
|
10
|
+
)
|
|
11
|
+
print("Classes:", train_ds.class_names)
|
|
12
|
+
preprocess = tf.keras.applications.vgg16.preprocess_input #resnet50.preprocess_input
|
|
13
|
+
train_ds = train_ds.map(lambda x,y: (preprocess(x),y)).prefetch(1)
|
|
14
|
+
val_ds = val_ds.map(lambda x,y: (preprocess(x),y)).prefetch(1)
|
|
15
|
+
base = tf.keras.applications.VGG16(weights='imagenet', include_top=False, input_shape=(160,160,3)) #ResNet50
|
|
16
|
+
base.trainable = False
|
|
17
|
+
model = tf.keras.Sequential([
|
|
18
|
+
base,
|
|
19
|
+
tf.keras.layers.GlobalAveragePooling2D(),
|
|
20
|
+
tf.keras.layers.Dense(128, activation='relu'),
|
|
21
|
+
tf.keras.layers.Dense(1, activation='sigmoid')
|
|
22
|
+
])
|
|
23
|
+
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
|
|
24
|
+
h1 = model.fit(train_ds, validation_data=val_ds, epochs=3)
|
|
25
|
+
loss1, acc1 = model.evaluate(val_ds)
|
|
26
|
+
print("Frozen VGG16 Accuracy:", acc1*100, "%")
|
|
27
|
+
base.trainable = True
|
|
28
|
+
for layer in base.layers[:-4]:
|
|
29
|
+
layer.trainable = False
|
|
30
|
+
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.00001), loss='binary_crossentropy', metrics=['accuracy'])
|
|
31
|
+
h2 = model.fit(train_ds, validation_data=val_ds, epochs=3)
|
|
32
|
+
loss2, acc2 = model.evaluate(val_ds)
|
|
33
|
+
print("Fine-tuned VGG16 Accuracy:", acc2*100, "%")
|
|
34
|
+
plt.bar(['Frozen VGG16','Fine-tuned VGG16'], [acc1*100,acc2*100])
|
|
35
|
+
plt.ylabel("Accuracy (%)")
|
|
36
|
+
plt.title("VGG16 Performance Comparison")
|
|
37
|
+
plt.show()
|
|
38
|
+
plt.plot(h1.history['val_accuracy'], label='Frozen VGG16')
|
|
39
|
+
plt.plot([x+3 for x in range(3)], h2.history['val_accuracy'], label='Fine-tuned VGG16')
|
|
40
|
+
plt.axvline(x=2, color='r', linestyle='--', label='Fine-tuning')
|
|
41
|
+
plt.xlabel("Epochs")
|
|
42
|
+
plt.ylabel("Validation Accuracy")
|
|
43
|
+
plt.title("VGG16 Accuracy Comparison")
|
|
44
|
+
plt.legend()
|
|
45
|
+
plt.show()
|