├── Archiv_Session_Spring_2017 ├── Exercises │ ├── 02_autoencoder.py │ ├── 02_autoencoder_poly.py │ ├── 02_autoencoder_with_adam.py │ ├── 02_swap_general_case.py │ ├── 02_swap_test.py │ ├── 03_Cheap_solution.ipynb │ ├── 03_PlayingAgent.ipynb │ ├── 03_pacman_agent.py │ ├── 04_BabyAgent.py │ ├── 04_Intelligent_Toy_Example.py │ ├── 04_Quantum_solution_Grover.py │ ├── 04_another_agent.py │ ├── 04_invasion_with_neural_network.py │ ├── 04_rl_toy_example.py │ ├── 04_rl_toy_example_q_learn.py │ ├── 04_tictactoe.py │ ├── 05_APS Captcha.ipynb │ ├── 05_APS_Cracker.ipynb │ ├── 05_Nielsen_adaption_buggy.py │ ├── 05_aps_capcha.ipynb │ ├── 05_wheres_wally.py │ ├── 06_ClassifyingAlgorithms.ipynb │ ├── 06_SVM.py │ ├── 06_aps_with_classifiers.ipynb │ ├── 06_wheres_wally_traditional.py │ ├── 07_Flux diagram Q-SVM.pdf │ ├── 07_Initialize_Q_state.py │ ├── 07_image_loader_and_encoder.py │ ├── 07_image_reduce_and_classical_svm.py │ ├── 07_qutipHHL.ipynb │ ├── 08_PredictingStocks.ipynb │ ├── 10_CIFAR with sklearn.ipynb │ ├── 10_Visualization.ipynb │ ├── 10_cifar_classifier.py │ ├── 11 Markov random field.ipynb │ ├── 11_Markov_random_field.ipynb │ ├── README.md │ ├── aps_captcha_images.zip │ ├── brain.h5 │ └── tools.py ├── README.md └── Tutorials │ ├── Advanced_Data_Science.ipynb │ ├── Git.md │ ├── Kaggle.md │ ├── Python_Introduction.ipynb │ ├── Python_for_Science.ipynb │ ├── Qml_approaches.png │ └── summary_on_meeting_6.tex ├── Archiv_Session_Spring_2018 ├── .DS_Store ├── Coding_Exercises │ ├── Week_01.py │ ├── autoencoders.ipynb │ ├── colorizer.ipynb │ ├── week1_confusion.py │ └── week1_confusion_CNN.py ├── README.md └── topic proposals.md ├── LICENSE ├── README.md └── topic proposals.md /Archiv_Session_Spring_2017/Exercises/02_autoencoder.py: -------------------------------------------------------------------------------- 1 | # QML-RG@ICFO Homework 1: Autoencoder 2 | # Alejandro Pozas-Kerstjens 3 | 4 | import tensorflow as tf 5 | import matplotlib.pyplot as plt 6 | import numpy as np 7 | 8 | n_input = 10 # Number of inputs 9 | n_coded = 6 # Number of nodes in encoder 10 | 11 | # Adam parameters 12 | learning_rate = 0.001 13 | beta1 = 0.9 14 | beta2 = 0.999 15 | epsilon = 1e-8 16 | 17 | ins = tf.placeholder("float", shape=(n_input,None)) # Initialize inputs as variables. None denotes arbitrary value 18 | 19 | encode = tf.Variable(tf.zeros([n_coded,n_input])) # Initialize encoder and decoder matrices 20 | decode = tf.Variable(tf.zeros([n_input,n_coded])) 21 | 22 | outs_true = ins 23 | encoded = tf.nn.sigmoid(tf.matmul(encode,ins)) 24 | outs_pred = tf.nn.sigmoid(tf.matmul(decode,encoded)) 25 | 26 | # Define cost function (mean square error) and optimization procedure 27 | cost = tf.reduce_mean(tf.pow(outs_true - outs_pred, 2)) 28 | optimizer = tf.train.AdamOptimizer(learning_rate,beta1,beta2,epsilon).minimize(cost) 29 | 30 | # Tools to evaluate the model 31 | correct_pred = tf.equal(tf.argmax(outs_true, 1),tf.argmax(outs_pred, 1)) 32 | accuracy = tf.reduce_mean(tf.cast(correct_pred,tf.float32)) 33 | 34 | #Initialization of training procedure 35 | init = tf.global_variables_initializer() 36 | 37 | nsteps=100000 38 | sess = tf.Session() # Define session 39 | sess.run(init) # Initialize session (begin computing stuff) 40 | 41 | feedins = np.random.choice([0,1],size=(n_input,100)) 42 | for i in range(nsteps): 43 | sess.run(optimizer, feed_dict = {ins: feedins}) # Train 44 | print(sess.run(accuracy, feed_dict = {ins: feedins})) 45 | 46 | # One-instance test 47 | test = np.random.choice([0,1],size=(n_input,1)) 48 | encode_decode_test = sess.run(outs_pred, feed_dict={ins: test}) 49 | plt.plot(range(n_input),test,'ro',range(n_input),encode_decode_test,'b^') 50 | plt.show() -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/02_autoencoder_poly.py: -------------------------------------------------------------------------------- 1 | # QML-RG@ICFO Homework 1: variant of autoencoder to recreate low order polynomials 2 | 3 | import tensorflow as tf 4 | import matplotlib.pyplot as plt 5 | import numpy as np 6 | 7 | n_input = 40 #number of input data points 8 | n_coded = 4 #number of coded data points 9 | 10 | #adam parameters 11 | learning_rate = 0.01 12 | beta1 = 0.9 13 | beta2 = 0.999 14 | epsilon = 1e-8 15 | 16 | #variables to learn 17 | encode = tf.Variable(tf.random_normal([n_coded,n_input])) 18 | decode = tf.Variable(tf.random_normal([n_input,n_coded])) 19 | 20 | #placeholder input to learn from that could be vector or matrix 21 | vec_in = tf.placeholder("float32", shape=(n_input,None)) 22 | 23 | #coding and decoding transformations, no need for activation functions with intended data set 24 | vec_encoded = tf.matmul(encode,vec_in) 25 | vec_out = tf.matmul(decode,vec_encoded) 26 | 27 | #optimization choice 28 | cost = tf.reduce_mean(tf.square(vec_out - vec_in)) #least squared error 29 | optimizer = tf.train.AdamOptimizer(learning_rate,beta1,beta2,epsilon) #choosing optimizer method 30 | train = optimizer.minimize(cost) #training by minimizing cost function 31 | 32 | #initilizing for training 33 | nsteps = 10000 34 | init = tf.global_variables_initializer() 35 | sess = tf.Session() 36 | sess.run(init) 37 | 38 | #training data set of random third order polynomials 39 | x = np.linspace(-1,1,n_input).reshape([n_input,1]) 40 | coeffs = np.random.uniform(size = [4, 10000], low = -1.0) 41 | training_data = coeffs[0,:] + coeffs[1,:]*x + coeffs[2,:]*x**2 + coeffs[3,:]*x**3 42 | 43 | #training 44 | for i in range(nsteps): 45 | sess.run(train, {vec_in: training_data}) 46 | print(sess.run(cost,{vec_in: training_data})) 47 | 48 | #plot result for random test data 49 | tcoeff = np.random.uniform(size = [4], low = -1.0) 50 | test_input = tcoeff[0] + tcoeff[1]*x + tcoeff[2]*x**2 + tcoeff[3]*x**3 51 | test_output = sess.run(vec_out,{vec_in: test_input}) 52 | plt.plot(range(n_input),test_input,'ro',range(n_input),test_output,'b^') 53 | plt.show() 54 | 55 | #close session 56 | sess.close() -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/02_autoencoder_with_adam.py: -------------------------------------------------------------------------------- 1 | from keras.models import Sequential 2 | from keras.layers import Dense 3 | import numpy 4 | 5 | #fix random seed for reproducibility 6 | seed = 7 7 | numpy.random.seed(seed) 8 | 9 | N=10 #Size of dataset N-dim list of 10-dim lists 10 | 11 | 12 | 13 | X = numpy.random.choice([0, 1], size=(N,10)) #generate matrix of size (N,10) with random entries of the list [0,1] 14 | 15 | #CREATE MODEL 16 | #------------------------------------------------------------------------------- 17 | model = Sequential() 18 | model.add(Dense(10, input_dim=10, init='uniform', activation='sigmoid')) 19 | model.add(Dense(3, init='uniform', activation='sigmoid')) 20 | model.add(Dense(10, init='uniform', activation='sigmoid')) 21 | 22 | """ 23 | -Sequential() is for a model where Layer comes after layer 24 | Sequential() opens a empty scratch, where we can add layers 25 | -Dense() gives a fully connected layer 26 | First entry (here 10) is the output dimension of the layer. aka we have 12 neurons in the first layer 27 | -So the first layer has 10 neurons and expects 10 input variables 28 | -init='uniform' initializes weights from uniform distribution (default value between 0 and 0.05) 29 | 'normal' would be from Gaussian distribution 30 | -activation defines the function that defines the output of the neuron 31 | 'relu' is a rectifier fct f(x) = max(0,x) 32 | 'sigmoid' sigmoid fct for binary outputs 33 | """ 34 | #------------------------------------------------------------------------------- 35 | #COMPILE MODEL 36 | model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) 37 | 38 | """ 39 | Define loss and cost function: 40 | 'binary_crossentropy' suggested for binary values 41 | 'adam' see adam paper 42 | 43 | metrics: 44 | Not entirely clear why this. 45 | Classification problems (spam / not spam, ill / not ill, 0 / 1) are supposed to use this 46 | """ 47 | #-------------------------------------------------------------------------------- 48 | #FIT MODEL 49 | model.fit(X, X, nb_epoch=2000, batch_size=10) 50 | 51 | #------------------------------------------------------------------------------- 52 | #EVALUATE MODEL 53 | scores = model.evaluate(X, X) 54 | print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100)) 55 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/02_swap_general_case.py: -------------------------------------------------------------------------------- 1 | from qutip import * 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # First state 6 | alpha1=np.asscalar(np.random.rand(1,1)) 7 | beta1=np.sqrt(1-alpha1**2) 8 | ket1 = alpha1*basis(2, 0) + beta1*basis(2, 1) 9 | 10 | 11 | # Second state 12 | alpha2=np.asscalar(np.random.rand(1,1)) 13 | beta2=np.sqrt(1-alpha2**2) 14 | ket2 = alpha2*basis(2, 0) + beta2*basis(2, 1) 15 | 16 | 17 | # Ancilla 18 | anc=(basis(2, 0) + basis(2, 1)).unit() 19 | 20 | # Input 21 | psi_in=tensor(anc,ket1,ket2) 22 | 23 | # Swap gate = fredkin+hadamar 24 | output=-hadamard_transform(N=3)*fredkin()*tensor(anc,ket1,ket2) # the minus is needed for hadarmad_transform(N=3) to work as 25 | # hadamard gate 26 | 27 | # Measurement 28 | down=basis(2,0) 29 | up=basis(2,1) 30 | 31 | result_down=tensor(down,ket1,ket2).dag()*output 32 | result_up=tensor(up,ket1,ket2).dag()*output 33 | 34 | # Checking results 35 | # With some easy algebra we can calculate the theoretical result for both measurements. 36 | # In this case, I have calculated the expected value for the state up=|1> 37 | 38 | expected_up=((beta1*alpha2)**2+(beta2*alpha1)**2-2*alpha1*beta1*alpha2*beta2)*(1/2) 39 | 40 | print(result_up) 41 | print(expected_up) 42 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/02_swap_test.py: -------------------------------------------------------------------------------- 1 | import qutip as qt 2 | import numpy as np 3 | 4 | """ 5 | fredkin hadamard 6 | ancilla---------O---------H------------Measure 7 | f1---------X---------------------- 8 | f2---------X---------------------- 9 | """ 10 | 11 | up = qt.qstate('u') #qstate down is (1,0), qstate up is (0,1) 12 | down = qt.qstate('d') 13 | ancilla = 1/np.sqrt(2)*(up + down) 14 | hadamard = qt.tensor(qt.snot(),qt.qeye(2),qt.qeye(2)) 15 | fredkin = qt.fredkin() #fredkin swapes f1 and f2 if ancilla = 1 16 | 17 | #------------------------------------------------------------------------ 18 | #Define Input 19 | f1 = 1/np.sqrt(2)*(up + down) 20 | f2 = up 21 | #------------------------------------------------------------------------ 22 | 23 | psi0 = qt.tensor(ancilla, f1, f2) 24 | 25 | output = hadamard*fredkin*psi0 26 | 27 | 28 | measure = qt.tensor(down,f1,f2).dag()*output #if f1 = f2 the output is (down,f1,f2) 29 | #therefore measure is 1 if f1 = f2 30 | #measure is 0.5 if f1, f2 are orthogonal 31 | 32 | print(measure) 33 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/03_Cheap_solution.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This is a few-liner implementation using [this fork](https://github.com/claymcleod/dqn) of the DQN reinforcement learning algorithm. It relies on Keras as a back-end for deep learning." 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": { 14 | "ExecuteTime": { 15 | "start_time": "2017-03-09T08:24:29.613Z" 16 | }, 17 | "collapsed": false 18 | }, 19 | "outputs": [ 20 | { 21 | "name": "stderr", 22 | "output_type": "stream", 23 | "text": [ 24 | "[2017-03-09 09:24:29,637] Making new env: MsPacman-v0\n" 25 | ] 26 | }, 27 | { 28 | "name": "stdout", 29 | "output_type": "stream", 30 | "text": [ 31 | "Training a new model\n" 32 | ] 33 | } 34 | ], 35 | "source": [ 36 | "import gym\n", 37 | "from dqn import Agent\n", 38 | "num_episodes = 20\n", 39 | "env = gym.make(\"MsPacman-v0\")\n", 40 | "agent = Agent(state_size=env.observation_space.shape,\n", 41 | " number_of_actions=env.action_space.n,\n", 42 | " save_name=\"MsPacman-v0\")\n", 43 | "for e in range(num_episodes):\n", 44 | " observation = env.reset()\n", 45 | " done = False\n", 46 | " agent.new_episode()\n", 47 | " total_cost = 0.0\n", 48 | " total_reward = 0.0\n", 49 | " frame = 0\n", 50 | " while not done:\n", 51 | " frame += 1\n", 52 | " #env.render()\n", 53 | " action, values = agent.act(observation)\n", 54 | " #action = env.action_space.sample()\n", 55 | " observation, reward, done, info = env.step(action)\n", 56 | " total_cost += agent.observe(reward)\n", 57 | " total_reward += reward\n", 58 | " print(\"total reward\", total_reward)\n", 59 | "print(\"mean cost\", total_cost/frame)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": { 66 | "collapsed": true 67 | }, 68 | "outputs": [], 69 | "source": [] 70 | } 71 | ], 72 | "metadata": { 73 | "kernelspec": { 74 | "display_name": "Python 3", 75 | "language": "python", 76 | "name": "python3" 77 | }, 78 | "language_info": { 79 | "codemirror_mode": { 80 | "name": "ipython", 81 | "version": 3 82 | }, 83 | "file_extension": ".py", 84 | "mimetype": "text/x-python", 85 | "name": "python", 86 | "nbconvert_exporter": "python", 87 | "pygments_lexer": "ipython3", 88 | "version": "3.6.0" 89 | } 90 | }, 91 | "nbformat": 4, 92 | "nbformat_minor": 2 93 | } 94 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/03_PlayingAgent.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "deletable": true, 7 | "editable": true 8 | }, 9 | "source": [ 10 | "# QML-RG@ICFO Homework 2: Playing Agent\n", 11 | "\n", 12 | "Alejandro Pozas-Kerstjens, Bogna Bylicka, Eric Brown, Osvaldo Jimenez, Raul Blazquez" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "metadata": {}, 18 | "source": [ 19 | "The OpenAI Gym (https://gym.openai.com) provides us with a lot of different examples and games in which to train a learning agent. The task is to develop one of such agents. We will create a neural network that, given the state of the game (actually, two consecutive states), it outputs a family of quality values (Q-values) for each next possible move. The move with higher Q-value is chosen and performed in the game. This theoretical formalism was taken from https://www.nervanasys.com/demystifying-deep-reinforcement-learning/" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": null, 25 | "metadata": { 26 | "collapsed": false, 27 | "deletable": true, 28 | "editable": true 29 | }, 30 | "outputs": [], 31 | "source": [ 32 | "# INITIALIZATION: libraries, parameters, network...\n", 33 | "\n", 34 | "from keras.models import Sequential # One layer after the other\n", 35 | "from keras.layers import Dense, Flatten # Dense layers are fully connected layers, Flatten layers flatten out multidimensional inputs\n", 36 | "from collections import deque # For storing moves \n", 37 | "\n", 38 | "import numpy as np\n", 39 | "import gym # To train our network\n", 40 | "env = gym.make('MountainCar-v0') # Choose game (any in the gym should work)\n", 41 | "\n", 42 | "import random # For sampling batches from the observations\n", 43 | "\n", 44 | "\n", 45 | "# Create network. Input is two consecutive game states, output is Q-values of the possible moves.\n", 46 | "model = Sequential()\n", 47 | "model.add(Dense(20, input_shape=(2,) + env.observation_space.shape, init='uniform', activation='relu'))\n", 48 | "model.add(Flatten()) # Flatten input so as to have no problems with processing\n", 49 | "model.add(Dense(18, init='uniform', activation='relu'))\n", 50 | "model.add(Dense(10, init='uniform', activation='relu'))\n", 51 | "model.add(Dense(env.action_space.n, init='uniform', activation='linear')) # Same number of outputs as possible actions\n", 52 | "\n", 53 | "model.compile(loss='mse', optimizer='adam', metrics=['accuracy'])\n", 54 | "\n", 55 | "# Parameters\n", 56 | "D = deque() # Register where the actions will be stored\n", 57 | "\n", 58 | "observetime = 500 # Number of timesteps we will be acting on the game and observing results\n", 59 | "epsilon = 0.7 # Probability of doing a random move\n", 60 | "gamma = 0.9 # Discounted future reward. How much we care about steps further in time\n", 61 | "mb_size = 50 # Learning minibatch size" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": null, 67 | "metadata": { 68 | "collapsed": false 69 | }, 70 | "outputs": [], 71 | "source": [ 72 | "# FIRST STEP: Knowing what each action does (Observing)\n", 73 | "\n", 74 | "observation = env.reset() # Game begins\n", 75 | "obs = np.expand_dims(observation, axis=0) # (Formatting issues) Making the observation the first element of a batch of inputs \n", 76 | "state = np.stack((obs, obs), axis=1)\n", 77 | "done = False\n", 78 | "for t in range(observetime):\n", 79 | " if np.random.rand() <= epsilon:\n", 80 | " action = np.random.randint(0, env.action_space.n, size=1)[0]\n", 81 | " else:\n", 82 | " Q = model.predict(state) # Q-values predictions\n", 83 | " action = np.argmax(Q) # Move with highest Q-value is the chosen one\n", 84 | " observation_new, reward, done, info = env.step(action) # See state of the game, reward... after performing the action\n", 85 | " obs_new = np.expand_dims(observation_new, axis=0) # (Formatting issues)\n", 86 | " state_new = np.append(np.expand_dims(obs_new, axis=0), state[:, :1, :], axis=1) # Update the input with the new state of the game\n", 87 | " D.append((state, action, reward, state_new, done)) # 'Remember' action and consequence\n", 88 | " state = state_new # Update state\n", 89 | " if done:\n", 90 | " env.reset() # Restart game if it's finished\n", 91 | " obs = np.expand_dims(observation, axis=0) # (Formatting issues) Making the observation the first element of a batch of inputs \n", 92 | " state = np.stack((obs, obs), axis=1)\n", 93 | "print('Observing Finished')" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": { 100 | "collapsed": false, 101 | "deletable": true, 102 | "editable": true 103 | }, 104 | "outputs": [], 105 | "source": [ 106 | "# SECOND STEP: Learning from the observations (Experience replay)\n", 107 | "\n", 108 | "minibatch = random.sample(D, mb_size) # Sample some moves\n", 109 | "\n", 110 | "inputs_shape = (mb_size,) + state.shape[1:]\n", 111 | "inputs = np.zeros(inputs_shape)\n", 112 | "targets = np.zeros((mb_size, env.action_space.n))\n", 113 | "\n", 114 | "for i in range(0, mb_size):\n", 115 | " state = minibatch[i][0]\n", 116 | " action = minibatch[i][1]\n", 117 | " reward = minibatch[i][2]\n", 118 | " state_new = minibatch[i][3]\n", 119 | " done = minibatch[i][4]\n", 120 | " \n", 121 | "# Build Bellman equation for the Q function\n", 122 | " inputs[i:i+1] = np.expand_dims(state, axis=0)\n", 123 | " targets[i] = model.predict(state)\n", 124 | " Q_sa = model.predict(state_new)\n", 125 | " \n", 126 | " if done:\n", 127 | " targets[i, action] = reward\n", 128 | " else:\n", 129 | " targets[i, action] = reward + gamma * np.max(Q_sa)\n", 130 | "\n", 131 | "# Train network to output the Q function\n", 132 | " model.train_on_batch(inputs, targets)\n", 133 | "print('Learning Finished')" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": null, 139 | "metadata": { 140 | "collapsed": false, 141 | "deletable": true, 142 | "editable": true 143 | }, 144 | "outputs": [], 145 | "source": [ 146 | "# THIRD STEP: Play!\n", 147 | "\n", 148 | "observation = env.reset()\n", 149 | "obs = np.expand_dims(observation, axis=0)\n", 150 | "state = np.stack((obs, obs), axis=1)\n", 151 | "done = False\n", 152 | "tot_reward = 0.0\n", 153 | "while not done:\n", 154 | " env.render() # Uncomment to see game running\n", 155 | " Q = model.predict(state) \n", 156 | " action = np.argmax(Q) \n", 157 | " observation, reward, done, info = env.step(action)\n", 158 | " obs = np.expand_dims(observation, axis=0)\n", 159 | " state = np.append(np.expand_dims(obs, axis=0), state[:, :1, :], axis=1) \n", 160 | " tot_reward += reward\n", 161 | "print('Game ended! Total reward: {}'.format(reward))" 162 | ] 163 | } 164 | ], 165 | "metadata": { 166 | "kernelspec": { 167 | "display_name": "Python 3", 168 | "language": "python", 169 | "name": "python3" 170 | }, 171 | "language_info": { 172 | "codemirror_mode": { 173 | "name": "ipython", 174 | "version": 3 175 | }, 176 | "file_extension": ".py", 177 | "mimetype": "text/x-python", 178 | "name": "python", 179 | "nbconvert_exporter": "python", 180 | "pygments_lexer": "ipython3", 181 | "version": "3.5.2" 182 | } 183 | }, 184 | "nbformat": 4, 185 | "nbformat_minor": 2 186 | } 187 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/03_pacman_agent.py: -------------------------------------------------------------------------------- 1 | # Coding group: Alexandre,... 2 | import gym 3 | import numpy as np 4 | from skimage.transform import resize 5 | from skimage.color import rgb2gray 6 | from collections import deque 7 | from matplotlib import pyplot as plt 8 | import tensorflow 9 | from keras.layers import Convolution2D, Dense, Flatten, Input, merge 10 | from keras.models import Sequential 11 | 12 | 13 | #https://elitedatascience.com/keras-tutorial-deep-learning-in-python 14 | 15 | # ============================= 16 | # ATARI Environment Wrapper 17 | # I took the class from this link 18 | # https://github.com/tflearn/tflearn/blob/master/examples/reinforcement_learning/atari_1step_qlearning.py 19 | # ============================= 20 | class AtariEnvironment(object): 21 | """ 22 | Small wrapper for gym atari environments. 23 | Responsible for preprocessing screens and holding on to a screen buffer 24 | of size action_repeat from which environment state is constructed. 25 | """ 26 | def __init__(self, gym_env, action_repeat): 27 | self.env = gym_env 28 | self.action_repeat = action_repeat 29 | 30 | # Agent available actions, such as LEFT, RIGHT, NOOP, etc... 31 | #To find the meaning of the actions, you should type env.get_action_meanings() 32 | self.gym_actions = range(gym_env.action_space.n) 33 | # Screen buffer of size action_repeat to be able to build 34 | # state arrays of size [1, action_repeat, 84, 84] 35 | self.state_buffer = deque() 36 | 37 | def get_initial_state(self): 38 | """ 39 | Resets the atari game, clears the state buffer. 40 | """ 41 | # Clear the state buffer 42 | self.state_buffer = deque() 43 | 44 | x_t = self.env.reset() 45 | x_t = self.get_preprocessed_frame(x_t) 46 | s_t = np.stack([x_t for i in range(self.action_repeat)], axis=0) 47 | 48 | for i in range(self.action_repeat-1): 49 | self.state_buffer.append(x_t) 50 | return s_t 51 | 52 | def get_preprocessed_frame(self, observation): 53 | """ 54 | 0) Atari frames: 210 x 160 55 | 1) Get image grayscale 56 | 2) Rescale image 110 x 84 57 | 3) Crop center 84 x 84 (you can crop top/bottom according to the game) 58 | """ 59 | return resize(rgb2gray(observation), (110, 84))[13:110 - 13, :] 60 | 61 | def step(self, action_index): 62 | """ 63 | Excecutes an action in the gym environment. 64 | Builds current state (concatenation of action_repeat-1 previous 65 | frames and current one). Pops oldest frame, adds current frame to 66 | the state buffer. Returns current state. 67 | """ 68 | #x_t1 is the screen 69 | #r_t is the reward 70 | #terminal is boolean indicating if the game is finished or not 71 | #info 72 | x_t1, r_t, terminal, info = self.env.step(self.gym_actions[action_index]) 73 | x_t1 = self.get_preprocessed_frame(x_t1) 74 | 75 | previous_frames = np.array(self.state_buffer) 76 | s_t1 = np.empty((self.action_repeat, 84, 84)) 77 | s_t1[:self.action_repeat-1, :] = previous_frames 78 | s_t1[self.action_repeat-1] = x_t1 79 | 80 | # Pop the oldest frame, add the current frame to the queue 81 | self.state_buffer.popleft() 82 | self.state_buffer.append(x_t1) 83 | 84 | return s_t1, r_t, terminal, info 85 | 86 | 87 | env = gym.make('MsPacman-v0') 88 | env.reset() 89 | c=AtariEnvironment(env,2) 90 | s_t=c.get_initial_state() 91 | #for _ in range(1000): 92 | # s_t1, r_t, terminal, info=c.step(env.action_space.sample()) 93 | #print(r_t) 94 | # env.render() 95 | # #env.step(env.action_space.sample()) 96 | # env.step(0) 97 | s_t1, r_t, terminal, info=c.step(env.action_space.sample()) 98 | nactions=env.action_space.n 99 | #model = Sequential() 100 | #h = Convolution2D(16, 8, 8, subsample=(4, 4),border_mode='same', activation='relu')(Input((1,84,84))) 101 | #h = Convolution2D(32, 4, 4, subsample=(2, 2),border_mode='same', activation='relu')(h) 102 | #h = Flatten()(h) 103 | #h = Dense(256, activation='relu')(h) 104 | #V = Dense(nactions)(h) 105 | model = Sequential() 106 | model.add(Convolution2D(16, 8, 8, subsample=(4, 4),border_mode='same', activation='relu',input_shape=(1,84,84))) 107 | model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy']) 108 | 109 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/04_BabyAgent.py: -------------------------------------------------------------------------------- 1 | +import random 2 | +import numpy as np 3 | + 4 | + 5 | +class Game(object): 6 | + 7 | + def __init__(self): 8 | + self.state = [0, 0, 0] 9 | + self.agent_position = 1 10 | + self.state[random.randint(0, 1)*2] = 1 11 | + self.status = "running" 12 | + 13 | + def moveto(self, position): 14 | + self.agent_position += position 15 | + if self.agent_position < 0: 16 | + self.agent_position = 0 17 | + elif self.agent_position > 2: 18 | + self.agent_position = 2 19 | + if self.status == "running" and self.state[self.agent_position] == 1: 20 | + self.status = "gameover" 21 | + return 1 22 | + else: 23 | + return 0 24 | + 25 | + 26 | +class BabyAgent(object): 27 | + 28 | + def __init__(self, game): 29 | + self.game = game 30 | + self.number_of_steps = 0 31 | + self.weights = np.array([1, 1]) 32 | + self.step = 0 33 | + 34 | + def next_move(self): 35 | + self.number_of_steps += 1 36 | + if game.agent_position != 1: 37 | + self.weights[(self.decision + 1) % 2] += 1 38 | + self.decision = np.random.choice(2, 1, 39 | + p=self.weights/sum(self.weights)) 40 | + if self.decision == 0: 41 | + self.step = -1 42 | + else: 43 | + self.step = 1 44 | + return self.step 45 | + 46 | +game = Game() 47 | +print(game.state) 48 | +agent = BabyAgent(game) 49 | +while game.status == "running": 50 | + agent_move = agent.next_move() 51 | + game.moveto(agent_move) 52 | + print(agent.weights) 53 | + print(agent.decision) 54 | + 55 | +print(agent.number_of_steps) 56 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/04_Intelligent_Toy_Example.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import random 3 | 4 | 5 | Q = np.zeros(shape = (2,3,2)) #shape for 2 reward states, 3 agent states, 2 actions 6 | Agent_state = [[1,0,0],[0,1,0],[0,0,1]] #possible agent positions 7 | Reward = [[1,0,0],[0,0,1]] #possible reward positions 8 | iterations = 100 #how many times do we play the game 9 | 10 | 11 | 12 | 13 | def moveto(position,agent_position, status): 14 | agent_position += position # x += y adds y to the variable x 15 | if agent_position < 0: #since we always add 1 or -1 randomly to the position it can happen, that we get negative indices 16 | agent_position = 0 #position -1 therefore is always set to 0 17 | #agent_position = self.agent_position if we set this it will be found in max 2 steps (CHEAT) 18 | elif agent_position > 2: #same for to big values. indices bigger than 2 are not in the state vector anymore 19 | agent_position = 2 20 | if status == "running" and state[agent_position] == 1: #if the agent is positioned at the same index as we initially set the reward 21 | status = "win" 22 | return 1, status, agent_position 23 | else: 24 | return 0, status, agent_position 25 | 26 | def next_move(): 27 | return random.randint(0, 1)*2-1 #gives randomly either 1 or -1 28 | 29 | def next_move_intelligent(gamma): 30 | R = Reward.index(state) 31 | A = agent_position 32 | action = [-1,1] 33 | ind = [0,1] 34 | 35 | 36 | test = [[Q[R][A][i] + gamma*Q[R][A+action[i]][j] for j in ind] for i in ind] 37 | 38 | a = test.index(max(test)) #find maximum value in test and check which index it is. This will then be the next action 39 | 40 | return action[a] 41 | 42 | #---------------------------------------------------------------------------------------------------------- 43 | #learning phase 44 | #---------------------------------------------------------------------------------------------------------- 45 | 46 | for i in range(0,iterations): 47 | i += 1 48 | move_memory = [] #initialize a memory for the moves for every new game 49 | state = [0,0,0] #empty the state for every new game 50 | state[random.randint(0, 1)*2] = 1 #places reward randomly in state 51 | agent_position = 1 #initializes agent in the middle 52 | status = "running" 53 | move_memory.append([Reward.index(state), agent_position, 'initialize',0]) 54 | 55 | while status == "running": 56 | agent_move = next_move() 57 | 58 | if agent_move < 0: 59 | action = "left" 60 | else: 61 | action = "right" 62 | 63 | reward, status, agent_position=moveto(agent_move, agent_position, status) #the argument of moveto is either 1 or -1 and is the value added to the position 64 | 65 | index_reward = Reward.index(state) #which vector in Reward is actually the reward position 66 | index_Agent = agent_position 67 | 68 | 69 | move_memory.append([index_reward,index_Agent,action, reward]) 70 | #print(move_memory) 71 | 72 | 73 | length = len(move_memory) 74 | 75 | for i in range(1,length): #CONSTRUCT Q MATRIX 76 | k = move_memory[i-1][0] #reward index 77 | l = move_memory[i-1][1] #agent index 78 | print(l) 79 | if action == "left": #change left / right to index 80 | m= 0 81 | else: 82 | m= 1 83 | 84 | Q[k][l][m]=move_memory[i][3] 85 | 86 | #------------------------------------------------------------------------------------- 87 | #Let Agent play now 88 | 89 | state = [0,0,0] #empty the state for every new game 90 | state[random.randint(0, 1)*2] = 1 #places reward randomly in state 91 | agent_position = 1 #initializes agent in the middle 92 | status = "running" 93 | moves = 0 94 | move_memory = [] 95 | 96 | 97 | while status == "running": 98 | moves += 1 99 | agent_move = next_move_intelligent(0.9) 100 | 101 | if agent_move < 0: 102 | action = "left" 103 | else: 104 | action = "right" 105 | 106 | reward, status, agent_position=moveto(agent_move, agent_position, status) #the argument of moveto is either 1 or -1 and is the value added to the position 107 | 108 | index_reward = Reward.index(state) #which vector in Reward is actually the reward position 109 | index_Agent = agent_position 110 | 111 | 112 | move_memory.append([index_reward,index_Agent,action, reward]) 113 | print(move_memory) 114 | 115 | print(moves) 116 | 117 | 118 | 119 | 120 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/04_Quantum_solution_Grover.py: -------------------------------------------------------------------------------- 1 | '''This is a toy reinforcement learning problem. The state space has three 2 | locations on a line, the agent starts in the middle one. Either on the left 3 | or the right, there is a reward. Reaching this, the game terminates. The agent 4 | has complete access to the state space of the game. The action space of the 5 | agent is left or right moves. It can choose left infinite many times if the 6 | reward is on the right: in this case, it will stay in the left-most cell 7 | forever. 8 | ''' 9 | 10 | import random 11 | import numpy as np 12 | import qutip as qt 13 | 14 | 15 | """ 16 | ----------.... ----- 17 | | 1 | 2 | .... | n | states 18 | ----------.... ----- 19 | """ 20 | 21 | class Gamer(): 22 | 23 | def __init__(self, n): #n will be the number of sites 24 | self.states = [qt.basis(n,i) for i in range(0,n)] #generate list with "Fock" states from 1 to n 25 | self.state = self.states[random.randint(0,n-1)] #initial state of invador is set randomly 26 | 27 | #Implement Grover 28 | self.s = 1/np.sqrt(n)*np.sum(self.states) 29 | self.Iwo = qt.qeye(n) - 2*self.state*self.state.dag() 30 | self.Is = qt.qeye(n) - 2*self.s*self.s.dag() 31 | self.grover = -self.Is*self.Iwo 32 | 33 | 34 | 35 | dim = 3 36 | 37 | game = Gamer(dim) 38 | 39 | 40 | for i in range(1,100): #number of iterations 41 | a = game.grover**i*game.s 42 | print(a.overlap(game.state)) #calc overlapp between iterated state a and target state game.state 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/04_another_agent.py: -------------------------------------------------------------------------------- 1 | 2 | ''' This agent is based on the Bellman equation, used to create a matrix which will help the agent to find the 3 | suitable move, given a certain position. The learning function constructs the matrix from random games. This matrix has 4 | an element for each reward position, agent position and move. There is an option at the end of the program to let the initial 5 | position of the agent not be only the center, to make things trickier! ''' 6 | 7 | import numpy as np 8 | import random 9 | 10 | 11 | Q = np.zeros(shape = (2,3,2)) # We shape the Q matrix from Bellman equation for 2 reward states, 3 agent states, 2 actions 12 | Agent_state = [[1,0,0],[0,1,0],[0,0,1]] #possible agent positions 13 | 14 | # An easy way to save the different states of the game is as follows: the reward can only have two 15 | # positions, so we assign 0 for left and 1 for right. For the agent, we can have three different 16 | # positions so 0 = left, 1 = center, 2 = right. Putting the reward + agent together, we can have 6 17 | # possible states, that we can number from 0 to 5 using S=position_agent+3*position_reward. Then 18 | # we can build a reward matrix, where for the states where there is a coincidence (S=0+0=0 and 19 | # S=2+3*1=5) we assing value one, and for the non coinciding states we assing 0: 20 | 21 | Reward=[1, 0, 0, 0, 0, 1] # Reward matrix from Bellman equation. 22 | 23 | state = [0,0,0] # empty state 24 | 25 | 26 | 27 | # Copy paste from rl_toy_example.py + comments from Patrick-------------------- 28 | 29 | def moveto(position,agent_position, status): 30 | agent_position += position # x += y adds y to the variable x 31 | if agent_position < 0: #since we always add 1 or -1 randomly to the position it can happen, that we get negative indices 32 | agent_position = 0 #position -1 therefore is always set to 0 33 | #agent_position = self.agent_position if we set this it will be found in max 2 steps (CHEAT) 34 | elif agent_position > 2: #same for to big values. indices bigger than 2 are not in the state vector anymore 35 | agent_position = 2 36 | if status == "running" and state[agent_position] == 1: #if the agent is positioned at the same index as we initially set the reward 37 | status = "win" 38 | return 1, status, agent_position 39 | else: 40 | return 0, status, agent_position 41 | 42 | def next_move(): 43 | return random.randint(0, 1)*2-1 #gives randomly either 1 or -1 44 | 45 | # ---------------------------------------------------------------------------- 46 | 47 | 48 | 49 | # Learning function ---------------------------------------------------------- 50 | 51 | def learning(idx_reward,agent_position,agent_move,gamma): # This function construct the matrix Q by means of the Bellman equation 52 | position_agent_0 = agent_position # Position of the Agent at time t 53 | position_agent_1 = moveto(agent_move, agent_position, status)[2] # Position of the Agent at time t+1 54 | 55 | if agent_move == -1: # we assing the value 0 for a left move (agent_move=-1) and 1 for a right move 56 | a_move = 0 57 | else: 58 | a_move = 1 59 | 60 | if idx_reward == 2: # we assign 1 to the right position for the reward (idx_reward=2) and 0 for left position 61 | n_idx_reward = 1 62 | else: 63 | n_idx_reward = 0 64 | 65 | # Bellman equation: 66 | Q[n_idx_reward, position_agent_0, a_move] = Reward[position_agent_1 + 3*n_idx_reward] + gamma*np.max([Q[n_idx_reward, position_agent_1, i] for i in [0,1]]) 67 | 68 | # ----------------------------------------------------------------------------- 69 | 70 | 71 | 72 | # Teached agent --------------------------------------------------------------- 73 | 74 | def cleverplayer(idx_reward,agent_position): 75 | 76 | if idx_reward == 2: # we assign 1 to the right position for the reward (idx_reward=2) and 0 for left position 77 | n_idx_reward = 1 78 | else: 79 | n_idx_reward = 0 80 | 81 | # We compare the value of Q for the given state and the two possible movements. The highest value 82 | # gives us the move to do! In the (rare) case the values are equal, we choose randomly 83 | if Q[n_idx_reward, agent_position, 0] > Q[n_idx_reward, agent_position, 1]: 84 | return +1 85 | elif Q[n_idx_reward, agent_position, 0] < Q[n_idx_reward, agent_position, 1]: 86 | return -1 87 | else: 88 | return random.randint(0, 1)*2-1 89 | 90 | # ----------------------------------------------------------------------------- 91 | 92 | 93 | 94 | # Learning phase: the goal is to construct the matrix Q by playing random games 95 | 96 | iterations=100 # Number of iterations of the learning process 97 | gamma=0.9 # gamma of the Bellman equation. Roughly, this parameter tells you how much you want to take in account from future moves 98 | 99 | for ii in range(0,iterations): 100 | idx_reward=random.randint(0, 1)*2 101 | state[idx_reward] = 1 #places reward randomly in state 102 | agent_position = 1 103 | agent_position = random.randint(0, 2) # Uncomment for random initial position (harder for the agent) 104 | status = "running" 105 | 106 | while status == "running": 107 | agent_move = next_move() 108 | learning(idx_reward, agent_position, agent_move,gamma) 109 | reward, status, agent_position = moveto(agent_move, agent_position, status) 110 | 111 | # ----------------------------------------------------------------------------- 112 | 113 | 114 | 115 | 116 | # Play! ----------------------------------------------------------------------- 117 | 118 | iterations_play=100 119 | number_steps = 0 120 | 121 | for jj in range(0,iterations_play): 122 | idx_reward=random.randint(0, 1)*2 123 | state[idx_reward] = 1 124 | agent_position = 1 125 | agent_position = random.randint(0, 2) # Uncomment for random initial position (harder for the agent) 126 | status = "running" 127 | 128 | 129 | while status == "running": 130 | number_steps += 1 #saving number of steps 131 | agent_move = cleverplayer(idx_reward,agent_position) 132 | reward, status, agent_position = moveto(agent_move, agent_position, status) 133 | 134 | print('\n The mean number of steps is ' + repr(number_steps / iterations_play) + '\n') 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/04_invasion_with_neural_network.py: -------------------------------------------------------------------------------- 1 | 2 | '''This is a 'Braingent' wich will use a small neural network with keras and tensorflow as backend to learn the Q-function. 3 | it's obviously way to complicated for this game but it works very well and it should be possible to adapt it to play in AiGym etc. 4 | At the end it contains a little sample code comparing the average number of moves the Braingent with Peters random agent. Since it saves the network weights, if you run 5 | the code multiple times the Braingent will get better and better. for me it quickly reaches a mean of 1+e, where e is the exploration factor , whith which you choose a random move 6 | in Deep Q learning. At last the Braingent is run with exploration turned off, e=0, which gives an average step number of 1.0 7 | ''' 8 | import random 9 | import numpy as np 10 | from keras.models import Model 11 | from keras.layers import Convolution2D, Dense, Flatten, Input, merge, Lambda 12 | from keras.optimizers import RMSprop 13 | from keras import backend as K 14 | 15 | import tensorflow as tf 16 | 17 | 18 | class Game(object): 19 | 20 | def __init__(self): 21 | self.state = [0, 0, 0] 22 | self.agent_position = 1 23 | self.victim_position = random.randint(0, 1)*2 24 | self.state[self.victim_position] = 1 25 | self.status = "running" 26 | 27 | def moveto(self, position): 28 | self.agent_position += position 29 | if self.agent_position < 0: 30 | self.agent_position = 0 31 | elif self.agent_position > 2: 32 | self.agent_position = 2 33 | if self.status == "running" and self.state[self.agent_position] == 1: 34 | self.status = "gameover" 35 | return 1 36 | else: 37 | return 0 38 | def stat(self): 39 | return [self.agent_position, self.victim_position] 40 | 41 | def reward(self): 42 | if self.agent_position == self.victim_position: 43 | return 1 44 | else: 45 | return 0 46 | 47 | 48 | 49 | class RandomAgent(object): 50 | 51 | def __init__(self, game): 52 | self.game = game 53 | self.number_of_steps = 0 54 | 55 | def next_move(self): 56 | self.number_of_steps += 1 57 | return random.randint(0, 1)*2-1 58 | 59 | class Braingent: 60 | 61 | def __init__(self,knowhow="brain.h5" ,cap=100,epsilon=0.1,gamma=0.9): 62 | self.knowhow=knowhow 63 | self.memory = [] 64 | self.gamma=gamma 65 | self.cap = cap 66 | self.epsilon=epsilon 67 | self.build_brain() 68 | 69 | def build_brain(self): 70 | inp = Input(shape=(2,)) 71 | l1 = Dense(3,activation='relu')(inp) 72 | out = Dense(2,activation='linear')(l1) 73 | self.brain=Model(inp,out) 74 | self.brain.compile(loss='mean_squared_error', optimizer='sgd') 75 | 76 | Ql = Lambda(lambda x: tf.reduce_max(x,axis=[1],keep_dims=False))(self.brain(inp)) 77 | self.Q = Model(inp,Ql) 78 | 79 | Al = Lambda(lambda x: tf.argmax(x,axis=1))(self.brain(inp)) 80 | self.A = Model(inp,Al) 81 | try: 82 | self.brain.load_weights(self.knowhow) 83 | except: 84 | print('new braingent, no knowhow') 85 | 86 | def estimateReward(self,states,rewards): 87 | return self.gamma*self.Q.predict(states) + rewards 88 | 89 | 90 | def chooseAction(self,state): 91 | if np.random.random() < self.epsilon: 92 | a = random.randint(-1,1) 93 | else: 94 | a= self.A.predict(state)[0] 95 | if a == 0.0: 96 | a = -1 97 | else: 98 | a = 1 99 | return a 100 | 101 | def new_game(self,game): 102 | self.game = game 103 | self.i = 0 104 | self.memory.append([]) 105 | self.memory= self.memory[-self.cap:] 106 | 107 | def act(self): 108 | state = np.asarray([self.game.stat()],dtype='float32') 109 | reward =np.asarray([self.game.reward()],dtype='float32') 110 | action = self.chooseAction(state) 111 | self.memory[-1].append([game.stat(),game.reward(),action]) 112 | game.moveto(action) 113 | self.i += 1 114 | 115 | def learn(self,batch_size): 116 | n_games= len(self.memory) 117 | X = [] 118 | Y = [] 119 | for i in range(batch_size-1): 120 | if n_games == 1: 121 | gm = 0 122 | else: 123 | gm = random.randint(0,n_games-1) 124 | n_moves = len(self.memory[gm]) 125 | if n_moves == 1: 126 | mv = 0 127 | else: 128 | mv = random.randint(0,n_moves-1) 129 | 130 | X.append(self.memory[gm][mv][0]) 131 | if mv < n_moves-1: 132 | state_mv1 = np.asarray([self.memory[gm][mv+1][0]],dtype='float32') 133 | rew = np.asarray([self.memory[gm][mv][1]],dtype='float32') 134 | target = self.estimateReward(state_mv1,rew)[0] 135 | Y.append([target,target]) 136 | else: 137 | target = self.memory[gm][mv][1] 138 | Y.append([target,target]) 139 | X = np.asarray(X,dtype='float32') 140 | Y = np.asarray(Y,dtype='float32') 141 | self.brain.train_on_batch(X,Y) 142 | self.brain.save_weights(self.knowhow) 143 | 144 | 145 | 146 | 147 | 148 | bragent= Braingent(epsilon=0.01) 149 | 150 | 151 | 152 | 153 | timelapse=[] 154 | 155 | for i in range(500): 156 | #print(i) 157 | game = Game() 158 | agent= RandomAgent(game) 159 | while game.status == 'running': 160 | game.moveto(agent.next_move()) 161 | timelapse.append(agent.number_of_steps) 162 | 163 | print('average number of moves per game for random agent:') 164 | print(np.array(timelapse).mean()) 165 | 166 | timelapse=[] 167 | 168 | for i in range(500): 169 | #print(i) 170 | game = Game() 171 | bragent.new_game(game) 172 | while game.status == 'running': 173 | bragent.act() 174 | bragent.learn(20) 175 | timelapse.append(bragent.i) 176 | 177 | 178 | print('average number of moves per game for braingent:') 179 | print(np.array(timelapse).mean()) 180 | 181 | timelapse=[] 182 | bragent.epsilon=0 183 | 184 | for i in range(500): 185 | #print(i) 186 | game = Game() 187 | bragent.new_game(game) 188 | while game.status == 'running': 189 | bragent.act() 190 | bragent.learn(20) 191 | timelapse.append(bragent.i) 192 | 193 | print('average number of moves per game for trained braingent without exploration:') 194 | print(np.array(timelapse).mean()) 195 | 196 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/04_rl_toy_example.py: -------------------------------------------------------------------------------- 1 | '''This is a toy reinforcement learning problem. The state space has three 2 | locations on a line, the agent starts in the middle one. Either on the left 3 | or the right, there is a reward. Reaching this, the game terminates. The agent 4 | has complete access to the state space of the game. The action space of the 5 | agent is left or right moves. It can choose left infinite many times if the 6 | reward is on the right: in this case, it will stay in the left-most cell 7 | forever. 8 | ''' 9 | 10 | import random 11 | 12 | 13 | class Game(object): 14 | 15 | def __init__(self): 16 | self.state = [0, 0, 0] 17 | self.agent_position = 1 18 | self.state[random.randint(0, 1)*2] = 1 19 | self.status = "running" 20 | 21 | def moveto(self, position): 22 | self.agent_position += position 23 | if self.agent_position < 0: 24 | self.agent_position = 0 25 | elif self.agent_position > 2: 26 | self.agent_position = 2 27 | if self.status == "running" and self.state[self.agent_position] == 1: 28 | self.status = "gameover" 29 | return 1 30 | else: 31 | return 0 32 | 33 | 34 | class RandomAgent(object): 35 | 36 | def __init__(self, game): 37 | self.game = game 38 | self.number_of_steps = 0 39 | 40 | def next_move(self): 41 | self.number_of_steps += 1 42 | return random.randint(0, 1)*2-1 43 | 44 | game = Game() 45 | agent = RandomAgent(game) 46 | while game.status == "running": 47 | agent_move = agent.next_move() 48 | game.moveto(agent_move) 49 | 50 | print(agent.number_of_steps) 51 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/04_rl_toy_example_q_learn.py: -------------------------------------------------------------------------------- 1 | '''This is a toy reinforcement learning problem. The state space has three 2 | locations on a line, the agent starts in the middle one. Either on the left 3 | or the right, there is a reward. Reaching this, the game terminates. The agent 4 | has complete access to the state space of the game. The action space of the 5 | agent is left or right moves. It can choose left infinite many times if the 6 | reward is on the right: in this case, it will stay in the left-most cell 7 | forever. 8 | ''' 9 | 10 | import random 11 | import numpy as np 12 | 13 | 14 | class Game(object): 15 | 16 | def __init__(self): 17 | self.state = [0, 0, 0] 18 | self.agent_position = 1 19 | self.state[random.randint(0, 1)*2] = 1 20 | self.status = "running" 21 | 22 | def moveto(self, position): 23 | self.agent_position += position 24 | if self.agent_position < 0: 25 | self.agent_position = 0 26 | elif self.agent_position > 2: 27 | self.agent_position = 2 28 | if self.status == "running" and self.state[self.agent_position] == 1: 29 | self.status = "gameover" 30 | return 1 31 | else: 32 | return 0 33 | 34 | class RandomAgent(object): 35 | 36 | def __init__(self, game): 37 | self.game = game 38 | self.number_of_steps = 0 39 | 40 | def next_move(self): 41 | self.number_of_steps += 1 42 | return random.randint(0, 1)*2-1 43 | 44 | class QAgent(object): 45 | 46 | def __init__(self, game): 47 | self.game = game 48 | self.number_of_steps = 0 49 | self.Q = np.ones([2,3,2])#np.random.uniform(size = [2,3,2]) 50 | 51 | def next_move(self, state): 52 | self.number_of_steps += 1 53 | return 2*np.argmax(self.Q[state, self.game.agent_position, :])-1 54 | 55 | def update_Q(self, old_position, move, new_position, reward): 56 | learn_rate, discount = 1.0, 0.5 57 | self.Q[state, old_position, move] += learn_rate*(reward + 58 | discount*np.max(self.Q[state, new_position, :]) - 59 | self.Q[state, old_position, move]) 60 | 61 | game = Game() 62 | agent = RandomAgent(game) 63 | while game.status == "running": 64 | agent_move = agent.next_move() 65 | game.moveto(agent_move) 66 | 67 | print(agent.number_of_steps) 68 | 69 | game_Q = Game() 70 | agent_Q = QAgent(game_Q) 71 | 72 | for i in range(0, 10): 73 | game_Q = Game() 74 | agent_Q.game = game_Q 75 | agent_Q.number_of_steps = 0 76 | state = int(game_Q.state == [0, 0, 1]) 77 | while game_Q.status == "running": 78 | agent_move = agent_Q.next_move(state) 79 | old_position = game_Q.agent_position 80 | reward = game_Q.moveto(agent_move) 81 | new_position = game_Q.agent_position 82 | agent_Q.update_Q(old_position, (agent_move + 1)//2, new_position, reward) 83 | 84 | print(agent_Q.number_of_steps) 85 | 86 | print(agent_Q.Q) -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/04_tictactoe.py: -------------------------------------------------------------------------------- 1 | '''This is a template with a randomly behaving agent playing tic-tac-toe against 2 | a perfect AI, that is, the best the agent can hope for is a tie. 3 | To make it work, you will xo (https://github.com/dwayne/xo-python/). Install 4 | it with pip, but *strictly* not with conda (that is a different xo): 5 | 6 | pip install xo 7 | 8 | It is written in pure Python, so you should not have any issues installing it. 9 | ''' 10 | 11 | import random 12 | from xo import ai 13 | from xo.game import Game 14 | 15 | 16 | def get_free_locations(game): 17 | free_locations = [] 18 | for i, cell in enumerate(game.board.cells): 19 | if cell == ' ': 20 | free_locations.append((i // 3 + 1, i % 3 + 1)) 21 | return free_locations 22 | 23 | 24 | class RandomAgent(object): 25 | 26 | def __init__(self, game): 27 | self.game = game 28 | 29 | def next_move(self): 30 | free_locations = get_free_locations(self.game) 31 | return free_locations[random.randint(0, len(free_locations))] 32 | 33 | 34 | # This is one round, ending with a reward 35 | game = Game() 36 | agent = RandomAgent(game) 37 | game.start('o') 38 | while True: 39 | ai_move = ai.evaluate(game.board, 'o').positions[0] 40 | status = game.moveto(*ai_move) 41 | print(game.board.toascii()) 42 | if status['name'] == 'gameover': 43 | break 44 | agent_move = agent.next_move() 45 | status = game.moveto(*agent_move) 46 | print(game.board.toascii()) 47 | if status['name'] == 'gameover': 48 | break 49 | if game.statistics['xwins'] == 1: 50 | agent_reward = 1 51 | elif game.statistics['squashed'] == 1: 52 | agent_reward = 0 53 | else: 54 | agent_reward = -1 55 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/05_Nielsen_adaption_buggy.py: -------------------------------------------------------------------------------- 1 | """This example is mostly taken from Nielsens book, Neural Networks and Deep Learning 2 | http://neuralnetworksanddeeplearning.com/chap1.html 3 | The aim was to build a Neural Network from the scratch, without Keras. 4 | The algorithm still contains a bug, such that the certainity to find Einstein decreases, instead of increases""" 5 | 6 | 7 | import numpy as np 8 | import random 9 | import tools 10 | 11 | def sigmoid(z): 12 | return 1.0/(1.0+np.exp(-z)) 13 | 14 | def sigmoid_prime(z): 15 | """Derivative of the sigmoid function.""" 16 | return sigmoid(z)*(1-sigmoid(z)) 17 | 18 | class Network(object): 19 | 20 | def __init__(self, sizes): #sizes is argument of Network() 21 | self.num_layers = len(sizes) #how many layers does list contain 22 | self.sizes = sizes 23 | self.biases = [np.random.randn(y, 1) for y in sizes[1:]] #sizes[1:] gives the elements of sizes from index 1 to the end 24 | #randn(a,b) generates an array of size a times b with random numbers 25 | #therefore biases is a list of (Nx1)-arrays containing the biases for all the nodes of a N-dim layer, except for the first layer 26 | self.weights = [np.random.randn(y, x) 27 | for x, y in zip(sizes[:-1], sizes[1:])] 28 | #zip(a,b) gives a list of pairs of the items in a,b (a_i,b_i) 29 | #therefore weights is a list of matrices, that contain the weights. E.g. for sizes = [2,3,1], there is a 2x3 and a 3x1 matrix 30 | #so matrixelement w_jk connects neuron j from the n+1 layer with the k neuron from the n-th layer. 31 | 32 | 33 | 34 | 35 | def feedforward(self, a): #input a has to be same dimension as 1st layer 36 | """Return the output of the network if "a" is input. Propagates the input through the network""" 37 | for b, w in zip(self.biases, self.weights): #b, w are the full weight and bias vectors, that contain all parameters 38 | a = sigmoid(np.dot(w, a)+b) #dot(a,b) for a = n x m and b = m x l gives n x l 39 | return a 40 | #if we define Network([a,b,c,d]) then w contains a x b matrix, b x c matrix and c x d matrix 41 | 42 | def SGD(self, training_data, epochs, mini_batch_size, eta, 43 | test_data=None): #stochastic gradient descent 44 | """Train the neural network using mini-batch stochastic 45 | gradient descent. The "training_data" is a list of tuples 46 | "(x, y)" representing the training inputs and the desired 47 | outputs. The other non-optional parameters are 48 | self-explanatory. If "test_data" is provided then the 49 | network will be evaluated against the test data after each 50 | epoch, and partial progress printed out. This is useful for 51 | tracking progress, but slows things down substantially.""" 52 | if test_data: n_test = len(test_data) #is there testdata? 53 | n = len(training_data) #check length of training data 54 | for j in range(epochs): #repeat for every epoch 55 | random.shuffle(training_data) #shuffle training data to choose mini batch 56 | mini_batches = [training_data[k:k+mini_batch_size] for k in range(0, n, mini_batch_size)] 57 | for mini_batch in mini_batches: 58 | self.update_mini_batch(mini_batch, eta) #update mini batch 59 | if test_data: 60 | print("Epoch {0}: {1} / {2}".format(j, self.evaluate(test_data), n_test)) 61 | #evaluate is fct in this class, that checks, how often the network predicted 62 | #the outcome exactly. 63 | else: 64 | print("Epoch {0} complete".format(j)) 65 | 66 | def update_mini_batch(self, mini_batch, eta): 67 | """Update the network's weights and biases by applying 68 | gradient descent using backpropagation to a single mini batch. 69 | The ``mini_batch`` is a list of tuples ``(x, y)``, and ``eta`` 70 | is the learning rate.""" 71 | nabla_b = [np.zeros(b.shape) for b in self.biases] 72 | nabla_w = [np.zeros(w.shape) for w in self.weights] 73 | for x, y in mini_batch: 74 | delta_nabla_b, delta_nabla_w = self.backprop(x, y) 75 | nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)] 76 | nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)] 77 | self.weights = [w-(eta/len(mini_batch))*nw 78 | for w, nw in zip(self.weights, nabla_w)] 79 | self.biases = [b-(eta/len(mini_batch))*nb 80 | for b, nb in zip(self.biases, nabla_b)] 81 | 82 | def backprop(self, x, y): 83 | """Return a tuple ``(nabla_b, nabla_w)`` representing the 84 | gradient for the cost function C_x. ``nabla_b`` and 85 | ``nabla_w`` are layer-by-layer lists of numpy arrays, similar 86 | to ``self.biases`` and ``self.weights``.""" 87 | nabla_b = [np.zeros(b.shape) for b in self.biases] 88 | nabla_w = [np.zeros(w.shape) for w in self.weights] 89 | # feedforward 90 | activation = x 91 | activations = [x] # list to store all the activations, layer by layer 92 | zs = [] # list to store all the z vectors, layer by layer 93 | for b, w in zip(self.biases, self.weights): 94 | z = np.dot(w, activation)+b 95 | zs.append(z) 96 | activation = sigmoid(z) 97 | activations.append(activation) 98 | # backward pass 99 | delta = self.cost_derivative(activations[-1], y) * \ 100 | sigmoid_prime(zs[-1]) 101 | nabla_b[-1] = delta 102 | nabla_w[-1] = np.dot(delta, activations[-2].transpose()) 103 | # Note that the variable l in the loop below is used a little 104 | # differently to the notation in Chapter 2 of the book. Here, 105 | # l = 1 means the last layer of neurons, l = 2 is the 106 | # second-last layer, and so on. It's a renumbering of the 107 | # scheme in the book, used here to take advantage of the fact 108 | # that Python can use negative indices in lists. 109 | for l in range(2, self.num_layers): 110 | z = zs[-l] 111 | sp = sigmoid_prime(z) 112 | delta = np.dot(self.weights[-l+1].transpose(), delta) * sp 113 | nabla_b[-l] = delta 114 | nabla_w[-l] = np.dot(delta, activations[-l-1].transpose()) 115 | return (nabla_b, nabla_w) 116 | 117 | def evaluate(self, test_data): 118 | """Return the number of test inputs for which the neural 119 | network outputs the correct result. Note that the neural 120 | network's output is assumed to be the index of whichever 121 | neuron in the final layer has the highest activation.""" 122 | test_results = [(np.argmax(self.feedforward(x)), y) 123 | for (x, y) in test_data] 124 | for (x,y) in test_data: 125 | print(x,y,self.feedforward(x)) 126 | return sum(int(x == y) for (x, y) in test_results) 127 | #e.g. for image recognition, we have the pixels and a variable that says what the picture contains 128 | #(e.g. x = 'Dog'). This function returns the sum of succesful recognitions, where the output y is 129 | #identical to the input x. 130 | 131 | def cost_derivative(self, output_activations, y): 132 | """Return the vector of partial derivatives \partial C_x / 133 | \partial a for the output activations.""" 134 | return (output_activations-y) 135 | 136 | 137 | training_set, training_labels= tools.load_images("images/train/") 138 | test_set, test_labels = tools.load_images("images/test/") 139 | 140 | resize_set, resize_labels= tools.prep_datas(training_set, training_labels) 141 | resize_test_set, resize_test_labels= tools.prep_datas(test_set, test_labels) 142 | 143 | training_data = list(zip(resize_set,resize_labels)) 144 | 145 | test_data = list(zip(resize_test_set,resize_test_labels)) 146 | 147 | net = Network([1024, 200, 1]) #output is only yes or no. Its marie or albert or not. 148 | net.SGD(training_data, 10000, 25, 0.1, test_data=test_data) #training data, epochs, mini_batch_size, eta ,test data 149 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/05_wheres_wally.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """Created on Wed Mar 15 10:09:24 2017""" 3 | import math 4 | import os 5 | import numpy as np 6 | from skimage import io 7 | from skimage import transform as tf 8 | from keras.models import Sequential 9 | from keras.layers.convolutional import Conv2D 10 | from keras.layers.convolutional import MaxPooling2D 11 | from keras.layers.core import Flatten 12 | from keras.layers.core import Dense 13 | from keras.optimizers import SGD 14 | from keras.utils import np_utils 15 | from keras.preprocessing.image import ImageDataGenerator 16 | from matplotlib import pyplot as plt 17 | 18 | 19 | def load_images(folder): 20 | images = [] 21 | labels = [] 22 | for file in os.listdir(folder): 23 | if file.endswith(".png"): 24 | images.append(io.imread(folder + file, as_grey=True)) 25 | if file.find("einstein") > -1: 26 | labels.append(1) 27 | elif file.find("curie") > -1: 28 | labels.append(2) 29 | elif os.path.splitext(file)[0].isdigit(): 30 | labels.append(int(os.path.splitext(file)[0])) 31 | else: 32 | labels.append(0) 33 | return images, labels 34 | 35 | 36 | def deshear(filename): 37 | image = io.imread(filename) 38 | distortion = image.shape[1] - image.shape[0] 39 | shear = tf.AffineTransform(shear=math.atan(distortion/image.shape[0])) 40 | return tf.warp(image, shear)[:, distortion:] 41 | 42 | 43 | def normalize_images(images): 44 | for i in range(len(images)): 45 | images[i] = images[i][0:100, 0:100] 46 | images[i] = images[i]/np.amax(images[i]) 47 | return np.array(images) 48 | 49 | 50 | class LeNet: 51 | 52 | def __init__(self, input_shape, conv_1, pool_1, conv_2, pool_2, hidden, 53 | classes): 54 | self.model = Sequential() 55 | # first set of CONV => RELU => POOL 56 | self.model.add(Conv2D(*conv_1, padding='same', activation='relu', 57 | data_format='channels_last', 58 | input_shape=input_shape)) 59 | self.model.add(MaxPooling2D(pool_1[0], pool_1[1])) 60 | # second set of CONV => RELU => POOL 61 | self.model.add(Conv2D(*conv_2, padding='same', activation='relu', 62 | data_format='channels_last')) 63 | self.model.add(MaxPooling2D(pool_2[0], pool_2[1])) 64 | # set of FC => RELU layers 65 | self.model.add(Flatten()) 66 | self.model.add(Dense(hidden, activation='relu')) 67 | # softmax classifier 68 | self.model.add(Dense(classes, activation='softmax')) 69 | 70 | # Loading image data sets and normalizing color scale 71 | training_set, training_labels = load_images("images/train/") 72 | test_set, test_labels = load_images("images/test/") 73 | rw_set, rw_file_labels = load_images("images/real_world/") 74 | training_set = normalize_images(training_set) 75 | training_set = training_set[..., np.newaxis] 76 | test_set = normalize_images(test_set) 77 | test_set = test_set[..., np.newaxis] 78 | rw_set = normalize_images(rw_set) 79 | rw_set = rw_set[..., np.newaxis] 80 | rw_set = np.array([x for (y, x) in sorted(zip(rw_file_labels, rw_set))]) 81 | 82 | # Getting labels for real world set from file 83 | f = open('images/real_world/labels.txt', "r") 84 | lines = f.readlines() 85 | rw_labels = [] 86 | for x in lines: 87 | rw_labels.append(int((x.split(' ')[1]).replace('\n', ''))) 88 | f.close() 89 | 90 | # Parameters for LeNet convolutional network 91 | classes = 3 # number of classes to identify 92 | hidden = 500 # number of nuerons in hidden layer 93 | conv_1 = (20, (15, 15)) # (num of filters in first layer, filter size) 94 | conv_2 = (50, (15, 15)) # (num of filters in second layer, filter size) 95 | pool_1 = ((6, 6), (6, 6)) # (size of pool matrix, stride) 96 | pool_2 = ((6, 6), (6, 6)) # (size of pool matrix, stride) 97 | 98 | # Converting integer labels to categorical labels 99 | training_labels = np_utils.to_categorical(training_labels, classes) 100 | test_labels = np_utils.to_categorical(test_labels, classes) 101 | rw_labels = np_utils.to_categorical(rw_labels, classes) 102 | 103 | # Creating LeNet from basic training data 104 | aps = LeNet(training_set[1].shape, conv_1, pool_1, conv_2, pool_2, hidden, 105 | classes) 106 | aps.model.compile(loss='categorical_crossentropy', optimizer='adam', 107 | metrics=["accuracy"]) 108 | print('\nTraining LeNet with basic training data\n') 109 | aps.model.fit(training_set, training_labels, batch_size=10, epochs=50, 110 | verbose=1) 111 | 112 | # Testing basic model of both sets 113 | test_probs = aps.model.predict(test_set) 114 | test_prediction = test_probs.argmax(axis=1) 115 | rw_probs = aps.model.predict(rw_set) 116 | rw_prediction = rw_probs.argmax(axis=1) 117 | 118 | (loss, accuracy) = aps.model.evaluate(test_set, test_labels, verbose=0) 119 | print('\nAccuracy in test set: {}\n'.format(accuracy)) 120 | (loss, accuracy) = aps.model.evaluate(rw_set, rw_labels, verbose=0) 121 | print('Accuracy in real world set: {}\n'.format(accuracy)) 122 | 123 | # Augmenting basic data set to improve performance in real world set 124 | datagen = ImageDataGenerator(rotation_range=10, shear_range=0.3, 125 | zoom_range=0.2, width_shift_range=0.15, 126 | height_shift_range=0.15, fill_mode='constant', 127 | cval=1) 128 | 129 | # Creating LeNet with augmmented data 130 | aps_aug = LeNet(training_set[1].shape, conv_1, pool_1, conv_2, pool_2, hidden, 131 | classes) 132 | aps_aug.model.compile(loss='categorical_crossentropy', optimizer='adam', 133 | metrics=["accuracy"]) 134 | print('Training LeNet with augmented training data\n') 135 | aps_aug.model.fit_generator(datagen.flow(training_set, training_labels, 136 | batch_size=10), 137 | steps_per_epoch=len(training_set), epochs=50, 138 | verbose=1) 139 | 140 | # Testing augmented model 141 | test_probs_aug = aps_aug.model.predict(test_set) 142 | test_prediction_aug = test_probs_aug.argmax(axis=1) 143 | rw_probs_aug = aps_aug.model.predict(rw_set) 144 | rw_prediction_aug = rw_probs_aug.argmax(axis=1) 145 | 146 | (loss, accuracy) = aps_aug.model.evaluate(rw_set, rw_labels, verbose=0) 147 | print('\nAccuracy in real world set: {}'.format(accuracy)) 148 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/06_SVM.py: -------------------------------------------------------------------------------- 1 | """ 2 | Created on Sun Mar 26 14:13:45 2017 3 | 4 | @author: phuem 5 | """ 6 | 7 | #http://scikit-learn.org/stable/modules/svm.html 8 | 9 | from sklearn import svm, metrics 10 | import tools 11 | import numpy as np 12 | import matplotlib.pyplot as plt 13 | 14 | #------------------------------------------------------------------------------ 15 | #Load and reshape Images with tools.py 16 | training_set, training_labels= tools.load_images("images/train/") 17 | test_set, test_labels = tools.load_images("images/test/") 18 | 19 | resize_set, resize_labels= tools.prep_datas(training_set, training_labels) 20 | resize_test_set, resize_test_labels= tools.prep_datas(test_set, test_labels) 21 | 22 | #--------------------------------------------------------------- 23 | #very ugly way to bring vectors to the right shape for SVC fit() 24 | a = [] 25 | for x in resize_set: 26 | a.append(x.tolist()) 27 | #---------------------------------------------------------------- 28 | X = a #reshaped images (training) 29 | y = resize_labels #labels 30 | 31 | clf = svm.SVC(gamma=1.0) #load SVC 32 | clf.fit(X, y) #fit SVC 33 | 34 | #------------------------------------------------------------------- 35 | #very ugly way to bring vectors to the right shape for SVC predict() 36 | a = [] 37 | for x in resize_test_set: 38 | a.append(x.tolist()) 39 | #------------------------------------------------------------------- 40 | 41 | predicted = clf.predict(a) #predict labels for new data 42 | expected = resize_test_labels #this are the real labels that we expect 43 | 44 | for index in range(0,len(resize_labels)): #Print Training Data 45 | plt.subplot(5, 5, index + 1) 46 | plt.axis('off') 47 | plt.imshow(training_set[index], cmap=plt.cm.gray_r, interpolation='nearest') 48 | plt.title('Training: %i' % resize_labels[index]) 49 | plt.show() 50 | 51 | for index in range(0,len(resize_test_labels)): #Print Test Result 52 | plt.subplot(3, 2, index + 1) 53 | plt.axis('off') 54 | plt.imshow(test_set[index], cmap=plt.cm.gray_r, interpolation='nearest') 55 | plt.title('Predicted_Label: %i' % predicted[index]) 56 | plt.show() 57 | #----------------------------------------------------------------------------- 58 | #Print Results 59 | print("Classification report for classifier %s:\n%s\n" 60 | % (clf, metrics.classification_report(expected, predicted))) 61 | print("Confusion matrix:\n%s" % metrics.confusion_matrix(expected, predicted)) 62 | #See wiki article about confusion matrix. If Matrix is diagonal, prediction worked well! 63 | 64 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/06_aps_with_classifiers.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Solving the Captcha problem with Random Forest and Suppor Vector" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "To be able to run this notebook you should have place in the folder Peter's program __image_loader.py__ and the folder __images__ used last week" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "import numpy as np\n", 24 | "import os\n", 25 | "from skimage.transform import resize\n", 26 | "from sklearn.ensemble import RandomForestClassifier\n", 27 | "from sklearn import svm\n", 28 | "import tools as im\n", 29 | "from matplotlib import pyplot as plt\n", 30 | "%matplotlib inline" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 4, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "path=os.getcwd()+'/' # finds the path of the folder in which the notebook is\n", 40 | "path_train=path+'images/train/'\n", 41 | "path_test=path+'images/test/'\n", 42 | "path_real=path+'images/real_world/'" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "We define the function __prep_datas__ (props to Alexandre), already used the previous week. However now we reshape the images from a 32x32 matrix (this value seems unnecessary, however the bigger the image the worst the classifiers will work) to a flat 1024 vector, a constraint given by the Random Forest classifier." 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 5, 55 | "metadata": { 56 | "collapsed": true 57 | }, 58 | "outputs": [], 59 | "source": [ 60 | "def prep_datas(xset,xlabels):\n", 61 | " X=list(xset)\n", 62 | " for i in range(len(X)):\n", 63 | " X[i]=resize(X[i],(32,32,1)) # reduce the size of the image from 100X100 to 32X32. Also flattens the color levels\n", 64 | " X[i]=np.reshape(X[i],1024) # reshape from 32x32 to a flat 1024 vector\n", 65 | " X=np.array(X) # transforms it into an array\n", 66 | " Y=np.asarray(xlabels) # transforms from list to array\n", 67 | " return X,Y" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "Then we load the training and the test set:" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 6, 80 | "metadata": { 81 | "scrolled": true 82 | }, 83 | "outputs": [], 84 | "source": [ 85 | "training_set, training_labels = im.load_images(path_train)\n", 86 | "X_train, Y_train = prep_datas(training_set,training_labels)\n", 87 | "\n", 88 | "test_set, test_labels = im.load_images(path_test)\n", 89 | "X_test,Y_test=prep_datas(test_set,test_labels)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "We define the classifiers Random Forest Classifier and Support Machine Classifier and we train them throught the fit function. Taking a linear kernel for SVC gives the best results for this classifier." 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 14, 102 | "metadata": { 103 | "scrolled": false 104 | }, 105 | "outputs": [ 106 | { 107 | "data": { 108 | "text/plain": [ 109 | "SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,\n", 110 | " decision_function_shape=None, degree=3, gamma='auto', kernel='linear',\n", 111 | " max_iter=-1, probability=False, random_state=None, shrinking=True,\n", 112 | " tol=0.001, verbose=False)" 113 | ] 114 | }, 115 | "execution_count": 14, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "classifierForest = RandomForestClassifier(n_estimators=1000)\n", 122 | "classifierSVC=svm.SVC(kernel='linear')\n", 123 | "\n", 124 | "classifierForest.fit(X_train, Y_train) \n", 125 | "classifierSVC.fit(X_train,Y_train) " 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "Let's test how good the system is doing" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 10, 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "name": "stdout", 142 | "output_type": "stream", 143 | "text": [ 144 | "[0 0 0 0 1 0]\n", 145 | "[0 0 0 0 1 0]\n", 146 | "[0 0 0 0 1 0]\n" 147 | ] 148 | } 149 | ], 150 | "source": [ 151 | "expectedF = Y_test\n", 152 | "predictedF = classifierForest.predict(X_test)\n", 153 | "predictedS = classifierSVC.predict(X_test)\n", 154 | "\n", 155 | "print(expectedF)\n", 156 | "print(predictedF)\n", 157 | "print(predictedS)" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "Now we load the real set of images and test it. This part of the program has been taken from [Alexandre's program](https://github.com/peterwittek/qml-rg/blob/master/Meeting%205/aps_capcha.ipynb) from last week. First we load the 'real world' images" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 15, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [ 173 | "real_world_set=[]\n", 174 | "for i in np.arange(1,73):\n", 175 | " filename=path+'images/real_world/'+str(i)+'.png'\n", 176 | " real_world_set.append(im.deshear(filename))\n", 177 | "fake_label=np.ones(len(real_world_set),dtype='int32')\n", 178 | "X_real,Y_real=prep_datas(real_world_set,fake_label)" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "Then we make the predictions with both classifiers" 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": 16, 191 | "metadata": { 192 | "collapsed": true 193 | }, 194 | "outputs": [], 195 | "source": [ 196 | "y_predF = classifierForest.predict(X_real)\n", 197 | "y_predS = classifierSVC.predict(X_real)" 198 | ] 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "metadata": {}, 203 | "source": [ 204 | "Finally we plot the results" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 17, 210 | "metadata": {}, 211 | "outputs": [ 212 | { 213 | "data": { 214 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY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ua0qNRwB3tSxvBQ6tHh8KbO13jdPUfS3w+qbXC/wBsAV4aRNrBVYz8QP+GuBrTT8GmLjJ\n40FT1jWqXmA58DOqi1qaWmebut8A/Fc/a11Q7wCAVcADLcvbqnVNNtfbafRNRBwBHAv8kIbWW32s\ncjsTf4x4Y2Y2tdZ/BP4WGG9Z18Q699p7G5fN1W1aoHn1HgmMAf9afbT2L9XfHDWtzqnWA1dWj/tS\n60ILgIGWE/HfqOtyI+JA4CvA+zPz8dbnmlRvZu7JibfVq4HjI+IFU57ve60RcSrwSGZunq5NE+qc\n4pXVfj2ZiY8BX9X6ZEPqXQIcB1yamccCv2XKRygNqXNSRDwDeBPwpanPzWetCy0AtgNrWpZXV+ua\n7OHqNhrMdDuNfoiIISZ++X8hM6+uVje2XoDMfAy4mYlzLU2r9RXAm6r/H+Mq4DUR8XmaV+ekbLmN\nCxOfVR9P8+rdBmyr3vUBfJmJQGhana1OBrZk5sPVcl9qXWgBcCuwNiKOrBJ2PRO3q2iyWW+n0Q8R\nEcCngXsz86KWpxpXb0QMR8SK6vEyJs5V/ISG1ZqZ52Xm6sw8golj81uZ+Zc0rM69ZriNS6PqzcyH\ngAci4qhq1WuBe2hYnVOcyVMf/0C/au33iZD9cGLlFOC/gf8BPtLveqbUdiXwILCLiX+1vBN4DhMn\nBe8Dvgk8u991VrW+kom3oXcAt1dfpzSxXuAY4Laq1ruA86v1jau1peYTeeokcCPrBJ4L/Lj6unvv\nz1MT62Xi6q/R6hi4BnhWE+usaj0AeBRY3rKuL7V6KwhJKtRC+whIkjRHBoAkFcoAkKRCGQCSVCgD\nQJIKZQBIUqEMAEkq1P8DrX2PVrecCTYAAAAASUVORK5CYII=\n", 215 | "text/plain": [ 216 | "" 217 | ] 218 | }, 219 | "metadata": {}, 220 | "output_type": "display_data" 221 | } 222 | ], 223 | "source": [ 224 | "f=open(path+'images/real_world/labels.txt',\"r\")\n", 225 | "lines=f.readlines()\n", 226 | "result=[]\n", 227 | "for x in lines:\n", 228 | " result.append((x.split('\t')[1]).replace('\\n',''))\n", 229 | "f.close()\n", 230 | "\n", 231 | "result=np.array([int(x) for x in result])\n", 232 | "result[result>1]=1\n", 233 | "plt.plot(y_predF,'o')\n", 234 | "plt.plot(1.2*y_predS,'o')\n", 235 | "plt.plot(2*result,'o')\n", 236 | "plt.ylim(-0.5,2.5);" 237 | ] 238 | }, 239 | { 240 | "cell_type": "markdown", 241 | "metadata": {}, 242 | "source": [ 243 | "As we can see, __SVC__ (orange dots) seems much more efficient than __Random Forest__ (blue dots). Different paremeters for the classifiers may improve the efficiency of RandomForest eventough differents sets were tried and all seemed to have errors. Data augmentation should also help to solve the problem." 244 | ] 245 | } 246 | ], 247 | "metadata": { 248 | "kernelspec": { 249 | "display_name": "Python 3", 250 | "language": "python", 251 | "name": "python3" 252 | }, 253 | "language_info": { 254 | "codemirror_mode": { 255 | "name": "ipython", 256 | "version": 3 257 | }, 258 | "file_extension": ".py", 259 | "mimetype": "text/x-python", 260 | "name": "python", 261 | "nbconvert_exporter": "python", 262 | "pygments_lexer": "ipython3", 263 | "version": "3.6.1" 264 | } 265 | }, 266 | "nbformat": 4, 267 | "nbformat_minor": 2 268 | } 269 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/06_wheres_wally_traditional.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """Created on Wed Mar 15 10:09:24 2017""" 3 | import math 4 | import os 5 | import numpy as np 6 | from skimage import io 7 | from skimage import transform as tf 8 | from matplotlib import pyplot as plt 9 | from skimage.transform import resize 10 | from sklearn import svm, metrics 11 | from sklearn.ensemble import RandomForestClassifier 12 | from sklearn.decomposition import PCA 13 | import xgboost as xgb 14 | from keras.models import Model 15 | from keras.layers import Input, Dense 16 | 17 | def load_images(folder): 18 | images = [] 19 | labels = [] 20 | for file in os.listdir(folder): 21 | if file.endswith(".png"): 22 | images.append(io.imread(folder + file, as_grey=True)) 23 | if file.find("einstein") > -1: 24 | labels.append(1) 25 | elif file.find("curie") > -1: 26 | labels.append(2) 27 | elif os.path.splitext(file)[0].isdigit(): 28 | labels.append(int(os.path.splitext(file)[0])) 29 | else: 30 | labels.append(0) 31 | return images, labels 32 | 33 | 34 | def prep_images(images,size): 35 | for i in range(len(images)): 36 | images[i] = resize(images[i], size) 37 | images[i] = np.reshape(images[i], size[0]*size[1])/np.amax(images[i]) 38 | return np.array(images) 39 | 40 | 41 | image_size = (32, 32) 42 | 43 | # Loading image data sets 44 | training_set, training_labels = load_images("images/train/") 45 | test_set, test_labels = load_images("images/test/") 46 | rw_set, rw_file_labels = load_images("images/real_world/") 47 | # Resize images to same dimensions, normalize the grey scale and return vectors 48 | training_set = prep_images(training_set, image_size) 49 | test_set = prep_images(test_set, image_size) 50 | rw_set = prep_images(rw_set, image_size) 51 | # Sort real world set by number in file name 52 | rw_set = np.array([x for (y, x) in sorted(zip(rw_file_labels, rw_set))]) 53 | # Get labels for real world set from file 54 | f = open('images/real_world/labels.txt', "r") 55 | lines = f.readlines() 56 | rw_labels = [] 57 | for x in lines: 58 | rw_labels.append(int((x.split(' ')[1]).replace('\n', ''))) 59 | f.close() 60 | # Reduce labels to only two categories 61 | training_labels = np.array(training_labels) 62 | training_labels[training_labels > 0] = 1 63 | rw_labels = np.array(rw_labels) 64 | rw_labels[rw_labels > 0] = 1 65 | # Support vector machine 66 | svm_model = svm.SVC(kernel='linear', C=10) 67 | svm_model.fit(training_set, training_labels) 68 | 69 | svm_test_predict = svm_model.predict(test_set) 70 | svm_rw_predict = svm_model.predict(rw_set) 71 | 72 | print("\nSVM\n") 73 | print("Test set confusion matrix:\n%s" % metrics.confusion_matrix(test_labels, svm_test_predict)) 74 | print("Real world confusion matrix:\n%s" % metrics.confusion_matrix(rw_labels, svm_rw_predict)) 75 | 76 | # Random forest 77 | forest_model = RandomForestClassifier(n_estimators=100) 78 | forest_model.fit(training_set, training_labels) 79 | 80 | rf_test_predict = forest_model.predict(test_set) 81 | rf_rw_predict = forest_model.predict(rw_set) 82 | 83 | print("\nRandom forest\n") 84 | print("Test set confusion matrix:\n%s" % metrics.confusion_matrix(test_labels, rf_test_predict)) 85 | print("Real world confusion matrix:\n%s" % metrics.confusion_matrix(rw_labels, rf_rw_predict)) 86 | 87 | # XGBoost 88 | xgb_model = xgb.XGBClassifier(scale_pos_weight=23/2) 89 | xgb_model.fit(training_set, training_labels) 90 | 91 | xgb_test_predict = xgb_model.predict(test_set) 92 | xgb_rw_predict = xgb_model.predict(rw_set) 93 | 94 | print("\nXGBoost\n") 95 | print("Test set confusion matrix:\n%s" % metrics.confusion_matrix(test_labels, xgb_test_predict)) 96 | print("Real world confusion matrix:\n%s" % metrics.confusion_matrix(rw_labels, xgb_rw_predict)) 97 | 98 | # Reducing dimension of data to two principal components for visualization 99 | pca = PCA(n_components=2) 100 | train_pca = pca.fit_transform(training_set) 101 | 102 | # create a mesh to plot in 103 | h = .1 # step size in the mesh 104 | x_min, x_max = train_pca[:, 0].min() - 1, train_pca[:, 0].max() + 1 105 | y_min, y_max = train_pca[:, 1].min() - 1, train_pca[:, 1].max() + 1 106 | xx, yy = np.meshgrid(np.arange(x_min, x_max, h), 107 | np.arange(y_min, y_max, h)) 108 | 109 | for i, clf in enumerate((svm_model, forest_model, xgb_model)): 110 | # Plot the decision boundary. For that, we will assign a color to each 111 | # point in the mesh [x_min, x_max]x[y_min, y_max]. 112 | plt.subplot(1, 3, i + 1) 113 | plt.subplots_adjust(wspace=0.4, hspace=0.4) 114 | 115 | Z = clf.predict(pca.inverse_transform(np.c_[xx.ravel(), yy.ravel()])) 116 | 117 | # Put the result into a color plot 118 | Z = Z.reshape(xx.shape) 119 | plt.contourf(xx, yy, Z, cmap=plt.cm.coolwarm, alpha=0.8) 120 | 121 | # Plot also the training points 122 | plt.scatter(train_pca[:, 0], train_pca[:, 1], c=training_labels, 123 | cmap=plt.cm.coolwarm) 124 | plt.xlabel('PCA 1') 125 | plt.ylabel('PCA 2') 126 | plt.xlim(xx.min(), xx.max()) 127 | plt.ylim(yy.min(), yy.max()) 128 | 129 | plt.show() 130 | 131 | input_dim = image_size[0]*image_size[1] 132 | encoding_dim = 2 133 | 134 | # Reducing dimension of data using autoencoder 135 | input_img = Input(shape=(input_dim,)) 136 | encoded = Dense(encoding_dim, activation='relu')(input_img) 137 | decoded = Dense(input_dim, activation='sigmoid')(encoded) 138 | autoencoder = Model(input_img, decoded) 139 | encoder = Model(input_img, encoded) 140 | encoded_input = Input(shape=(encoding_dim,)) 141 | decoder_layer = autoencoder.layers[-1] 142 | decoder = Model(encoded_input, decoder_layer(encoded_input)) 143 | 144 | autoencoder.compile(optimizer='adam', loss='binary_crossentropy') 145 | 146 | autoencoder.fit(training_set, training_set, 147 | epochs=2000, batch_size=25, shuffle=True, verbose=0) 148 | 149 | encoded_imgs = encoder.predict(training_set) 150 | 151 | # create a mesh to plot in 152 | h = .1 # step size in the mesh 153 | x_min, x_max = encoded_imgs[:, 0].min(), encoded_imgs[:, 0].max() 154 | y_min, y_max = encoded_imgs[:, 1].min(), encoded_imgs[:, 1].max() 155 | xx, yy = np.meshgrid(np.arange(x_min, x_max, h), 156 | np.arange(y_min, y_max, h)) 157 | 158 | for i, clf in enumerate((svm_model, forest_model, xgb_model)): 159 | # Plot the decision boundary. For that, we will assign a color to each 160 | # point in the mesh [x_min, x_max]x[y_min, y_max]. 161 | plt.subplot(1, 3, i + 1) 162 | plt.subplots_adjust(wspace=0.4, hspace=0.4) 163 | 164 | Z = clf.predict(decoder.predict(np.c_[xx.ravel(), yy.ravel()])) 165 | 166 | # Put the result into a color plot 167 | Z = Z.reshape(xx.shape) 168 | plt.contourf(xx, yy, Z, cmap=plt.cm.coolwarm, alpha=0.8) 169 | 170 | # Plot also the training points 171 | plt.scatter(encoded_imgs[:, 0], encoded_imgs[:, 1], c=training_labels, 172 | cmap=plt.cm.coolwarm) 173 | plt.xlabel('Auto 1') 174 | plt.ylabel('Auto 2') 175 | plt.xlim(xx.min(), xx.max()) 176 | plt.ylim(yy.min(), yy.max()) 177 | 178 | plt.show() 179 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/07_Flux diagram Q-SVM.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peterwittek/qml-rg/998841a9c6d4fb8630c814221b91048eeba8d0fd/Archiv_Session_Spring_2017/Exercises/07_Flux diagram Q-SVM.pdf -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/07_Initialize_Q_state.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import qutip as qt 3 | import timeit 4 | 5 | """ 6 | This file creates the K matrix from any given dataset vector [a,b,c,...] and creates the quantum state for a given label vector 7 | [1,-1,-1,1,...] 8 | The idea will be to encode the images into a 4-dim vector and apply the Q-SVM from the paper from week6. 9 | """ 10 | 11 | 12 | class Oracle(): # construct quantum state for data and label 13 | 14 | def __init__(self, data, label, **kwargs): 15 | 16 | self.M = len(data) # number of training data 17 | self.N = len(data[0]) # length of training vectors 18 | if len(data) != len(label): 19 | exit('Error: not same number of data and labels!') 20 | self.norms = [] 21 | self.qstates = [] 22 | # Prepare quantum states of training data 23 | # Very important for states x and labels y is that the fock state |0> is not 24 | # used to encode the classical data --> indices start at 1 not 0!! 25 | for i in data: 26 | i = np.array(i) 27 | norm = np.linalg.norm(i) 28 | self.state = 1 / norm * \ 29 | sum([i[k - 1] * qt.fock(self.N + 1, k) 30 | for k in range(1, self.N + 1)]) 31 | self.qstates.append(self.state.unit()) # save quantum states 32 | self.norms.append(norm) # save classical norms 33 | # make qstate for label vector 34 | self.qlabels = 1 / (np.linalg.norm(label)) * sum( 35 | [label[i - 1] * qt.fock(self.M + 1, i) for i in range(1, self.M + 1)]) 36 | 37 | # construct oracle operator for training 38 | self.Train_Oracle = sum([qt.tensor(self.norms[i - 1] * self.qstates[i - 1] * qt.fock(self.M + 1, 0).dag( 39 | ), qt.fock(self.M + 1, i) * qt.fock(self.M + 1, i).dag()) for i in range(1, self.M + 1)]) 40 | 41 | 42 | def kernel(train_oracle, qstat, m, n): # m is number of datasets, n the length of data vector 43 | 44 | #chi = 1/(np.linalg.norm(norm))*sum([norm[i]*qt.tensor(qt.fock(m,i),qstat[i]) for i in range(0,m)]) 45 | chi = train_oracle * \ 46 | sum([qt.tensor(qt.fock(m + 1, 0), qt.fock(m + 1, i)) 47 | for i in range(1, m + 1)]) 48 | # Generate chi by applying Oracle to sum(|i>|0>) 49 | 50 | #--------------------------------------------------- 51 | # Try partial trace manually. 52 | def operator(k): 53 | return qt.tensor(qt.qeye(n + 1), qt.fock(m + 1, k)) 54 | chidens = chi * chi.dag() 55 | 56 | trace = sum([operator(i).dag() * chidens * operator(i) 57 | for i in range(n)]) 58 | #------------------------------------------------------------------- 59 | # Qutip buildt in function for ptrace 60 | # all other components than the 0-th are traced out! 61 | trace2 = chidens.ptrace(0) 62 | 63 | return trace2 64 | 65 | 66 | # constructs the K matrix to the right shape. m is the number of training 67 | # sets, gamma is the parameter from the paper 68 | def matrix_construct(matrix, m, gamma): 69 | J = np.zeros((m + 1, m + 1), dtype=complex) 70 | for i in range(1, m + 1): 71 | J[0][i] = 1 72 | for i in range(1, m + 1): 73 | J[i][0] = 1 74 | K = np.zeros((m + 1, m + 1), dtype=complex) 75 | gam_matrix = np.zeros((m + 1, m + 1), dtype=complex) 76 | for i in range(1, m + 1): 77 | for j in range(1, m + 1): 78 | K[i][j] = matrix[i - 1][0][j - 1] 79 | gam_matrix[i][i] = 1 / gamma 80 | 81 | return J, K, gam_matrix 82 | 83 | 84 | #-------------------------------------------------------------------------- 85 | training_set = [[1, 0.3, 0.85, 1], [1, 1, 1, 0], 86 | [0, 1, 1, 1]] # fictive training set 87 | label_set = [-1, 1, 1] # fictive training labels 88 | a = Oracle(training_set, label_set) 89 | # a.Train_Oracle 90 | K = kernel(a.Train_Oracle, a.qstates, a.M, a.N) 91 | j, k, gamma = matrix_construct(K, a.M, 2) 92 | w, v = np.linalg.eig(k) 93 | print(v) 94 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/07_image_loader_and_encoder.py: -------------------------------------------------------------------------------- 1 | import math 2 | import os 3 | from skimage import io 4 | from skimage import transform as tf 5 | from matplotlib import pyplot as plt 6 | from skimage.transform import resize 7 | import numpy as np 8 | """This code loads the images from the APS capture and encodes them with a neural network 9 | in a lower dimensional vector. 10 | For 20 hidden layers and 1000 epochs the encoding is already quite good. (This can be seen by 11 | comparing the original and the encoded-decoded image) 12 | To get to even lower dimensions, we manually lower the dimension in the end. 13 | """ 14 | #---------------------------------------------------------------------------- 15 | # Load Images 16 | 17 | 18 | def load_images(folder): 19 | images = [] 20 | labels = [] 21 | for file in os.listdir(folder): 22 | if file.endswith(".png"): 23 | images.append(io.imread(folder + file)) 24 | if file.find("einstein") > -1 or file.find("curie") > -1: 25 | labels.append(1) 26 | else: 27 | labels.append(0) 28 | return images, labels 29 | 30 | #---------------------------------------------------------------------------- 31 | # Resize Images 32 | 33 | 34 | def prep_datas(xset, xlabels): 35 | X = list(xset) 36 | for i in range(len(X)): 37 | # reduce the size of the image from 100X100 to 32X32. Also flattens the 38 | # color levels 39 | X[i] = resize(X[i], (32, 32, 1)) 40 | X = [np.reshape(x, (1024,)) for x in X] # reshape list (1024,) is crucial 41 | Y = xlabels 42 | return X, Y 43 | 44 | #------------------------------------------------------------------------------ 45 | # Load and reshape Images with image_loader.py 46 | training_set, training_labels = load_images("images/train/") 47 | test_set, test_labels = load_images("images/test/") 48 | 49 | resize_set, resize_labels = prep_datas(training_set, training_labels) 50 | resize_test_set, resize_test_labels = prep_datas(test_set, test_labels) 51 | # print(resize_set) 52 | #--------------------------------------------------------------- 53 | # very ugly way to bring vectors to the right shape for SVC fit() 54 | a = [] 55 | for x in resize_set: 56 | a.append(x.tolist()) 57 | X = a 58 | 59 | from keras.models import Model, Sequential 60 | from keras.layers import Input, Dense 61 | from keras import regularizers 62 | 63 | #---------------------------------------------------------------- 64 | # Nice code from Keras example 65 | encoding_dim = 100 66 | 67 | input_img = Input(shape=(1024,)) 68 | # add a Dense layer with a L1 activity regularizer 69 | encoded = Dense(encoding_dim, activation='relu')(input_img) 70 | decoded = Dense(1024, activation='sigmoid')(encoded) 71 | 72 | #---------------------------------------------------------------- 73 | # this model paps the input to the reconstruction 74 | autoencoder = Model(input_img, decoded) 75 | #---------------------------------------------------------------- 76 | # this model maps an input to its encoded representation 77 | encoder = Model(input_img, encoded) 78 | 79 | #---------------------------------------------------------------- 80 | # And the decoder 81 | # create a placeholder for an encoded (n-dimensional) input 82 | encoded_input = Input(shape=(encoding_dim,)) 83 | # retrieve the last layer of the autoencoder model 84 | decoder_layer = autoencoder.layers[-1] 85 | # create the decoder model 86 | decoder = Model(encoded_input, decoder_layer(encoded_input)) 87 | #------------------------------------------------------------------- 88 | # This is now the real training 89 | autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy') 90 | autoencoder.fit(X, X, epochs=1000, batch_size=10) 91 | 92 | autoencoder.save("save_model.h5") # save model 93 | 94 | #-------------------------------------------------------------------- 95 | # To use the model in the end we need these two functions 96 | # encode and decode some digits 97 | # note that we take them from the *test* set 98 | 99 | encoded_imgs = encoder.predict(X) 100 | decoded_imgs = decoder.predict(encoded_imgs) 101 | 102 | #--------------------------------------------------------------------- 103 | # NOW compare resized (32x32) image with encoded image 104 | # reshape vectors again to images 105 | decoded_imgs = [np.reshape(x, (32, 32)) for x in decoded_imgs] 106 | original_imgs = [np.reshape(x, (32, 32)) 107 | for x in X] # reshape vectors again to images 108 | 109 | for i in range(len(X)): 110 | decoded = np.asarray(decoded_imgs[i]) 111 | original = np.asarray(original_imgs[i]) 112 | plt.subplot(2, 1, 1) 113 | plt.imshow(decoded.squeeze(), cmap='gray') 114 | plt.subplot(2, 1, 2) 115 | plt.imshow(original.squeeze(), cmap='gray') 116 | plt.show() 117 | 118 | #------------------------------------------------------------------------ 119 | # The vector encoded_imgs now is the data we need for the SVD! 120 | # encoding_dim can be changed, depending on how many input dimensions we 121 | # want for the SVD 122 | 123 | # We can also reduce the dimensions again manually 124 | outputdim = 4 # what final size do we want 125 | length = len(encoded_imgs) # how many pictures 126 | output = [] 127 | 128 | for k in range(length): 129 | encoded = encoded_imgs[k] # which picture do we want to compress? 130 | list = [] 131 | for i in range(outputdim): 132 | list.append( 133 | sum([x for x in encoded[i:(i + int(encoding_dim / outputdim))]])) 134 | output.append(list) 135 | print(output) 136 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/07_image_reduce_and_classical_svm.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """Created on Wed Mar 15 10:09:24 2017""" 3 | import os 4 | import numpy as np 5 | from skimage import io 6 | from matplotlib import pyplot as plt 7 | from skimage.transform import resize 8 | from sklearn import svm, metrics 9 | from sklearn.decomposition import PCA 10 | from keras.models import Model 11 | from keras.layers import Input, Dense 12 | 13 | 14 | def load_images(folder): 15 | images = [] 16 | labels = [] 17 | for file in os.listdir(folder): 18 | if file.endswith(".png"): 19 | images.append(io.imread(folder + file, as_grey=True)) 20 | if file.find("einstein") > -1: 21 | labels.append(1) 22 | elif file.find("curie") > -1: 23 | labels.append(2) 24 | elif os.path.splitext(file)[0].isdigit(): 25 | labels.append(int(os.path.splitext(file)[0])) 26 | else: 27 | labels.append(0) 28 | return images, labels 29 | 30 | 31 | def prep_images(images, size): 32 | for i in range(len(images)): 33 | images[i] = resize(images[i], size) 34 | images[i] = np.reshape(images[i], size[0] * size[1]) 35 | if np.amax(images[i]) != 0: 36 | images[i] = images[i] / np.amax(images[i]) 37 | return np.array(images) 38 | 39 | 40 | # Loading image data sets 41 | training_set, training_labels = load_images("images/train/") 42 | test_set, test_labels = load_images("images/test/") 43 | rw_set, rw_file_labels = load_images("images/real_world/") 44 | training_orig = training_set[:] 45 | test_orig = test_set[:] 46 | rw_orig = rw_set[:] 47 | # Resize images to same dimensions, normalize the grey scale and return vectors 48 | image_size = (100, 100) 49 | training_set = prep_images(training_set, image_size) 50 | test_set = prep_images(test_set, image_size) 51 | rw_set = prep_images(rw_set, image_size) 52 | # Sort real world set by number in file name 53 | rw_set = np.array([x for (y, x) in sorted(zip(rw_file_labels, rw_set))]) 54 | # Get labels for real world set from file 55 | f = open('images/real_world/labels.txt', "r") 56 | lines = f.readlines() 57 | rw_labels = [] 58 | for x in lines: 59 | rw_labels.append(int((x.split(' ')[1]).replace('\n', ''))) 60 | f.close() 61 | # Reduce labels to only two categories 62 | training_labels = np.array(training_labels) 63 | training_labels[training_labels > 0] = 1 64 | rw_labels = np.array(rw_labels) 65 | rw_labels[rw_labels > 0] = 1 66 | 67 | # Now compress images in various ways for input into SVM 68 | # First method: skimage transform resize 69 | compress_size = (2, 2) 70 | training_resize = prep_images(training_orig, compress_size) 71 | test_resize = prep_images(test_orig, compress_size) 72 | 73 | # Second method: PCA 74 | pca = PCA(n_components=compress_size[0] * compress_size[1]) 75 | training_pca = pca.fit_transform(training_set) 76 | test_pca = pca.transform(test_set) 77 | pca_norm = np.amax(training_pca) 78 | training_pca = training_pca / pca_norm # normalizing for better SVM 79 | test_pca = test_pca / pca_norm 80 | 81 | # Third method: autoencoder 82 | input_dim = image_size[0] * image_size[1] 83 | encoding_dim = compress_size[0] * compress_size[1] 84 | input_img = Input(shape=(input_dim,)) 85 | encoded = Dense(encoding_dim, activation='relu')(input_img) 86 | decoded = Dense(input_dim, activation='sigmoid')(encoded) 87 | autoencoder = Model(input_img, decoded) 88 | encoder = Model(input_img, encoded) 89 | encoded_input = Input(shape=(encoding_dim,)) 90 | decoder_layer = autoencoder.layers[-1] 91 | decoder = Model(encoded_input, decoder_layer(encoded_input)) 92 | 93 | autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy') 94 | 95 | autoencoder.fit(training_set, training_set, 96 | epochs=2000, batch_size=10, verbose=0) 97 | 98 | training_auto = encoder.predict(training_set) 99 | test_auto = encoder.predict(test_set) 100 | auto_norm = np.amax(training_auto) 101 | training_auto = training_auto / auto_norm # normalizing for better SVM 102 | test_auto = test_auto / auto_norm 103 | 104 | # Support vector machine with resized images 105 | svm_resize = svm.SVC(kernel='rbf', C=5000) 106 | svm_resize.fit(training_resize, training_labels) 107 | 108 | svm_resize_predict = svm_resize.predict(test_resize) 109 | 110 | print("\nSVM with resized images\n") 111 | print("Training set confusion matrix:\n%s" 112 | % metrics.confusion_matrix(training_labels, 113 | svm_resize.predict(training_resize))) 114 | print("Test set confusion matrix:\n%s" 115 | % metrics.confusion_matrix(test_labels, 116 | svm_resize_predict)) 117 | 118 | # SVM with data reduced by PCA 119 | svm_pca = svm.SVC(kernel='rbf', C=5000) 120 | svm_pca.fit(training_pca, training_labels) 121 | 122 | svm_pca_predict = svm_pca.predict(test_pca) 123 | 124 | print("\nSVM with images reduced by PCA\n") 125 | print("Test set confusion matrix:\n%s" 126 | % metrics.confusion_matrix(training_labels, 127 | svm_pca.predict(training_pca))) 128 | print("Test set confusion matrix:\n%s" 129 | % metrics.confusion_matrix(test_labels, 130 | svm_pca_predict)) 131 | 132 | # SVM with data reduced by autoencoder 133 | svm_auto = svm.SVC(kernel='rbf', C=10000) 134 | svm_auto.fit(training_auto, training_labels) 135 | 136 | svm_auto_predict = svm_auto.predict(test_auto) 137 | 138 | print("\nSVM with images reduced by autoencoder\n") 139 | print("Test set confusion matrix:\n%s" 140 | % metrics.confusion_matrix(training_labels, 141 | svm_auto.predict(training_auto))) 142 | print("Test set confusion matrix:\n%s" 143 | % metrics.confusion_matrix(test_labels, 144 | svm_auto_predict)) 145 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/07_qutipHHL.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "ExecuteTime": { 8 | "end_time": "2017-04-27T07:45:59.499157Z", 9 | "start_time": "2017-04-27T09:45:58.999933+02:00" 10 | }, 11 | "collapsed": true, 12 | "deletable": true, 13 | "editable": true 14 | }, 15 | "outputs": [], 16 | "source": [ 17 | "from __future__ import print_function, division\n", 18 | "import qutip as qt\n", 19 | "import numpy as np\n", 20 | "import scipy.linalg as sp\n", 21 | "import math\n", 22 | "import cmath\n", 23 | "π = math.pi" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": null, 29 | "metadata": { 30 | "ExecuteTime": { 31 | "end_time": "2017-04-27T07:45:59.515068Z", 32 | "start_time": "2017-04-27T09:45:59.500688+02:00" 33 | }, 34 | "collapsed": true, 35 | "deletable": true, 36 | "editable": true 37 | }, 38 | "outputs": [], 39 | "source": [ 40 | "def preprocess(mat, vec):\n", 41 | "\n", 42 | " if mat.shape[0] != mat.shape[1] or mat.shape[0] != vec.shape[0]:\n", 43 | " raise Exception(\"Matrix A should be square and b should have a matching dimension!\")\n", 44 | " \n", 45 | " if not mat.isherm:\n", 46 | " zero_block = np.zeros(mat.shape)\n", 47 | " mat = qt.Qobj(np.bmat([[zero_block, mat.full()] , \n", 48 | " [mat.dag().full(), zero_block]]))\n", 49 | " vec = qt.Qobj(np.hstack([b_init.full().flatten(), zero_block[0]]))\n", 50 | " \n", 51 | " ### Normalise b and remember normalisation factor\n", 52 | " if vec.norm() != 1:\n", 53 | " nf = vec.norm()\n", 54 | " vec = vec / nf\n", 55 | " else:\n", 56 | " nf = 1\n", 57 | "\n", 58 | " return mat, vec, nf" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": null, 64 | "metadata": { 65 | "ExecuteTime": { 66 | "end_time": "2017-04-27T07:45:59.544150Z", 67 | "start_time": "2017-04-27T09:45:59.516961+02:00" 68 | }, 69 | "collapsed": true, 70 | "deletable": true, 71 | "editable": true 72 | }, 73 | "outputs": [], 74 | "source": [ 75 | "def qft(N):\n", 76 | " mat = 1 / math.sqrt(N) * qt.Qobj([[cmath.exp(1j * 2 * π * i * j / N)\n", 77 | " for i in range(N)] for j in range(N)])\n", 78 | " return mat\n", 79 | "\n", 80 | "def angle(k, t0, C):\n", 81 | " return math.asin(- C * t0 /(2 * π * k)) # Sign is for appropriate phases in the rotation, but should not affect the solution\n", 82 | "\n", 83 | "\n", 84 | "def rot(k, t0, C):\n", 85 | " return qt.Qobj([[math.cos(angle(k, t0, C)), math.sin(angle(k, t0, C))],\n", 86 | " [- math.sin(angle(k, t0, C)), math.cos(angle(k, t0, C))]])\n", 87 | "\n", 88 | "# def inv(Quantobj):\n", 89 | "# mat = Quantobj.full()\n", 90 | "# matinv = np.linalg.inv(mat)\n", 91 | "# invQobj = qt.Qobj(matinv)\n", 92 | "# invQobj.dims = Quantobj.dims\n", 93 | "# return invQobj" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": { 100 | "ExecuteTime": { 101 | "end_time": "2017-04-27T07:45:59.572331Z", 102 | "start_time": "2017-04-27T09:45:59.546005+02:00" 103 | }, 104 | "collapsed": true, 105 | "deletable": true, 106 | "editable": true 107 | }, 108 | "outputs": [], 109 | "source": [ 110 | "A_init = qt.Qobj([[0.2, 0.1],[0.1, 0.2]])\n", 111 | "b_init = qt.Qobj([[0.2] , [0.7]])\n", 112 | "prec = 1000 # Choose the dimension of the ancillary state\n", 113 | "\n", 114 | "# Produce a Hermitian matrix A given the problem matrix A_int\n", 115 | "# and a unit-length vector b given the problem vector b_int \n", 116 | "A, b, nf1 = preprocess(A_init, b_init)\n", 117 | "\n", 118 | "eigenvals, eigenvecs = A.eigenstates() # The eigendecomposition of A\n", 119 | "\n", 120 | "# Condition number, of use for estimating constants\n", 121 | "κ = eigenvals.max() / eigenvals.min()\n", 122 | "\n", 123 | "# Additive error achieved in the estimation of |x>, of use for estimating constants\n", 124 | "ϵ = 0.2" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": { 130 | "deletable": true, 131 | "editable": true 132 | }, 133 | "source": [ 134 | "### STEP 1: STATE PREPARATION OF b\n", 135 | "[HHL: page 2, column 2, center]\n", 136 | "\n", 137 | "Write b in the eigenbasis of A" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": null, 143 | "metadata": { 144 | "ExecuteTime": { 145 | "end_time": "2017-04-27T07:45:59.587948Z", 146 | "start_time": "2017-04-27T09:45:59.574509+02:00" 147 | }, 148 | "collapsed": true, 149 | "deletable": true, 150 | "editable": true 151 | }, 152 | "outputs": [], 153 | "source": [ 154 | "# Create the matrix that diagonalizes A\n", 155 | "diagonalizer = qt.Qobj(np.array([eigenvecs[i].full().T.flatten()\n", 156 | " for i in range(len(eigenvals))]))\n", 157 | "b = diagonalizer * b\n", 158 | "A = diagonalizer.dag() * A * diagonalizer" 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": { 164 | "deletable": true, 165 | "editable": true 166 | }, 167 | "source": [ 168 | "### STEP 2: QUANTUM SIMULATION AND QUANTUM PHASE ESTIMATION\n", 169 | "[HHL: page 2, column 2, bottom]" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": null, 175 | "metadata": { 176 | "ExecuteTime": { 177 | "end_time": "2017-04-27T07:45:59.820937Z", 178 | "start_time": "2017-04-27T09:45:59.589888+02:00" 179 | }, 180 | "collapsed": false, 181 | "deletable": true, 182 | "editable": true 183 | }, 184 | "outputs": [], 185 | "source": [ 186 | "T = prec\n", 187 | "t0 = κ / ϵ # It should be O(κ/ϵ), whatever that means\n", 188 | "ψ0 = qt.Qobj([[math.sqrt(2 / T) * math.sin(π * (τ + 0.5) / T)] \n", 189 | " for τ in range(T)])\n", 190 | "\n", 191 | "# Order is b, τ, and then ancilla\n", 192 | "evo = qt.tensor(qt.identity(A.shape[0]), qt.ket2dm(qt.basis(T, 0))) \n", 193 | "\n", 194 | "for τ in range(1, T):\n", 195 | " evo += qt.tensor((1j * A * τ * t0 / T).expm(), qt.ket2dm(qt.basis(T, τ)))\n", 196 | "\n", 197 | "ψev = evo * qt.tensor(b, ψ0)\n", 198 | "\n", 199 | "ftrans = qt.tensor(qt.identity(b.shape[0]), qft(T))\n", 200 | "\n", 201 | "ψfourier = ftrans * ψev # This is Eq. 3 in HHL" 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": {}, 207 | "source": [ 208 | "### STEP 2-1: Making true the assumption of perfect phase estimation" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": null, 214 | "metadata": { 215 | "collapsed": false 216 | }, 217 | "outputs": [], 218 | "source": [ 219 | "# w = (ψfourier[:T] / b[0]).argmax()\n", 220 | "# prj = qt.ket2dm(qt.basis(T, w))\n", 221 | "\n", 222 | "# ψfourier = qt.tensor(qt.identity(b.shape[0]), prj) * ψfourier" 223 | ] 224 | }, 225 | { 226 | "cell_type": "markdown", 227 | "metadata": { 228 | "deletable": true, 229 | "editable": true 230 | }, 231 | "source": [ 232 | "### STEP 3-1: Conditional Rotation of Ancilla" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": null, 238 | "metadata": { 239 | "ExecuteTime": { 240 | "end_time": "2017-04-27T07:45:59.837373Z", 241 | "start_time": "2017-04-27T09:45:59.822534+02:00" 242 | }, 243 | "collapsed": false, 244 | "deletable": true, 245 | "editable": true 246 | }, 247 | "outputs": [], 248 | "source": [ 249 | "total_state = qt.tensor(ψfourier, qt.basis(2, 0)) # Add ancilla for swapping\n", 250 | "\n", 251 | "C = 1 / κ # Constant, should be O(1/κ)\n", 252 | "\n", 253 | "# Do conditional rotation only on τ and ancilla\n", 254 | "rotation = qt.tensor(qt.ket2dm(qt.basis(T, 0)), qt.identity(2)) \n", 255 | "\n", 256 | "for τ in range(1, T):\n", 257 | " rotation += qt.tensor(qt.ket2dm(qt.basis(T, τ)), rot(τ, t0, C))\n", 258 | " \n", 259 | "final_state = qt.tensor(qt.identity(b.shape[0]), rotation) * total_state" 260 | ] 261 | }, 262 | { 263 | "cell_type": "markdown", 264 | "metadata": { 265 | "deletable": true, 266 | "editable": true 267 | }, 268 | "source": [ 269 | "### STEP 3-2: Post-selection on $\\left|1\\right\\rangle$ on the ancilla register\n", 270 | "We perform the post-selection by projecting onto the desired ancilla state and later tracing out the ancilla and the $\\left|\\psi_0\\right\\rangle$ registers." 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": null, 276 | "metadata": { 277 | "ExecuteTime": { 278 | "end_time": "2017-04-27T07:45:59.921419Z", 279 | "start_time": "2017-04-27T09:45:59.874610+02:00" 280 | }, 281 | "collapsed": false, 282 | "deletable": true, 283 | "editable": true, 284 | "scrolled": false 285 | }, 286 | "outputs": [], 287 | "source": [ 288 | "projector = qt.tensor(qt.identity(b.shape[0]), qt.identity(T), qt.ket2dm(qt.basis(2, 1)))\n", 289 | "postsel = projector * final_state\n", 290 | "prob1 = qt.expect(projector, final_state)\n", 291 | "\n", 292 | "# Trace out ancilla and τ registers, leaving only the b register\n", 293 | "finalstate = qt.ket2dm(postsel).ptrace([0]) / prob1\n", 294 | "finalstate.eigenenergies()" 295 | ] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "metadata": { 300 | "deletable": true, 301 | "editable": true 302 | }, 303 | "source": [ 304 | "$\\left|finalstate\\right\\rangle$ is essentially a pure state (it should be if all the process was perfect), so now we just isolate that main part, that is our $\\left|x\\right\\rangle$ (after a transformation if our original A was not Hermitian) in the basis that diagonalizes $A$." 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": null, 310 | "metadata": { 311 | "ExecuteTime": { 312 | "end_time": "2017-04-27T07:45:59.922294Z", 313 | "start_time": "2017-04-27T07:45:59.047Z" 314 | }, 315 | "collapsed": false, 316 | "deletable": true, 317 | "editable": true, 318 | "scrolled": false 319 | }, 320 | "outputs": [], 321 | "source": [ 322 | "fsevls, fsevcs = finalstate.eigenstates()\n", 323 | "x = math.sqrt(fsevls.max()) * fsevcs[fsevls.argmax()]\n", 324 | "x" 325 | ] 326 | }, 327 | { 328 | "cell_type": "markdown", 329 | "metadata": { 330 | "deletable": true, 331 | "editable": true 332 | }, 333 | "source": [ 334 | "This state is supposed to be $\\left|x\\right\\rangle=A^{-1}\\left|b\\right\\rangle=\\sum_j \\beta_j\\lambda_j^{-1}\\left|u_j\\right\\rangle$, although I don't understand why it has complex numbers.\n", 335 | "\n", 336 | "IMPORTANT NOTE: If A_init is not Hermitian, the output $\\left|x\\right\\rangle$ has dimension double than the input b_init. It is to be interpreted as the representation of the vector (0, x) in the basis that diagonalizes $C = [[0,\\,A], [A^{\\dagger},\\, 0]]$" 337 | ] 338 | }, 339 | { 340 | "cell_type": "markdown", 341 | "metadata": {}, 342 | "source": [ 343 | "Eq. (A26) in HHL is wrong, but Eq. (A27) is right. The global sign in Eq. (A29) is wrong, as well as the $\\sqrt{2}$ (it should be in the denominator). The problem with the $\\sqrt{2}$ carries until Eq. (A31)" 344 | ] 345 | } 346 | ], 347 | "metadata": { 348 | "kernelspec": { 349 | "display_name": "Python 3", 350 | "language": "python", 351 | "name": "python3" 352 | }, 353 | "language_info": { 354 | "codemirror_mode": { 355 | "name": "ipython", 356 | "version": 3 357 | }, 358 | "file_extension": ".py", 359 | "mimetype": "text/x-python", 360 | "name": "python", 361 | "nbconvert_exporter": "python", 362 | "pygments_lexer": "ipython3", 363 | "version": "3.5.2" 364 | } 365 | }, 366 | "nbformat": 4, 367 | "nbformat_minor": 2 368 | } 369 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/10_cifar_classifier.py: -------------------------------------------------------------------------------- 1 | '''Very Simple Cifar Classifier 2 | ''' 3 | 4 | # System 5 | from __future__ import print_function 6 | import numpy as np 7 | import os 8 | from PIL import Image 9 | from tools import CifarLoader 10 | 11 | #Keras 12 | from keras.datasets import mnist 13 | from keras.models import Sequential 14 | from keras.datasets import cifar10 15 | #from keras.regularizers import WeightRegularizer, ActivityRegularizer 16 | from keras.layers.core import Dense, Dropout, Activation, Flatten 17 | from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D, AveragePooling2D 18 | from keras.layers.normalization import BatchNormalization 19 | from keras.utils import np_utils 20 | from keras.optimizers import SGD 21 | from keras.preprocessing.image import ImageDataGenerator 22 | from keras.callbacks import BaseLogger, Callback, CSVLogger 23 | 24 | from matplotlib import pyplot as plt 25 | 26 | import math 27 | import os 28 | from matplotlib import pyplot as plt 29 | import numpy as np 30 | from six.moves import cPickle 31 | 32 | class classifier(): 33 | def __init__(self, **kwargs): 34 | 35 | cats = [] 36 | dogs = [] 37 | cat_lab = [] 38 | dog_lab = [] 39 | #load all batches 40 | for i in range(1,nr_batch+1): 41 | new_data = CifarLoader('CIFAR10/Images/data_batch_'+str(i)) 42 | cats = new_data.cats + cats 43 | dogs = new_data.dogs + dogs 44 | cat_lab = new_data.cats_label + cat_lab 45 | dog_lab = new_data.dogs_label + dog_lab 46 | 47 | X_train = np.array(cats + dogs) 48 | Y_train = np.array(cat_lab + dog_lab) 49 | 50 | #---------------------------------------------------------------- 51 | #Neural Network 52 | 53 | from keras import backend as K 54 | K.set_image_dim_ordering('th') 55 | model = Sequential() 56 | 57 | model.add(Convolution2D(32, 3, 3, border_mode='same',input_shape=(img_channels, img_rows, img_cols))) 58 | model.add(Activation('relu')) 59 | model.add(Convolution2D(32, 3, 3)) 60 | model.add(Activation('relu')) 61 | model.add(MaxPooling2D(pool_size=(2, 2))) 62 | #model.add(Dropout(0.25)) 63 | 64 | model.add(Convolution2D(64, 3, 3, border_mode='same')) 65 | model.add(Activation('relu')) 66 | model.add(Convolution2D(64, 3, 3)) 67 | model.add(Activation('relu')) 68 | model.add(MaxPooling2D(pool_size=(2, 2))) 69 | model.add(Dropout(0.25)) 70 | 71 | model.add(Flatten()) 72 | model.add(Dense(512)) 73 | model.add(Activation('relu')) 74 | model.add(Dropout(0.5)) 75 | model.add(Dense(nb_classes)) 76 | model.add(Activation('softmax')) 77 | 78 | sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) 79 | model.compile(loss=loss_function, 80 | optimizer=sgd, 81 | metrics=['accuracy']) 82 | 83 | X_train = X_train.astype('float32') 84 | 85 | X_train /= 255 86 | model.fit(X_train, Y_train, 87 | batch_size=32, epochs=nb_epoch) 88 | model.save('my_model_relabel_.h5') 89 | model.save_weights('my_model_weights_relabel_.h5') 90 | #----------------------------------------------------------------------------- 91 | 92 | loss_function = 'categorical_crossentropy' 93 | 94 | nr_batch = 1 #how many batches of cifar shall be included (1 to 5 possible) 95 | nb_classes = 10 96 | nb_epoch = 100 97 | data_augmentation = True 98 | 99 | # input image dimensions 100 | img_rows, img_cols = 32, 32 101 | # the CIFAR10 images are RGB 102 | img_channels = 3 103 | 104 | classifier() 105 | 106 | 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/11_Markov_random_field.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# QML - RG Homework 11: Markov Random Fields\n", 8 | "### Alejandro Pozas-Kerstjens" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "metadata": { 15 | "collapsed": false, 16 | "deletable": true, 17 | "editable": true 18 | }, 19 | "outputs": [], 20 | "source": [ 21 | "from skimage import io\n", 22 | "from skimage.transform import resize\n", 23 | "from functools import reduce # To do multiple-argument multiplications\n", 24 | "import numpy as np\n", 25 | "from numpy.linalg import norm\n", 26 | "import matplotlib.pyplot as plt" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": {}, 32 | "source": [ 33 | "### Pre-processing: Loading, resizing, normalizing and binarizing the image" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 2, 39 | "metadata": { 40 | "collapsed": false 41 | }, 42 | "outputs": [ 43 | { 44 | "data": { 45 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQ8AAAD8CAYAAABpXiE9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADdVJREFUeJzt3W2sZVV9x/HvrzPDECIIOI1Mh1EgndoqNQVvAKUxk4Ip\nTAzTRJrACwEjudVKqo1NSjTRhKQp+sKmBCKZABEaA6Rg8G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DB/2of361bsE+StcDb\nWd2nZZetqaperKrXusXrgA+sYj3LWckcjlSN+PWI5V7XYAxztBqvkEzSacsMcHHXvhj49vwOSY5J\nsr5rb2Du6db5/9+QYTwIbElyYpLDmLsgOv+OzmCd5wP3VHfFaZUsW9O88+XzmDunHZcZ4KLujsIZ\nwL6B09GxGOXrEd04S76uwYjnaCU1Nc3RKK5Ar/CK8DuAfwOeAe4Gju3WTwHXde0PAY8xd8fhMeCT\nq1DHNuauRj8LfLFbdwVwXtc+HPhXYBb4L+CkEczNcjX9I/BENy/3An+4irXcDOwF3mDuXP2TwKeA\nT3XbA1zT1foYMDWC+VmupssG5ud+4EOrWMufAgU8CuzqPtvGOUcrrOmA58jH0yU1maTTFkkHEcND\nUhPDQ1ITw0NSE8NDUhPDQ1ITw0NSk/8Hq3f09PDbfSEAAAAASUVORK5CYII=\n", 46 | "text/plain": [ 47 | "" 48 | ] 49 | }, 50 | "metadata": {}, 51 | "output_type": "display_data" 52 | } 53 | ], 54 | "source": [ 55 | "einstein = io.imread('einstein.png')\n", 56 | "\n", 57 | "einstein = einstein / einstein.max()\n", 58 | "\n", 59 | "size = 3 # Choosing bigger sizes gives problems when computing the partition function\n", 60 | "\n", 61 | "einstein = resize(einstein, (size, size), mode='constant')\n", 62 | "\n", 63 | "def binarize(pixel):\n", 64 | " if pixel > 0.5:\n", 65 | " return 1\n", 66 | " elif pixel <= 0.5:\n", 67 | " return -1\n", 68 | "\n", 69 | "vfunc = np.vectorize(binarize)\n", 70 | "\n", 71 | "einst = vfunc(einstein)\n", 72 | "plt.imshow(einst)\n", 73 | "plt.show()\n", 74 | "\n", 75 | "training_set = np.array([einst])" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "### Compute positive phase terms (constant for every iteration)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 3, 88 | "metadata": { 89 | "collapsed": false, 90 | "deletable": true, 91 | "editable": true 92 | }, 93 | "outputs": [], 94 | "source": [ 95 | "hpos = np.zeros(training_set[0].shape)\n", 96 | "hpos = 1 / len(training_set) * np.sum(training_set, axis=0)\n", 97 | "\n", 98 | "neighbors = 3 # Number of sites we are going to consider as nearest neighbors\n", 99 | "\n", 100 | "Jpos = np.zeros((size, size , neighbors, neighbors))\n", 101 | "\n", 102 | "for i in range(size):\n", 103 | " for j in range(size):\n", 104 | " for k in range(neighbors):\n", 105 | " for l in range(neighbors):\n", 106 | " if ((i + k > size - 1) | (j + l > size - 1)) | ((k == 0) & (l == 0)):\n", 107 | " , # Condition to avoid correlations between top and bottom rows, or left and right columns\n", 108 | " else:\n", 109 | " Jpos[i][j][k][l] = np.sum(training_set[:, i + k, j + l]\n", 110 | " * training_set[:, i, j], axis=0) / len(training_set)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "NOTE: The structure of J is funny. The first two labels $(i,j)$ denote each spin. The two second ones $(j,k)$ denote the neighbors of the corresponding spin, i.e., spin $(i+k,j+l)$. Self-energies $J_{i,j,0,0}$ are not taken into account. Only neighbors to the right and down need to be stored due to the symmetry of the interaction." 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": {}, 123 | "source": [ 124 | "### Functions needed for computing the negative phase terms" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 4, 130 | "metadata": { 131 | "collapsed": true 132 | }, 133 | "outputs": [], 134 | "source": [ 135 | "# Initialize parameters\n", 136 | "\n", 137 | "htrained = 2 * np.random.rand(size, size) - np.ones(training_set[0].shape)\n", 138 | "Jtrained = np.zeros(Jpos.shape)\n", 139 | "Jtrained[abs(Jpos) > 0] = 2 * np.random.rand() - 1 # Funny way to initialize only relevant cells in J\n", 140 | "\n", 141 | "def potential(z, h): # Potential energy of a configuration\n", 142 | " return np.sum(np.multiply(h, z))\n", 143 | "def interactions(z, J): # Interaction energy of a configuration\n", 144 | " return np.sum(np.array([J[i][j][k][l] * z[i][j] * z[i + k][j + l] for i in range(size)\n", 145 | " for j in range(size) for k in range(size - i) for l in range(size - j)]))\n", 146 | "def P(z, h, J, norm): # Thermal distribution of configurations with temperature=1\n", 147 | " return np.exp(-np.sum(potential(z, h)) - np.sum(interactions(z, J))) / norm\n", 148 | "def mse(a, b): # Mean squared error (used during training to assess convergence)\n", 149 | " return ((a - b) ** 2).mean()\n", 150 | "\n", 151 | "# Generate all possible configurations of spins for a given size\n", 152 | "m = size ** 2\n", 153 | "d = np.array(range(2 ** m))\n", 154 | "allconfs = (((d[:,None] & (1 << np.arange(m)))) > 0).astype(int).reshape(2 ** m, size, size)\n", 155 | "allconfs = 2 * allconfs - np.ones(allconfs.shape) # Change 0s by -1s" 156 | ] 157 | }, 158 | { 159 | "cell_type": "markdown", 160 | "metadata": {}, 161 | "source": [ 162 | "### Training" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 5, 168 | "metadata": { 169 | "collapsed": false, 170 | "deletable": true, 171 | "editable": true, 172 | "scrolled": true 173 | }, 174 | "outputs": [ 175 | { 176 | "name": "stdout", 177 | "output_type": "stream", 178 | "text": [ 179 | "Iterations done: 12\n", 180 | "Training complete\n" 181 | ] 182 | } 183 | ], 184 | "source": [ 185 | "hpre = np.zeros(htrained.shape)\n", 186 | "Jpre = np.zeros(Jtrained.shape)\n", 187 | "\n", 188 | "a = 1 # For debugging and iterations counting\n", 189 | "ϵ = 10 ** (-8) # Precision of results\n", 190 | "while (mse(hpre, htrained) > ϵ) | (mse(Jpre, Jtrained) > ϵ):\n", 191 | " hpre = htrained\n", 192 | " Jpre = Jtrained\n", 193 | " norm = np.sum([np.exp(-np.sum(potential(conf, htrained)) - np.sum(interactions(conf, Jtrained))) for conf in allconfs])\n", 194 | "\n", 195 | " # Compute negative phase terms\n", 196 | " hneg = np.zeros(training_set[0].shape)\n", 197 | " for i in range(size):\n", 198 | " for j in range(size):\n", 199 | " hneg[i][j] = (np.sum([P(z, htrained, Jtrained, norm) for z in allconfs[allconfs[:, i, j]==1]])\n", 200 | " - np.sum([P(z, htrained, Jtrained, norm) for z in allconfs[allconfs[:, i, j]==-1]]))\n", 201 | "\n", 202 | " Jneg = np.zeros(Jpos.shape)\n", 203 | " for i in range(size):\n", 204 | " for j in range(size):\n", 205 | " for k in range(neighbors):\n", 206 | " for l in range(neighbors):\n", 207 | " if ((i + k > size - 1) | (j + l > size - 1)) | ((k == 0) & (l == 0)):\n", 208 | " ,\n", 209 | " else:\n", 210 | " Jneg[i][j][k][l] = (np.sum([P(z, htrained, Jtrained, norm) for z in allconfs[(allconfs[:, i, j]==1) & (allconfs[:, i + k, j + l]==1)]])\n", 211 | " + np.sum([P(z, htrained, Jtrained, norm) for z in allconfs[(allconfs[:, i, j]==-1) & (allconfs[:, i + k, j + l]==-1)]])\n", 212 | " - np.sum([P(z, htrained, Jtrained, norm) for z in allconfs[(allconfs[:, i, j]==-1) & (allconfs[:, i + k, j + l]==1)]])\n", 213 | " - np.sum([P(z, htrained, Jtrained, norm) for z in allconfs[(allconfs[:, i, j]==1) & (allconfs[:, i + k, j + l]==-1)]]))\n", 214 | " \n", 215 | " # Update parameters\n", 216 | " htrained = htrained + hpos - hneg\n", 217 | " Jtrained = Jtrained + Jpos - Jneg\n", 218 | " \n", 219 | " # Idea to keep the parameters in [-1, 1].\n", 220 | " # If there is a parameter outside [-1, 1], normalize all by the highest value\n", 221 | " if any(abs(x) > 1 for x in np.ndarray.flatten(htrained)) | any(abs(x) > 1 for x in np.ndarray.flatten(Jtrained)):\n", 222 | " hm = abs(htrained).max()\n", 223 | " Jm = abs(Jtrained).max()\n", 224 | " mx = np.array([hm, Jm]).max()\n", 225 | " htrained = htrained / mx\n", 226 | " Jtrained = Jtrained / mx\n", 227 | " \n", 228 | " # Have a check that everything is running (in a fancy way :P)\n", 229 | " print(\"Iterations done: %i\" % a, end='\\r')\n", 230 | " a += 1\n", 231 | "print(\"\\nTraining complete\") " 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "metadata": {}, 237 | "source": [ 238 | "### Sampling" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 6, 244 | "metadata": { 245 | "collapsed": false, 246 | "deletable": true, 247 | "editable": true 248 | }, 249 | "outputs": [ 250 | { 251 | "data": { 252 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQ8AAAD8CAYAAABpXiE9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADd5JREFUeJzt3W+sZVV9xvHv0zsDxIgCM0SmwyCQUlvbaoEbQG0MKZgC\nMUwTaYIvFIzmViupNpqUaIIJSVP1hU2tREKACI0BUjBwbTAEClSbBmQgA8NAkAtvmHEqONhBgoUO\n/fXF3ZDj5f6bdfY95wx+P8nJWXvvdfb6sYY87L9MqgpJOlC/Ne4CJB2cDA9JTQwPSU0MD0lNDA9J\nTQwPSU2GCo8kRyW5M8mT3feRS/R7Ncn27jM7zJiSJkOGec4jydeB56vqq0kuBY6sqr9dpN+LVfXW\nIeqUNGGGDY8ngDOrak+STcC9VfWuRfoZHtKbzLDh8d9VdUTXDvCL15YX9NsPbAf2A1+tqluX2N8M\nMAMwxdSpb+FtzbW92f3ue14adwl6E3jwkZd/XlVHt/x2xfBIchdwzCKbvgxcNxgWSX5RVW+47pFk\nc1XtTnIicDdwVlU9tdy4b8tRdXrOWs0/w2+kO366fdwl6E1gatPcg1U13fLbdSt1qKqzl9qW5GdJ\nNg2ctjy7xD52d99PJ7kXOBlYNjwkTbZhb9XOAhd17YuA2xZ2SHJkkkO79kbgA8BjQ44racyGDY+v\nAh9K8iRwdrdMkukkV3d9fh/YluRh4B7mr3kYHtJBbsXTluVU1V7gDRcmqmob8Kmu/Z/AHw0zjqTJ\n4xOmkpoYHpKaGB6SmhgekpoYHpKaGB6SmhgekpoYHpKaGB6SmhgekpoYHpKaGB6SmhgekpoYHpKa\nGB6SmhgekpoYHpKaGB6SmhgekpoYHpKaGB6Smhgek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npfV1HQx8K0R\n/Tl9EDgFeHSJ7ecBPwACnAHcPwE1nQn864jmZxNwStc+HPjJIn9eI52jVdZ0wHM0MUcewFbguq59\nHfDnY6jhNGCuqp6uqleAG7u6Bg3WeTNw1mu3ocdY08hU1Q+B55fpshW4vubdBxyRZNOYaxqZWt3r\nGiOdo1XWdMAmKTzeUVV7uvZ/Ae9Yot9hSbYluS9J3wGzGXhmYHkXb5zk1/tU1X5gH7Ch5zoOtCaA\nj3SHwDcn2bKG9axktfWO2vuSPJzkB0n+YBQDLvO6xtjmaDWvkKx2jnp9t2UlSe4Cjllk05cHF6qq\nkix1D/mdVbU7yYnA3Ul2VNVTfdd6kPk+cENVvZzkL5k/MvrTMdc0SR5i/t+b116PuBVY9vWIYXWv\na9wCfL6qXljLsVZrhZoOeI5GeuRRVWdX1R8u8rkN+Nlrh27d97NL7GN39/00cC/zKdqX3cDgf7WP\n7dYt2ifJOuDtrO3TsivWVFV7q+rlbvFq4NQ1rGclq5nDkaoRvx6x0usajGGO1uIVkkk6bZkFLura\nFwG3LeyQ5Mgkh3btjcw/3brw/xsyjAeAk5KckOQQ5i+ILryjM1jnBcDd1V1xWiMr1rTgfPl85s9p\nx2UW+Hh3R+EMYN/A6ehYjPL1iG6cZV/XYMRztJqamuZoFFegV3lFeAPwb8CTwF3AUd36aeDqrv1+\nYAfzdxx2AJ9cgzrOY/5q9FPAl7t1lwPnd+3DgH9h/p34HwMnjmBuVqrp74Gd3bzcA/zeGtZyA7AH\n+F/mz9U/CXwa+HS3PcAVXa07gOkRzM9KNV0yMD/3Ae9fw1r+BCjgEWB79zlvnHO0ypoOeI58PF1S\nk0k6bZF0EDE8JDUxPCQ1MTwkNTE8JDUxPCQ1MTwkNfl/7y76IvLKjYIAAAAASUVORK5CYII=\n", 253 | "text/plain": [ 254 | "" 255 | ] 256 | }, 257 | "metadata": {}, 258 | "output_type": "display_data" 259 | } 260 | ], 261 | "source": [ 262 | "## Given some partial image, we are going to choose the most probable configuration\n", 263 | "\n", 264 | "keep = [0] # Rows that we want to keep from the original image\n", 265 | "\n", 266 | "norm = np.sum([np.exp(-np.sum(potential(conf, htrained)) - np.sum(interactions(conf, Jtrained))) for conf in allconfs])\n", 267 | "possible = np.array([[z, P(z, htrained, Jtrained, norm)]\n", 268 | " for z in allconfs[reduce(np.multiply,[np.prod(allconfs[:, i]==einst[i], axis=1) for i in keep]).astype(bool)]])\n", 269 | "\n", 270 | "tru = possible[:, 0][np.argmax(possible[:,1])]\n", 271 | "\n", 272 | "plt.imshow(tru)\n", 273 | "plt.show()" 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "Well, it's not particularly good, but I think we cannot expect much more from a 3x3 image" 281 | ] 282 | } 283 | ], 284 | "metadata": { 285 | "kernelspec": { 286 | "display_name": "Python 3", 287 | "language": "python", 288 | "name": "python3" 289 | }, 290 | "language_info": { 291 | "codemirror_mode": { 292 | "name": "ipython", 293 | "version": 3 294 | }, 295 | "file_extension": ".py", 296 | "mimetype": "text/x-python", 297 | "name": "python", 298 | "nbconvert_exporter": "python", 299 | "pygments_lexer": "ipython3", 300 | "version": "3.5.2" 301 | } 302 | }, 303 | "nbformat": 4, 304 | "nbformat_minor": 2 305 | } 306 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/README.md: -------------------------------------------------------------------------------- 1 | This is the folder where we collect the solutions. Functions that we use in several solutions are gathered in `tools.py`. Otherwise, every solution is prefixed by the respective meeting. 2 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/aps_captcha_images.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peterwittek/qml-rg/998841a9c6d4fb8630c814221b91048eeba8d0fd/Archiv_Session_Spring_2017/Exercises/aps_captcha_images.zip -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/brain.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peterwittek/qml-rg/998841a9c6d4fb8630c814221b91048eeba8d0fd/Archiv_Session_Spring_2017/Exercises/brain.h5 -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Exercises/tools.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """Created on Wed Mar 15 10:09:24 2017""" 3 | import math 4 | import os 5 | import numpy as np 6 | from matplotlib import pyplot as plt 7 | from six.moves import cPickle 8 | from skimage import io 9 | from skimage import transform as tf 10 | from skimage.transform import resize 11 | 12 | #---------------------------------------------------------------------------- 13 | #Load Images 14 | def load_images(folder): 15 | images = [] 16 | labels = [] 17 | for file in os.listdir(folder): 18 | if file.endswith(".png"): 19 | images.append(io.imread(folder + file)) 20 | if file.find("einstein") > -1 or file.find("curie") > -1: 21 | labels.append(1) 22 | else: 23 | labels.append(0) 24 | return images, labels 25 | 26 | #---------------------------------------------------------------------------- 27 | #Resize Images 28 | def prep_datas(xset,xlabels): 29 | X=list(xset) 30 | 31 | for i in range(len(X)): 32 | X[i]=resize(X[i],(32,32,1)) #reduce the size of the image from 100X100 to 32X32. Also flattens the color levels 33 | print(np.shape(X)) 34 | X=[np.reshape(x, (1024,)) for x in X] #reshape list (1024,) is crucial. 35 | print(np.shape(X)) 36 | print('data',X) 37 | Y = xlabels 38 | return X,Y 39 | 40 | #---------------------------------------------------------------------------- 41 | #Deshear function to make pictures "straight" 42 | def deshear(filename): 43 | image = io.imread(filename) 44 | distortion = image.shape[1] - image.shape[0] 45 | shear = tf.AffineTransform(shear=math.atan(distortion/image.shape[0])) 46 | return tf.warp(image, shear)[:, distortion:] 47 | 48 | #----------------------------------------------------------------------------- 49 | #Load CIFAR10 images 50 | class CifarLoader(): 51 | """IMPORTANT: The data array contains 3072 bytes, where the first 1024 are for 52 | red, the next green and the last blue. 53 | """ 54 | def __init__(self, path, **kwargs): 55 | f = open(path, 'rb') 56 | datadict = cPickle.load(f,encoding='latin1') 57 | f.close() 58 | X = datadict["data"] 59 | #print(X) 60 | Y = datadict['labels'] 61 | X = X.reshape(10000,3, 32, 32).astype("float") 62 | self.X = X 63 | Y_Neuron = Y 64 | Y = np.array(Y) 65 | 66 | #------------------------------------------------------------------ 67 | #Separate Cats and Dogs, get indices of the animals 68 | cat_label = 3 #cifar labels cat with 3 69 | cat_indices = [] 70 | 71 | for i in range(0,len(Y)): 72 | if Y[i] == cat_label: 73 | cat_indices.append(i) 74 | 75 | dog_label = 5 #cifar labels dog with 5 76 | dog_indices = [] 77 | 78 | for i in range(0,len(Y)): 79 | if Y[i] == dog_label: 80 | dog_indices.append(i) 81 | 82 | #make lists of cat and dog images 83 | #and list of the labels 84 | #to be able to generalize to other cifar images I relabel 3 --> [0,0,0,1,0,0...] 85 | #and 5 to [0,0,0,0,0,1,0..] 86 | self.cats = [] 87 | self.dogs = [] 88 | self.cats_label = [] 89 | self.dogs_label = [] 90 | for i in cat_indices: 91 | self.cats.append(X[i]) 92 | self.cats_label.append([0,0,0,1,0,0,0,0,0,0]) 93 | for i in dog_indices: 94 | self.dogs.append(X[i]) 95 | self.dogs_label.append([0,0,0,0,0,1,0,0,0,0]) 96 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Tutorials/Git.md: -------------------------------------------------------------------------------- 1 | Introduction 2 | ============ 3 | 4 | If you are not using a version control system, you are making your life complicated. Manuscripts, code, and pretty much everything you produce as a scientist should go under a version control system. The simplest way to think to think about it is that a version control system gives you an unlimited undo history. Instead of naming your files whatever_version1.tex, ..., whatever_version678623_2.tex, a version control system allows you record each edit and compare them easily. The second advantage is that it makes collaboration very easy. The current most widespread version control system is git, which was originally developed for streamlining the workflow on the Linux kernel, and it is still used for that, and in about ten million other projects. 5 | 6 | Contemporary version control systems, including git, are distributed. This means that you always work on the local copy of a project (called a *repository* in git). If you are working alone on a manuscript or a bit of code, that is fine. If you want to collaborate with others, you need a host for your repository. GitHub is the biggest such host for open source projects, but, for instance, Overleaf, an online collaborative editor for TeX files, also uses git under the hood. 7 | 8 | In the grand scheme of things, I see git and GitHub as enabling technologies to pursue open science. Credit is properly attributed, licences can be clarified, and it is easy to pitch in. If your work includes computational bits, you can easily create a repository with the details and share them as a computational appendix that you can link from the paper itself. This ensure the reproduceability of your results and lowers the barrier for others to jump on the research topics that you are interested in. 9 | 10 | We structure this tutorial along the following topics: 11 | 12 | 1. Using git as an annotated, unlimited undo history, with possibility for parallel branches and merges between them. 13 | 14 | 2. Using git for collaboration: hosting a repository, cloning, pushing, pulling, conflict resolution. 15 | 16 | 3. Overleaf, where git and LaTeX meet for great benefit human race. 17 | 18 | 4. GitHub, open source projects, forking, public repositories, pull requests. 19 | 20 | 5. Best practices. 21 | 22 | The tutorial is, in principle, agnostic to the operating system that you use. 23 | 24 | 25 | Global configuration 26 | ==================== 27 | Before you start, you must have a GitHub account. Technically you do not need a GitHub account for using git, but the way we configure, we assume the existence of an account for reasons that will become clear later. Assuming that you are Jordi Campesinyo, and your GitHub account is jordicampesinyo, do this: 28 | 29 | ```bash 30 | git config --global user.email "jordicampesinyo@users.noreply.github.com" 31 | git config --global user.name "Jordi Campesinyo" 32 | ``` 33 | 34 | While we are at it, log in to GitHub, and go to Settings->Emails. Check the box "Keep my email address private." We set the above email address to match the anonymous email address here. This connection ensures that your future commits will be credited to you. 35 | 36 | ICFO special 37 | ============ 38 | We all love port restrictions! It is a stroke of luck that IT provides us with extensive port blocking. To be able to use git like a normal human being, you have to add a configuration setting. Locate your `.gitconfig` file. On Linux and Mac, it should be in `$HOME/.gitconfig`. On Windows, it should be `C:\Users\MyLogin`, or something along those lines. You can query the actual location with `git config --list --show-origin`. Once you got it, open it in a text editor, and append these two lines to the end of the file: 39 | 40 | ``` 41 | [url "https://github.com/"] 42 | insteadOf = git://github.com/ 43 | ``` 44 | Fetching upstream should work now. Rejoice. 45 | 46 | Git as an unlimited undo history 47 | ================================ 48 | Create a folder called `whatever` and a file in it called `anything.txt`. In this folder, open a terminal a command prompt. Initialize your git repository and add this new file to track: 49 | 50 | ```bash 51 | git init 52 | git add anything.txt 53 | ``` 54 | 55 | From now on, git tracks all changes done to `anything.txt`. Add some text to this file in your favourite text editor and save it. Then make a *commit*: 56 | 57 | ```bash 58 | git commit -am "Added initial text" 59 | ``` 60 | 61 | The message that you add is basically in annotation in the history of your edits. How frequently you make commits, that is, how frequently you add these labels, is up to you. Note that until a commit is made, your changes are not saved to the history that git maintains. 62 | 63 | You can view the history of your edits with this command: 64 | 65 | ```bash 66 | git log 67 | ``` 68 | 69 | If you want to see what exactly changed between edits, you can do this: 70 | 71 | ```bash 72 | git log -p -1 73 | ``` 74 | 75 | where `-1` refers to the length of history you wish to review. 76 | 77 | **Exercise 1**. Edit the text a bit more, make one more commit, and review the changes in the last two commits. 78 | 79 | It often happens that you want to explore a parallel idea that may or may not make it to the final version of whatever you are working on. It could be a section in a manuscript or some tricky bit of coding. To help working with these explorations, git allows you to have parallel histories by creating *branches*. Let us create a new branch and switch to it: 80 | 81 | ```bash 82 | git branch Curses 83 | git checkout Curses 84 | ``` 85 | 86 | Now every commit you make will go to this branch alone. Add some curses to `anything.txt`, and commit: 87 | 88 | ```bash 89 | git commit -am "Some fine curses added" 90 | ``` 91 | 92 | Now switch back to the main branch: 93 | 94 | ```bash 95 | git checkout master 96 | ``` 97 | 98 | If you look at the text file or the commit history, there is no sign of curses. You can continue editing undisturbed if you want to. Then, at any point in time, you can go back to the curses branch: 99 | 100 | ```bash 101 | git checkout Curses 102 | ``` 103 | 104 | If you finally made up your mind that the curses should be a part of your text, you can *merge* this branch with master: 105 | 106 | ```bash 107 | git merge master Curses 108 | ``` 109 | 110 | Now if you go back to master, you will see the merge: 111 | 112 | ```bash 113 | git checkout master 114 | git log 115 | ``` 116 | 117 | 118 | Collaboration: An example through Overleaf 119 | ========================================== 120 | Go to [Overleaf](https://overleaf.com/) and hit "Create a New Paper." No registration is necessary. You will be directed to a new LaTeX manuscript, and the URL will be something similar to this one: 121 | 122 | [https://www.overleaf.com/8253629qgvxqbvqvyzc#/29202944/](https://www.overleaf.com/8253629qgvxqbvqvyzc#/29202944/) 123 | 124 | You can start sending around this link to co-authors to work on the manuscript collaboratively. But this is not want we want. This new manuscript [is actually a git repository](https://www.overleaf.com/blog/195-new-collaborate-online-and-offline-with-overleaf-and-git-beta). You can *clone* it by removing everything from the link above starting with `#`, and changing `www` to `git`: 125 | 126 | ```bash 127 | git clone https://git.overleaf.com/8253629qgvxqbvqvyzc 128 | ``` 129 | 130 | Cloning means you make a local copy of the repository. You can rename the cloned repository to anything. In this case, it has the name `8253629qgvxqbvqvyzc`, but you can call it "The best Nature-candidate manuscript ever". It does not matter. Change into the directory. From the directory, you can do everything you did in the previous section: add text, commit, and branch. 131 | 132 | How do you make your changes available to others? You *push* your commits: 133 | 134 | ```bash 135 | git push 136 | ``` 137 | 138 | How do you receive the changes others made? You *pull* their commits (that they already pushed): 139 | 140 | ```bash 141 | git pull 142 | ``` 143 | 144 | If someone pushed before you, git will ask you to pull first anyway. 145 | 146 | Pull actually has two phases: *fetching* and merging. Fetch means bringing the changes to your local copy of the repository, and merging is the same as when you were merging branches. A pull works fine if there is no conflict. If there is, you have to resolve it. The strength of git is the efficiency of automatic conflict resolution and the help it gives you for manual resolutions. 147 | 148 | **Exercise 2**. Work in pairs on the same Overleaf repository. Create a clone each, and create a conflicting edit by making changes on the same line. Push your local changes and try pulling. Resolve the conflict. 149 | 150 | You can manually separate pulling into its two phases, which is often necessary when many people work on the same project: 151 | 152 | ```bash 153 | git fetch 154 | git merge 155 | ``` 156 | 157 | Trick: technically, Overleaf is for LaTeX projects. The repositories are not public: you must know the URL to access it. This makes them semi-private. You can add arbitrary files to repository (e.g. Python scripts) and use it as it would any other git repository. It is a cheap way to work on secretive projects with others. 158 | 159 | GitHub 160 | ====== 161 | This is the real deal. GitHub is the best place to host open source projects and collaborate on them. It made contributions easy, introducing the idea of "social coding." The downside: all repositories are public, unless you pay. This is why the trick with Overleaf is handy for giving you a free private repository. 162 | 163 | We need to introduce three more concepts. The first one is *forking*: it means that you copy somebody else's repository. It is like the branches before, except that branches are within a local copy of a repository, whereas forking duplicates repositories. Then *upstream* is the original repository that you forked. If you make changes to your fork and want to merge them back to upstream, you send a *pull request* (PR). Forking and sending PRs happen on GitHub, not on the git command line. 164 | 165 | If you have not done it yet, go to [https://github.com/peterwittek/qml-rg](https://github.com/peterwittek/qml-rg) and hit fork. Creating a fork is exactly that complicated. Now clone it to have a local copy of **your fork**, and not the upstream: 166 | 167 | ```bash 168 | git clone https://github.com/jordicampesinyo/qml-rg 169 | ``` 170 | 171 | So far so good. Now you can make edits, and so and so forth. If you are happy with your changes, and your would like to send a PR, first check if your future PR can be merged. To do this, you must keep track of the upstream, in this case: 172 | 173 | ```bash 174 | git remote add upstream git://github.com/peterwittek/qml-rg.git 175 | ``` 176 | Note that this command must be run in the project's directory. Otherwise, you will receive a *fatal* error. 177 | 178 | Then, before every PR, fetch and merge with the upstream: 179 | 180 | ``` 181 | git fetch upstream 182 | git merge upstream/master master 183 | ``` 184 | 185 | If all is well, push to your fork and send a PR on GitHub. 186 | 187 | **Exercise 3.** Send a PR that can be merged. 188 | 189 | GitHub encourages conversations. If you have a question regarding a project, you can always [open an issue](https://github.com/peterwittek/qml-rg/issues). Furthermore, you can comment on just about everything: PRs, specific commits, and so on. 190 | 191 | Best practices 192 | ============== 193 | 194 | - In LaTeX files, keep every sentence a new line. This ensures easy conflict resolution. 195 | 196 | - If you like or use an open source project on GitHub, give it a star. It means a lot to the developers, both in terms of self-glorification, and also in terms of job prospects. The GitHub profile is an integral part of a computer science CV, and it is akin to your academic citation index. 197 | 198 | - Never track generated files. For instance, don't track the .aux file of LaTeX stuff. Repositories are to keep track of the source files, not of the junk or of the results. 199 | 200 | - Tracking binary files that are subject to change is a bad idea. Git keeps track of relative changes as opposed to snapshots, and tracking binary files has a huge overhead. 201 | 202 | - Do make the details of your computations available if you publish a paper. You can link a repository or a notebook from your manuscript, and also from the comment section of the arXiv page. 203 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Tutorials/Kaggle.md: -------------------------------------------------------------------------------- 1 | Introduction 2 | ============ 3 | Kaggle made learning data science less boring. Its sole purpose used to be hosting competitions, but it evolved into an engaging platform to learn about data science, compete for jobs, it provides a great way to interact with data scientists who actually know it better (and also with ones who think they know it better). If you have a shortage of opportunities to interact with complete morons, Kaggle is also here to help. Kaggle gamified the learning process via a progression system, so you have yet another pointless indicator to track, apart from your i10 and h5 indices. 4 | 5 | Today Kaggle has a documentation problem: they rolled out features fast, and the system is confusing for beginners. There is a [wiki](https://www.kaggle.com/wiki/Home) and a blog aptly named [No Free Hunch](http://blog.kaggle.com/), but they are not especially well structured for newcomers. In this tutorial, we will go through the various features, and promote you from rookie to less rookie. 6 | 7 | A new problem 8 | ============= 9 | Kaggle [joined](http://blog.kaggle.com/2017/03/08/kaggle-joins-google-cloud/) Google Cloud. Since then, many things broke and I conjecture that even more data is stolen about your self. The most notable new annoyance is that the Kaggle interface now often freezes Firefox (at least with version 53, but possibly with others too). Use an alternative browser if you can. 10 | 11 | Progression system 12 | ================== 13 | The Kaggle [progression system](https://www.kaggle.com/progression) makes you feel like a karate champion. Registration earns you a Novice rank, but Contibutor is just a few clicks away. Unfortunately, [it shows](https://www.kaggle.com/rankings): about 78% of the Kagglers are contributors, and the next level is definitely more than a few clicks away. Above Contributor, your progress is divided into categories, and the overall tier is your maximum among all categories. 14 | 15 | Let's go through the easy steps: 16 | 17 | - Add your bio: slave traders sold you to a quantum theory group. 18 | 19 | - Add your location: the boondocks. 20 | 21 | - Add your occupation: quantum whateveratician. 22 | 23 | - Add your organization: ICFO-光子科学研究所. 24 | 25 | - SMS verify your account: this is needed because real money is involved in the competitions, and because of the importance of the rankings in seeking a job. Kaggle rules say there should be one and only one human per account. 26 | 27 | - Cast 1 upvote: click on anything you fancy. 28 | 29 | Datasets 30 | ======== 31 | Kaggle hosts a mesmerizing array of small and medium-sized datasets, mainly contributed by users. There is everything from [food facts](https://www.kaggle.com/openfoodfacts/world-food-facts) through [song lyrics](https://www.kaggle.com/mousehead/songlyrics) to [Game of Thrones](https://www.kaggle.com/mylesoneill/game-of-thrones). At least four Pokémon datasets also await your data science prowess. If you look hard enough, there is also a [collection](https://www.kaggle.com/peterwittek/scirate-quant-ph) on papers published in quant-ph. 32 | 33 | A common problem with the datasets is that somehow it is always the part missing that you want to investigate. A good example is the IMDB collection: there isn't a single entry with Kevin Bacon. How are you going to investigate the impact of the Bacon number on box office performance then? These datasets are mainly for fooling around and demonstrating algorithms. For any non-trivial work, you probably want to scrape your own data or enter a competition, where datasets were compiled by teams of experts. 34 | 35 | You can do two things with a dataset: (i) download it and run analysis it on locally; or (ii) run a kernel on it. The latter is more interesting, so we are discussing this next. 36 | 37 | The many meanings of the word 'kernel' 38 | ====================================== 39 | The word 'kernel' is gruesomely overloaded in maths and computer science. The basic uses that are most relevant to us are as follows: 40 | 41 | - The natural meaning: a kernel defines an integral transformation $(Tf)(x)=\int_{y{\in}Y}K(y,x)f(y)dy$. 42 | 43 | - Kernel methods like support vector machines, kernel PCA, or kernel k-means, but also Gaussian processes and kernel density estimators: the dot product between data points (or the correlations) can be replaced by a positive semidefinite integral kernel. The connection is via reproducing kernel Hilbert spaces. 44 | 45 | - Kernel of a linear map. 46 | 47 | - Computational kernel: part of a numerical algorithm that performs the computationally intensive part. 48 | 49 | - OS kernel: (i) the stuff that produces the blue screen of death on Windows; (ii) the thing that makes other operating systems work. 50 | 51 | - Jupyter kernel: language back-end for notebooks, e.g. Python, R, and Julia kernels. 52 | 53 | - Kernel on Kaggle: a script or a notebook run on the cloud infrastructure of Kaggle. They typically run associated with one or more datasets, but there are ones independent of actual data. The name stems from the use of the term in Jupyter. 54 | 55 | The cloud infrastructure for running kernels is pretty convenient since it lets you use an extensive set of Python libraries without having to install anything locally on their computer (e.g. Windows users can use XGBoost this way). On the other hand, the cloud instances are [severely limited](https://www.kaggle.com/wiki/Scripts): maximum execution time is ten minutes (forget deep learning), Internet access is fully blocked so you are confined to data and code that are already there, and the RAM is only 8GB. 56 | 57 | The next task is to run a kernel. The two straightforward ways to do it: 58 | 59 | - The easy way: go to kernels, pick one you like, fork it and run it. 60 | 61 | - The less easy way: hit "New Kernel" either under at a dataset or under Kernels, and start coding on your own. 62 | 63 | Competitions 64 | ============ 65 | It is surprisingly easy to enter a competition and get a decent ranking without doing much. The process is as follows: 66 | 67 | 1. Pick a competition with a low number of participants. 68 | 69 | 2. Look at the most upvoted discussion. It probably refers to code hosted somewhere with a high-scoring solution. 70 | 71 | 3. Download the code, run it on the competition files, and submit the results. 72 | 73 | I discovered this accidentally when the non-improved variant of a solution got me in the top 40% of the Julia competition (which nobody cared about). 74 | 75 | In any case, keep in mind that (i) the network in ICFO cannot handle files larger than zero bytes; and (ii) your computer will eventually process the data. Therefore competitions involving videos or images are not the best first targets, so don't go for the 95GB sea lion collection. The Russian Housing Market is more like it. This will also run on the cloud infrastructure: fork a kernel and run it there. 76 | 77 | When you first download the competition dataset, you must accept the conditions of the competition sponsor. This always includes sharing the code of the winning entry with at least the sponsor, but quite possibly an open source licence that permits commercial use. MIT license seems to be a safe bet. 78 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Tutorials/Qml_approaches.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peterwittek/qml-rg/998841a9c6d4fb8630c814221b91048eeba8d0fd/Archiv_Session_Spring_2017/Tutorials/Qml_approaches.png -------------------------------------------------------------------------------- /Archiv_Session_Spring_2017/Tutorials/summary_on_meeting_6.tex: -------------------------------------------------------------------------------- 1 | \RequirePackage[l2tabu,orthodox]{nag} 2 | \pdfoutput=1 3 | \pdfminorversion=3 4 | \documentclass[compress]{beamer}\usetheme{Warsaw}\usecolortheme{crane}\useoutertheme[subsection=false]{smoothbars} 5 | \setbeamertemplate{navigation symbols}{} 6 | \definecolor{links}{HTML}{2A1B81} 7 | \hypersetup{pdfpagemode=FullScreen,colorlinks,linkcolor=,urlcolor=links} 8 | \usepackage[english]{babel} 9 | \usepackage{amsmath} 10 | \usepackage{amssymb} 11 | \usepackage{latexsym} 12 | \usepackage{graphicx} 13 | \usepackage{xcolor} 14 | \usepackage{url} 15 | \usepackage{listings} 16 | \usepackage{multirow} 17 | \usepackage{subfigure} 18 | \usepackage{algorithmic} 19 | \usepackage{tikz} 20 | \usetikzlibrary{er} 21 | \usetikzlibrary{shadows} 22 | \tikzstyle{every attribute} = [top color=white, bottom color=yellow!40, drop shadow] 23 | 24 | \title[Initial Foray into ML and Quantum-Enhanced Protocols]{Initial Foray into Machine Learning and Quantum-Enhanced Protocols} 25 | 26 | \author{Quantum Machine Learning Reading Group @ ICFO} 27 | 28 | \date{30 March 2017} 29 | 30 | \subject{Talks} 31 | 32 | \begin{document} 33 | \frame[plain]{ 34 | \titlepage 35 | } 36 | 37 | \section{Classical Machine Learning} 38 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 39 | \begin{frame}{How do AI and machine learning relate?} 40 | \begin{itemize} 41 | \item Better not draw Venn diagrams\ldots 42 | \item Roughly: deep learning $\subset$ machine learning $\subset$ AI. 43 | \item There is a good deal of overlap between statistical inference and machine learning. 44 | \item Between optimal control theory and machine learning: reinforcement learning. 45 | \end{itemize} 46 | \end{frame} 47 | 48 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 49 | \begin{frame}{One possible categorization of schools of thought} 50 | \begin{itemize} 51 | \item Connectionism: neural networks and empirical risk minimization. 52 | \item Structural risk minimization: support vector machines, regularized boosting\ldots 53 | \item Probabilistic inference: Bayesian and Markov networks. 54 | \item Symbolic AI: first-order logic or other form of formal reasoning. 55 | \end{itemize} 56 | \end{frame} 57 | 58 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 59 | \begin{frame}{Statistics and sample complexity} 60 | \begin{itemize} 61 | \item Descriptive and \textbf{inferential} statistics. 62 | \item Assumptions derive from probability theory. 63 | \item Parameters enter through assumed probability distributions. 64 | \begin{itemize} 65 | \item It is often assumed that the data is generated by \textbf{certain probability distributions} described by a finite number of unknown parameters. 66 | \end{itemize} 67 | \item Law of large numbers. 68 | \begin{itemize} 69 | \item Concentration theorems. 70 | \end{itemize} 71 | \item Independent and identically distributed (IID) random variables. 72 | \item We can establish guarantees on accuracy based on the \textbf{sample size}. 73 | \item Example: metrology. 74 | \begin{itemize} 75 | \item Standard quantum limit: scales as $1/N$. 76 | \item Heisenberg limit: scales as $1/N^2$. 77 | \end{itemize} 78 | \end{itemize} 79 | \end{frame} 80 | 81 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 82 | \begin{frame}{Model complexity and parsimony} 83 | \begin{itemize} 84 | \item Aristotle (4th century B.C.): ``We may assume the superiority ceteris paribus [other things being equal] of the demonstration which derives from fewer postulates or hypotheses.'' 85 | \item Occam's razor (1300s): ``Among competing hypotheses, the one with the fewest assumptions should be selected.'' 86 | \item \textbf{Combinatorial explosion} in AI and uncertainty. The problem of \textbf{overfitting}. 87 | \item Number of free parameters is insufficient to characterize the learning capacity of a function family (see hyperplanes versus the sine function). 88 | \item Combinatorial measure of model complexity: VC dimension. 89 | \item Control of model complexity: via \textbf{regularization} of the cost function. We usually use a convex regularization term that we can solve efficiently classically ($l_2$-norm: XGBoost \href{https://arxiv.org/abs/1603.02754}{(Chen \& Guestrin, 2016)} and the least-squares formulation of support vector machines in \href{https://arxiv.org/abs/1307.0471}{Rebentrost, Mohseni \& Lloyd, 2014}). 90 | \end{itemize} 91 | \end{frame} 92 | 93 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 94 | \begin{frame}{The essence of statistical learning theory} 95 | Elements of balance: 96 | \begin{itemize} 97 | \item Sample complexity. 98 | \item Computational complexity. 99 | \item Model complexity. 100 | \end{itemize} 101 | \begin{block}{Generalization bounds} 102 | \footnotesize{ 103 | $P\left(E(h) \leq E_S(h) + F(\textrm{model complexity of function family }\mathcal{H}, N, \eta)\right)\geq 1-\eta$} 104 | \end{block} 105 | \begin{itemize} 106 | \item $\mathcal{H}$ is the function family we optimize over. 107 | \item $F$ goes to zero as $N\rightarrow\infty$. 108 | \item $F$ monotonically increases with model complexity. 109 | \item IID assumption. 110 | \item $E(h)$ is the error of the learned function $h$ over the whole distribution given the sample. 111 | \item $E_S$ is the error on the sample, the training error, the empirical risk. 112 | \end{itemize} 113 | \end{frame} 114 | 115 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 116 | \begin{frame}{What's an easy problem? What's a hard one?} 117 | From easier to harder: 118 | \begin{itemize} 119 | \item Supervised learning: this is where deep learning shines. See \href{http://doi.org/10.1038/nature14539}{LeCun et al., 2015}, \href{https://github.com/peterwittek/qml-rg\#meeting-5}{coding exercise for Meeting 5}. 120 | \item Semi-supervised learning. 121 | \item Unsupervised learning. E.g. autoencoders (\href{https://arxiv.org/abs/1612.01045}{Wan et al., 2016}; \href{https://github.com/peterwittek/qml-rg\#meeting-2}{coding exercise for Meeting 2}). 122 | \item Generative models. 123 | \item Reinforcement learning. See \href{http://doi.org/10.1038/nature16961}{Silver et al., 2016}, coding exercises for \href{https://github.com/peterwittek/qml-rg\#meeting-3}{Meetings 3} and \href{https://github.com/peterwittek/qml-rg\#meeting-4}{4}. 124 | \end{itemize} 125 | \end{frame} 126 | 127 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 128 | \begin{frame}{Why does deep learning work?} 129 | \begin{itemize} 130 | \item It has an enormous model complexity: the generalization bound does not seem to be tight. 131 | \item But: 132 | \begin{itemize} 133 | \item Massive amounts of labeled data are available, so $N$ is large. With \emph{data augmentation} (see coding exercises for \href{https://github.com/peterwittek/qml-rg\#meeting-5}{Meetings 5} and \href{https://github.com/peterwittek/qml-rg\#meeting-6}{6}), $N$ can be increased further. 134 | \item Massively parallel computational resources and matching toolkits became common place (see \href{https://arxiv.org/abs/1603.04467}{Abadi et al., 2016}; coding exercise for \href{https://github.com/peterwittek/qml-rg\#meeting-2}{Meeting 2} and all subsequent task with DL). Furthermore, research is extensive on improving optimization techniques for deep architectures (\href{https://arxiv.org/abs/1412.6980}{Kingma \& Ba, 2014}). 135 | \end{itemize} 136 | \item The layers take care of feature engineering that used to be the primary preoccupation of data scientists of yore (then called data miners). Compare the results you got with the various learning algorithms on the APS captcha exercise. 137 | \item Also: deep networks can be regularized (see the upcoming Meeting 7). 138 | \end{itemize} 139 | \end{frame} 140 | 141 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 142 | \section{Quantum-Enhanced Learning} 143 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 144 | \begin{frame}{Quantum-enhanced protocols} 145 | \begin{figure} 146 | \includegraphics[height=.7\textheight]{Qml_approaches.png} 147 | \end{figure} 148 | Source: \href{https://en.wikipedia.org/wiki/Quantum_machine_learning}{Wikipedia article on Quantum Machine Learning} 149 | \end{frame} 150 | 151 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 152 | \begin{frame}{Adiabatic quantum optimization} 153 | Solve a nonconvex optimization directly through the adiabatic theorem. 154 | \begin{itemize} 155 | \item Nonconvex objective allows for regularization by 0-1 loss or $l_0$ norm. 156 | \item Quantum boosting (\href{https://www.google.com/googleblogs/pdfs/nips_demoreport_120709_research.pdf}{Neven et al., 2009}). 157 | \item The possibility is there for a better trade-off between sparsity and accuracy. 158 | \item The problem of embedding to the hardware: 159 | \begin{itemize} 160 | \item Representation width (1 bit as opposed to the usual 32-bit floating point numbers. It is a form of regularization. 161 | \item Limited batch size. 162 | \item Embedding the connectivity. 163 | \item Finite temperature, inaccuracy of couplings\ldots 164 | \end{itemize} 165 | \end{itemize} 166 | \end{frame} 167 | 168 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 169 | \begin{frame}{Sampling} 170 | Gate-based model: 171 | \begin{itemize} 172 | \item Quantum deep learning (\href{http://arxiv.org/abs/1412.3489}{Wiebe et al., 2014}) 173 | \begin{itemize} 174 | \item Learn stacked Boltzmann machines layer by layer. 175 | \item Uses classical mean-field approximation and efficient state preparation protocol. 176 | \end{itemize} 177 | \end{itemize} 178 | Via annealing: 179 | \begin{itemize} 180 | \item Upcoming paper on Meeting 7. 181 | \item This is how D-Wave is typically used today. 182 | \end{itemize} 183 | \end{frame} 184 | 185 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 186 | \begin{frame}{Harrow-Hassidim-Lloyd algorithm} 187 | \begin{itemize} 188 | \item Exponentially faster inversion of well-conditioned matrices (\href{https://arxiv.org/abs/0811.3171}{Harrow et al., 2009}). 189 | \item Applying it to machine learning started an avalanche. 190 | \item Quantum support vector machines based on the least-squares formulation (\href{https://arxiv.org/abs/1307.0471}{Rebentrost et al., 2014}). 191 | \begin{itemize} 192 | \item Exponential speedup with some caveats. 193 | \item Sparsity? 194 | \end{itemize} 195 | \item For continuous variables, see \href{http://arxiv.org/abs/1603.06222}{Lau et al., 2016}. 196 | \end{itemize} 197 | \end{frame} 198 | 199 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 200 | \begin{frame}{Reinforcement learning in a quantum environment} 201 | \href{https://arxiv.org/abs/1610.08251}{Dunjko et al., 2016}, and coding exercise for \href{https://github.com/peterwittek/qml-rg\#meeting-4}{Meeting 4}. 202 | \begin{itemize} 203 | \item What does it even mean that a quantum agent learns? How do we verify that it learned? 204 | \item Contrast deliberation phase with the deep learning and self-play part of AlphaGo (\href{http://doi.org/10.1038/nature16961}{Silver et al., 2016}). 205 | \item Result: the best we can hope for is a Grover-like speedup once we start verifying the agent. 206 | \end{itemize} 207 | \end{frame} 208 | 209 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 210 | \begin{frame}{Implementations and feasibility} 211 | \begin{itemize} 212 | \item Potential photonic implementation with classical tuning of parameters: quantum feedforward networks and autoencoders (\href{https://arxiv.org/abs/1612.01045}{Wan et al., 2016}). 213 | \item Continuous-variable quantum optics: claimed exponential speedup. (\href{http://arxiv.org/abs/1603.06222}{Lau et al., 2016}). 214 | \item D-Wave related work, e.g. \href{https://www.google.com/googleblogs/pdfs/nips_demoreport_120709_research.pdf}{Neven et al., 2009}, and an upcoming paper. 215 | \item \href{http://dx.doi.org/10.1103/physrevlett.114.140504}{Experimental realization of a quantum support vector machine on four qubits} and \href{http://dx.doi.org/10.1088/1367-2630/17/12/123010}{a feasibility study of QRAM}. 216 | \end{itemize} 217 | \end{frame} 218 | 219 | \section{What's Next} 220 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 221 | \begin{frame}{The Next Six Meetings} 222 | \begin{columns}[T] 223 | \begin{column}{.5\textwidth} 224 | \textbf{Classical} 225 | \begin{enumerate} 226 | \item Regularization in deep learning. 227 | \item Sequence learning. 228 | \item Probabilistic graphical models. 229 | \item Generative adversarial networks. 230 | \item Manifold learning\vspace{1.2em} 231 | 232 | \item Recommendation systems and matrix completion. 233 | \end{enumerate} 234 | \end{column} 235 | \begin{column}{.5\textwidth} 236 | \textbf{Quantum} 237 | \begin{enumerate} 238 | \item Actual quantum Hamiltonian in BMs. 239 | \item Sequential causal models. 240 | \item Quantum graphical models.\vspace{1.2em} 241 | 242 | \item Unidentified quantum whatever topic. 243 | \item Quantum topological analysis. 244 | \item Quantum matrix completion. 245 | \end{enumerate} 246 | \end{column} 247 | \end{columns} 248 | \vspace{1em} 249 | 250 | Planned finish date for first phase: 18 May. 251 | \end{frame} 252 | 253 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 254 | \begin{frame}{Upcoming Tutorials} 255 | \begin{itemize} 256 | \item Advanced Data Science (18 April). 257 | \item Kaggle (2 May). 258 | \end{itemize} 259 | 260 | The plan afterwards is to replace tutorials by entering a Kaggle competition as a team, with regular meetings. 261 | 262 | \end{frame} 263 | 264 | \end{document} 265 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2018/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peterwittek/qml-rg/998841a9c6d4fb8630c814221b91048eeba8d0fd/Archiv_Session_Spring_2018/.DS_Store -------------------------------------------------------------------------------- /Archiv_Session_Spring_2018/Coding_Exercises/Week_01.py: -------------------------------------------------------------------------------- 1 | '''Trains a simple convnet on the MNIST dataset. 2 | Gets to 99.25% test accuracy after 12 epochs 3 | (there is still a lot of margin for parameter tuning). 4 | 16 seconds per epoch on a GRID K520 GPU. 5 | From https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py 6 | ''' 7 | 8 | from __future__ import print_function 9 | import keras 10 | from keras.datasets import mnist 11 | from keras.models import Sequential 12 | from keras.layers import Dense, Dropout, Flatten 13 | from keras.layers import Conv2D, MaxPooling2D 14 | from keras import backend as K 15 | 16 | batch_size = 128 17 | num_classes = 10 18 | epochs = 12 19 | 20 | # input image dimensions 21 | img_rows, img_cols = 28, 28 22 | 23 | # the data, shuffled and split between train and test sets 24 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 25 | 26 | if K.image_data_format() == 'channels_first': 27 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 28 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 29 | input_shape = (1, img_rows, img_cols) 30 | else: 31 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 32 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 33 | input_shape = (img_rows, img_cols, 1) 34 | 35 | x_train = x_train.astype('float32') 36 | x_test = x_test.astype('float32') 37 | x_train /= 255 38 | x_test /= 255 39 | print('x_train shape:', x_train.shape) 40 | print(x_train.shape[0], 'train samples') 41 | print(x_test.shape[0], 'test samples') 42 | 43 | # convert class vectors to binary class matrices 44 | y_train = keras.utils.to_categorical(y_train, num_classes) 45 | y_test = keras.utils.to_categorical(y_test, num_classes) 46 | 47 | model = Sequential() 48 | model.add(Conv2D(32, kernel_size=(3, 3), 49 | activation='relu', 50 | input_shape=input_shape)) 51 | model.add(Conv2D(64, (3, 3), activation='relu')) 52 | model.add(MaxPooling2D(pool_size=(2, 2))) 53 | model.add(Dropout(0.25)) 54 | model.add(Flatten()) 55 | model.add(Dense(128, activation='relu')) 56 | model.add(Dropout(0.5)) 57 | model.add(Dense(num_classes, activation='softmax')) 58 | 59 | model.compile(loss=keras.losses.categorical_crossentropy, 60 | optimizer=keras.optimizers.Adadelta(), 61 | metrics=['accuracy']) 62 | 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test, y_test)) 68 | score = model.evaluate(x_test, y_test, verbose=0) 69 | print('Test loss:', score[0]) 70 | print('Test accuracy:', score[1]) -------------------------------------------------------------------------------- /Archiv_Session_Spring_2018/Coding_Exercises/week1_confusion.py: -------------------------------------------------------------------------------- 1 | # Author: Gorka Muñoz 2 | 3 | # Example of use of the confusion scheme introduced in https://arxiv.org/abs/1610.02048 4 | # to differenciate phases. In this case, we will try to differentiate between two numbers 5 | # of the MNIST database. The value we get from the Linear Discriminant Analysis will make 6 | # the form of the order parameter. The goal is to find the critical point, e.g. in which 7 | # value of the order parameter there is a phase transition. In the context of the MNIST, 8 | # we want to find the value of the LDA which differentiates between the two chosen numbers. 9 | 10 | import numpy as np 11 | import matplotlib.pyplot as plt 12 | from sklearn import discriminant_analysis 13 | from keras.datasets import mnist 14 | from scipy.signal import argrelextrema 15 | from keras.models import Sequential 16 | from keras.layers import Dense 17 | 18 | 19 | #----------------------------------------------------------------------------- 20 | # Data preparation 21 | 22 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 23 | 24 | number_1 = 7 25 | number_2 = 0 26 | 27 | ## Train set 28 | x_train = x_train[(y_train == number_1) | (y_train == number_2)] 29 | y_train = y_train[(y_train == number_1) | (y_train == number_2)] 30 | 31 | X_m = x_train.astype(float) 32 | y_m = y_train.astype(float) 33 | 34 | X_m = X_m.reshape(X_m.shape[0], X_m.shape[1]**2) 35 | 36 | #X_m = X_m[1:5000,:] # we truncate the data to have a faster program. Feel free to test with hole database 37 | #y_m = y_m[1:5000] 38 | 39 | ## Test set 40 | x_test = x_test[(y_test == number_1) | (y_test == number_2)] 41 | y_test = y_test[(y_test == number_1) | (y_test == number_2)] 42 | 43 | x_test = x_test.astype(float) 44 | y_test = y_test.astype(float) 45 | 46 | x_test = x_test.reshape(x_test.shape[0], x_test.shape[1]**2) 47 | 48 | 49 | 50 | #----------------------------------------------------------------------------- 51 | # Projection on to the first 2 linear discriminant components 52 | 53 | print("Computing Linear Discriminant Analysis projection") 54 | # Train 55 | X2 = X_m.copy() 56 | X2.flat[::X_m.shape[1] + 1] += 0.01 # Make X invertible 57 | X_lda = discriminant_analysis.LinearDiscriminantAnalysis(n_components=2).fit_transform(X2, y_m) 58 | 59 | plt.figure() 60 | plt.title('Linear Discriminant Analysis') 61 | plt.scatter(X_lda[(y_m == number_1)], y_m[y_m == number_1]) 62 | plt.scatter(X_lda[(y_m == number_2)], y_m[y_m == number_2]) 63 | 64 | # Test 65 | X3 = x_test.copy() 66 | X3.flat[::x_test.shape[1] + 1] += 0.01 # Make X invertible 67 | X_lda_test = discriminant_analysis.LinearDiscriminantAnalysis(n_components=2).fit_transform(X3, y_test) 68 | 69 | 70 | 71 | #----------------------------------------------------------------------------- 72 | # Neural Network 73 | def build_model(): 74 | model = Sequential() 75 | model.add(Dense(80, input_dim=X_m.shape[1], init='uniform', activation='sigmoid')) 76 | model.add(Dense(50, activation = 'sigmoid')) 77 | model.add(Dense(1, init='uniform', activation='sigmoid')) 78 | 79 | model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) 80 | 81 | return model 82 | 83 | 84 | #----------------------------------------------------------------------------- 85 | # Confusion scheme 86 | 87 | # We go through the possible values of critical c 88 | values_c = np.linspace(min(X_lda)[0], max(X_lda)[0], 15) 89 | 90 | acc = [] 91 | count = 0 92 | for c in values_c: 93 | print(np.where(values_c == c)[0][0]) 94 | y_nn = np.zeros_like(y_m) 95 | y_nn[(X_lda[:,0] < c)] = 0 96 | y_nn[(X_lda[:,0] > c)] = 1 97 | 98 | model = build_model() 99 | model.fit(X_m, y_nn, epochs=5, batch_size=10, verbose=1) 100 | 101 | predictions = model.predict(x_test) 102 | 103 | y_nn_test = np.zeros_like(y_test) 104 | y_nn_test[(X_lda_test[:,0] < c)] = 0 105 | y_nn_test[(X_lda_test[:,0] > c)] = 1 106 | 107 | # plt.figure() 108 | # plt.scatter(X_lda, y_nn) 109 | # plt.scatter(X_lda_test, predictions) 110 | # plt.show() 111 | 112 | acc.append(sum(abs(predictions[:,0] - y_nn_test))) 113 | 114 | critical_value = values_c[argrelextrema(np.array(acc), np.less)[0][0]] 115 | 116 | 117 | #----------------------------------------------------------------------------- 118 | # Results 119 | 120 | plt.figure() 121 | plt.title('W_shaped performance') 122 | plt.xlabel('LDA value') 123 | plt.xlabel('Accuracy of the NN') 124 | plt.plot(values_c, acc) 125 | 126 | 127 | plt.figure() 128 | plt.title('Linear Discrimant Analysis') 129 | plt.ylabel('Number of MNIST') 130 | plt.xlabel('LDA value') 131 | plt.scatter(X_lda_test, y_test,) 132 | plt.plot((critical_value, critical_value), (number_1, number_2), 'r-', label = 'Critical point') 133 | plt.legend() 134 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2018/Coding_Exercises/week1_confusion_CNN.py: -------------------------------------------------------------------------------- 1 | # Author: Gorka Muñoz 2 | 3 | # Example of use of the confusion scheme introduced in https://arxiv.org/abs/1610.02048 4 | # to differenciate phases. In this case, we will try to differentiate between two numbers 5 | # of the MNIST database. The value we get from the Linear Discriminant Analysis will make 6 | # the form of the order parameter. The goal is to find the critical point, e.g. in which 7 | # value of the order parameter there is a phase transition. In the context of the MNIST, 8 | # we want to find the value of the LDA which differentiates between the two chosen numbers. 9 | 10 | import numpy as np 11 | import matplotlib.pyplot as plt 12 | from sklearn import discriminant_analysis 13 | from keras.datasets import mnist 14 | from scipy.signal import argrelextrema 15 | import keras 16 | from keras.models import Sequential 17 | from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D 18 | from keras.layers import Conv2D, MaxPooling2D 19 | from keras.models import Sequential 20 | from keras import backend as K 21 | 22 | 23 | #----------------------------------------------------------------------------- 24 | # Data preparation 25 | 26 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 27 | 28 | number_1 = 1 29 | number_2 = 0 30 | 31 | ## Train set 32 | x_train = x_train[(y_train == number_1) | (y_train == number_2)] 33 | y_train = y_train[(y_train == number_1) | (y_train == number_2)] 34 | 35 | X_m = x_train.astype(float) 36 | y_m = y_train.astype(float) 37 | 38 | X_m_CNN = X_m 39 | X_m = X_m.reshape(X_m.shape[0], X_m.shape[1]**2) 40 | 41 | # X_m = X_m[1:1000,:] # we truncate the data to have a faster program. Feel free to test with hole database 42 | # y_m = y_m[1:1000] 43 | 44 | ## Test set 45 | x_test = x_test[(y_test == number_1) | (y_test == number_2)] 46 | y_test = y_test[(y_test == number_1) | (y_test == number_2)] 47 | 48 | x_test = x_test.astype(float) 49 | y_test = y_test.astype(float) 50 | 51 | x_test_CNN = x_test 52 | x_test = x_test.reshape(x_test.shape[0], x_test.shape[1]**2) 53 | 54 | # Reshaping Images for CNN 55 | img_rows, img_cols = 28, 28 56 | if K.image_data_format() == 'channels_first': 57 | X_m_CNN = X_m_CNN.reshape(X_m_CNN.shape[0], 1, img_rows, img_cols) 58 | x_test_CNN = x_test_CNN.reshape(x_test_CNN[0], 1, img_rows, img_cols) 59 | input_shape = (1, img_rows, img_cols) 60 | else: 61 | X_m_CNN = X_m_CNN.reshape(X_m_CNN.shape[0], img_rows, img_cols, 1) 62 | x_test_CNN = x_test_CNN.reshape(x_test_CNN.shape[0], img_rows, img_cols, 1) 63 | input_shape = (img_rows, img_cols, 1) 64 | 65 | X_m_CNN = X_m_CNN.astype('float32') 66 | x_test_CNN = x_test_CNN.astype('float32') 67 | X_m_CNN /= 255 68 | x_test_CNN /= 255 69 | 70 | #----------------------------------------------------------------------------- 71 | # Projection on to the first 2 linear discriminant components 72 | 73 | print("Computing Linear Discriminant Analysis projection") 74 | # Train 75 | X2 = X_m.copy() 76 | X2.flat[::X_m.shape[1] + 1] += 0.01 # Make X invertible 77 | X_lda = discriminant_analysis.LinearDiscriminantAnalysis(n_components=2).fit_transform(X2, y_m) 78 | 79 | plt.figure() 80 | plt.title('Linear Discriminant Analysis') 81 | plt.scatter(X_lda[(y_m == number_1)], y_m[y_m == number_1]) 82 | plt.scatter(X_lda[(y_m == number_2)], y_m[y_m == number_2]) 83 | 84 | # Test 85 | X3 = x_test.copy() 86 | X3.flat[::x_test.shape[1] + 1] += 0.01 # Make X invertible 87 | X_lda_test = discriminant_analysis.LinearDiscriminantAnalysis(n_components=2).fit_transform(X3, y_test) 88 | 89 | 90 | 91 | #----------------------------------------------------------------------------- 92 | # Neural Network 93 | def build_model(): 94 | model = Sequential() 95 | model.add(Dense(50, input_dim=X_m.shape[1], init='uniform', activation='sigmoid')) 96 | model.add(Dense(50, init='uniform', activation = 'sigmoid')) 97 | model.add(Dense(50, init='uniform', activation = 'sigmoid')) 98 | model.add(Dense(50, init='uniform', activation = 'sigmoid')) 99 | model.add(Dense(2, init='uniform', activation='sigmoid')) 100 | 101 | model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) 102 | 103 | return model 104 | 105 | def CNN_build_model(): 106 | model = Sequential() 107 | model.add(Conv2D(32, kernel_size=(3, 3), init='uniform', 108 | activation='relu', 109 | input_shape=input_shape)) 110 | model.add(Conv2D(64, (3, 3), init='uniform', activation='relu')) 111 | model.add(Conv2D(64, (3, 3), init='uniform', activation='relu')) 112 | model.add(MaxPooling2D(pool_size=(2, 2))) 113 | # model.add(Dropout(0.25)) 114 | model.add(Flatten()) 115 | model.add(Dense(128, init='uniform', activation='relu')) 116 | model.add(Dense(128, init='uniform', activation='relu')) 117 | model.add(Dense(128, init='uniform', activation='relu')) 118 | # model.add(Dropout(0.5)) 119 | model.add(Dense(2, init='uniform', activation='softmax')) 120 | model.compile(loss=keras.losses.categorical_crossentropy, 121 | optimizer=keras.optimizers.Adadelta(), 122 | metrics=['accuracy']) 123 | 124 | return model 125 | 126 | 127 | #----------------------------------------------------------------------------- 128 | # Confusion scheme 129 | 130 | # We go through the possible values of critical c 131 | values_c = np.linspace(min(X_lda)[0], max(X_lda)[0], 15) 132 | #values_c = [values_c[3]] 133 | 134 | acc = [] 135 | count = 0 136 | for c in values_c: 137 | print(np.where(values_c == c)[0][0]) 138 | y_nn = np.zeros_like(y_m) 139 | y_nn[(X_lda[:,0] < c)] = 0 140 | y_nn[(X_lda[:,0] > c)] = 1 141 | y_train_2D = keras.utils.to_categorical(y_nn, 2) 142 | # y_train_2D = y_nn 143 | #model = build_model() 144 | model = CNN_build_model() 145 | model.fit(X_m_CNN, y_train_2D, epochs=5, batch_size=256, verbose=1) 146 | 147 | predictions = model.predict(x_test_CNN) 148 | 149 | y_nn_test = np.zeros_like(y_test) 150 | y_nn_test[(X_lda_test[:,0] < c)] = 0 151 | y_nn_test[(X_lda_test[:,0] > c)] = 1 152 | y_test_2D = keras.utils.to_categorical(y_nn_test, 2) 153 | 154 | acc.append(sum(abs(predictions[:,1] - y_nn_test))) 155 | #acc.append(sum(abs(predictions[:,0] - y_nn_test))) 156 | print(acc) 157 | 158 | critical_value = values_c[argrelextrema(np.array(acc), np.less)[0][0]] 159 | 160 | 161 | #----------------------------------------------------------------------------- 162 | # Results 163 | 164 | plt.figure() 165 | plt.title('W_shaped performance') 166 | plt.xlabel('LDA value') 167 | plt.xlabel('Accuracy of the NN') 168 | plt.plot(values_c, acc) 169 | plt.savefig('W_shape') 170 | 171 | 172 | plt.figure() 173 | plt.title('Linear Discrimant Analysis') 174 | plt.ylabel('Number of MNIST') 175 | plt.xlabel('LDA value') 176 | plt.scatter(X_lda_test, y_test,) 177 | plt.plot((critical_value, critical_value), (number_1, number_2), 'r-', label = 'Critical point') 178 | plt.legend() 179 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2018/README.md: -------------------------------------------------------------------------------- 1 | Quantum Machine Learning Reading Group @ ICFO Fall Session 2017 2 | ============================================= 3 | 4 | This is the second edition of the QML Reading group at ICFO. The archive of the first edition is [here](https://github.com/peterwittek/qml-rg/tree/master/Archiv_Session_Spring_2017). We will face some restructuring and aim for much 5 | more self organization than in the last Session. Accoring to the wishes of a lot of participants in the spring 6 | session, we will focus a bit more on classical ML and try to repeat some of the topics that have been discussed 7 | in spring and really try to understand these techniques. Some people also complained, that they got lost in the 8 | coding exercises very early in the spring session. In this matter we want to slow down a bit and 9 | restart with the basics. 10 | 11 | So for the first sessions we have the following structure in mind. 12 | 13 | 1. We don't choose papers anymore, we will focus more on topics like (tSNE, Neural Networks, etc.) 14 | So there will be one topic each session and someone presents it. Some literature will be provided. 15 | 16 | 2. For the coding we think about simple tasks for the beginning. We for example have 17 | https://grads.yazabi.com/ in mind. This is an introduction into ML and could also be done individually. 18 | 19 | 20 | Topics will be announced a week in advance. 21 | Coding will be done collaboratively through this repository. 22 | Parallely for advanced programmer we can start another Kaggle competition. 23 | 24 | The reading group requires commitment: apart from the 1.5-2 contact hours a week, at least another 2-3 hours must be dedicated to reading and coding. 25 | You are not expected to know machine learning or programming before joining the group, but you are expected to commit the time necessary to catch up and develop the relevant skills. 26 | 27 | The language of choice is Python 3. 28 | 29 | Resources 30 | --------- 31 | The broader QML community is still taking shape. 32 | We are attempting to organize it through the website [quantummachinelearning.org](http://quantummachinelearning.org/). You can also sign up for the mailing list there. 33 | Please also consider contributing to the recently rewritten [Wikipedia article on QML](https://en.wikipedia.org/wiki/Quantum_machine_learning). 34 | Apart from new content, stylistic and grammatical edits, figures, and translations are all welcome. 35 | 36 | The best way to learn machine learning is by doing it. 37 | The book [Python Machine Learning](https://www.packtpub.com/big-data-and-business-intelligence/python-machine-learning) is a good starter, along with its [GitHub repository](https://github.com/rasbt/python-machine-learning-book). 38 | [Kaggle](http://kaggle.com/) is a welcoming community of data scientists. 39 | It is not only about competitions: several hundred datasets are hosted on Kaggle, along with notebooks and scripts (collectively known as kernels) that do interesting stuff with the data. 40 | These provide perfect stepping stones for beginners. 41 | Find a dataset that is close to your personal interests and dive in. 42 | For a sufficiently engaging theoretical introduction to machine learning, the book [The Elements of Statistical Learning: Data Mining, Inference, and Prediction](https://statweb.stanford.edu/~tibs/ElemStatLearn/) is highly recommended. 43 | 44 | [Anaconda](https://www.continuum.io/downloads) is the recommended Python distribution if you are new to the language. 45 | It ships with most of the scientific and machine learning ecosystem around Python. 46 | It includes [Scikit-learn](http://scikit-learn.org/), which is excellent for prototyping machine learning models. 47 | For scalable deep learning, [Keras](https://keras.io/) is recommended: it can transparently change between TensorFlow and Theano as back-ends. 48 | [QuTiP](http://qutip.org/) is an excellent quantum simulation library, and with the latest version (4.1), it is [reasonably straightforward](http://qutip.org/docs/4.1/installation.html#platform-independent-installation) to install it in Anaconda with [conda-forge](https://conda-forge.github.io/). 49 | QuTiP is somewhat limited in scalability, so perhaps it is worth checking out other simulators, such as [ProjectQ](http://projectq.ch/). 50 | 51 | We will follow the good practices of [software carpentry](http://software-carpentry.org/), that is, elementary IT skills that every scientist should have. 52 | In particular, we use [git](https://rogerdudler.github.io/git-guide/) as a version control system and host the repository right here on GitHub. 53 | When editing text or [Markdown](https://guides.github.com/features/mastering-markdown/) documents like this one, please write every sentence in a new line to ensure that conflicts are efficiently resolved when several people edit the same file. 54 | 55 | Meeting 1 56 | --------- 57 | 10.00-11.30, 19. October 2017, Seminar Room (201). 58 | 59 | **Topic:** 60 | 61 | - Repetition Deep Learning: Neural Networks, Convolutional Neural Networks 62 | 63 | **Reading:** 64 | 65 | This week we will read two papers about finding phase transitions in many body systems with neural networks. 66 | - ['Learning phase transitions by confusion'](https://arxiv.org/abs/1610.02048), by Evert P.L. van Nieuwenburg,Ye-Hua Liu, Sebastian D. Huber. 67 | - ['Machine learning phases of matter'](https://arxiv.org/abs/1605.01735), by Juan Carrasquilla, Roger G. Melko. 68 | 69 | **Coding exercises:** 70 | 71 | - The first week does not have a real coding exercise. 72 | Instead, please ensure that your computational environment is up and running. 73 | Python with the recommended libraries should be there, along with an editor. 74 | Please make keras running in Python. On windows it is not so easy. Plan a day for this. 75 | In the exercise folder is a python file 'week_01.py', which contains a simple CNN for the MNIST dataset. 76 | Try to make this run and play a bit with it. 77 | Either use your favourite text editor, or opt for Spyder, which is also bundled in Anaconda. 78 | Ensure that you can open and run Jupyter notebooks. 79 | 80 | - Go through the confusion code 'week1_confusion.py' and try to check if confusion is feasible for deep neural networks. For this add more layers to the NN and also try using more neurons each layer. 81 | 82 | - Go through a git tutorial, like the one linked under Resources, fork this repository, clone it, 83 | and add the upstream repo to follow. 84 | 85 | If you need help, we will be around. 86 | 87 | 88 | Meeting 2 89 | --------- 90 | 10.00-11.30, 26. October 2017, Seminar Room (201). 91 | 92 | **Topic:** 93 | 94 | The topic of the session will be **autoencoders**, their applications and limitations. We will follow part of the keras [tutorial](https://blog.keras.io/building-autoencoders-in-keras.html). 95 | 96 | **Coding exercise:** 97 | 98 | This weeks homework consists on three tasks: 99 | 100 | 1. Construct a simple autoencoder based on the MNIST database or, if you are brave, the CIFAR10 database. 101 | 2. Use an autoencoder to denoise images. 102 | 3. Build an autoencoder with a compressed representation of dimension 2 (this means 2 neurons in the central layer). Use the enconding part to extract features, plot them and compare the results to the Linear Discriminant Analysis (for an example, see this [link](http://scikit-learn.org/stable/auto_examples/manifold/plot_lle_digits.html)). This task is a simplification of the procedure explained in this week's classical paper. 103 | 104 | **Reading:** 105 | 106 | - G. E. Hinton and R. R. Salakhutdinov, [Reducing the Dimensionality of Data with Neural Networks](https://www.cs.toronto.edu/~hinton/science.pdf), 2006. 107 | 108 | - Jonathan Romero, Jonathan P. Olson, Alan Aspuru-Guzik, [Quantum autoencoders for efficient compression of quantum data](https://arxiv.org/abs/1612.02806), 2017. 109 | 110 | Meeting 3 111 | --------- 112 | 10.00-11.30, 02. November 2017, Seminar Room (201). 113 | 114 | **Topic:** 115 | 116 | The topic of the session will be **variational autoencoders (vAE)** 117 | 118 | **Coding exercise:** 119 | 120 | We propose something different for this week. We will have a main exercise, plus some extra ones that arose from the discussions. Feel free to choose any that you like. However, the main exercise is the recommended one for those who want to know the basics of ML. 121 | 122 | - **Main exercise**: Build an autoencoder that colors images. Use the images in the CIFAR10 dataset and transform them to grayscale using the following function: 123 | 124 | ```python 125 | def rgb2gray(rgb): 126 | 127 | r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2] 128 | gray = 0.2989 * r + 0.5870 * g + 0.1140 * b 129 | 130 | return gray 131 | ``` 132 | 133 | - **Extra exercises**: 134 | - Take a classification network for MNIST (such as [this one](https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py)) and, after training, remove the classification layer and append a reversed copy of it to test its performance as an autoencoder. 135 | - Create a denoising autoencoder, where the noise model in during training is variable and chosen at random. 136 | - Explore pre-training autoencoders with RBMs, in the spirit of [Hinton's paper](https://www.cs.toronto.edu/~hinton/science.pdf). 137 | 138 | **Reading:** 139 | 140 | Since some people would like to have more introductory reading, we suggest: 141 | 142 | - Michael Nielsens, http://neuralnetworksanddeeplearning.com/ 143 | - A very nice intuitive introduction into vAE http://kvfrans.com/variational-autoencoders-explained/ 144 | - A bit an advanced intro into vAE https://wiseodd.github.io/techblog/2016/12/10/variational-autoencoder/ 145 | 146 | Meeting 4 147 | --------- 148 | 10.00-11.30, 09. November 2017, Seminar Room (201). 149 | 150 | **Topic:** 151 | 152 | The topic of this week's session will be **Reinforcement Learning (RL)**. We will introduce the topic, talk about Q-learning algorithms and how to extend them to create deep Q-networks. 153 | 154 | **Coding exercise:** 155 | 156 | - **Main exercise**: Due to the complexity of the main homework of the previous week, the colourizing autoencoder, we will continue to work on it until next thursday. 157 | 158 | - **Extra exercise**: You can start to explore the world of RL by creating a agent that can play to the Frozen Lake game. [Here](https://medium.com/emergent-future/simple-reinforcement-learning-with-tensorflow-part-0-q-learning-with-tables-and-neural-networks-d195264329d0) is a nice tutorial. 159 | 160 | **Reading:** 161 | 162 | - For an introduction on RL with coding examples, take a look on to the different parts of this [tutorial](https://medium.com/emergent-future/simple-reinforcement-learning-with-tensorflow-part-0-q-learning-with-tables-and-neural-networks-d195264329d0), specially Parts 0, 1, 1.5 and 2. 163 | 164 | 165 | Meeting 5 166 | --------- 167 | 10.00-11.30, 16. November 2017, Seminar Room (201). 168 | 169 | 170 | **Topic:** 171 | 172 | The topic for this week is **Convolutional autoencoders**. The discussion is centered around the Alexandre's code. You can find it in the codes folder. 173 | 174 | **Reading:** 175 | 176 | This nice [tutorial](http://tinyclouds.org/colorize/) explains how to apply a convolutional autoencoder to colorize images. It also introduces the network ResNet, which will be discussed in future sessions. 177 | 178 | 179 | Meeting 6 180 | --------- 181 | 10.00-11.30, 23. November 2017, Aquarium Room (AMR) (280). 182 | 183 | The topic of this week's session will be, again, **Reinforcement Learning (RL)**. We will continue from the basics we discussed on week 4 and introduce the concepts of Policy Network and Deep Q Networks. 184 | 185 | 186 | **Reading:** 187 | 188 | - We will take a look into the state of the art RL algorithms. One of the hottests one right now is the new [AlphaGo Zero](https://www.nature.com/nature/journal/v550/n7676/full/nature24270.html), which beats with unsupervised learning the older, but still interesting, [AlphaGo](http://www.nature.com/nature/journal/v529/n7587/full/nature16961.html) algorithm. 189 | 190 | 191 | **Coding exercise:** 192 | 193 | - Create an actor-critic that can solve the [Cart-Pole game](https://gym.openai.com/envs/CartPole-v0/). A very enlightening implementation of the Actor-Critic model on the Frozen-Lake game can be found [here](http://www.rage.net/~greg/2016-07-05-ActorCritic-with-OpenAI-Gym.html). An simple program to solve the Cart-Pole game can be found [here](https://github.com/GaetanJUVIN/Deep_QLearning_CartPole/blob/master/cartpole.py) 194 | 195 | 196 | Meeting 7 197 | --------- 198 | 10.00-11.30, 30. November 2017, Aquarium Room (AMR) (280). 199 | 200 | The topic of this week's session will be, again, **Reinforcement Learning (RL)**. We will finally try to find some time on the AlphaGo paper. 201 | 202 | 203 | **Reading:** 204 | 205 | - Same as last week 206 | 207 | Meeting 8 208 | --------- 209 | 10.00-11.30, 14. December 2017, Aquarium Room (AMR) (280). 210 | 211 | The topic of this week's session will be, **Capsule Neural Networks** that might be a very promising future direction for feedforward neural networks. The main idea is that different types of information (shapes, orientations...) are stored in different "capsules" in every layer, each independent of one another, the information in different capsules is sent/re-routed into different capsules in the following layer depending on the information itself. A less-technical description may be found [here](https://hackernoon.com/what-is-a-capsnet-or-capsule-network-2bfbe48769cc). It might be very interesting as well to dig in the [keras implementation](https://github.com/XifengGuo/CapsNet-Keras) 212 | 213 | **Reading:** 214 | [Dynamic Routing Between Capsules](https://arxiv.org/abs/1710.09829), of Geoffrey E. Hinton, Google Brain, Toronto 215 | 216 | 217 | 218 | 219 | Meeting 9 220 | --------- 221 | 10.00-11.30, 18. January 2018, Nest Yellow Meeting Room (NYMR) (226). 222 | 223 | 224 | This week we will read [A Quantum Extension of Variational Bayes Inference](https://arxiv.org/abs/1712.04709). The authors show a quantum method for variational Bayes inference, which is the method underlying variational autoencoders, claiming to outperform current methods. 225 | 226 | 227 | Meeting 10 228 | --------- 229 | 10.00-11.30, 25. January 2018, Nest Yellow Meeting Room (NYMR) (226). 230 | 231 | The topic of this week's session will be, **Boltzmann Machines**. We will first have a general introduction and then we will discuss a paper about finding ground states of many body Hamiltonians with Boltzmann machines. 232 | 233 | **Reading:** 234 | [Solving the Quantum Many-Body Problem with Artificial Neural Networks](https://arxiv.org/abs/1606.02318), of Giuseppe Carleo and Matthias Troyer. 235 | 236 | 237 | Meeting 11 238 | --------- 239 | 10.00-11.30, 1. February 2018, Nest Yellow Meeting Room (NYMR) (226). 240 | 241 | Finally we will be able to discuss [A Quantum Extension of Variational Bayes Inference](https://arxiv.org/abs/1712.04709). Since most people don't seem to be very excited about this paper we will keep it short. 242 | 243 | After that we will again focus on **Boltzmann Machines**. There will not be anything to read. The idea for this session is more to pick up the discussion from last week and really try to understand Restricted Boltzmann Machines. How are they trained, what are they capable of, what not, etc., etc... 244 | 245 | **Reading:** 246 | Nothing specific. Do internet research about RBM and prepare questions we can discuss. 247 | 248 | 249 | Meeting 12 250 | --------- 251 | 10.00-11.30, 8. February 2018, Nest Yellow Meeting Room (NYMR) (226). 252 | 253 | The focus of the session will be **Deep Boltzmann machines**. We will focus on the seminal paper for the Deep Boltzmann Machines, and from there explain also the Deep Belief Nets. You can find a nice summary and Theano applications for the DBN [here](http://deeplearning.net/tutorial/DBN.html) and [here](https://codeburst.io/deep-learning-deep-belief-network-fundamentals-d0dcfd80d7d4). In case we have time, we will also discuss an interesting method for learning DBM via adaptive MCMC. 254 | 255 | **Reading:** 256 | - [Deep Boltzmann Machines](http://proceedings.mlr.press/v5/salakhutdinov09a/salakhutdinov09a.pdf), Salakhutdinov and Hinton (2009). 257 | - (Extra) [Learning Deep Boltzmann Machines using Adaptive MCMC](http://www.cs.utoronto.ca/~rsalakhu/papers/adapt.pdf), Salakhutdinov (2010). 258 | - (Extra extra) [A Fast Learning Algorithm for Deep Belief Nets](http://www.cs.toronto.edu/~hinton/absps/ncfast.pdf), Hinton et al. (2006). 259 | 260 | Meeting 13 261 | --------- 262 | 10.00-11.30, 15. February 2018, Nest Yellow Meeting Room (NYMR) (226). 263 | 264 | In this session we will continue to talk about **Boltzmann machines**, reviewing some recent applications of the RBM and DBN. We will also take a look into the implemention of machine learning algorithms in the [Rigetti Quantum Computer](https://rigetti.com/). 265 | 266 | **Reading:** 267 | - Rigetti's paper: [Unsupervised Machine Learning on a Hybrid Quantum Computer](https://arxiv.org/pdf/1712.05771.pdf). 268 | 269 | Some interesting applications of Boltzmann machines and how they compare to Autoencoders: 270 | 271 | - [Comparison of both models when denoising images.](https://arxiv.org/abs/1301.3468v6) 272 | - [Comparison of AE, RBM and DBN when doing face reconstruction.](http://ieeexplore.ieee.org/document/4530478/) 273 | - In this [article](https://arxiv.org/pdf/1210.8353.pdf), the authors show an algorithm that combines the two methods. 274 | - Algorithm for credit card fraud detection, with [Autoencoders](https://weiminwang.blog/2017/06/23/credit-card-fraud-detection-using-auto-encoder-in-tensorflow-2/) and with [RBM](https://weiminwang.blog/2017/08/05/credit-card-fraud-detection-2-using-restricted-boltzmann-machine-in-tensorflow/) 275 | - [Music generation by means of an RBM](http://danshiebler.com/2016-08-10-musical-tensorflow-part-one-the-rbm/) 276 | 277 | Meeting 14 278 | --------- 279 | 10.00-11.30, 22. February 2018, Nest Yellow Meeting Room (NYMR) (226). 280 | 281 | This session will serve as a kick-off for the **Deep Boltzmann Machine** project. To start, we will review the code of a RBM and DBN. Then, we will discuss how to implement the DBM algorithm and the goals we want to achieve. You can take a look into the original codes from R. Salakhutdinov for [learning DBM](http://www.cs.toronto.edu/~rsalakhu/DBM.html) (warning, Matlab content...). 282 | 283 | In addition, we will further study the use of machine learning for Credit Cards fraud detection. 284 | 285 | 286 | Topics proposals for next meetings 287 | --------- 288 | 289 | - 15.2. XGBoost, Decision Tree, Random Forest (Alex?) 290 | 291 | 292 | 293 | Quantum Machine Learning 294 | ------------------------ 295 | 296 | Since last weeks we focused mostly on classical ML we would like to include some QML papers in our reading group. To at least have an overview, what is happening in the QML community, every week someone of the group should quickly go through the arxiv of the past week and give a quick update of the papers. Max 15 minutes. If there is time left it would also make sense to go to older QML publications. 297 | 298 | We already agreed on an order, with whom will start: 299 | 300 | Alexandre (start 18.1), Patrick, 301 | Alejandro, Alex, Jessica, Mauri. 302 | 303 | Tutorial 1 304 | ---------- 305 | 16.00-17.30, ???, Yellow Lecture Room (247). 306 | 307 | This tutorial is about the importance of using a version control system. The focus is on git, starting with using it locally, for collaborating on LaTeX documents on Overleaf, and finishing with some examples using GitHub. The [notes](Tutorials/Git.md) are in the Tutorials folder. 308 | -------------------------------------------------------------------------------- /Archiv_Session_Spring_2018/topic proposals.md: -------------------------------------------------------------------------------- 1 | Topic proposals @ ICFO Fall Session 2017 2 | ============================================= 3 | 4 | This file contains the topics the attendants of the Reading Group are interested with. Future weeks papers and exercises will be chosen in order to satisfy this list. Write down any proposal you have. 5 | 6 | - **Topics** 7 | - Boltzmann Machines. (January) Troyers paper: https://arxiv.org/pdf/1606.02318.pdf, classical paper 8 | - BM and deep believ, BM and D-wave 9 | - Batchnormalization. https://arxiv.org/abs/1502.03167 10 | - Graphical probabilistic models. 11 | - resNet 12 | - Random Forest, XGBoost... 13 | - deep and cheap learning, universal approx theorem 14 | 15 | 16 | - **Classical papers** (Dec 14) 17 | - [Dynamic Routing between capsules](https://arxiv.org/abs/1710.09829). I have read in a few places already that this might be a very promising future direction for feedforward neural networks. The main idea is that different types of information (shapes, orientations...) are stored in different "capsules" in every layer, each independent of one another, the information in different capsules is sent/re-routed into different capsules in the following layer depending on the information itself. A less-technical description may be found [here](https://hackernoon.com/what-is-a-capsnet-or-capsule-network-2bfbe48769cc). It might be very interesting as well to dig in the [keras implementation](https://github.com/XifengGuo/CapsNet-Keras). 18 |   19 | - [Learning to act by predicting the future](https://arxiv.org/abs/1611.01779). A new proposal of mapping Reinforcement Learning to a supervised task, that allows for the definition of complex goals that change over time with the circumstances. In particular, this method crushed A3C in a Doom competition in 2016. For an introductive view, see [this blogpost](https://www.oreilly.com/ideas/reinforcement-learning-for-complex-goals-using-tensorflow). Also, a keras implementation can be found [here](https://github.com/flyyufelix/Direct-Future-Prediction-Keras). 20 | 21 | - **Quantum papers** 22 | - [An efficient quantum algorithm for generative machine learning](https://arxiv.org/abs/1711.02038). The title is quite self-explanatory. Indeed, the authors describe a quantum algorithm that allows for creating generative models in an efficient way. It might be interesting to discuss now that we know what a generative model is. 23 | - [A Quantum Extension of Variational Bayes Inference](https://arxiv.org/abs/1712.04709). The authors show a quantum method for variational Bayes inference, which is the method underlying variational autoencoders, claiming to outperform current methods. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Quantum Machine Learning Reading Group @ ICFO Fall Session 2018 2 | ============================================= 3 | 4 | This is the second edition of the QML Reading group at ICFO. The archive of the first edition is [here](https://github.com/peterwittek/qml-rg/tree/master/Archiv_Session_Spring_2017) and [here](https://github.com/peterwittek/qml-rg/tree/master/Archiv_Session_Spring_2018). We will face some restructuring and aim for more self initiative than in the last Session. We will go back to the roots of this RG and just discuss publications of interest that will be chosen by someone. 5 | 6 | So for the first sessions we have the following structure in mind. 7 | 8 | 1. We will again define papers to read for each session. We are happy if people come forward with interesting topics or papers. This session will more be about keeping track of latest advances in ML, QML and ML assisted physics. 9 | 10 | 2. Most people attending will already be quite advanced in ML. Therefore we will not start with ML basics in this group. But we are aware that there are ML beginners who attend the RG and we are also happy to explain basics if needed. 11 | 12 | 3. We are not sure yet if there will be coding exercises. In the last session most people were too busy anyway. But we are always happy to discuss coding problems or suggestions. 13 | 14 | 15 | Topics will be announced a week in advance. 16 | Coding will be done collaboratively through this repository. 17 | 18 | The reading group requires commitment: apart from the 1.5-2 contact hours a week, at least another 2-3 hours must be dedicated to reading and coding. 19 | You are not expected to know machine learning or programming before joining the group, but you are expected to commit the time necessary to catch up and develop the relevant skills. 20 | 21 | The language of choice is Python 3. 22 | 23 | Resources 24 | --------- 25 | The broader QML community is still taking shape. 26 | We are attempting to organize it through the website [quantummachinelearning.org](http://quantummachinelearning.org/). You can also sign up for the mailing list there. 27 | Please also consider contributing to the recently rewritten [Wikipedia article on QML](https://en.wikipedia.org/wiki/Quantum_machine_learning). 28 | Apart from new content, stylistic and grammatical edits, figures, and translations are all welcome. 29 | 30 | The best way to learn machine learning is by doing it. 31 | The book [Python Machine Learning](https://www.packtpub.com/big-data-and-business-intelligence/python-machine-learning) is a good starter, along with its [GitHub repository](https://github.com/rasbt/python-machine-learning-book). For the deep learning part, you can have look at the [github of the course](https://github.com/PatrickHuembeli/QML-Course-UPC-2018) we gave at the upc where we discuss convolutional neural networks, Boltzman machines and reinforcement learning. 32 | [Kaggle](http://kaggle.com/) is a welcoming community of data scientists. 33 | It is not only about competitions: several hundred datasets are hosted on Kaggle, along with notebooks and scripts (collectively known as kernels) that do interesting stuff with the data. 34 | These provide perfect stepping stones for beginners. 35 | Find a dataset that is close to your personal interests and dive in. 36 | For a sufficiently engaging theoretical introduction to machine learning, the book [The Elements of Statistical Learning: Data Mining, Inference, and Prediction](https://statweb.stanford.edu/~tibs/ElemStatLearn/) is highly recommended. 37 | 38 | [Anaconda](https://www.continuum.io/downloads) is the recommended Python distribution if you are new to the language. 39 | It ships with most of the scientific and machine learning ecosystem around Python. 40 | It includes [Scikit-learn](http://scikit-learn.org/), which is excellent for prototyping machine learning models. 41 | For scalable deep learning, [Keras](https://keras.io/) is easy to start with, bit it uses the more intransparent and complicated tensorflow backend. For simple implementations we recommend it, but as soon as someone wants to implement more advanced neural networks we recommend changing to pytorch. 42 | [QuTiP](http://qutip.org/) is an excellent quantum simulation library, and with the latest version (4.1), it is [reasonably straightforward](http://qutip.org/docs/4.1/installation.html#platform-independent-installation) to install it in Anaconda with [conda-forge](https://conda-forge.github.io/). 43 | QuTiP is somewhat limited in scalability, so perhaps it is worth checking out other simulators, such as [ProjectQ](http://projectq.ch/). 44 | 45 | We will follow the good practices of [software carpentry](http://software-carpentry.org/), that is, elementary IT skills that every scientist should have. 46 | In particular, we use [git](https://rogerdudler.github.io/git-guide/) as a version control system and host the repository right here on GitHub. 47 | When editing text or [Markdown](https://guides.github.com/features/mastering-markdown/) documents like this one, please write every sentence in a new line to ensure that conflicts are efficiently resolved when several people edit the same file. 48 | 49 | Meeting 1 50 | --------- 51 | 10.30-12.00, 18.October 2018, Aquarium Room (AMR) (280). 52 | 53 | **Topic:** 54 | 55 | - We will have a look at this [paper](https://www.semanticscholar.org/paper/Knowledge-graph-refinement%3A-A-survey-of-approaches-Paulheim/93b6329091e215b9ef007a85c07635f09e7b8adb). Knowledge graph refinement: A survey of approaches and evaluation methods. 56 | 57 | 58 | Meeting 2 59 | --------- 60 | 10.30-12.00, 8.November 2018, Aquarium Room (AMR) (280). 61 | 62 | **Topic:** 63 | 64 | - We will go through the paper [Bayesian Deep Learning on a Quantum Computer](https://arxiv.org/abs/1806.11463). This will include a short introduction to supervised learning in feedforward neural networks for the newcomers, and notes on Bayesian learning. 65 | 66 | 67 | Meeting 3 68 | --------- 69 | 10.30-12.00, 15.November 2018, Aquarium Room (AMR) (280). 70 | 71 | **Topic:** 72 | 73 | - Our journey through [Bayesian Deep Learning on a Quantum Computer](https://arxiv.org/abs/1806.11463) continues. We will also review equivalences between GP training and training of deep neural networks and quantum-assisted training of GPs. 74 | 75 | Meeting 4 76 | --------- 77 | 09.30-11.00, 13.December 2018, Aquarium Room (AMR) (280). 78 | 79 | **Topic:** 80 | 81 | - In this session we will review the recent paper [[1]](https://arxiv.org/abs/1807.04271), where the author proposes a classical algorithm that mimics the quantum-algorithm for recommendation systems [[2]](http://drops.dagstuhl.de/opus/volltexte/2017/8154/pdf/LIPIcs-ITCS-2017-49.pdf) by using stochastic sampling [[3]](https://www.math.cmu.edu/~af1p/Texfiles/SVD.pdf). For preparation, it is recommended going over [1] and reading the nice introduction of [3]. Knowing [2]? Even better. 82 | 83 | Meeting 5 84 | --------- 85 | 10.30-12.00, 10.January 2019, Aquarium Room (AMR) (280). 86 | 87 | **Topic:** 88 | 89 | - To kick-start the 2019 reading group, I will follow the recent trend of showing the loopholes of by presenting [this paper](https://arxiv.org/abs/1803.11173). 90 | 91 | The basic message is to argue that that there are major problems when trying to perform gradient descent on classically parametrised quantum circuits (i.e. 'quantum neural networks'), since the gradient will be essentially zero everywhere. 92 | 93 | Meeting 6 94 | --------- 95 | 10.30-12.00, 17.January 2019, Aquarium Room (AMR) (280). 96 | 97 | - In this session we will focus on Reinforcement Learning. We will breifly review the basics of RL and Q-learning. You can take a look at this [repo](https://github.com/gorkamunoz/QML-Course-UPC-2018/tree/master/RL) for an introduction to the topic. Then, we will see how this [paper](https://arxiv.org/pdf/1705.00565.pdf ) applies such algorithms to do Quantum Control. 98 | 99 | Meeting 7 100 | --------- 101 | 10.30-12.00, 24.January 2019, Aquarium Room (AMR) (280). 102 | 103 | **Topic:** 104 | 105 | - This week we look at methods of interpretability in classical machine learning. For this we start with an [overview](https://www.sciencedirect.com/science/article/pii/S1051200417302385) of activation maximization, sensitivity analysis and layer-wiserelevancepropagation. The 2nd paper [Learning Deep Features for Discriminative Localization](http://cnnlocalization.csail.mit.edu/Zhou_Learning_Deep_Features_CVPR_2016_paper.pdf) shows how heatmaps can be implemented in a very simple way. 106 | And finally [this](https://distill.pub/2018/building-blocks/) and [this](https://distill.pub/2017/feature-visualization/) distill publications show how activation maximization looks like if used on proper deep learning examples. 107 | 108 | - You can find examples how to program CAM with GAP for [keras](https://towardsdatascience.com/multi-label-classification-and-class-activation-map-on-fashion-mnist-1454f09f5925) and [pytorch](https://towardsdatascience.com/training-convolutional-neural-networks-to-categorize-clothing-with-pytorch-30b6d399f05f) 109 | 110 | Meeting 8 111 | --------- 112 | 10.30-12.00, 31.January 2019, **Seminar Room!!** 113 | 114 | **Topic:** 115 | 116 | We will have a look at this [paper](https://arxiv.org/pdf/1901.06526.pdf). It describes how to formulate division and matrix inversion as a QUBO problem that can be solved by a quantum annealer like DWAVE. 117 | 118 | 119 | Meeting 9 120 | --------- 121 | 10.30-12.00, 7.February 2019, Aquarium Room (AMR) (280). 122 | 123 | **Topic:** 124 | 125 | This week we have a look at [QAOA on CV systems](https://arxiv.org/pdf/1902.00409.pdf). 126 | 127 | -------------------------------------------------------------------------------- /topic proposals.md: -------------------------------------------------------------------------------- 1 | Topic proposals @ ICFO Fall Session 2018 2 | ============================================= 3 | 4 | This file contains the topics the attendants of the Reading Group are interested in. Future weeks papers and exercises will be chosen in order to satisfy this list. Write down any proposal you have. 5 | 6 | - **Topics** 7 | - Boltzmann Machines 8 | - D-wave 9 | - Strawberryfields 10 | - Graphical probabilistic models. 11 | - Random Forest, XGBoost... 12 | - General unsupervised methods 13 | - deep and cheap learning, universal approx theorem 14 | 15 | 16 | --------------------------------------------------------------------------------