Skip to content

Instantly share code, notes, and snippets.

@sotelo
Last active March 3, 2016 22:34
Show Gist options
  • Save sotelo/de05e5173d4315a302ff to your computer and use it in GitHub Desktop.
Save sotelo/de05e5173d4315a302ff to your computer and use it in GitHub Desktop.
test simple model file
from blocks.main_loop import MainLoop
from blocks.model import Model
from blocks.utils import shared_floatx_zeros, shared_floatx
from theano import tensor, config, function
from play.bricks.custom import (DeepTransitionFeedback, GMMEmitter,
SPF0Emitter)
from blocks.extensions.monitoring import TrainingDataMonitoring
from blocks.extensions import Printing
###################
# Define parameters of the model
###################
batch_size = 64 #for tpbtt
frame_size = 257 + 2
seq_size = 128
k = 20
target_size = frame_size * k
depth_x = 4
hidden_size_mlp_x = 2000
depth_theta = 4
hidden_size_mlp_theta = 2000
hidden_size_recurrent = 2000
depth_recurrent = 3
lr = 2e-4
floatX = theano.config.floatX
save_dir = './results/'
save_dir = os.path.join(save_dir,'blizzard/')
experiment_name = "baseline_sp"
#################
# Prepare dataset
#################
from parrot.datasets.blizzard import blizzard_stream
train_stream = blizzard_stream(('train',), batch_size)
valid_stream = blizzard_stream(
('valid',), batch_size, seq_length = 200,
num_examples = 64, sorting_mult = 1)
x_tr = next(train_stream.get_epoch_iterator())
#################
# Model
#################
f0 = tensor.matrix('f0')
voiced = tensor.matrix('voiced')
start_flag = tensor.scalar('start_flag')
sp = tensor.tensor3('spectrum')
f0s = f0.dimshuffle(0,1,'x')
voiceds = voiced.dimshuffle(0,1,'x')
x = tensor.concatenate([sp, f0s, voiceds], 2)
activations_x = [Rectifier()]*depth_x
dims_x = [frame_size] + [hidden_size_mlp_x]*(depth_x-1) + \
[hidden_size_recurrent]
activations_theta = [Rectifier()]*depth_theta
dims_theta = [hidden_size_recurrent] + \
[hidden_size_mlp_theta]*depth_theta
mlp_x = MLP(activations = activations_x,
dims = dims_x)
feedback = DeepTransitionFeedback(mlp = mlp_x)
transition = [GatedRecurrent(dim=hidden_size_recurrent,
name = "gru_{}".format(i) ) for i in range(depth_recurrent)]
transition = RecurrentStack( transition,
name="transition", skip_connections = True)
mlp_theta = MLP( activations = activations_theta,
dims = dims_theta)
emitter = SPF0Emitter(mlp = mlp_theta,
name = "emitter")
source_names = [name for name in transition.apply.states if 'states' in name]
readout = Readout(
readout_dim = hidden_size_recurrent,
source_names =source_names,
emitter=emitter,
feedback_brick = feedback,
name="readout")
generator = SequenceGenerator(readout=readout,
transition=transition,
name = "generator")
generator.weights_init = IsotropicGaussian(0.01)
generator.biases_init = Constant(0.)
generator.push_initialization_config()
generator.transition.biases_init = IsotropicGaussian(0.01,1)
generator.transition.push_initialization_config()
generator.initialize()
cost_matrix = generator.cost_matrix(x)
cost = cost_matrix.mean() + 0.*start_flag
cost.name = "nll"
cg = ComputationGraph(cost)
model = Model(cost)
monitoring_variables = [cost]
#################
# Algorithm
#################
algorithm = GradientDescent(
cost=cost, parameters=cg.parameters,
step_rule=CompositeRule([StepClipping(10.0), Adam(lr)]))
train_monitor = TrainingDataMonitoring(
variables=[cost],
every_n_batches=1,
prefix="train")
extensions=[
train_monitor,
Printing(after_batch = True)]
main_loop = MainLoop(
model=model,
data_stream=train_stream,
algorithm=algorithm,
extensions = extensions)
main_loop.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment