# coding: utf-8 # *Python Machine Learning 2nd Edition* by [Sebastian Raschka](https://sebastianraschka.com) and Vahid Mirjalili, Packt Publishing Ltd. 2017 # # Code Repository: https://github.com/rasbt/python-machine-learning-book-2nd-edition # # Code License: [MIT License](https://github.com/rasbt/python-machine-learning-book-2nd-edition/blob/master/LICENSE.txt) # # Python Machine Learning - Code Examples # # Chapter 13 - Parallelizing Neural Network Training with TensorFlow # - [Building, compiling, and running machine learning models with TensorFlow](#Building,-compiling,-and-running-machine-learning-models-with-TensorFlow) # - [Performance challenges of expensive computations](#Performance-challenges-of-expensive-computations) # - [What is TensorFlow?](#What-is-TensorFlow?) # - [First steps with TensorFlow](#First-steps-with-TensorFlow) # - [Working with array structures](#Working-with-array-structures) # - [Developing a simple model with low-level TensorFlow API](#Developing-a-simple-model-with-low-level-TensorFlow-API) # - [Training neural networks efficiently with high-level TensorFlow APIs](#Training-neural-networks-efficiently-with-high-level-TensorFlow-APIs) # - [Building multilayer neural networks using TensorFlow's Layers API](#Building-multilayer-neural-networks-using-TensorFlow's-Layers-API) # - [Developing Multilayer Neurals Network with Keras](#Developing-Multilayer-Neural-Network-with-Keras) # - [Choosing activation functions for multilayer networks](#Choosing-activation-functions-for-multilayer-networks) # - [Logistic function recap](#Logistic-function-recap) # - [Estimating class probabilities in multi-class classification via the softmax function](#Estimating-class-probabilities-in-multi-class-classification-via-the-softmax-function) # - [Broadening the output spectrum by using a hyperbolic tangent](#Broadening-the-output-spectrum-by-using-a-hyperbolic-tangent) # - [Rectified Linear Unit activation](#Rectified-Linear-Unit-activation) # - [Summary](#Summary) # Note that the optional watermark extension is a small IPython notebook plugin that I developed to make the code reproducible. You can just skip the following line(s). # In[1]: # *The use of `watermark` is optional. You can install this IPython extension via "`pip install watermark`". For more information, please see: https://github.com/rasbt/watermark.* # In[2]: # In[3]: from IPython.display import Image # ## Building, compiling, and running machine learning models with TensorFlow # ### Performance challenges of expensive computations # In[4]: # ### What is TensorFlow? # ### First steps with TensorFlow # In[5]: import tensorflow as tf ## create a graph g = tf.Graph() with g.as_default(): x = tf.placeholder(dtype=tf.float32, shape=(None), name='x') w = tf.Variable(2.0, name='weight') b = tf.Variable(0.7, name='bias') z = w*x + b init = tf.global_variables_initializer() ## create a session and pass in graph g with tf.Session(graph=g) as sess: ## initialize w and b: sess.run(init) ## evaluate z: for t in [1.0, 0.6, -1.8]: print('x=%4.1f --> z=%4.1f'%( t, sess.run(z, feed_dict={x:t}))) # In[6]: with tf.Session(graph=g) as sess: sess.run(init) print(sess.run(z, feed_dict={x:[1., 2., 3.]})) # ### Working with array structures # In[7]: import tensorflow as tf import numpy as np g = tf.Graph() with g.as_default(): x = tf.placeholder(dtype=tf.float32, shape=(None, 2, 3), name='input_x') x2 = tf.reshape(x, shape=(-1, 6), name='x2') ## calculate the sum of each column xsum = tf.reduce_sum(x2, axis=0, name='col_sum') ## calculate the mean of each column xmean = tf.reduce_mean(x2, axis=0, name='col_mean') with tf.Session(graph=g) as sess: x_array = np.arange(18).reshape(3, 2, 3) print('input shape: ', x_array.shape) print('Reshaped:\n', sess.run(x2, feed_dict={x:x_array})) print('Column Sums:\n', sess.run(xsum, feed_dict={x:x_array})) print('Column Means:\n', sess.run(xmean, feed_dict={x:x_array})) # ### Developing a simple model with low-level TensorFlow API # In[8]: import tensorflow as tf import numpy as np X_train = np.arange(10).reshape((10, 1)) y_train = np.array([1.0, 1.3, 3.1, 2.0, 5.0, 6.3, 6.6, 7.4, 8.0, 9.0]) # In[9]: class TfLinreg(object): def __init__(self, x_dim, learning_rate=0.01, random_seed=None): self.x_dim = x_dim self.learning_rate = learning_rate self.g = tf.Graph() ## build the model with self.g.as_default(): ## set graph-level random-seed tf.set_random_seed(random_seed) self.build() ## create initializer self.init_op = tf.global_variables_initializer() def build(self): ## define placeholders for inputs self.X = tf.placeholder(dtype=tf.float32, shape=(None, self.x_dim), name='x_input') self.y = tf.placeholder(dtype=tf.float32, shape=(None), name='y_input') print(self.X) print(self.y) ## define weight matrix and bias vector w = tf.Variable(tf.zeros(shape=(1)), name='weight') b = tf.Variable(tf.zeros(shape=(1)), name="bias") print(w) print(b) self.z_net = tf.squeeze(w*self.X + b, name='z_net') print(self.z_net) sqr_errors = tf.square(self.y - self.z_net, name='sqr_errors') print(sqr_errors) self.mean_cost = tf.reduce_mean(sqr_errors, name='mean_cost') optimizer = tf.train.GradientDescentOptimizer( learning_rate=self.learning_rate, name='GradientDescent') self.optimizer = optimizer.minimize(self.mean_cost) # In[10]: lrmodel = TfLinreg(x_dim=X_train.shape[1], learning_rate=0.01) # In[11]: def train_linreg(sess, model, X_train, y_train, num_epochs=10): ## initialiaze all variables: W & b sess.run(model.init_op) training_costs = [] for i in range(num_epochs): _, cost = sess.run([model.optimizer, model.mean_cost], feed_dict={model.X:X_train, model.y:y_train}) training_costs.append(cost) return training_costs # In[12]: sess = tf.Session(graph=lrmodel.g) training_costs = train_linreg(sess, lrmodel, X_train, y_train) # In[13]: import matplotlib.pyplot as plt plt.plot(range(1,len(training_costs) + 1), training_costs) plt.tight_layout() plt.xlabel('Epoch') plt.ylabel('Training Cost') #plt.savefig('images/13_01.png', dpi=300) plt.show() # In[14]: def predict_linreg(sess, model, X_test): y_pred = sess.run(model.z_net, feed_dict={model.X:X_test}) return y_pred # In[15]: plt.scatter(X_train, y_train, marker='s', s=50, label='Training Data') plt.plot(range(X_train.shape[0]), predict_linreg(sess, lrmodel, X_train), color='gray', marker='o', markersize=6, linewidth=3, label='LinReg Model') plt.xlabel('x') plt.ylabel('y') plt.legend() plt.tight_layout() #plt.savefig('images/13_02.png') plt.show() # ## Training neural networks efficiently with high-level TensorFlow APIs # ### Building multilayer neural networks using TensorFlow's Layers API # - See Chapter 12 for details on MNIST # In[16]: # unzips mnist import sys import gzip import shutil import os if (sys.version_info > (3, 0)): writemode = 'wb' else: writemode = 'w' zipped_mnist = [f for f in os.listdir('./') if f.endswith('ubyte.gz')] for z in zipped_mnist: with gzip.GzipFile(z, mode='rb') as decompressed, open(z[:-3], writemode) as outfile: outfile.write(decompressed.read()) # In[17]: import struct def load_mnist(path, kind='train'): """Load MNIST data from `path`""" labels_path = os.path.join(path, '%s-labels-idx1-ubyte' % kind) images_path = os.path.join(path, '%s-images-idx3-ubyte' % kind) with open(labels_path, 'rb') as lbpath: magic, n = struct.unpack('>II', lbpath.read(8)) labels = np.fromfile(lbpath, dtype=np.uint8) with open(images_path, 'rb') as imgpath: magic, num, rows, cols = struct.unpack(">IIII", imgpath.read(16)) images = np.fromfile(imgpath, dtype=np.uint8).reshape(len(labels), 784) images = ((images / 255.) - .5) * 2 return images, labels # In[18]: ## loading the data X_train, y_train = load_mnist('.', kind='train') print('Rows: %d, Columns: %d' %(X_train.shape[0], X_train.shape[1])) X_test, y_test = load_mnist('.', kind='t10k') print('Rows: %d, Columns: %d' %(X_test.shape[0], X_test.shape[1])) ## mean centering and normalization: mean_vals = np.mean(X_train, axis=0) std_val = np.std(X_train) X_train_centered = (X_train - mean_vals)/std_val X_test_centered = (X_test - mean_vals)/std_val del X_train, X_test print(X_train_centered.shape, y_train.shape) print(X_test_centered.shape, y_test.shape) # In[19]: import tensorflow as tf n_features = X_train_centered.shape[1] n_classes = 10 random_seed = 123 np.random.seed(random_seed) g = tf.Graph() with g.as_default(): tf.set_random_seed(random_seed) tf_x = tf.placeholder(dtype=tf.float32, shape=(None, n_features), name='tf_x') tf_y = tf.placeholder(dtype=tf.int32, shape=None, name='tf_y') y_onehot = tf.one_hot(indices=tf_y, depth=n_classes) h1 = tf.layers.dense(inputs=tf_x, units=50, activation=tf.tanh, name='layer1') h2 = tf.layers.dense(inputs=h1, units=50, activation=tf.tanh, name='layer2') logits = tf.layers.dense(inputs=h2, units=10, activation=None, name='layer3') predictions = { 'classes' : tf.argmax(logits, axis=1, name='predicted_classes'), 'probabilities' : tf.nn.softmax(logits, name='softmax_tensor') } # In[20]: ## define cost function and optimizer: with g.as_default(): cost = tf.losses.softmax_cross_entropy( onehot_labels=y_onehot, logits=logits) optimizer = tf.train.GradientDescentOptimizer( learning_rate=0.001) train_op = optimizer.minimize(loss=cost) init_op = tf.global_variables_initializer() # In[21]: def create_batch_generator(X, y, batch_size=128, shuffle=False): X_copy = np.array(X) y_copy = np.array(y) if shuffle: data = np.column_stack((X_copy, y_copy)) np.random.shuffle(data) X_copy = data[:, :-1] y_copy = data[:, -1].astype(int) for i in range(0, X.shape[0], batch_size): yield (X[i:i+batch_size, :], y[i:i+batch_size]) # In[22]: ## create a session to launch the graph sess = tf.Session(graph=g) ## run the variable initialization operator sess.run(init_op) ## 50 epochs of training: training_costs = [] for epoch in range(50): training_loss = [] batch_generator = create_batch_generator( X_train_centered, y_train, batch_size=64, shuffle=True) for batch_X, batch_y in batch_generator: ## prepare a dict to feed data to our network: feed = {tf_x:batch_X, tf_y:batch_y} _, batch_cost = sess.run([train_op, cost], feed_dict=feed) training_costs.append(batch_cost) print(' -- Epoch %2d ' 'Avg. Training Loss: %.4f' % ( epoch+1, np.mean(training_costs) )) # In[23]: ## do prediction on the test set: feed = {tf_x : X_test_centered} y_pred = sess.run(predictions['classes'], feed_dict=feed) print('Test Accuracy: %.2f%%' % ( 100*np.sum(y_pred == y_test)/y_test.shape[0])) # ### Developing Multilayer Neural Networks with Keras # In[24]: X_train, y_train = load_mnist('./', kind='train') print('Rows: %d, Columns: %d' %(X_train.shape[0], X_train.shape[1])) X_test, y_test = load_mnist('./', kind='t10k') print('Rows: %d, Columns: %d' %(X_test.shape[0], X_test.shape[1])) ## mean centering and normalization: mean_vals = np.mean(X_train, axis=0) std_val = np.std(X_train) X_train_centered = (X_train - mean_vals)/std_val X_test_centered = (X_test - mean_vals)/std_val del X_train, X_test print(X_train_centered.shape, y_train.shape) print(X_test_centered.shape, y_test.shape) # In[25]: import tensorflow as tf import tensorflow.contrib.keras as keras np.random.seed(123) tf.set_random_seed(123) # In[26]: y_train_onehot = keras.utils.to_categorical(y_train) print('First 3 labels: ', y_train[:3]) print('\nFirst 3 labels (one-hot):\n', y_train_onehot[:3]) # In[27]: model = keras.models.Sequential() model.add( keras.layers.Dense( units=50, input_dim=X_train_centered.shape[1], kernel_initializer='glorot_uniform', bias_initializer='zeros', activation='tanh')) model.add( keras.layers.Dense( units=50, input_dim=50, kernel_initializer='glorot_uniform', bias_initializer='zeros', activation='tanh')) model.add( keras.layers.Dense( units=y_train_onehot.shape[1], input_dim=50, kernel_initializer='glorot_uniform', bias_initializer='zeros', activation='softmax')) sgd_optimizer = keras.optimizers.SGD( lr=0.001, decay=1e-7, momentum=.9) model.compile(optimizer=sgd_optimizer, loss='categorical_crossentropy') # In[28]: history = model.fit(X_train_centered, y_train_onehot, batch_size=64, epochs=50, verbose=1, validation_split=0.1) # In[29]: y_train_pred = model.predict_classes(X_train_centered, verbose=0) print('First 3 predictions: ', y_train_pred[:3]) # In[30]: y_train_pred = model.predict_classes(X_train_centered, verbose=0) correct_preds = np.sum(y_train == y_train_pred, axis=0) train_acc = correct_preds / y_train.shape[0] print('First 3 predictions: ', y_train_pred[:3]) print('Training accuracy: %.2f%%' % (train_acc * 100)) # In[31]: y_test_pred = model.predict_classes(X_test_centered, verbose=0) correct_preds = np.sum(y_test == y_test_pred, axis=0) test_acc = correct_preds / y_test.shape[0] print('Test accuracy: %.2f%%' % (test_acc * 100)) # ## Choosing activation functions for multilayer networks # ### Logistic function recap # In[5]: import numpy as np X = np.array([1, 1.4, 2.5]) ## first value must be 1 w = np.array([0.4, 0.3, 0.5]) def net_input(X, w): return np.dot(X, w) def logistic(z): return 1.0 / (1.0 + np.exp(-z)) def logistic_activation(X, w): z = net_input(X, w) return logistic(z) print('P(y=1|x) = %.3f' % logistic_activation(X, w)) # In[33]: # W : array with shape = (n_output_units, n_hidden_units+1) #     note that the first column are the bias units W = np.array([[1.1, 1.2, 0.8, 0.4], [0.2, 0.4, 1.0, 0.2], [0.6, 1.5, 1.2, 0.7]]) # A : data array with shape = (n_hidden_units + 1, n_samples) #     note that the first column of this array must be 1 A = np.array([[1, 0.1, 0.4, 0.6]]) Z = np.dot(W, A[0]) y_probas = logistic(Z) print('Net Input: \n', Z) print('Output Units:\n', y_probas) # In[34]: y_class = np.argmax(Z, axis=0) print('Predicted class label: %d' % y_class) # ### Estimating class probabilities in multi-class classification via the softmax function # In[35]: def softmax(z): return np.exp(z) / np.sum(np.exp(z)) y_probas = softmax(Z) print('Probabilities:\n', y_probas) # In[36]: np.sum(y_probas) # ### Broadening the output spectrum by using a hyperbolic tangent # In[7]: import matplotlib.pyplot as plt def tanh(z): e_p = np.exp(z) e_m = np.exp(-z) return (e_p - e_m) / (e_p + e_m) z = np.arange(-5, 5, 0.005) log_act = logistic(z) tanh_act = tanh(z) plt.ylim([-1.5, 1.5]) plt.xlabel('net input $z$') plt.ylabel('activation $\phi(z)$') plt.axhline(1, color='black', linestyle=':') plt.axhline(0.5, color='black', linestyle=':') plt.axhline(0, color='black', linestyle=':') plt.axhline(-0.5, color='black', linestyle=':') plt.axhline(-1, color='black', linestyle=':') plt.plot(z, tanh_act, linewidth=3, linestyle='--', label='tanh') plt.plot(z, log_act, linewidth=3, label='logistic') plt.legend(loc='lower right') plt.tight_layout() #plt.savefig('images/13_03.png') plt.show() # ### Rectified Linear Unit activation # In[38]: # ## Summary # ... # --- # # Readers may ignore the next cell. # In[ ]: # In[ ]: