|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf8 -*- |
| 3 | +import numpy as np |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +import tensorflow as tf |
| 6 | +import math |
| 7 | + |
| 8 | + |
| 9 | +def plot(matrix, labels, ntrain=None): |
| 10 | + nsamples, nfeatures = matrix.shape |
| 11 | + fig, (col1, col2) = plt.subplots(1, 2, sharey=True) |
| 12 | + col1.autoscale(False) |
| 13 | + col1.set_xlim((0, nfeatures)) |
| 14 | + col1.set_ylim((0, nsamples)) |
| 15 | + matrix_normed = matrix / np.max(matrix, axis=1)[:, None] |
| 16 | + col1.imshow(matrix_normed, origin='lower', cmap='viridis', aspect='auto') |
| 17 | + col2.plot(labels, range(nsamples)) |
| 18 | + if ntrain is not None: |
| 19 | + col1.axhline(ntrain, color='white', linewidth=2, alpha=0.5) |
| 20 | + col2.axhline(ntrain, color='red') |
| 21 | + col1.set_xlabel('features') |
| 22 | + col1.set_ylabel('examples') |
| 23 | + col2.set_xlabel('label') |
| 24 | + |
| 25 | + |
| 26 | +def main(): |
| 27 | + dataset = np.load('dataset.npz') |
| 28 | + design_matrix = dataset['design_matrix'] |
| 29 | + labels = dataset['depths'] |
| 30 | + |
| 31 | + mask = np.all(np.isfinite(design_matrix), axis=1) |
| 32 | + design_matrix = design_matrix[mask] |
| 33 | + labels = labels[mask] |
| 34 | + |
| 35 | + nsamples, nfeatures = design_matrix.shape |
| 36 | + print(nsamples, nfeatures) |
| 37 | + |
| 38 | + # Number of examples to use for training |
| 39 | + # ntrain = 1000 |
| 40 | + ntrain = 200 |
| 41 | + |
| 42 | + # label indices: |
| 43 | + # 0: year, 1: month, 2:day, 3:hour, 4:min, 5:sec, 6:lat, 7:lon |
| 44 | + # 8: depth, 9: M0, 10: Mw, 11:strike1, 12: dip1, 13: rake1, 14:strike2, |
| 45 | + # 15:dip2, 16:rake2 |
| 46 | + plot(design_matrix, labels, ntrain) |
| 47 | + |
| 48 | + # normalize design matrix |
| 49 | + design_matrix /= design_matrix.max(axis=1)[:, None] |
| 50 | + #design_matrix -= design_matrix.mean() |
| 51 | + |
| 52 | + # extract training data |
| 53 | + training_matrix = design_matrix[0:ntrain, :] |
| 54 | + |
| 55 | + # Model definition |
| 56 | + x = tf.placeholder(tf.float32, [None, nfeatures], name='x') |
| 57 | + y_label = tf.placeholder(tf.float32, [None, nfeatures], name='y_label') |
| 58 | + |
| 59 | + layer_sizes = [4, 1] |
| 60 | + nlayers = len(layer_sizes) |
| 61 | + autoencoder = create(x, layer_sizes) |
| 62 | + |
| 63 | + optimizer = tf.train.MomentumOptimizer(0.1, 0.001).minimize(autoencoder['cost']) |
| 64 | + |
| 65 | + init = tf.initialize_all_variables() |
| 66 | + |
| 67 | + x0 = training_matrix.reshape(ntrain, nfeatures) |
| 68 | + |
| 69 | + # plot original and reconstructed data |
| 70 | + fig, (col1, col2) = plt.subplots(1, 2, sharex=True, sharey=True) |
| 71 | + col1.imshow(x0, aspect='auto', vmin=0., vmax=1.) |
| 72 | + image = col2.imshow(np.zeros_like(x0), aspect='auto', vmin=0., vmax=1.) |
| 73 | + fig.show() |
| 74 | + plt.pause(0.01) |
| 75 | + |
| 76 | + with tf.Session() as sess: |
| 77 | + sess.run(init) |
| 78 | + |
| 79 | + for istep in range(200000): |
| 80 | + o, c = sess.run([optimizer, autoencoder['cost']], feed_dict={x: x0, |
| 81 | + y_label: x0}) |
| 82 | + |
| 83 | + if (istep % 1000 == 0): |
| 84 | + print('Loss at step {}: {}'.format(istep, c)) |
| 85 | + pre_labels = sess.run(autoencoder['decoded'], feed_dict={x: x0}) |
| 86 | + image.set_data(pre_labels) |
| 87 | + plt.draw() |
| 88 | + plt.pause(0.01) |
| 89 | + |
| 90 | + pre_labels = sess.run(autoencoder['decoded'], feed_dict={x: x0}) |
| 91 | + layer1_calc = sess.run(autoencoder['encoded'], feed_dict={x: x0}) |
| 92 | + weights = sess.run(autoencoder['weights']) |
| 93 | + |
| 94 | + writer = tf.train.SummaryWriter('./', sess.graph) |
| 95 | + writer.close() |
| 96 | + |
| 97 | + # plot encoded data |
| 98 | + fig, (col1, col2) = plt.subplots(1, 2, sharey=True) |
| 99 | + col1.imshow(x0, aspect='auto') |
| 100 | + for icoeff, coeffs in enumerate(layer1_calc.T): |
| 101 | + col2.plot(coeffs, range(ntrain)) |
| 102 | + |
| 103 | + # plot weights |
| 104 | + fig, axes = plt.subplots(nlayers, 1) |
| 105 | + if nlayers == 1: |
| 106 | + axes = [axes] |
| 107 | + for icoeff, coeffs in enumerate(weights): |
| 108 | + for iline, line in enumerate(coeffs.T): |
| 109 | + axes[icoeff].plot(line) |
| 110 | + |
| 111 | + plt.show() |
| 112 | + |
| 113 | + |
| 114 | +def create(x, layer_sizes): |
| 115 | + # Build the encoding layers |
| 116 | + next_layer_input = x |
| 117 | + activation = [tf.nn.sigmoid, tf.nn.tanh] |
| 118 | + |
| 119 | + encoding_matrices = [] |
| 120 | + for ilayer, dim in enumerate(layer_sizes): |
| 121 | + input_dim = int(next_layer_input.get_shape()[1]) |
| 122 | + |
| 123 | + # Initialize W using random values in interval [-1/sqrt(n) , 1/sqrt(n)] |
| 124 | + W = tf.Variable(tf.random_uniform([input_dim, dim], |
| 125 | + -1.0 / math.sqrt(input_dim), 1.0 / math.sqrt(input_dim))) |
| 126 | + # Initialize b to zero |
| 127 | + b = tf.Variable(tf.random_uniform([dim], -1, 1.)) |
| 128 | + |
| 129 | + # We are going to use tied-weights so store the W matrix for later |
| 130 | + # reference. |
| 131 | + encoding_matrices.append(W) |
| 132 | + |
| 133 | + output = activation[ilayer](tf.matmul(next_layer_input, W) + b) |
| 134 | + |
| 135 | + # the input into the next layer is the output of this layer |
| 136 | + next_layer_input = output |
| 137 | + |
| 138 | + # The fully encoded x value is now stored in the next_layer_input |
| 139 | + encoded_x = next_layer_input |
| 140 | + |
| 141 | + # build the reconstruction layers by reversing the reductions |
| 142 | + layer_sizes.reverse() |
| 143 | + encoding_matrices.reverse() |
| 144 | + activation.reverse() |
| 145 | + |
| 146 | + for i, dim in enumerate(layer_sizes[1:] + [int(x.get_shape()[1])]): |
| 147 | + # we are using tied weights, so just lookup the encoding matrix for |
| 148 | + # this step and transpose it |
| 149 | + W = tf.transpose(encoding_matrices[i]) |
| 150 | + b = tf.Variable(tf.random_uniform([dim], -1, 1.)) |
| 151 | + output = activation[i](tf.matmul(next_layer_input, W) + b) |
| 152 | + next_layer_input = output |
| 153 | + |
| 154 | + # the fully encoded and reconstructed value of x is here: |
| 155 | + reconstructed_x = next_layer_input |
| 156 | + lsq_error = tf.sqrt(tf.reduce_mean(tf.square(x - reconstructed_x))) |
| 157 | + cost = lsq_error |
| 158 | + |
| 159 | + #alpha = 1. |
| 160 | + #for weights in encoding_matrices[1:]: |
| 161 | + # constraint = -tf.minimum(tf.reduce_min(weights), 0) |
| 162 | + # cost += alpha * constraint |
| 163 | + |
| 164 | + return { |
| 165 | + 'weights': encoding_matrices, |
| 166 | + 'encoded': encoded_x, |
| 167 | + 'decoded': reconstructed_x, |
| 168 | + 'cost': cost |
| 169 | + } |
| 170 | + |
| 171 | + |
| 172 | +if __name__ == "__main__": |
| 173 | + main() |
0 commit comments