Browse Source

Added runner for SimpleGAN examples.

Kristian Schultz 4 years ago
parent
commit
65ae35cb22
2 changed files with 308 additions and 0 deletions
  1. 107 0
      library/analysis.py
  2. 201 0
      run_all_with_SimpleGAN.ipynb

+ 107 - 0
library/analysis.py

@@ -0,0 +1,107 @@
+from library.exercise import Exercise
+from library.dataset import DataSet, TrainTestData
+from library.GanExamples import StupidToyListGan
+from library.SimpleGan import SimpleGan
+from library.Repeater import Repeater
+
+import pickle
+import numpy as np
+import random
+from imblearn.datasets import fetch_datasets
+
+
+def loadDataset(datasetName):
+    pickle_in = open(f"{datasetName}.pickle", "rb")
+    pickle_dict = pickle.load(pickle_in)
+
+    myData = pickle_dict["folding"]
+    k = myData[0]
+
+    labels = np.concatenate((k[1], k[3]), axis=0).astype(float)
+    features = np.concatenate((k[0], k[2]), axis=0).astype(float)
+    label_1 = list(np.where(labels == 1)[0])
+    label_0 = list(np.where(labels == 0)[0])
+    features_1 = features[label_1]
+    features_0 = features[label_0]
+    
+    return DataSet(data0=features_0, data1=features_1)
+
+
+def getRandGen(initValue, incValue=257, multValue=101, modulus=65537):
+    value = initValue
+    while True:
+        value = ((multValue * value) + incValue) % modulus
+        yield value
+            
+def genShuffler():
+    randGen = getRandGen(2021)
+
+    def shuffler(data):
+        data = list(data)
+        size = len(data)
+        shuffled = []
+        while size > 0:
+            p = next(randGen) % size
+            size -= 1
+            shuffled.append(data[p])
+            data = data[0:p] + data[(p + 1):]
+        return np.array(shuffled)
+    return shuffler
+
+
+def runExerciseForSimpleGAN(datasetName):
+    ganName = "SimpleGAN"
+    print()
+    print()
+    print("///////////////////////////////////////////")
+    print(f"// Running {ganName} on {datasetName}")
+    print("///////////////////////////////////////////")
+    print()
+    data = loadDataset(datasetName)
+    gan = SimpleGan(numOfFeatures=data.data0.shape[1])
+    random.seed(2021)
+    shuffler = genShuffler()
+    exercise = Exercise(shuffleFunction=shuffler, numOfShuffles=5, numOfSlices=5)
+    exercise.run(gan, data)
+    exercise.saveResultsTo(f"{datasetName}-{ganName}.csv")
+    exercise.saveResultsTo(f"{ganName}-{datasetName}.csv")
+    
+    
+def runExerciseForRepeater(datasetName):
+    ganName = "Repeater"
+    print()
+    print()
+    print("///////////////////////////////////////////")
+    print(f"// Running {ganName} on {datasetName}")
+    print("///////////////////////////////////////////")
+    print()
+    data = loadDataset(datasetName)
+    gan = Repeater()
+    random.seed(2021)
+    shuffler = genShuffler()
+    exercise = Exercise(shuffleFunction=shuffler, numOfShuffles=5, numOfSlices=5)
+    exercise.run(gan, data)
+    exercise.saveResultsTo(f"{datasetName}-{ganName}.csv")
+    exercise.saveResultsTo(f"{ganName}-{datasetName}.csv")
+    
+testSets = [
+    "folding_abalone_17_vs_7_8_9_10",
+    "folding_abalone9-18",
+    "folding_car_good",
+    "folding_car-vgood",
+    "folding_flare-F",
+    "folding_hypothyroid",
+    "folding_kddcup-guess_passwd_vs_satan",
+    "folding_kr-vs-k-three_vs_eleven",
+    "folding_kr-vs-k-zero-one_vs_draw",
+    "folding_shuttle-2_vs_5",
+    "folding_winequality-red-4",
+    "folding_yeast4",
+    "folding_yeast5",
+    "folding_yeast6"
+    ]
+
+def runAllTestSets(dataSetList):
+    for dsFileName in dataSetList:
+        runExerciseForSimpleGAN(dataSetList)
+        runExerciseForRepeater(dataSetList)

+ 201 - 0
run_all_with_SimpleGAN.ipynb

@@ -0,0 +1,201 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "fiscal-madonna",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "from library.analysis import runExerciseForSimpleGAN"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "sticky-scott",
+   "metadata": {
+    "scrolled": false
+   },
+   "outputs": [],
+   "source": [
+    "runExerciseForSimpleGAN(\"folding_abalone_17_vs_7_8_9_10\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "continent-cement",
+   "metadata": {
+    "scrolled": false
+   },
+   "outputs": [],
+   "source": [
+    "runExerciseForSimpleGAN(\"folding_abalone9-18\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "acceptable-setting",
+   "metadata": {
+    "scrolled": false
+   },
+   "outputs": [],
+   "source": [
+    "runExerciseForSimpleGAN(\"folding_car_good\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "marked-programmer",
+   "metadata": {
+    "scrolled": false
+   },
+   "outputs": [],
+   "source": [
+    "runExerciseForSimpleGAN(\"folding_car-vgood\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "familiar-creator",
+   "metadata": {
+    "scrolled": false
+   },
+   "outputs": [],
+   "source": [
+    "runExerciseForSimpleGAN(\"folding_flare-F\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "affected-constitution",
+   "metadata": {
+    "scrolled": false
+   },
+   "outputs": [],
+   "source": [
+    "runExerciseForSimpleGAN(\"folding_hypothyroid\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "outdoor-asset",
+   "metadata": {
+    "scrolled": false
+   },
+   "outputs": [],
+   "source": [
+    "runExerciseForSimpleGAN(\"folding_kddcup-guess_passwd_vs_satan\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "alternate-reconstruction",
+   "metadata": {
+    "scrolled": false
+   },
+   "outputs": [],
+   "source": [
+    "runExerciseForSimpleGAN(\"folding_kr-vs-k-three_vs_eleven\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "certified-facial",
+   "metadata": {
+    "scrolled": false
+   },
+   "outputs": [],
+   "source": [
+    "runExerciseForSimpleGAN(\"folding_kr-vs-k-zero-one_vs_draw\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "personalized-portuguese",
+   "metadata": {
+    "scrolled": false
+   },
+   "outputs": [],
+   "source": [
+    "runExerciseForSimpleGAN(\"folding_shuttle-2_vs_5\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "studied-screen",
+   "metadata": {
+    "scrolled": false
+   },
+   "outputs": [],
+   "source": [
+    "runExerciseForSimpleGAN(\"folding_winequality-red-4\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "worse-seafood",
+   "metadata": {
+    "scrolled": false
+   },
+   "outputs": [],
+   "source": [
+    "runExerciseForSimpleGAN(\"folding_yeast4\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "ultimate-calgary",
+   "metadata": {
+    "scrolled": false
+   },
+   "outputs": [],
+   "source": [
+    "runExerciseForSimpleGAN(\"folding_yeast5\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "activated-wildlife",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "runExerciseForSimpleGAN(\"folding_yeast6\")"
+   ]
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3",
+   "language": "python",
+   "name": "python3"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 3
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython3",
+   "version": "3.8.5"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}