Skip to content

Commit

Permalink
trying seq2seq w/ shorted seq
Browse files Browse the repository at this point in the history
  • Loading branch information
pkmital committed Apr 5, 2018
1 parent 8a5940a commit c4d6df2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 41 deletions.
6 changes: 3 additions & 3 deletions lstm_mdn.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ def create_model(batch_size=50,
source_output)
mse = tf.losses.mean_squared_error(sample, source_output)
else:
weighted_mse_loss = tf.constant(0)
mse = tf.constant(0)
loss = mdn_loss
weighted_mse_loss = tf.constant(0.0)
mse = tf.constant(0.0)
loss = mdn_loss + weighted_mse_loss

return {
'source': source,
Expand Down
4 changes: 2 additions & 2 deletions seq2seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,7 @@ def create_model(batch_size=50,
cat=tfd.Categorical(probs=weights), components=components)

with tf.variable_scope('loss'):
p = gauss.log_prob(decoder_output)
negloglike = -tf.reduce_logsumexp(p, axis=1)
negloglike = -gauss.log_prob(decoder_output)
weighted_reconstruction = tf.reduce_mean(
tf.expand_dims(weights, 2) * means, 3)
mdn_loss = tf.reduce_mean(negloglike)
Expand All @@ -399,6 +398,7 @@ def create_model(batch_size=50,
'keep_prob': keep_prob,
'encoding': encoder_state,
'decoding': infer_outputs,
'weighted': weighted_reconstruction,
'loss': loss,
'mdn_loss': mdn_loss,
'mse_loss': mse_loss
Expand Down
47 changes: 25 additions & 22 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

def euler():
data = np.load('euler.npy')
mean_data = np.mean(data)
std_data = np.std(data)
data = (data.reshape([data.shape[0], -1]) - mean_data) / std_data
data_mean = np.mean(data)
data_std = np.std(data)
data = (data.reshape([data.shape[0], -1]) - data_mean) / data_std
n_features = data.shape[-1]
batch_size = 50
sequence_length = 240
Expand All @@ -21,8 +21,8 @@ def euler():

train.infer(
data=data,
mean_data=mean_data,
std_data=std_data,
data_mean=data_mean,
data_std=data_std,
batch_size=batch_size,
sequence_length=sequence_length,
n_features=n_features,
Expand All @@ -37,9 +37,9 @@ def euler():

def euler_v2():
data = np.load('euler.npy')
mean_data = np.mean(data)
std_data = np.std(data)
data = (data.reshape([data.shape[0], -1]) - mean_data) / std_data
data_mean = np.mean(data)
data_std = np.std(data)
data = (data.reshape([data.shape[0], -1]) - data_mean) / data_std
n_features = data.shape[-1]
batch_size = 50
sequence_length = 240
Expand All @@ -53,8 +53,8 @@ def euler_v2():

train.infer(
data=data,
mean_data=mean_data,
std_data=std_data,
data_mean=data_mean,
data_std=data_std,
batch_size=batch_size,
sequence_length=sequence_length,
n_features=n_features,
Expand All @@ -69,9 +69,9 @@ def euler_v2():

def euler_v3():
data = np.load('euler.npy')
mean_data = np.mean(data)
std_data = np.std(data)
data = (data.reshape([data.shape[0], -1]) - mean_data) / std_data
data_mean = np.mean(data)
data_std = np.std(data)
data = (data.reshape([data.shape[0], -1]) - data_mean) / data_std
n_features = data.shape[-1]
sequence_length = 120
input_embed_size = None
Expand All @@ -94,8 +94,8 @@ def euler_v3():
res = train.infer(
source=source,
target=target,
mean_data=mean_data,
std_data=std_data,
data_mean=data_mean,
data_std=data_std,
batch_size=batch_size,
sequence_length=sequence_length,
n_features=n_features,
Expand All @@ -111,12 +111,15 @@ def euler_v3():
def euler_v4():
data = np.load('euler.npy')
data = data.reshape(data.shape[0], -1)
mean_data = np.mean(data, axis=0)
std_data = np.std(data, axis=0)
data = (data - mean_data) / std_data
data_mean = np.mean(data, axis=0)
data_std = np.std(data, axis=0)
idxs = np.where(data_std > 0)[0]
data_mean = data_mean[idxs]
data_std = data_std[idxs]
data = (data[:, idxs] - data_mean) / data_std
n_features = data.shape[-1]
batch_size = 20
sequence_length = 500
batch_size = 64
sequence_length = 120
input_embed_size = None
n_neurons = 512
n_layers = 3
Expand All @@ -127,8 +130,8 @@ def euler_v4():

res = train.train(
data=data,
mean_data=mean_data,
std_data=std_data,
data_mean=data_mean,
data_std=data_std,
batch_size=batch_size,
sequence_length=sequence_length,
n_features=n_features,
Expand Down
25 changes: 13 additions & 12 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def batch_generator(data, sequence_length, batch_size=50):


def train(data,
mean_data,
std_data,
data_mean,
data_std,
n_epochs=1000,
batch_size=100,
sequence_length=240,
Expand Down Expand Up @@ -116,8 +116,8 @@ def train(data,

def infer(source,
target,
mean_data,
std_data,
data_mean,
data_std,
batch_size,
sequence_length,
ckpt_path='./',
Expand All @@ -134,24 +134,25 @@ def infer(source,
sess.run(init_op)
saver = tf.train.Saver()
saver.restore(sess, os.path.join(ckpt_path, model_name))
recon, enc = sess.run(
[net['decoding'], net['encoding']],
weighted, recon, enc = sess.run(
[net['weighted'], net['decoding'], net['encoding']],
feed_dict={
net['source']: source,
net['keep_prob']: 1.0
})
src = (source * std_data) + mean_data
tgt = (target * std_data) + mean_data
res = (recon[0] * std_data) + mean_data
src = (source * data_std) + data_mean
tgt = (target * data_std) + data_mean
res = (recon[0] * data_std) + data_mean
wgt = (weighted[0] * data_std) + data_mean
fig, axs = plt.subplots(2, 2)
axs[0][0].plot(src.reshape(-1, src.shape[-1]))
axs[0][0].set_title('Source')
axs[0][1].plot(tgt.reshape(-1, tgt.shape[-1]))
axs[0][1].set_title('Target (Original)')
axs[1][0].plot(src.reshape(-1, src.shape[-1]))
axs[1][0].set_title('Source')
axs[1][0].plot(wgt.reshape(-1, src.shape[-1]))
axs[1][0].set_title('Target (Synthesis Weighted)')
axs[1][1].plot(res.reshape(-1, res.shape[-1]))
axs[1][1].set_title('Target (Synthesis)')
axs[1][1].set_title('Target (Synthesis Sampling)')
np.save('source.npy', src)
np.save('target.npy', tgt)
np.save('encoding.npy', enc)
Expand Down
4 changes: 2 additions & 2 deletions train_lstm.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def train(data,
'{}: total_loss: {} total_weighted_mse: {}'.
format(it_i, loss, weighted_mse),
end='\r')
current_learning_rate = max(0.0001, current_learning_rate * 0.99)
current_learning_rate = max(0.0001, current_learning_rate * 0.995)
print('iteration: {}, learning rate: {}'.format(
it_i, current_learning_rate))
print(
Expand Down Expand Up @@ -198,7 +198,7 @@ def test_euler():
n_gaussians = 10
use_mdn = True
model_name = 'lstm_mdn-euler'
restore_name = None
restore_name = 'lstm_mdn-euler-365'
overfit = False

if overfit:
Expand Down

0 comments on commit c4d6df2

Please sign in to comment.