Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ISC4_IA_ML
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
kevineri.bonga
ISC4_IA_ML
Commits
d3cd4e12
Commit
d3cd4e12
authored
8 months ago
by
Kevin Bonga
Browse files
Options
Downloads
Patches
Plain Diff
adding a gaussian noise function
parent
7299f2fd
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
exos/tp3_autoencoder/main.py
+43
-1
43 additions, 1 deletion
exos/tp3_autoencoder/main.py
with
43 additions
and
1 deletion
exos/tp3_autoencoder/main.py
+
43
−
1
View file @
d3cd4e12
...
@@ -22,6 +22,26 @@ def add_noise(data, noise_factor=0.5):
...
@@ -22,6 +22,26 @@ def add_noise(data, noise_factor=0.5):
noisy_data
=
np
.
clip
(
noisy_data
,
0.
,
1.
)
noisy_data
=
np
.
clip
(
noisy_data
,
0.
,
1.
)
return
noisy_data
return
noisy_data
def
add_binary_noise
(
data
,
noise_type
=
'
uniform
'
,
noise_factor
=
0.1
):
"""
Add noise to binary data. For values=0, add a positive quantity. For values=1, subtract a positive quantity.
The noise can be determined by a uniform or Gaussian distribution.
:param data: numpy.ndarray: The input binary data.
:param noise_type: str: The type of noise distribution (
'
uniform
'
or
'
gaussian
'
).
:param noise_factor: float: The factor controlling the amount of noise.
:return: numpy.ndarray: The noisy data.
"""
if
noise_type
==
'
uniform
'
:
noise
=
noise_factor
*
np
.
random
.
uniform
(
low
=
0.0
,
high
=
1.0
,
size
=
data
.
shape
)
elif
noise_type
==
'
gaussian
'
:
noise
=
noise_factor
*
np
.
random
.
normal
(
loc
=
0.0
,
scale
=
1.0
,
size
=
data
.
shape
)
else
:
raise
ValueError
(
"
Invalid noise_type. Choose
'
uniform
'
or
'
gaussian
'
.
"
)
noisy_data
=
np
.
where
(
data
==
0
,
data
+
noise
,
data
-
noise
)
# Ajouter du bruit aux valeurs égales à 0 et soustraire du bruit aux valeurs égales à 1
noisy_data
=
np
.
clip
(
noisy_data
,
0.
,
1.
)
# Clipper les valeurs pour qu'elles restent dans la plage [0, 1]
return
noisy_data
# Auto-encodeur avec une couche cachée
# Auto-encodeur avec une couche cachée
def
build_single_layer_autoencoder
(
input_dim
,
hidden_dim
):
def
build_single_layer_autoencoder
(
input_dim
,
hidden_dim
):
"""
"""
...
@@ -101,3 +121,25 @@ train_and_evaluate_autoencoder(three_layer_autoencoder, X_train, X_test)
...
@@ -101,3 +121,25 @@ train_and_evaluate_autoencoder(three_layer_autoencoder, X_train, X_test)
# Entraîner et évaluer l'auto-encodeur avec des données bruitées
# Entraîner et évaluer l'auto-encodeur avec des données bruitées
train_and_evaluate_noisy_autoencoder
(
three_layer_autoencoder
,
X_train
,
X_test
,
noise_factor
=
0.5
)
train_and_evaluate_noisy_autoencoder
(
three_layer_autoencoder
,
X_train
,
X_test
,
noise_factor
=
0.5
)
# Entraîner et évaluer un auto-encodeur avec des données binaires bruitées
def
train_and_evaluate_binary_noisy_autoencoder
(
autoencoder
,
X_train
,
X_test
,
noise_type
=
'
uniform
'
,
noise_factor
=
0.1
,
epochs
=
50
,
batch_size
=
256
):
"""
Train and evaluate an autoencoder model on binary noisy data.
:param autoencoder: the autoencoder model to train and evaluate
:param X_train: training data without noise (clean data)
:param X_test: test data without noise (clean data)
:param noise_type: the type of noise distribution (
'
uniform
'
or
'
gaussian
'
)
:param noise_factor: the factor controlling the amount of noise to add to the data
:param epochs: number of epochs to train the model
:param batch_size: batch size for training
"""
X_train_noisy
=
add_binary_noise
(
X_train
,
noise_type
,
noise_factor
)
X_test_noisy
=
add_binary_noise
(
X_test
,
noise_type
,
noise_factor
)
autoencoder
.
fit
(
X_train_noisy
,
X_train
,
epochs
=
epochs
,
batch_size
=
batch_size
,
shuffle
=
True
,
validation_data
=
(
X_test_noisy
,
X_test
))
reconstructed
=
autoencoder
.
predict
(
X_test
)
mse
=
np
.
mean
(
np
.
power
(
X_test
-
reconstructed
,
2
),
axis
=
1
)
# Calculer l'erreur quadratique moyenne pour chaque exemple de test (comparaison entre les données originales et les données reconstruites)
print
(
"
Mean Squared Error with binary noise:
"
,
np
.
mean
(
mse
))
# Afficher l'erreur quadratique moyenne
# Utilisation de la nouvelle fonction
train_and_evaluate_binary_noisy_autoencoder
(
three_layer_autoencoder
,
X_train
,
X_test
,
noise_type
=
'
uniform
'
,
noise_factor
=
0.1
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment