Skip to content

Commit

Permalink
issue #18: Recon subtask for first layer validations
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermevarela committed Oct 2, 2018
1 parent 68e0ec2 commit ba59b4c
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 100 deletions.
2 changes: 0 additions & 2 deletions models/estimators.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,6 @@ def valid_eval(Y, prefix='valid'):
[deep_srl.cost, deep_srl.optimize, deep_srl.predict, deep_srl.error],
feed_dict={X: X_batch, T: T_batch, L: L_batch}
)
if (step) % 250 == 0:
import code; code.interact(local=dict(globals(), **locals()))

total_loss += loss
total_error += error
Expand Down
13 changes: 8 additions & 5 deletions models/optimizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,19 @@ def __init__(self, X, T, L, r_depth=-1,

# down branch --> Handles classification
# [BATCH, MAX_TIME, 2*hidden_size[:1]] this tensor is zero padded from 3rd position
self.propagator_0 = InterleavedPropagator(X, L, hidden_size[:r_depth], ru=ru)

self.propagator_0 = InterleavedPropagator(X, L, hidden_size[:r_depth], ru=ru, i=0)
# merge the represenations
# print(self.predictor_0.get_shape())
# print(self.propagator_0.get_shape())
self.Xhat = tf.concat((self.propagator_0.propagate, tf.cast(self.predictor_0.predict, tf.float32)), axis=2)
self.Rflat = self.predictor_0.predict
self.Rhat = tf.one_hot(self.Rflat, 3, on_value=1, off_value=0)
# Non zero features over
# self.mask = tf.boolean_mask(self.Rhat, self.Rflat)
self.Xhat = tf.concat((self.propagator_0.propagate, tf.cast(self.Rhat, tf.float32)), axis=2)

# joint propagator
self.propagator_1 = InterleavedPropagator(self.Xhat, L, hidden_size[r_depth:], ru=ru)
self.predictor_1 = CRFPredictor(self.propagator_1, self.C, L, i=1)
self.propagator_1 = InterleavedPropagator(self.Xhat, L, hidden_size[r_depth:], ru=ru, i=1)
self.predictor_1 = CRFPredictor(self.propagator_1.propagate, self.C, L, i=1)

else:
raise NotImplementedError('This combination of parameters is not implemented')
Expand Down
85 changes: 12 additions & 73 deletions models/predictors.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@

class Predictor(object):

def __init__(self, V, T, seqlens, i=0):
self.predictor = 'CRF'
def __init__(self, V, T, L, i=0):
scope_id = 'CRF-{:}'.format(i)

self.V = V
self.T = T
self.Tflat = tf.cast(tf.argmax(T, 2), tf.int32)
self.seqlens = seqlens
self.L = L
self.i = i
self.score
self.cost
self.predict
self.Tflat = tf.cast(tf.argmax(T, 2), tf.int32)

with tf.variable_scope(scope_id):
self.score
self.cost
self.predict

def score(self):
raise NotImplementedError('Predictor must implement cost')
Expand Down Expand Up @@ -71,7 +73,7 @@ def cost(self):
'''
scope_id = 'cost{:}'.format(self.i)
with tf.variable_scope(scope_id):
args = (self.S, self.Tflat, self.seqlens)
args = (self.S, self.Tflat, self.L)
log_likelihood, self.transition_params = crf_log_likelihood(*args)

return tf.reduce_mean(-log_likelihood)
Expand All @@ -91,7 +93,7 @@ def predict(self):
scope_id = 'prediction{:}'.format(self.i)
with tf.variable_scope(scope_id):
# Compute the viterbi sequence and score.
args = (self.S, self.transition_params, self.seqlens)
args = (self.S, self.transition_params, self.L)
viterbi_sequence, viterbi_score = crf_decode(*args)

return tf.cast(viterbi_sequence, tf.int32)
Expand All @@ -107,67 +109,4 @@ def wo_shape(self):
def bo_shape(self):
# Static dimensions are of class Dimension(n)
t = int(self.T.get_shape()[-1])
return (t,)

# class CRFDualLabelPredictor(Predictor):
# '''Computes the viterbi_score for dual label tasks

# Previous works show that the argument recognition subtask is
# important. Have it being computed in parallel instead of
# having it computed as a pipeline

# Instead of having:
# B-A0, I-A0, B-V, B-A1, I-A1, I-A1

# Have:
# (B, A0), (I, A0), (B, V), (B, A1), (I, A1), (I, A1)

# Extends:
# Predictor
# '''
# def __init__(self, Scores, T, seqlens):
# self.predictor = 'CRF'

# self.Scores = Scores
# self.T = tf.cast(tf.argmax(T, 2), tf.int32)
# self.seqlens = seqlens

# self.cost
# self.predict

# @lazy_property
# def cost(self):
# '''Computes the viterbi_score after the propagation step, returns the cost.

# Consumes the representation coming from propagation layer, evaluates
# the log_likelihod and parameters

# Decorators:
# lazy_property

# Returns:
# cost {tf.float64} -- A scalar holding the average log_likelihood
# of the loss by estimatiof
# '''
# with tf.variable_scope('cost'):
# log_likelihood, self.transition_params = tf.contrib.crf.crf_log_likelihood(self.Scores, self.T, self.seqlens)

# return tf.reduce_mean(-log_likelihood)

# @lazy_property
# def predict(self):
# '''Decodes the viterbi score for the inputs

# Consumes both results from propagation and and cost layers

# Decorators:
# lazy_property

# Returns:
# [type] -- [description]
# '''
# with tf.variable_scope('prediction'):
# # Compute the viterbi sequence and score.
# viterbi_sequence, viterbi_score = tf.contrib.crf.crf_decode(self.Scores, self.transition_params, self.seqlens)

# return tf.cast(viterbi_sequence, tf.int32)
return (t,)
31 changes: 17 additions & 14 deletions models/propagators.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ def get_unit(sz, ru='BasicLSTM'):

class Propagator(object):

def __init__(self, V, seqlens, hidden_sz_list, ru='BasicLSTMCell'):
self.ru = ru
self.propagator = 'interleaved'
def __init__(self, V, L, hidden_sz_list, ru='BasicLSTMCell', i=0):
scope_id = 'DB{:}{:}'.format(ru, i)

self.hidden_sz_list = hidden_sz_list
self.V = V
self.seqlens = seqlens
self.L = L

self.propagate
self.ru = ru
self.i = i

self.hidden_sz_list = hidden_sz_list
with tf.variable_scope(scope_id):
self.propagate

def propagate(self):
raise NotImplementedError('Propagator must implement propagate')
Expand Down Expand Up @@ -68,7 +71,7 @@ def propagate(self):
outputs_fw, states = tf.nn.dynamic_rnn(
cell=self.cell_fw,
inputs=self.V,
sequence_length=self.seqlens,
sequence_length=self.L,
dtype=tf.float32,
time_major=False
)
Expand All @@ -78,21 +81,21 @@ def propagate(self):

inputs_bw = tf.reverse_sequence(
outputs_fw,
self.seqlens,
self.L,
batch_axis=0,
seq_axis=1
)

outputs_bw, states = tf.nn.dynamic_rnn(
cell=self.cell_bw,
inputs=inputs_bw,
sequence_length=self.seqlens,
sequence_length=self.L,
dtype=tf.float32,
time_major=False
)
outputs_bw = tf.reverse_sequence(
outputs_bw,
self.seqlens,
self.L,
batch_axis=0,
seq_axis=1
)
Expand All @@ -108,15 +111,15 @@ def propagate(self):
outputs_fw, states = tf.nn.dynamic_rnn(
cell=self.cell_fw,
inputs=inputs_fw,
sequence_length=self.seqlens,
sequence_length=self.L,
dtype=tf.float32,
time_major=False)

with tf.variable_scope('bw{:}'.format(i + 1)):
inputs_bw = tf.concat((outputs_fw, h), axis=2)
inputs_bw = tf.reverse_sequence(
inputs_bw,
self.seqlens,
self.L,
batch_axis=0,
seq_axis=1
)
Expand All @@ -125,13 +128,13 @@ def propagate(self):
outputs_bw, states = tf.nn.dynamic_rnn(
cell=self.cell_bw,
inputs=inputs_bw,
sequence_length=self.seqlens,
sequence_length=self.L,
dtype=tf.float32,
time_major=False)

outputs_bw = tf.reverse_sequence(
outputs_bw,
self.seqlens,
self.L,
batch_axis=0,
seq_axis=1
)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit ba59b4c

Please sign in to comment.