#!/usr/bin/env python

import climate
import io
import numpy as np
import theanets
import scipy.io
import os
import tempfile
import urllib
import zipfile

logging = climate.get_logger('lstm-chime')

climate.enable_default_logging()

# do fixed segments for now (warning: each segment does not correspond to real utterances!)
SEQLEN = 100
BATCH_SIZE = 50
TRAIN_NC = os.path.join(tempfile.gettempdir(), 'chime1_train.nc')
VALID_NC = os.path.join(tempfile.gettempdir(), 'chime1_valid.nc')

if not os.path.isfile(TRAIN_NC) or not os.path.isfile(VALID_NC):
    # get the data files from the repository at https://github.com/craffel/lstm_benchmarks
    zipurl = 'https://github.com/craffel/lstm_benchmarks/archive/master.zip'
    logging.info('attempting data copy from url: %s', zipurl)
    z = zipfile.ZipFile(io.BytesIO(urllib.urlopen(zipurl).read()))
    with open(TRAIN_NC, 'wb') as savefile:
        savefile.write(z.read('lstm_benchmarks-master/data/train_1_speaker.nc'))
    with open(VALID_NC, 'wb') as savefile:
        savefile.write(z.read('lstm_benchmarks-master/data/val_1_speaker.nc'))
    z.close()

def batch_at(data, start):
    dtype, shape = 'int32', (SEQLEN, BATCH_SIZE)
    if len(data.shape) > 1:
        dtype, shape = 'f', (SEQLEN, BATCH_SIZE, data.shape[-1])
    X = np.zeros(shape, dtype=dtype)
    for b in range(BATCH_SIZE):
        s = start + SEQLEN * b
        X[:, b] = data[s:s + SEQLEN]
    return X

def batches(dataset):
    steps = dataset.dimensions['numTimesteps']
    def sample():
        i = np.random.randint(steps - SEQLEN * BATCH_SIZE)
        s = batch_at(dataset.variables['inputs'].data, i)
        t = batch_at(dataset.variables['targetClasses'].data, i)
        return [s, t]
    return sample

e = theanets.Experiment(
    theanets.recurrent.Classifier,
    layers=(39, ('lstm', 100), ('lstm', 200), ('lstm', 78), 51),
    recurrent_error_start=0,
    output_activation='softmax',
    hidden_activation='tanh',
    batch_size=BATCH_SIZE,
)
e.train(batches(scipy.io.netcdf_file(open(TRAIN_NC))),
        batches(scipy.io.netcdf_file(open(VALID_NC))))

#!/usr/bin/env python

import climate
import io
import numpy as np
import theanets
import scipy.io
import os
import tempfile
import urllib
import zipfile

logging = climate.get_logger('lstm-chime')

climate.enable_default_logging()

# do fixed segments for now (warning: each segment does not correspond to real utterances!)
SEQLEN = 100
BATCH_SIZE = 50
TRAIN_NC = os.path.join(tempfile.gettempdir(), 'chime1_train.nc')
VALID_NC = os.path.join(tempfile.gettempdir(), 'chime1_valid.nc')

if not os.path.isfile(TRAIN_NC) or not os.path.isfile(VALID_NC):
    # get the data files from the repository at https://github.com/craffel/lstm_benchmarks
    zipurl = 'https://github.com/craffel/lstm_benchmarks/archive/master.zip'
    logging.info('attempting data copy from url: %s', zipurl)
    z = zipfile.ZipFile(io.BytesIO(urllib.urlopen(zipurl).read()))
    with open(TRAIN_NC, 'wb') as savefile:
        savefile.write(z.read('lstm_benchmarks-master/data/train_1_speaker.nc'))
    with open(VALID_NC, 'wb') as savefile:
        savefile.write(z.read('lstm_benchmarks-master/data/val_1_speaker.nc'))
    z.close()

def batch_at(data, start):
    dtype, shape = 'int32', (SEQLEN, BATCH_SIZE)
    if len(data.shape) > 1:
        dtype, shape = 'f', (SEQLEN, BATCH_SIZE, data.shape[-1])
    X = np.zeros(shape, dtype=dtype)
    for b in range(BATCH_SIZE):
        s = start + SEQLEN * b
        X[:, b] = data[s:s + SEQLEN]
    return X

def batches(dataset):
    steps = dataset.dimensions['numTimesteps']
    def sample():
        i = np.random.randint(steps - SEQLEN * BATCH_SIZE)
        s = batch_at(dataset.variables['inputs'].data, i)
        t = batch_at(dataset.variables['targetClasses'].data, i)
        return [s, t]
    return sample

e = theanets.Experiment(
    theanets.recurrent.Classifier,
    layers=(39, ('lstm', 156), ('lstm', 300), ('lstm', 102), 51),
    recurrent_error_start=0,
    output_activation='softmax',
    hidden_activation='tanh',
    batch_size=BATCH_SIZE,
)
e.train(batches(scipy.io.netcdf_file(open(TRAIN_NC))),
        batches(scipy.io.netcdf_file(open(VALID_NC))))

Update LSTM example to match benchmark configuration.