class Batcher: def __init__(self, batch_dim, train_percentage=90): from Utilities.Sound import get_sound, sound_cut from Utilities.Pretreatment import Normalize data = sound_cut(get_sound("XqaJ2Ol5cC4").astype(np.float32)) cut = data.shape[0] * train_percentage // 100 self.pre = Normalize() self.pre.fit(data) self.train_data = self.pre.cmp(data[:cut]) self.valid_data = self.pre.cmp(data[cut:]) self.batch_dim = batch_dim self.n_batch = 0 self.train_max = self.train_data.shape[0] - batch_dim[ 0] - batch_dim[1] * batch_dim[2] - 1 self.starts = np.arange(0, self.train_max - batch_dim[2], batch_dim[0]) np.random.shuffle(self.starts) def get_batch(self): self.n_batch += 1 batch_dim = self.batch_dim start = self.starts[self.n_batch % self.starts.shape[0]] x_data = np.asarray([ self.train_data[start + i:start + i + batch_dim[1] * batch_dim[2]].reshape( (batch_dim[1], batch_dim[2])) for i in range(batch_dim[0]) ]) y_data = np.asarray([ self.train_data[start + i + batch_dim[2]:start + i + (batch_dim[1] + 1) * batch_dim[2]].reshape( (batch_dim[1], batch_dim[2])) for i in range(batch_dim[0]) ]) return x_data, y_data def get_valid_batch(self): batch_dim = self.batch_dim x_data = np.asarray([ self.valid_data[i:i + batch_dim[1] * batch_dim[2]].reshape( (batch_dim[1], batch_dim[2])) for i in range(batch_dim[0]) ]) y_data = np.asarray([ self.valid_data[i + batch_dim[2]:i + (batch_dim[1] + 1) * batch_dim[2]].reshape( (batch_dim[1], batch_dim[2])) for i in range(batch_dim[0]) ]) return x_data, y_data def get_percentage(self): return 100 * self.n_batch * self.batch_dim[0] // self.train_max def get_params(self): return self.n_batch def set_params(self, params): self.n_batch = params
def __init__(self, batch_dim, train_percentage=90): from Utilities.Sound import get_sound, sound_cut from Utilities.Pretreatment import Normalize data = sound_cut(get_sound("XqaJ2Ol5cC4").astype(np.float32)) cut = data.shape[0] * train_percentage // 100 self.pre = Normalize() self.pre.fit(data) self.train_data = self.pre.cmp(data[:cut]) self.valid_data = self.pre.cmp(data[cut:]) self.batch_dim = batch_dim self.n_batch = 0 self.train_max = self.train_data.shape[0] - batch_dim[ 0] - batch_dim[1] * batch_dim[2] - 1 self.starts = np.arange(0, self.train_max - batch_dim[2], batch_dim[0]) np.random.shuffle(self.starts)
class Batcher: def __init__(self, batch_dim, train_percentage=90): from Utilities.Sound import get_sound, sound_cut from Utilities.Pretreatment import Normalize data = sound_cut(get_sound("XqaJ2Ol5cC4").astype(np.float32)) cut = data.shape[0]*train_percentage//100 self.pre = Normalize() self.pre.fit(data) self.train_data = self.pre.cmp(data[:cut]) self.valid_data = self.pre.cmp(data[cut:]) self.batch_dim = batch_dim self.n_batch = 0 self.train_max = self.train_data.shape[0]-batch_dim[0]-batch_dim[1]*batch_dim[2]-1 self.starts = np.arange(0, self.train_max-batch_dim[2], batch_dim[0]) np.random.shuffle(self.starts) def get_batch(self): self.n_batch += 1 batch_dim = self.batch_dim start = self.starts[self.n_batch%self.starts.shape[0]] x_data = np.asarray([self.train_data[start+i:start+i+batch_dim[1]*batch_dim[2]].reshape((batch_dim[1], batch_dim[2])) for i in range(batch_dim[0])]) y_data = np.asarray([self.train_data[start+i+batch_dim[2]:start+i+(batch_dim[1]+1)*batch_dim[2]].reshape((batch_dim[1], batch_dim[2])) for i in range(batch_dim[0])]) return x_data, y_data def get_valid_batch(self): batch_dim = self.batch_dim x_data = np.asarray([self.valid_data[i:i+batch_dim[1]*batch_dim[2]].reshape((batch_dim[1], batch_dim[2])) for i in range(batch_dim[0])]) y_data = np.asarray([self.valid_data[i+batch_dim[2]:i+(batch_dim[1]+1)*batch_dim[2]].reshape((batch_dim[1], batch_dim[2])) for i in range(batch_dim[0])]) return x_data, y_data def get_percentage(self): return 100*self.n_batch*self.batch_dim[0]//self.train_max def get_params(self): return self.n_batch def set_params(self, params): self.n_batch = params
def __init__(self, batch_dim, train_percentage=90): from Utilities.Sound import get_sound, sound_cut from Utilities.Pretreatment import Normalize data = sound_cut(get_sound("XqaJ2Ol5cC4").astype(np.float32)) cut = data.shape[0]*train_percentage//100 self.pre = Normalize() self.pre.fit(data) self.train_data = self.pre.cmp(data[:cut]) self.valid_data = self.pre.cmp(data[cut:]) self.batch_dim = batch_dim self.n_batch = 0 self.train_max = self.train_data.shape[0]-batch_dim[0]-batch_dim[1]*batch_dim[2]-1 self.starts = np.arange(0, self.train_max-batch_dim[2], batch_dim[0]) np.random.shuffle(self.starts)
def __init__(self, name, batch_dim, preprocess=Normalize(), rate=16000, n_valid=128): self.name = name self.batch_dim = batch_dim self.preprocess = preprocess self.rate = rate self.n_valid = n_valid self.valid_len = (batch_dim[0] + 1) * batch_dim[2] * n_valid self.sound = sound_cut(get_sound(name).astype(config.floatX)) self.time = self.sound.shape[0] / float(rate) self.preprocess.fit(self.sound[:-self.valid_len]) self.train = self.preprocess.cmp(self.sound[:-self.valid_len]) self.valid = self.preprocess.cmp(self.sound[-self.valid_len:]) self.n_batch = 0