Saturday, March 16, 2019

Installation of Tensorflow within Python Env and Jupyter

1. Download Anaconda:
https://www.anaconda.com/distribution/#macos


Error while creating a Session with Tensorflow (command line)
>>> sess = tf.Session()

I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA

Solution:
>>> import os

>>> os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

Anaconda Cheatsheet:
https://github.com/conda/conda-docs/files/1252315/conda-cheatsheet.pdf

References:
https://medium.com/@margaretmz/anaconda-jupyter-notebook-tensorflow-and-keras-b91f381405f8

Saturday, September 22, 2018

Play Pong (Atari) using AI Agent


1. How to add Atari module
*From Terminal:

cd gym; 
brew install cmake boost boost-python sdl2 swig wget; 
pip install -e '.[atari]’

*From PyCharm install:
atari-py

2. Source code

Reference: https://gist.github.com/karpathy/a4166c7fe253700972fcbc77e4ea32c5)


Below is bit modified version to fix certain errors due to versions of libraries,



""" Trains an agent with (stochastic) Policy Gradients on Pong. Uses OpenAI Gym. """
import numpy as np
import _pickle as pickle
#import cPickle as pickle
import gym
# hyperparameters
H = 200 # number of hidden layer neuronsbatch_size = 10 # every how many episodes to do a param update?#learning_rate = 1e-4
learning_rate = 1e-2gamma = 0.99 # discount factor for rewarddecay_rate = 0.99 # decay factor for RMSProp leaky sum of grad^2resume = False # resume from previous checkpoint?render = False
# model initialization
D = 80 * 80 # input dimensionality: 80x80 gridif resume:
model = pickle.load(open('save.p', 'rb'))
else:
model = {}
model['W1'] = np.random.randn(H, D) / np.sqrt(D) # "Xavier" initialization model['W2'] = np.random.randn(H) / np.sqrt(H)
grad_buffer = {k: np.zeros_like(v) for k, v in model.items()} # update buffers that add up gradients over a batchrmsprop_cache = {k: np.zeros_like(v) for k, v in model.items()} # rmsprop memory

def sigmoid(x):
return 1.0 / (1.0 + np.exp(-x)) # sigmoid "squashing" function to interval [0,1]

def prepro(I):
""" prepro 210x160x3 uint8 frame into 6400 (80x80) 1D float vector """ I = I[35:195] # crop I = I[::2, ::2, 0] # downsample by factor of 2 I[I == 144] = 0 # erase background (background type 1) I[I == 109] = 0 # erase background (background type 2) I[I != 0] = 1 # everything else (paddles, ball) just set to 1 return I.astype(np.float).ravel()

def discount_rewards(r):
""" take 1D float array of rewards and compute discounted reward """ discounted_r = np.zeros_like(r)
running_add = 0 for t in reversed(range(0, r.size)):
if r[t] != 0: running_add = 0 # reset the sum, since this was a game boundary (pong specific!) running_add = running_add * gamma + r[t]
discounted_r[t] = running_add
return discounted_r

def policy_forward(x):
h = np.dot(model['W1'], x)
h[h < 0] = 0 # ReLU nonlinearity logp = np.dot(model['W2'], h)
p = sigmoid(logp)
return p, h # return probability of taking action 2, and hidden state

def policy_backward(eph, epdlogp):
""" backward pass. (eph is array of intermediate hidden states) """ dW2 = np.dot(eph.T, epdlogp).ravel()
dh = np.outer(epdlogp, model['W2'])
dh[eph <= 0] = 0 # backpro prelu dW1 = np.dot(dh.T, epx)
return {'W1': dW1, 'W2': dW2}

env = gym.make("Pong-v0")
observation = env.reset()
prev_x = None # used in computing the difference framexs, hs, dlogps, drs = [], [], [], []
running_reward = Nonereward_sum = 0episode_number = 0while True:
if render: env.render()
# preprocess the observation, set input to network to be difference image cur_x = prepro(observation)
x = cur_x - prev_x if prev_x is not None else np.zeros(D)
prev_x = cur_x
# forward the policy network and sample an action from the returned probability aprob, h = policy_forward(x)
action = 2 if np.random.uniform() < aprob else 3 # roll the dice!
# record various intermediates (needed later for backprop)
xs.append(x) # observation hs.append(h) # hidden state y = 1 if action == 2 else 0 # a "fake label" dlogps.append(
y - aprob) # grad that encourages the action that was taken to be taken (see http://cs231n.github.io/neural-networks-2/#losses if confused)
# step the environment and get new measurements
observation, reward, done, info = env.step(action)
reward_sum += reward
drs.append(reward) # record reward (has to be done after we call step() to get reward for previous action)
if done: # an episode finished episode_number += 1
# stack together all inputs, hidden states, action gradients, and rewards for this episode epx = np.vstack(xs)
eph = np.vstack(hs)
epdlogp = np.vstack(dlogps)
epr = np.vstack(drs)
xs, hs, dlogps, drs = [], [], [], [] # reset array memory
# compute the discounted reward backwards through time
discounted_epr = discount_rewards(epr)
# standardize the rewards to be unit normal (helps control the gradient estimator variance) discounted_epr -= np.mean(discounted_epr)
discounted_epr /= np.std(discounted_epr)
epdlogp *= discounted_epr # modulate the gradient with advantage (PG magic happens right here.) grad = policy_backward(eph, epdlogp)
for k in model: grad_buffer[k] += grad[k] # accumulate grad over batch
# perform rmsprop parameter update every batch_size episodes
if episode_number % batch_size == 0:
for k, v in model.items():
g = grad_buffer[k] # gradient rmsprop_cache[k] = decay_rate * rmsprop_cache[k] + (1 - decay_rate) * g ** 2 model[k] += learning_rate * g / (np.sqrt(rmsprop_cache[k]) + 1e-5)
grad_buffer[k] = np.zeros_like(v) # reset batch gradient buffer
# boring book-keeping
running_reward = reward_sum if running_reward is None else running_reward * 0.99 + reward_sum * 0.01 print ('resetting env. episode reward total was %f. running mean: %f' % (reward_sum, running_reward))
if episode_number % 100 == 0: pickle.dump(model, open('save.p', 'wb'))
reward_sum = 0 observation = env.reset() # reset env prev_x = None
if reward != 0: # Pong has either +1 or -1 reward exactly when game ends. print('ep %d: game finished, reward: %f' % (episode_number, reward))



Friday, September 14, 2018

Create 1 x 1 Pixel Image (Gray & RGB) using Python

1. Gray Image (for the pixel intensity from 0 to 255)

import numpy as np

import matplotlib.pyplot as plt

w = 1h = 1
imgArray = np.zeros([h, w, 3],dtype = np.uint8)

for i in range(1,256):
    plt.subplot(16,16,i)
    plt.axis('off')
    imgArray[:, :] = [i - 1]
    print(str(i) + ' >>> ' + str(imgArray))
    plt.imshow(imgArray)

plt.show()

Output
1 >>> [[[0 0 0]]]
2 >>> [[[1 1 1]]]
3 >>> [[[2 2 2]]]
4 >>> [[[3 3 3]]]
5 >>> [[[4 4 4]]]
6 >>> [[[5 5 5]]]
7 >>> [[[6 6 6]]]
8 >>> [[[7 7 7]]]
9 >>> [[[8 8 8]]]
10 >>> [[[9 9 9]]]
11 >>> [[[10 10 10]]]
12 >>> [[[11 11 11]]]
...
256 >>> [[[255 255 255]]]

2. RGB Image (for the pixel intensity from 0 to 255)

import numpy as np
import matplotlib.pyplot as plt

w = 1h = 1
imgArray = np.zeros([h, w, 3],dtype = np.uint8)

for i in range(1,256):
    plt.subplot(16,16,i)
    plt.axis('off')
    imgArray[:, :] = [255, 128, i - 1]
    print(str(i) + ' >>> ' + str(imgArray))
    plt.imshow(imgArray)

plt.show()

Output
1 >>> [[[255 128   0]]]
2 >>> [[[255 128   1]]]
3 >>> [[[255 128   2]]]
4 >>> [[[255 128   3]]]
5 >>> [[[255 128   4]]]
6 >>> [[[255 128   5]]]
7 >>> [[[255 128   6]]]
8 >>> [[[255 128   7]]]
9 >>> [[[255 128   8]]]
10 >>> [[[255 128   9]]]
11 >>> [[[255 128  10]]]
12 >>> [[[255 128  11]]]
...
256 >>> [[[255 128  255]]]

Friday, September 7, 2018

Handwritten Number Predictor (Machine Learning)

import tensorflow as tf
import keras
import numpy as np

mnist = keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()

x_train = keras.utils.normalize(x_train, axis=1)

x_test = keras.utils.normalize(x_test, axis=1)

model = keras.models.Sequential()

model.add(keras.layers.Flatten(input_shape=(28,28)))
model.add(keras.layers.Dense(128, activation=tf.nn.relu))
model.add(keras.layers.Dense(128, activation=tf.nn.relu))
model.add(keras.layers.Dense(10, activation=tf.nn.softmax))

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy',metrics=['accuracy'])

model.fit(x_train,y_train,epochs=1)

val_loss , val_acc = model.evaluate(x_test, y_test)
print(val_loss, val_acc)

prediction = model.predict([x_test])
print(np.argmax(prediction[0]))

OUTPUT:
7

Create new user Mac OSX command line


Commands to create new user:
$ sudo dscl . -create /Users/<NEW> UserShell /bin/bash
$ sudo dscl . -create /Users/<NEW> RealName "<NAME>"
$ sudo dscl . -create /Users/<NEW> UniqueID 1001
$ sudo dscl . -create /Users/<NEW> PrimaryGroupID 1000
$ sudo dscl . -create /Users/<NEW> NFSHomeDirectory /Local/Users/<NEW>
$ sudo dscl . -passwd /Users/<NEW> password
$ sudo dscl . -append /Groups/admin GroupMembership <NEW>

Change User:
sudo -s -u <NEW>


whoami



Setup Machine Learning Environment using python 3.6.1, Tensorflow (Keras), Jupyter

Issue: Your Mac OSX has been pre-installed with python 2.x which is not preferred version for the latest tensorflow version.

Solutions:  

Option : 1 Using brew to install specific version of python 3.6.1

Use below steps to install 3.6.1 version of the python, and then create virtual environment with this new python version to finally install tensorflow within the virtual environment.

1. Uninstall python 3.7 version which is incompatible with tensorflow latest version (at the time of writing this blog) 

brew uninstall --ignore-dependenciew python

2. Install python 3.6.1

pyenv install 3.6.1

3. List all the versions of python

pyenv versions

4. Change global version of python (to set active python version out of the above list)

pyenv global 3.6.1

5. Check python version (this step should display above 3.6.1 version, if not follow below step)

python --version

6. Check location of the newly installed python 3.6.1

pyenv which python


/Users/<USER>/.pyenv/versions/3.6.1/bin/python

7. Create virtual environment with python 3.6.1


/Users/<USER>/.pyenv/versions/3.6.1/bin/python -m venv myenv

8. Activate the virtual environment 


source myenv/bin/activate

9. Install tensorflow inside the virtual environment (myenv)

pip3 install tensorflow

10. Start using jupyter within the myenv

jupyter notebook

Option : 2 Using brew to install specific version of python 3.6.1


brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb






Friday, August 24, 2018

Rebuild MNIST Number Image from the Vector

IDE 
pyCharm CE

Source 
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

#Load data
X_train, y_train),(X_test, y_test) = tf.keras.datasets.mnist.load_data()

#60000, which is the value of the 0th dimention i.e. shape[0]
image_record_numbers = X_train.shape[0]

#record from the mnist train data to be plotted, total 60000
image_record_number = 0

#calculate number of pixels for each image, X_train.shape[1] returns 28 & X_train.shape[2] returns 28
number_of_pixels = X_train.shape[1] * X_train.shape[2];

#get a vector (784 pixels), representsation of the image with 28 x 28
X_train_record_vectors = X_train.reshape(image_record_numbers, number_of_pixels).astype('float32')

#Convert back a vector (784 pixels), into the Image of 28 x 28 pixels
X_train_record_matrix = X_train_record_vectors[image_record_number].reshape((28,28))

#Plot the image using reshaped vector
plt.imshow(X_train_record_matrix,cmap=plt.get_cmap('gray'))
plt.show()

Output 



Installation of Tensorflow within Python Env and Jupyter

1. Download Anaconda: https://www.anaconda.com/distribution/#macos Error while creating a Session with Tensorflow (command line) >...