Skip to content

Commit

Permalink
the previous noise function generated a negative when setting the noi…
Browse files Browse the repository at this point in the history
…se_level to 1. (since this was the flip probability). This implementation generates a fully random pattern on noise_level 1.
  • Loading branch information
younesStrittmatter committed Jan 18, 2025
1 parent f9824cb commit 4a9b851
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions Scripts/Cog Neuro Labs/Lab1_2021_DynamicsAndPerception_v1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,22 @@
" Xs[i] = X\n",
" return Xs\n",
"\n",
"def add_noise(x_, noise_level=.2):\n",
" noise = np.random.choice(\n",
" [1, -1], size=len(x_), p=[1-noise_level, noise_level])\n",
" return x_ * noise\n",
"def add_noise(pattern, noise_level=0.2):\n",
" \"\"\"\n",
" Add noise to a `pattern` by replacing each pixel with a random value (-1 or 1)\n",
" with probability `noise_level`\n",
" \"\"\"\n",
" # Generate a random pattern of 1 and -1\n",
" random_pattern = np.random.choice([-1, 1], size=pattern.shape)\n",
"\n",
" # Generate a noise mask based on the noise level\n",
" noise_mask = np.random.rand(pattern.shape[0]) < noise_level\n",
"\n",
" # Blend the original pattern and the random pattern based on the noise mask\n",
" noisy_pattern = pattern.copy()\n",
" noisy_pattern[noise_mask] = random_pattern[noise_mask]\n",
"\n",
" return noisy_pattern\n",
"\n",
" \n"
],
Expand Down

0 comments on commit 4a9b851

Please sign in to comment.