In [1]:
from __future__ import print_function, division
import numpy as np
import matplotlib.pyplot as plt
import theano
import theano.tensor as T
In [2]:
SEQ_LENGTH = 1000
def generate_single_example(power, duration, start):
activation = np.ones(duration, dtype=np.float32) * power
sequence = np.zeros(SEQ_LENGTH, dtype=np.float32)
sequence[start:start+duration] += activation
return sequence
In [3]:
N_SEQ_PER_BATCH = 5
def generate_example(*args, **kwargs):
data = np.empty((N_SEQ_PER_BATCH, SEQ_LENGTH, 1))
for i in range(N_SEQ_PER_BATCH):
data[i,:,0] = generate_single_example(*args, **kwargs)
return data
In [4]:
error = 50
power = 100
short_seq_t = generate_single_example(power=power, duration=100, start=100)
short_seq_x = generate_single_example(power=power+error, duration=100, start=100)
long_seq_t = generate_single_example(power=power, duration=500, start=100)
long_seq_x = generate_single_example(power=power+error, duration=500, start=100)
In [5]:
def cost(x, t):
return np.mean((x - t) ** 2)
In [6]:
cost(short_seq_t, short_seq_x)
Out[6]:
In [7]:
cost(long_seq_t, long_seq_x)
Out[7]:
In [8]:
short_seq_t[short_seq_t > 0]
Out[8]:
In [48]:
from theano.ifelse import ifelse
THRESHOLD = 0
def cost2(x, t):
sq_error = (x - t) ** 2
def mask_and_mean_sq_error(mask):
masked_sq_error = sq_error[mask.nonzero()]
mean = masked_sq_error.mean()
mean = ifelse(T.isnan(mean), 0.0, mean)
return mean
above_thresh_mean = mask_and_mean_sq_error(t > THRESHOLD)
below_thresh_mean = mask_and_mean_sq_error(t <= THRESHOLD)
return (above_thresh_mean + below_thresh_mean) / 2.0
In [49]:
cost2(theano.shared(short_seq_x), theano.shared(short_seq_t)).eval()
Out[49]:
In [50]:
cost2(short_seq_x, short_seq_t)
Out[50]:
In [51]:
cost2(theano.shared(np.zeros(TOTAL_LENGTH, dtype=np.float32)), theano.shared(np.zeros(TOTAL_LENGTH, dtype=np.float32))).eval()
Out[51]:
In [6]:
(t > THRESHOLD).shape[0].eval()
In [35]:
(0-0) ** 2
Out[35]:
In [41]:
t = generate_example(power=power, duration=100, start=100)
# t = theano.shared(t)
In [42]:
n_above_thresh_per_seq = (t > 0).sum(axis=1)
In [43]:
n_below_thresh_per_seq = SEQ_LENGTH - n_above_thresh_per_seq
In [49]:
n_above_thresh_per_seq
Out[49]:
In [45]:
mask = np.ones(t.shape)
In [52]:
mask[(t > 0)] *= np.ones(500)
In [29]:
mask.shape
Out[29]:
In [25]:
(t > 0).nonzero().eval()
In [13]:
mask.shape
Out[13]:
In [59]:
#for seq_i in range(t.shape[0]):
seq_i = 0
THRESHOLD = 5
error = t.copy()
elements_above_thresh = t[seq_i, :, 0] > THRESHOLD
n_above_thresh = elements_above_thresh.sum()
error[seq_i, elements_above_thresh, 0] *= 1 / n_above_thresh
n_below_thresh = SEQ_LENGTH - n_above_thresh
error[seq_i, -elements_above_thresh, 0] *= 1 / n_below_thresh
error[seq_i,:,0].sum() / 2
Out[59]:
In [55]:
n_above_thresh
Out[55]:
In [56]:
SEQ_LENGTH
Out[56]:
In [61]:
theano.shared(np.zeros(10, dtype=np.float32)).eval()
Out[61]:
In [ ]: