Skip to content

Commit 36721ba

Browse files
committed
new files, non-negative autoencoder
1 parent 32a8adb commit 36721ba

File tree

8 files changed

+189
-13
lines changed

8 files changed

+189
-13
lines changed
File renamed without changes.
File renamed without changes.

microseisms/analysis_tf_autoencoder.py renamed to microseisms/tf_autoencoder.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ def main():
4545
# 15:dip2, 16:rake2
4646
plot(design_matrix, labels, ntrain)
4747

48-
# normalize each row of the design matrix
48+
# normalize design matrix
4949
design_matrix /= design_matrix.max(axis=1)[:, None]
50+
design_matrix_mean = design_matrix.mean()
51+
design_matrix -= design_matrix_mean
5052

5153
training_matrix = design_matrix[0:ntrain, :]
5254

@@ -109,11 +111,13 @@ def create(x, layer_sizes):
109111
input_dim = int(next_layer_input.get_shape()[1])
110112

111113
# Initialize W using random values in interval [-1/sqrt(n) , 1/sqrt(n)]
112-
W = tf.Variable(tf.random_uniform([input_dim, dim], -1.0 / math.sqrt(input_dim), 1.0 / math.sqrt(input_dim)))
114+
W = tf.Variable(tf.random_uniform([input_dim, dim],
115+
-1.0 / math.sqrt(input_dim), 1.0 / math.sqrt(input_dim)))
113116
# Initialize b to zero
114-
b = tf.Variable(tf.zeros([dim]))
117+
b = tf.Variable(tf.random_uniform([dim], -1., 1.))
115118

116-
# We are going to use tied-weights so store the W matrix for later reference.
119+
# We are going to use tied-weights so store the W matrix for later
120+
# reference.
117121
encoding_matrices.append(W)
118122

119123
output = tf.nn.tanh(tf.matmul(next_layer_input, W) + b)
@@ -128,12 +132,12 @@ def create(x, layer_sizes):
128132
layer_sizes.reverse()
129133
encoding_matrices.reverse()
130134

131-
for i, dim in enumerate(layer_sizes[1:] + [ int(x.get_shape()[1])]) :
135+
for i, dim in enumerate(layer_sizes[1:] + [int(x.get_shape()[1])]):
132136
# we are using tied weights, so just lookup the encoding matrix for
133137
# this step and transpose it
134138
W = tf.transpose(encoding_matrices[i])
135139
b = tf.Variable(tf.zeros([dim]))
136-
output = tf.nn.tanh(tf.matmul(next_layer_input,W) + b)
140+
output = tf.nn.tanh(tf.matmul(next_layer_input, W) + b)
137141
next_layer_input = output
138142

139143
# the fully encoded and reconstructed value of x is here:
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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()
File renamed without changes.
File renamed without changes.

tensorflow_simple/neuralnetwork_regression.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77

88
def main():
99
# ---- dataset ----
10-
npoints = 400
11-
x0 = np.linspace(0., 10 * np.pi, npoints)
10+
npoints = 200
11+
x0 = np.linspace(0., 2 * np.pi, npoints)
1212
noise = np.random.normal(scale=0.1, size=npoints)
1313
y0 = (np.sin(x0) + noise)
1414
# y0 = noise
1515

1616
# ---- definitions ----
1717
# Model
18-
nnetwork = 400
18+
nnetwork = 20
1919

2020
x = tf.placeholder(tf.float32, [None, 1], name='x')
2121
y_label = tf.placeholder(tf.float32, [None, 1], name='y')
@@ -25,8 +25,7 @@ def main():
2525

2626
h1 = tf.nn.tanh(tf.matmul(x, W1) + b1)
2727

28-
29-
W2 = tf.Variable(tf.random_uniform([nnetwork, 1], -nnetwork, nnetwork))
28+
W2 = tf.Variable(tf.random_uniform([nnetwork, 1], -10., 10.))
3029
b2 = tf.Variable(tf.zeros([1]))
3130

3231
y = tf.matmul(h1, W2) + b2
@@ -35,8 +34,8 @@ def main():
3534
cost = tf.reduce_mean(tf.square(y_label - y))
3635

3736
# optimizer
38-
stepsize = 0.001
39-
optimizer = tf.train.GradientDescentOptimizer(stepsize).minimize(cost)
37+
stepsize = 0.1
38+
optimizer = tf.train.AdamOptimizer(stepsize).minimize(cost)
4039

4140
# ---- run tensorflow ----
4241
init = tf.initialize_all_variables()

0 commit comments

Comments
 (0)