Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Applied Generative AI with GANS/2. Introduction to GAN.ipynb
4834 views
Kernel: Python 3 (ipykernel)

Introduction to GAN (Generative Adversarial Network)

1. What is a GAN?

A GAN (Generative Adversarial Network) is a Generative AI model made of two neural networks that compete with each other:

  1. Generator (G)

    • Creates fake data (e.g., fake images)

    • Tries to look as real as possible

  2. Discriminator (D)

    • Checks whether data is real or fake

    • Acts like a judge

Simple Analogy

  • Generator = Student trying to copy exam answers

  • Discriminator = Teacher trying to catch cheating

  • Over time, the student gets so good that the teacher cannot tell the difference


2. Why Do We Need GANs?

Traditional ML:

  • Predicts labels (classification)

  • Predicts numbers (regression)

GANs:

  • Create new data

  • Images, text, audio, synthetic records

Example use cases:

  • Fake face generation

  • Data augmentation

  • Synthetic medical data

  • Image enhancement


3. Basic GAN Workflow

  1. Start with random noise

  2. Generator converts noise → fake data

  3. Discriminator sees:

    • Real data

    • Fake data

  4. Discriminator gives feedback

  5. Generator improves based on feedback

  6. Repeat until fake data looks real

A Simple Python Code to understand GAN concept

#Learn a distribution of real numbers around 10 import numpy as np # Real data: numbers around 10 real_data = np.random.normal(10, 1, 1000) real_data
array([10.56765609, 11.18429197, 9.9151507 , 8.71786127, 10.54361901, 11.28414511, 10.69936346, 10.34007762, 8.8027879 , 9.48893044, 10.25357166, 10.99645882, 8.7492453 , 11.17847834, 10.16448916, 10.89138337, 10.15231153, 10.00416903, 10.81787773, 10.03123128, 11.06787302, 9.91109554, 10.61142852, 9.01822488, 9.95607684, 9.38454449, 8.13005413, 10.67045101, 11.80577713, 8.51148851, 10.29286 , 9.38979813, 9.91101741, 11.77830479, 10.3390091 , 10.97801236, 9.14053873, 11.47993125, 9.58579866, 8.83829711, 11.22154019, 8.59138095, 11.57460614, 10.28705773, 9.97875273, 8.95145312, 9.1639222 , 9.57432477, 10.11284362, 11.76846352, 8.85043224, 9.5262758 , 9.84879991, 9.98147176, 9.72589621, 9.67224225, 10.1958389 , 10.55351201, 9.28786973, 9.4120672 , 10.03850164, 10.33391633, 10.27287082, 9.94203606, 10.84041094, 10.47244661, 11.37777381, 9.76132794, 10.80237041, 11.38517466, 10.28769569, 11.27352994, 9.16770234, 11.1958652 , 9.7536387 , 9.6644222 , 11.66159369, 7.86447081, 10.03253872, 10.48170079, 10.30789942, 11.33199877, 10.94708292, 9.80642501, 8.55799691, 8.78713239, 9.94969007, 9.63573784, 10.98468574, 9.15363114, 10.26407997, 9.35099088, 8.8792188 , 10.85057921, 10.31299493, 8.77422698, 10.67417906, 9.70880541, 9.44002019, 10.35191441, 9.2014801 , 8.45649709, 9.68135133, 9.79010238, 9.14112199, 8.27780965, 10.28022414, 9.86825834, 10.10302389, 10.13966277, 9.34100972, 9.26345441, 9.5253524 , 8.59720123, 9.19367761, 8.98888705, 9.08054859, 8.56502141, 10.32144562, 10.71953338, 10.89756259, 9.49738868, 9.38458553, 10.29280562, 9.18271634, 9.54116179, 10.14243686, 9.46528743, 11.19477717, 10.21737181, 10.73057302, 10.60089978, 7.85515315, 9.43091215, 8.70349182, 8.55425979, 9.28590484, 10.31768644, 10.17498525, 7.8471149 , 11.38403104, 9.36990544, 9.54612996, 11.06901915, 10.40938297, 10.23811679, 10.94025659, 12.65409977, 11.00746688, 9.41702884, 10.19962753, 9.73678321, 12.0378682 , 10.05751119, 8.43673276, 9.31711194, 11.63843698, 8.72913763, 10.33547498, 11.00811518, 11.3345097 , 8.27308705, 11.63249176, 10.18464099, 11.00131626, 9.75306662, 9.87143892, 9.7302255 , 9.77393116, 10.0324121 , 10.51909612, 9.67631786, 10.63399382, 10.4305378 , 9.88581565, 10.77379559, 11.50253544, 10.0659774 , 10.48878527, 10.7090662 , 9.89524041, 11.39681416, 8.41895968, 10.70846854, 8.16281375, 9.3201689 , 8.15856812, 10.24592071, 7.27097367, 10.30899703, 9.58144018, 10.7286884 , 10.44097305, 10.86202578, 9.76900644, 10.61365305, 9.41089957, 9.71943127, 9.61828284, 10.39670884, 10.38467517, 10.85910279, 10.91834498, 9.24765835, 7.64733376, 9.3068134 , 10.38786791, 8.9587822 , 10.36288266, 9.44489809, 11.2164398 , 10.62902817, 9.66648332, 9.35788529, 10.39846591, 11.99626337, 10.6799042 , 8.51137483, 10.25413342, 10.74159177, 9.38948181, 9.09353502, 10.09432886, 10.90667272, 10.02358244, 9.4912103 , 7.97340182, 9.83190357, 10.39645766, 10.49104511, 9.89335459, 9.85400808, 10.24660871, 9.87434029, 8.789423 , 9.28131289, 10.86665943, 10.8272109 , 9.60080785, 9.42884854, 12.90477456, 10.81885213, 10.04815056, 11.30296717, 8.67689449, 8.33395311, 11.27968229, 9.65111545, 7.9998422 , 11.32587137, 9.55498666, 9.85934869, 10.15445093, 10.0695238 , 9.75675306, 8.98228048, 8.11327165, 8.98998204, 9.35221823, 9.1657851 , 8.6656008 , 9.03268182, 10.81841972, 10.18882269, 10.43046129, 10.15000936, 10.91606236, 10.70769339, 9.8439196 , 10.75421391, 10.44111675, 10.87022284, 10.46433359, 9.59166704, 9.1781998 , 9.10200162, 8.6350133 , 11.93339639, 10.63941375, 11.20469123, 10.39566277, 9.84830175, 10.92895148, 10.68702075, 9.22368221, 9.6522971 , 9.56646021, 10.34294609, 11.35083298, 9.91079123, 9.72197326, 11.06430231, 10.32440451, 10.62901174, 8.10456803, 10.77528427, 10.306097 , 9.60747902, 9.40076177, 11.51110973, 9.19397608, 8.36932534, 9.85932761, 9.23783692, 10.21853262, 9.70132097, 9.97864067, 10.95820309, 9.7890259 , 10.2700336 , 10.14906443, 10.87852053, 9.77266115, 9.56300296, 9.14831892, 9.04619199, 11.71691048, 9.75050707, 10.12965981, 9.73748535, 8.97430917, 10.03569583, 8.23212311, 10.09212798, 9.06218417, 11.82319348, 9.08928362, 8.81451728, 9.65991461, 10.06121398, 9.28294671, 10.3233695 , 11.289761 , 9.53146533, 11.59739329, 10.62971559, 12.03276734, 9.85298908, 10.8955836 , 10.95352976, 10.29572291, 9.72247803, 10.79638664, 9.30783803, 8.64810188, 10.07223595, 11.37454885, 11.29850319, 7.5368512 , 10.04901636, 9.58816643, 8.26191666, 9.21651597, 10.13646905, 9.40930657, 9.17966321, 8.55499732, 9.46204861, 8.84020154, 11.67103953, 9.14492669, 10.76588638, 9.031661 , 9.12838519, 10.80213424, 11.36473928, 10.67578397, 10.91703655, 9.18933138, 9.18561488, 10.32942973, 9.383037 , 10.64445136, 11.08639014, 10.54765214, 10.94285812, 10.97676094, 10.10303716, 9.83077015, 10.42734628, 9.0062944 , 9.54348824, 9.73651153, 9.62191158, 7.44051861, 9.68433034, 9.56131021, 11.50841935, 9.87172601, 8.56981767, 8.96216364, 9.30333131, 9.98251975, 9.94016357, 10.61817983, 10.43534558, 9.72088781, 9.45474868, 12.02642114, 8.5710426 , 9.03514086, 10.50521458, 9.74922799, 11.49110048, 10.87161283, 8.12734813, 10.05083639, 9.91871475, 10.22669902, 9.76485434, 7.90574996, 9.63932609, 10.23561789, 8.50221533, 10.30008386, 9.56370006, 10.4302067 , 10.72073998, 10.08224854, 8.66428092, 9.38692556, 10.82194234, 9.15126476, 9.11042451, 9.71857659, 10.19616174, 9.71279337, 8.34349367, 9.65074472, 9.29292432, 9.53899952, 10.79637484, 11.16052253, 10.73427659, 8.82561539, 10.0422684 , 10.47659418, 9.65744938, 11.66152414, 10.68519043, 9.18056834, 11.27446452, 9.48423585, 9.79113049, 11.26110351, 12.25503301, 10.87994165, 9.39433636, 11.2172421 , 10.13784688, 10.82685783, 9.585374 , 9.64446326, 10.35889777, 11.73596844, 9.29995647, 8.71154482, 8.87350212, 10.53291826, 9.75984862, 10.07501718, 10.38750663, 8.84558356, 9.74745825, 11.41556112, 9.78866019, 11.03046586, 11.95424524, 9.35353239, 10.32483462, 8.8638689 , 9.3487615 , 10.26970433, 12.65827236, 8.62486594, 9.2057081 , 8.44663086, 10.50315478, 8.08023837, 9.38262994, 10.15029754, 9.69455759, 8.81619736, 9.30367711, 11.20370392, 9.47233086, 10.46304661, 9.40388819, 9.75543409, 9.15817349, 10.17484146, 12.43230206, 11.69287487, 11.08483044, 11.79979503, 10.48538715, 9.22693209, 11.19215441, 9.97199136, 9.54968319, 10.91047992, 8.30585218, 10.35241063, 10.38342125, 10.3107556 , 9.21624419, 10.46816883, 10.3239161 , 10.52655804, 10.70343779, 9.55869967, 9.80294146, 11.59227597, 9.34452198, 10.16491963, 8.39935871, 9.21264803, 10.40100288, 8.3394339 , 9.32281001, 10.30938614, 11.25396592, 8.00631544, 9.62499409, 9.30663327, 10.09390422, 10.1954263 , 10.51587616, 10.08439541, 11.07864611, 10.78715076, 7.95247051, 9.39135413, 8.38564241, 9.2806113 , 10.61081617, 11.3260782 , 9.80733782, 10.03146611, 10.68126236, 10.53148544, 11.50573697, 9.8601736 , 8.77465141, 8.55189492, 10.03922136, 9.44954494, 7.69478353, 9.05563918, 8.59164432, 10.29691026, 10.55721183, 10.8393477 , 9.64931654, 9.05970874, 11.50520776, 9.42964498, 7.93432282, 9.56562172, 9.34072145, 10.47358987, 9.62229382, 9.87774992, 9.27825176, 9.33187459, 9.98770242, 10.44646936, 10.37645085, 11.32443753, 9.34206298, 9.16985094, 10.30997199, 9.64705053, 9.3935999 , 9.9349164 , 9.35955682, 10.57919169, 10.69724603, 9.50875438, 11.96842127, 11.43382937, 10.92255383, 9.4421491 , 10.84550451, 9.65306872, 10.34011818, 8.50883345, 10.28093387, 10.91156439, 10.23604328, 10.03233677, 9.38179091, 11.24490362, 10.1742545 , 9.36739272, 10.62411815, 11.38128806, 11.78739697, 12.32381469, 9.85427211, 10.70945252, 8.5569772 , 10.64072796, 10.75526689, 8.07765658, 11.43059193, 8.84778767, 8.9560182 , 9.23942407, 10.06593558, 9.18389301, 9.45981646, 10.00866028, 9.69006452, 9.56004293, 9.27931535, 8.72774975, 11.97432553, 10.81514666, 8.69113947, 8.77254818, 7.96632723, 11.08236834, 10.13562758, 8.95004758, 11.44440636, 10.41792761, 10.34014402, 9.36853166, 12.0908794 , 10.00779272, 8.24899011, 9.29540447, 10.43453856, 9.22730388, 9.37835197, 9.45306056, 9.01448015, 10.33381563, 9.21412256, 11.31636217, 9.218545 , 10.72384853, 9.58767712, 9.63253948, 9.37136718, 11.11187595, 10.74009179, 9.89537543, 9.54392151, 10.78825067, 10.45820444, 10.19392681, 9.02168373, 9.05991089, 11.02845176, 10.91090713, 10.36229023, 10.52479223, 11.81831858, 11.68291246, 9.98360883, 8.69933656, 9.25171384, 9.1568344 , 10.25185435, 9.13783223, 11.62362313, 9.51412604, 11.49703743, 8.52340025, 10.33242861, 9.57111736, 11.59001061, 10.19654922, 8.84814479, 10.24563398, 9.23357754, 9.27625785, 8.7835526 , 11.2194899 , 8.77607485, 8.89882086, 10.56271102, 8.74626531, 11.73263556, 9.36910342, 9.57516771, 10.79695607, 8.62692924, 11.28746117, 10.5793754 , 7.84257497, 8.62844712, 9.07020029, 10.20941815, 10.69115465, 11.3272306 , 9.4605384 , 10.63023299, 9.54303133, 9.13641127, 8.47118286, 10.63968084, 10.07369757, 10.45357354, 11.85272929, 9.64520772, 9.6145588 , 10.92335305, 10.36403061, 9.60726868, 9.54252445, 11.84360649, 10.25501974, 10.4312052 , 9.34183545, 10.37867206, 9.25330509, 9.31849341, 11.08107525, 11.01194655, 9.50248156, 11.57109746, 8.55030829, 11.95769028, 9.69315081, 9.33199228, 9.8007026 , 10.58900411, 10.74425222, 9.96924808, 10.98958327, 9.4280946 , 9.74579059, 8.4028441 , 9.96355214, 9.37040933, 9.06383678, 10.63020891, 10.40088869, 11.32840084, 9.16607767, 10.71103666, 9.99054852, 8.71645192, 10.33191843, 11.68320063, 10.09945693, 8.78283321, 10.08271988, 7.80584944, 9.40416202, 8.24552912, 9.7577787 , 11.54092374, 9.91828163, 10.51240624, 9.62390641, 10.62501281, 10.91447232, 10.19884547, 9.55481311, 9.02818134, 9.69766733, 11.28809185, 9.56782601, 9.58851686, 7.8606732 , 11.0730837 , 9.52159286, 9.52595345, 8.0288729 , 10.46328168, 10.65131426, 8.57881909, 8.52948878, 10.00911308, 8.45210396, 9.98893309, 10.76732917, 9.96957552, 8.84787756, 9.45603054, 8.86503732, 9.42601469, 9.09971388, 11.12321446, 9.90528225, 11.78207214, 12.06387025, 9.10565836, 10.80713481, 10.93307893, 9.06059584, 9.77131666, 9.6154624 , 8.69883732, 10.84430117, 9.61993226, 11.06555189, 10.02638705, 8.78943195, 8.27184209, 8.7984957 , 10.58124027, 8.74106897, 8.98258279, 9.83816718, 9.8011214 , 9.57794557, 9.21101742, 9.10753294, 9.56807662, 12.22000978, 10.56371772, 9.87965857, 9.75952259, 10.54956792, 11.07012496, 11.34398098, 9.96524997, 10.39996363, 11.45892425, 10.76277374, 10.88725404, 8.32306202, 10.13542757, 11.85851765, 9.08388983, 10.29808557, 11.16740379, 9.55382885, 9.44546207, 9.80358364, 10.14972087, 9.76220777, 11.66010941, 11.65897627, 10.25952995, 11.39289483, 10.15573945, 9.89790624, 10.35397062, 11.35937674, 8.73427686, 10.37581886, 11.51115975, 9.56308244, 10.7071019 , 12.07449331, 10.60595604, 10.47025423, 10.65198529, 9.73546763, 9.16722731, 9.73673783, 9.46012054, 9.92994139, 10.82545706, 9.26953651, 10.19988009, 10.45048422, 10.66044142, 9.47996961, 10.14181758, 10.26057806, 10.09604717, 10.38245721, 12.29015118, 8.13922389, 9.55316433, 9.46603532, 9.22457613, 10.12235351, 8.83681309, 10.94012217, 9.75905557, 12.67063297, 9.34342959, 10.71451771, 10.11123499, 9.27475815, 9.21269498, 8.02271527, 7.988547 , 10.27620915, 10.33707638, 10.98852637, 8.74163004, 9.96770685, 10.61266439, 11.39646608, 6.95443189, 10.0399599 , 9.74610561, 11.03264426, 10.00988019, 11.01963824, 10.46376596, 11.06786585, 11.29678072, 10.56348839, 10.41211176, 9.15389545, 6.30743927, 10.19298637, 9.54773982, 9.70168106, 10.23820391, 11.44008685, 8.47301064, 8.98059405, 10.60710058, 11.0486683 , 9.95109513, 8.53977037, 10.56128439, 9.16680792, 11.35872504, 9.98919051, 8.50994019, 10.2361364 , 9.51380813, 12.00273412, 8.81995189, 9.34161633, 11.69066801, 10.66967109, 9.49362636, 10.61765522, 10.49881581, 8.35802299, 8.69058957, 8.76549088, 9.32571032, 8.62662157, 10.12982689, 10.27229051, 9.01598807, 10.27679811, 11.06119425, 9.17606736, 10.25962208, 10.98992966, 10.12716967, 11.81498359, 9.57070564, 10.66677166, 8.13433714, 11.37135063, 8.77877561, 10.9308067 , 9.93598598, 11.09745699, 11.29759825, 11.25406849, 9.60003401, 8.91777565, 9.21768965, 10.61531544, 9.00733928, 10.27574095, 10.60841883, 10.27126294, 8.59993342, 7.92365443, 10.61045426, 9.69675047, 10.33840671, 8.82114255, 9.52527693, 10.48123256, 11.30560093, 8.85705821, 10.22360477, 8.56956433, 10.11776877, 8.91576495, 8.44300031, 8.58969221, 10.65342872, 10.14378373, 8.48006168, 9.91070769, 9.75993555, 7.51605255, 10.5971578 , 9.56315264, 8.06051098, 8.97650003, 10.06577103, 9.21986454, 12.21033952, 10.07874756, 11.54938368, 9.87528076, 10.61662051, 9.30181414, 8.54935645])
# create Fake Data def generator(size): # Generates random numbers (initially bad) return np.random.uniform(0, 20, size)
## Discriminator (Judges Data) def discriminator(data): # Simple rule: values near 10 are more likely real return np.exp(-abs(data - 10))
# Test Generator Output fake_data = generator(5) print("Fake Data:", fake_data) print("Discriminator Scores:", discriminator(fake_data))
Fake Data: [18.34996294 10.86329849 12.39410218 11.91614647 7.18871103] Discriminator Scores: [2.36405280e-04 4.21768585e-01 9.12545725e-02 1.47173006e-01 6.01274403e-02]

Example 2

pip install torch
Requirement already satisfied: torch in c:\users\suyashi144893\appdata\local\anaconda3\lib\site-packages (2.7.1) Requirement already satisfied: filelock in c:\users\suyashi144893\appdata\local\anaconda3\lib\site-packages (from torch) (3.9.0) Requirement already satisfied: typing-extensions>=4.10.0 in c:\users\suyashi144893\appdata\local\anaconda3\lib\site-packages (from torch) (4.12.2) Requirement already satisfied: sympy>=1.13.3 in c:\users\suyashi144893\appdata\local\anaconda3\lib\site-packages (from torch) (1.14.0) Requirement already satisfied: networkx in c:\users\suyashi144893\appdata\local\anaconda3\lib\site-packages (from torch) (3.1) Requirement already satisfied: jinja2 in c:\users\suyashi144893\appdata\local\anaconda3\lib\site-packages (from torch) (3.1.2) Requirement already satisfied: fsspec in c:\users\suyashi144893\appdata\local\anaconda3\lib\site-packages (from torch) (2024.9.0) Requirement already satisfied: mpmath<1.4,>=1.1.0 in c:\users\suyashi144893\appdata\local\anaconda3\lib\site-packages (from sympy>=1.13.3->torch) (1.3.0) Requirement already satisfied: MarkupSafe>=2.0 in c:\users\suyashi144893\appdata\local\anaconda3\lib\site-packages (from jinja2->torch) (2.1.1) Note: you may need to restart the kernel to use updated packages.
[notice] A new release of pip is available: 25.2 -> 25.3 [notice] To update, run: python.exe -m pip install --upgrade pip
import torch import torch.nn as nn # Generator class Generator(nn.Module): def __init__(self): super().__init__() self.fc = nn.Linear(1, 1) def forward(self, x): return self.fc(x) # Discriminator class Discriminator(nn.Module): def __init__(self): super().__init__() self.fc = nn.Linear(1, 1) self.sigmoid = nn.Sigmoid() def forward(self, x): return self.sigmoid(self.fc(x)) G = Generator() D = Discriminator() # Random noise z = torch.randn(5, 1) # Fake data fake_data = G(z) # Judge fake data decision = D(fake_data) print("Fake Data:", fake_data.detach().numpy()) print("Discriminator Decision:", decision.detach().numpy())
Fake Data: [[-0.07713338] [-0.28153205] [-0.4695596 ] [-0.04739679] [ 0.0164116 ]] Discriminator Decision: [[0.44639194] [0.41693193] [0.3903405 ] [0.4507146 ] [0.46001434]]

Key Takeaways

  • GAN = Generator + Discriminator

  • Generator creates data

  • Discriminator evaluates data

  • They improve by competing

  • GANs learn data distribution, not labels