Skip to content

Commit b6eea84

Browse files
committed
updating many at once
1 parent 4fc19ff commit b6eea84

26 files changed

+454
-22
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
22
*.pyc
3-
*~
3+
*~
4+
*.ckpt

ch02_basics/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
logs

ch02_basics/gradient.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import tensorflow as tf
2+
3+
def my_loss_function(var, data):
4+
return tf.abs(tf.sub(var, data))
5+
6+
def my_other_loss_function(var, data):
7+
return tf.square(tf.sub(var, data))
8+
9+
data = tf.placeholder(tf.float32)
10+
var = tf.Variable(1.)
11+
loss = my_loss_function(var, data)
12+
var_grad = tf.gradients(loss, [var])[0]
13+
14+
with tf.Session() as sess:
15+
sess.run(tf.initialize_all_variables())
16+
var_grad_val = sess.run(var_grad, feed_dict={data: 4})
17+
print(var_grad_val)

ch05_clustering/audio_clustering.py

+37-15
Original file line numberDiff line numberDiff line change
@@ -14,55 +14,77 @@
1414
chromo = tf.placeholder(tf.float32)
1515
max_freqs = tf.argmax(chromo, 0)
1616

17+
1718
def get_next_chromogram(sess):
1819
audio_file = sess.run(filename)
1920
F = Chromagram(audio_file, nfft=16384, wfft=8192, nhop=2205)
20-
return F.X
21+
return F.X, audio_file
22+
2123

2224
def extract_feature_vector(sess, chromo_data):
2325
num_features, num_samples = np.shape(chromo_data)
2426
freq_vals = sess.run(max_freqs, feed_dict={chromo: chromo_data})
2527
hist, bins = np.histogram(freq_vals, bins=range(num_features + 1))
26-
return hist.astype(float) / num_samples
28+
normalized_hist = hist.astype(float) / num_samples
29+
return normalized_hist
30+
2731

2832
def get_dataset(sess):
2933
num_files = sess.run(count_num_files)
3034
coord = tf.train.Coordinator()
3135
threads = tf.train.start_queue_runners(coord=coord)
32-
xs = []
33-
for i in range(num_files):
34-
chromo_data = get_next_chromogram(sess)
35-
x = [extract_feature_vector(sess, chromo_data)]
36-
x = np.matrix(x)
37-
if len(xs) == 0:
38-
xs = x
39-
else:
40-
xs = np.vstack((xs, x))
41-
return xs
36+
xs = list()
37+
names = list()
38+
plt.figure()
39+
for _ in range(num_files):
40+
chromo_data, filename = get_next_chromogram(sess)
41+
42+
plt.subplot(1, 2, 1)
43+
plt.imshow(chromo_data, cmap='Greys', interpolation='nearest')
44+
plt.title('Visualization of Sound Spectrum')
45+
46+
plt.subplot(1, 2, 2)
47+
freq_vals = sess.run(max_freqs, feed_dict={chromo: chromo_data})
48+
plt.hist(freq_vals)
49+
plt.title('Histogram of Notes')
50+
plt.xlabel('Musical Note')
51+
plt.ylabel('Count')
52+
plt.savefig('{}.png'.format(filename))
53+
plt.clf()
54+
55+
plt.clf()
56+
names.append(filename)
57+
x = extract_feature_vector(sess, chromo_data)
58+
xs.append(x)
59+
xs = np.asmatrix(xs)
60+
return xs, names
61+
4262

4363
def initial_cluster_centroids(X, k):
4464
return X[0:k, :]
4565

66+
4667
def assign_cluster(X, centroids):
4768
expanded_vectors = tf.expand_dims(X, 0)
4869
expanded_centroids = tf.expand_dims(centroids, 1)
4970
distances = tf.reduce_sum(tf.square(tf.sub(expanded_vectors, expanded_centroids)), 2)
5071
mins = tf.argmin(distances, 0)
5172
return mins
5273

74+
5375
def recompute_centroids(X, Y):
5476
sums = tf.unsorted_segment_sum(X, Y, k)
5577
counts = tf.unsorted_segment_sum(tf.ones_like(X), Y, k)
5678
return sums / counts
5779

80+
5881
with tf.Session() as sess:
5982
sess.run(tf.initialize_all_variables())
60-
X = get_dataset(sess)
61-
print(X)
83+
X, names = get_dataset(sess)
6284
centroids = initial_cluster_centroids(X, k)
6385
i, converged = 0, False
6486
while not converged and i < max_iterations:
6587
i += 1
6688
Y = assign_cluster(X, centroids)
6789
centroids = sess.run(recompute_centroids(X, Y))
68-
print(centroids)
90+
print(zip(sess.run(Y), names))
95.7 KB
Binary file not shown.
26.8 KB
Loading
270 KB
Binary file not shown.
27.1 KB
Loading
218 KB
Binary file not shown.
27.8 KB
Loading
212 KB
Binary file not shown.
26.9 KB
Loading
580 KB
Binary file not shown.
30.4 KB
Loading

ch06_planning/datastore.py

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
from yahoo_finance import Share
2+
from matplotlib import pyplot as plt
3+
import numpy as np
4+
import random
5+
import tensorflow as tf
6+
import random
7+
8+
9+
class DecisionPolicy:
10+
def select_action(self, current_state, step):
11+
pass
12+
13+
def update_q(self, state, action, reward, next_state):
14+
pass
15+
16+
17+
class RandomDecisionPolicy(DecisionPolicy):
18+
def __init__(self, actions):
19+
self.actions = actions
20+
21+
def select_action(self, current_state, step):
22+
action = self.actions[random.randint(0, len(self.actions) - 1)]
23+
return action
24+
25+
26+
class QLearningDecisionPolicy(DecisionPolicy):
27+
def __init__(self, actions, input_dim):
28+
self.epsilon = 0.9
29+
self.gamma = 0.001
30+
self.actions = actions
31+
output_dim = len(actions)
32+
h1_dim = 200
33+
34+
self.x = tf.placeholder(tf.float32, [None, input_dim])
35+
self.y = tf.placeholder(tf.float32, [output_dim])
36+
W1 = tf.Variable(tf.random_normal([input_dim, h1_dim]))
37+
b1 = tf.Variable(tf.constant(0.1, shape=[h1_dim]))
38+
h1 = tf.nn.relu(tf.matmul(self.x, W1) + b1)
39+
W2 = tf.Variable(tf.random_normal([h1_dim, output_dim]))
40+
b2 = tf.Variable(tf.constant(0.1, shape=[output_dim]))
41+
self.q = tf.nn.relu(tf.matmul(h1, W2) + b2)
42+
43+
loss = tf.square(self.y - self.q)
44+
self.train_op = tf.train.AdagradOptimizer(0.01).minimize(loss)
45+
self.sess = tf.Session()
46+
self.sess.run(tf.initialize_all_variables())
47+
48+
def select_action(self, current_state, step):
49+
threshold = min(self.epsilon, step / 1000.)
50+
if random.random() < threshold:
51+
# Exploit best option with probability epsilon
52+
action_q_vals = self.sess.run(self.q, feed_dict={self.x: current_state})
53+
action_idx = np.argmax(action_q_vals) # TODO: replace w/ tensorflow's argmax
54+
action = self.actions[action_idx]
55+
else:
56+
# Explore random option with probability 1 - epsilon
57+
action = self.actions[random.randint(0, len(self.actions) - 1)]
58+
return action
59+
60+
def update_q(self, state, action, reward, next_state):
61+
action_q_vals = self.sess.run(self.q, feed_dict={self.x: state})
62+
next_action_q_vals = self.sess.run(self.q, feed_dict={self.x: next_state})
63+
next_action_idx = np.argmax(next_action_q_vals)
64+
action_q_vals[0, next_action_idx] = reward + self.gamma * next_action_q_vals[0, next_action_idx]
65+
action_q_vals = np.squeeze(np.asarray(action_q_vals))
66+
self.sess.run(self.train_op, feed_dict={self.x: state, self.y: action_q_vals})
67+
68+
69+
def run_simulation(policy, initial_budget, initial_num_stocks, prices, hist, debug=False):
70+
budget = initial_budget
71+
num_stocks = initial_num_stocks
72+
share_value = 0
73+
transitions = list()
74+
for i in range(len(prices) - hist - 1):
75+
if i % 100 == 0:
76+
print('progress {:.2f}%'.format(float(100*i) / (len(prices) - hist - 1)))
77+
current_state = np.asmatrix(np.hstack((prices[i:i+hist], budget, num_stocks)))
78+
current_portfolio = budget + num_stocks * share_value
79+
action = policy.select_action(current_state, i)
80+
share_value = float(prices[i + hist + 1])
81+
if action == 'Buy' and budget >= share_value:
82+
budget -= share_value
83+
num_stocks += 1
84+
elif action == 'Sell' and num_stocks > 0:
85+
budget += share_value
86+
num_stocks -= 1
87+
else:
88+
action = 'Hold'
89+
new_portfolio = budget + num_stocks * share_value
90+
reward = new_portfolio - current_portfolio
91+
next_state = np.asmatrix(np.hstack((prices[i+1:i+hist+1], budget, num_stocks)))
92+
transitions.append((current_state, action, reward, next_state))
93+
policy.update_q(current_state, action, reward, next_state)
94+
95+
portfolio = budget + num_stocks * share_value
96+
if debug:
97+
print('${}\t{} shares'.format(budget, num_stocks))
98+
return portfolio
99+
100+
101+
def run_simulations(policy, budget, num_stocks, prices, hist):
102+
num_tries = 10
103+
final_portfolios = list()
104+
for i in range(num_tries):
105+
final_portfolio = run_simulation(policy, budget, num_stocks, prices, hist)
106+
final_portfolios.append(final_portfolio)
107+
avg, std = np.mean(final_portfolios), np.std(final_portfolios)
108+
return avg, std
109+
110+
111+
def get_prices(share_symbol, start_date, end_date, cache_filename='stock_prices.npy'):
112+
try:
113+
stock_prices = np.load(cache_filename)
114+
except IOError:
115+
share = Share(share_symbol)
116+
stock_hist = share.get_historical(start_date, end_date)
117+
stock_prices = [stock_price['Open'] for stock_price in stock_hist]
118+
np.save(cache_filename, stock_prices)
119+
120+
return stock_prices
121+
122+
123+
def plot_prices(prices):
124+
plt.title('Opening stock prices')
125+
plt.xlabel('day')
126+
plt.ylabel('price ($)')
127+
plt.plot(prices)
128+
plt.savefig('prices.png')
129+
130+
131+
if __name__ == '__main__':
132+
prices = get_prices('MSFT', '1992-07-22', '2016-07-22')
133+
plot_prices(prices)
134+
actions = ['Buy', 'Sell', 'Hold']
135+
hist = 200
136+
# policy = RandomDecisionPolicy(actions)
137+
policy = QLearningDecisionPolicy(actions, hist + 2)
138+
budget = 1000.0
139+
num_stocks = 0
140+
avg, std = run_simulations(policy, budget, num_stocks, prices, hist)
141+
print(avg, std)
142+

ch06_planning/hmm.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import tensorflow as tf
2+
import numpy as np
3+
4+
# Num states
5+
K = 2
6+
7+
# Num observations
8+
N = 3
9+
10+
# Observations
11+
Y = [0, 1, 2]
12+
13+
# Prior (K)
14+
prior = tf.constant([0.6, 0.4], dtype=tf.double)
15+
16+
# Transition matrix (K x K)
17+
T = tf.constant([[0.7, 0.3],
18+
[0.4, 0.6]], dtype=tf.double)
19+
20+
# Emission matrix (K x N)
21+
B = tf.constant([[0.5, 0.4, 0.1],
22+
[0.1, 0.3, 0.6]], dtype=tf.double)
23+
24+
# (K x T)
25+
T1 = tf.Variable(tf.zeros([N, K], dtype=tf.double))
26+
T2 = tf.Variable(tf.zeros([N, K], dtype=tf.double))
27+
28+
with tf.Session() as sess:
29+
sess.run(tf.initialize_all_variables())
30+
y0 = Y[0]
31+
t1_update = tf.mul(B[:, y0], prior)
32+
t1_update_val = sess.run(t1_update)
33+
print(np.shape(t1_update_val))
34+
print(t1_update_val)
35+
T1_val = tf.scatter_update(T1, [0], [[1., 1.]])
36+
print(sess.run(T1_val))

ch06_planning/prices.png

39.6 KB
Loading

ch06_planning/stock_prices.npy

59.1 KB
Binary file not shown.

ch06_planning/stock_prices_backup.npy

48 KB
Binary file not shown.

ch08_autoencoder/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cifar-10-batches-py

ch08_autoencoder/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Info
2+
3+
Example of how to implement an autoencoder in TensorFlow
4+
5+
# Setup
6+
7+
* [Install TensorFlow](https://www.tensorflow.org/get_started/os_setup.html)
8+
9+
* [Install h5py](http://docs.h5py.org/en/latest/build.html)
10+
11+
$ sudo apt-get install python-h5py
12+
13+
# Run
14+
15+
See `main.py` for API usage.
16+
Running it will train and test an autoencoder on the famous Iris dataset.
17+
18+
$ python main.py

ch08_autoencoder/autoencoder.py

+35-6
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,36 @@ def get_batch(X, size):
66
return X[a]
77

88
class Autoencoder:
9-
def __init__(self, input_dim, hidden_dim, epoch=1000, batch_size=50, learning_rate=0.001):
9+
def __init__(self, input_dim, hidden_dim, epoch=1000, batch_size=10, learning_rate=0.001):
1010
self.epoch = epoch
1111
self.batch_size = batch_size
1212
self.learning_rate = learning_rate
1313

1414
x = tf.placeholder(dtype=tf.float32, shape=[None, input_dim])
1515
with tf.name_scope('encode'):
16-
weights = tf.Variable(tf.random_normal([input_dim, hidden_dim], dtype=tf.float32))
17-
biases = tf.Variable(tf.zeros([hidden_dim]))
16+
weights = tf.Variable(tf.random_normal([input_dim, hidden_dim], dtype=tf.float32), name='weights')
17+
biases = tf.Variable(tf.zeros([hidden_dim]), name='biases')
1818
encoded = tf.nn.sigmoid(tf.matmul(x, weights) + biases)
1919
with tf.name_scope('decode'):
20-
weights = tf.Variable(tf.random_normal([hidden_dim, input_dim], dtype=tf.float32))
21-
biases = tf.Variable(tf.zeros([input_dim]))
20+
weights = tf.Variable(tf.random_normal([hidden_dim, input_dim], dtype=tf.float32), name='weights')
21+
biases = tf.Variable(tf.zeros([input_dim]), name='biases')
2222
decoded = tf.matmul(encoded, weights) + biases
2323

2424
self.x = x
2525
self.encoded = encoded
2626
self.decoded = decoded
2727

2828
self.loss = tf.sqrt(tf.reduce_mean(tf.square(tf.sub(self.x, self.decoded))))
29+
30+
self.all_loss = tf.sqrt(tf.reduce_mean(tf.square(tf.sub(self.x, self.decoded)), 1))
2931
self.train_op = tf.train.AdamOptimizer(self.learning_rate).minimize(self.loss)
3032
self.saver = tf.train.Saver()
3133

3234
def train(self, data):
3335
with tf.Session() as sess:
3436
sess.run(tf.initialize_all_variables())
3537
for i in range(self.epoch):
36-
for j in range(50):
38+
for j in range(500):
3739
batch_data = get_batch(data, self.batch_size)
3840
l, _ = sess.run([self.loss, self.train_op], feed_dict={self.x: batch_data})
3941
if i % 10 == 0:
@@ -55,3 +57,30 @@ def get_params(self):
5557
self.saver.restore(sess, './model.ckpt')
5658
weights, biases = sess.run([self.weights1, self.biases1])
5759
return weights, biases
60+
61+
def classify(self, data, labels):
62+
with tf.Session() as sess:
63+
sess.run(tf.initialize_all_variables())
64+
self.saver.restore(sess, './model.ckpt')
65+
hidden, reconstructed = sess.run([self.encoded, self.decoded], feed_dict={self.x: data})
66+
reconstructed = reconstructed[0]
67+
# loss = sess.run(self.all_loss, feed_dict={self.x: data})
68+
print('data', np.shape(data))
69+
print('reconstructed', np.shape(reconstructed))
70+
loss = np.sqrt(np.mean(np.square(data - reconstructed), axis=1))
71+
print('loss', np.shape(loss))
72+
horse_indices = np.where(labels == 7)[0]
73+
not_horse_indices = np.where(labels != 7)[0]
74+
horse_loss = np.mean(loss[horse_indices])
75+
not_horse_loss = np.mean(loss[not_horse_indices])
76+
print('horse', horse_loss)
77+
print('not horse', not_horse_loss)
78+
return hidden[7,:]
79+
80+
def decode(self, encoding):
81+
with tf.Session() as sess:
82+
sess.run(tf.initialize_all_variables())
83+
self.saver.restore(sess, './model.ckpt')
84+
reconstructed = sess.run(self.decoded, feed_dict={self.encoded: encoding})
85+
img = np.reshape(reconstructed, (32, 32))
86+
return img

0 commit comments

Comments
 (0)