def feature_spec(self): ''' data meta''' output_shapes = (tf.TensorShape([self.window_len, self.final_feat_dim]), tf.TensorShape([])) output_types = (tf.float32, tf.int32) return output_shapes, output_types
def test_generate_synthetic_data(self): ''' generate sythetic data unittest''' input_shape = tf.TensorShape([2, 3]) input_value = 1 input_dtype = tf.float32 label_shape = tf.TensorShape([2]) label_value = 2 label_dtype = tf.int32 nepoch = 2 data_set = misc.generate_synthetic_data(input_shape, input_value, input_dtype, label_shape, label_value, label_dtype, nepoch) iterator = data_set.make_one_shot_iterator() with self.cached_session(use_gpu=False, force_gpu=False): data, label = iterator.get_next() self.assertAllEqual(data.eval(), np.ones(shape=input_shape, dtype=np.float32)) self.assertAllEqual( label.eval(), 2 * np.ones(shape=label_shape, dtype=np.float32)) with self.assertRaises(tf.errors.OutOfRangeError): data.eval()
def feature_spec(self): """Get shapes for feature.""" feature_shapes = [(tf.TensorShape([self.max_seq_len]), tf.TensorShape([]))] if not self.infer_without_label: feature_shapes.append(tf.TensorShape([self.intent_num_classes])) feature_shapes.append(tf.TensorShape([self.max_seq_len])) if len(feature_shapes) == 1: return feature_shapes[0] return tuple(feature_shapes)
def batch_input_shape(self): ''' batch input TensorShape''' feat, label = self.__getitem__(0) feat_shape = {} for key, val in feat.items(): feat_shape[key] = tf.TensorShape((None,) + val.shape[1:]) label_shape = tf.TensorShape((None,) + label.shape[1:]) return feat_shape, label_shape
def feature_spec(self, batch_size_): # pylint: disable=arguments-differ ''' uttid: [] feat: [feat_shape] src_len: [] label: [None] tgt_len: [] ''' values = None batch_size = None time = None if self.dummy: batch_size = batch_size_ time = 10 logging.info("Dummy data: batch size {} time {}".format( batch_size, time)) types = (tf.string, tf.float32, tf.int32, tf.int32, tf.int32) if self.batch_mode or self.dummy: # batch of examples shapes = ( #uttid tf.TensorShape([batch_size]), # input tf.TensorShape([batch_size, time, *self.feat_shape]), # input len tf.TensorShape([batch_size]), # output tf.TensorShape([batch_size, time]), # output len tf.TensorShape([batch_size]), ) else: # one example shapes = ( #uttid tf.TensorShape([]), # input tf.TensorShape([time, *self.feat_shape]), # input len tf.TensorShape([]), # output tf.TensorShape([time]), # output len tf.TensorShape([]), ) if self.dummy: values = ("uttid_1", 1, 2, 3, 4) logging.info("Dummy data: shapes {}".format(shapes)) logging.info("Dummy data: types {}".format(types)) logging.info("Dummy data: values {}".format(values)) return types, shapes, values
def batch_input_shape(self): ''' batch input TensorShape ''' feature, labels = self.__getitem__(0) feature_shape, label_shape = {}, {} for feature_key, feature_val in feature.items(): feature_shape[feature_key] = tf.TensorShape((None, ) + feature_val.shape[1:]) for label_key, label_val in labels.items(): label_shape[label_key] = tf.TensorShape((None, ) + label_val.shape[1:]) return feature_shape, label_shape
def feature_spec(self): output_shapes = ( tf.TensorShape([]), # utt tf.TensorShape([]), # segid tf.TensorShape([None, self.feat_size, 1]), # audio_feat, without batch dim e.g. (3000, 40, 3) tf.TensorShape([]), # spkid ) output_types = ( tf.string, tf.int32, tf.float32, tf.int32, ) assert len(output_shapes) == len(output_types) return output_shapes, output_types
def feature_spec(self): output_shapes = ( tf.TensorShape(self.feature_shape), # audio_feat e.g. (3000, 40, 3) tf.TensorShape([]), # label tf.TensorShape([]), # filename tf.TensorShape([]), # clip_id tf.TensorShape([1]), # soft_label, disabled for speaker model ) output_types = ( tf.float32, tf.int32, tf.string, tf.int32, tf.float32, ) assert len(output_shapes) == len(output_types) return output_shapes, output_types
def splice(feat, left_context, right_context): ''' splice frame with context param: feat, tf.float32, [batch, time, feat] return: feat, tf.float32, [batch, time, feat*(left_context + 1 + right_context)] reference: https://github.com/kaldi-asr/kaldi/src/feat/feature-functions.cc#L205:6 ''' def _loop_continue(time, end_time, context, unused_left_context, right_context, unused_output_tas): del unused_output_tas del unused_left_context return time < end_time def _loop_body(time, end_time, context, left_context, right_context, output_tas): shape = tf.shape(context) B, _, D = shape[0], shape[1], shape[2] N = (1 + left_context + right_context) * D new_feat = context[:, time:time + left_context + 1 + right_context, :] new_feat = tf.reshape(new_feat, [B, N]) new_output_tas = output_tas.write(time, new_feat) return (time + 1, end_time, context, left_context, right_context, new_output_tas) with tf.control_dependencies([ tf.assert_greater_equal(left_context, 0), tf.assert_greater_equal(right_context, 0) ]): T = tf.shape(feat)[1] output_tas = _new_tensor_array('splice_feat_ta', T, dtype=tf.float32) time = tf.constant(0, tf.int32) first = tf.tile(feat[:, 0:1, :], [1, left_context, 1]) last = tf.tile(feat[:, -1:, :], [1, right_context, 1]) context = tf.concat([first, feat], axis=1) context = tf.concat([context, last], axis=1) loop_vars = (time, T, context, left_context, right_context, output_tas) parallel_iterations = 10 shape_invariants = tf.nest.map_structure( lambda t: tf.TensorShape(None), loop_vars) (time, end_time, context, left_context, right_context, output_tas) = tf.while_loop(_loop_continue, _loop_body, loop_vars=loop_vars, shape_invariants=shape_invariants, parallel_iterations=parallel_iterations, swap_memory=False) del context del left_context del right_context batch_spliced_feats = output_tas.stack() batch_spliced_feats = tf.transpose(batch_spliced_feats, [1, 0, 2]) return batch_spliced_feats
def feature_spec(self): """Get shapes for feature.""" if not self.infer_without_label: feature_shapes = [ (tf.TensorShape([tf.Dimension(None)]), tf.TensorShape([]), tf.TensorShape([tf.Dimension(None)]), tf.TensorShape([])) ] feature_shapes.append(tf.TensorShape([tf.Dimension(None)])) else: feature_shapes = [(tf.TensorShape([tf.Dimension(None)]), tf.TensorShape([]))] if len(feature_shapes) == 1: return feature_shapes[0] return tuple(feature_shapes)
def batch_extract_feature(waveforms, params): ''' waveforms: [batch, samples, audio_channels] return: features [batch, nframes, feat_size, channles] ''' def _to_tensor_array(name, v, clear_after_read=None): ''' create TensorArray from v, of size batch.''' ta = tf.TensorArray(v.dtype, batch, name=name, clear_after_read=clear_after_read) ta = ta.unstack(v) return ta def _loop_continue(time, inputs, unused_output_tas): del unused_output_tas batch = tf.shape(inputs)[0] return time < batch def _loop_body(time, inputs, output_tas): feat = extract_feature(inputs[time, ...], params) new_output_tas = output_tas.write(time, feat) return (time + 1, inputs, new_output_tas) batch = tf.shape(waveforms)[0] output_tas = _new_tensor_array('batch_feat', batch, dtype=tf.float32) time = tf.constant(0, tf.int32) loop_vars = (time, waveforms, output_tas) parallel_iterations = 10 shape_invariants = tf.nest.map_structure(lambda t: tf.TensorShape(None), loop_vars) (time, inputs, output_tas) = tf.while_loop(_loop_continue, _loop_body, loop_vars=loop_vars, shape_invariants=shape_invariants, parallel_iterations=parallel_iterations, swap_memory=False) del inputs batch_feats = output_tas.stack() return batch_feats
def beam_search(symbols_to_logits_fn, initial_ids, beam_size, decode_length, vocab_size, alpha, eos_id, states=None, stop_early=True, INF=1. * 1e20): """Beam search with length penalties.""" batch_size = utils.shape_list(initial_ids)[0] initial_log_probs = tf.constant([[0.] + [-INF] * (beam_size - 1)]) # (batch_size, beam_size) alive_log_probs = tf.tile(initial_log_probs, [batch_size, 1]) alive_seq = utils.expand_to_beam_size(initial_ids, beam_size) # (batch_size, beam_size, 1) alive_seq = tf.expand_dims(alive_seq, axis=2) if states: states = nest.map_structure( lambda state: utils.expand_to_beam_size(state, beam_size), states) else: states = {} # (batch_size, beam_size, 1) finished_seq = tf.zeros(utils.shape_list(alive_seq), tf.int32) # (batch_size, beam_size) finished_scores = tf.ones([batch_size, beam_size]) * -INF # (batch_size, beam_size) finished_flags = tf.zeros([batch_size, beam_size], tf.bool) def grow_finished(finished_seq, finished_scores, finished_flags, curr_seq, curr_scores, curr_finished): """ Given sequences and scores from finished sequence and current finished sequence , will gather the top k=beam size sequences to update finished seq. """ # padding zero for finished seq finished_seq = tf.concat( [finished_seq, tf.zeros([batch_size, beam_size, 1], tf.int32)], axis=2) # mask unfinished curr seq curr_scores += (1. - tf.to_float(curr_finished)) * -INF # concatenating the sequences and scores along beam axis # (batch_size, 2xbeam_size, seq_len) curr_finished_seq = tf.concat([finished_seq, curr_seq], axis=1) curr_finished_scores = tf.concat([finished_scores, curr_scores], axis=1) curr_finished_flags = tf.concat([finished_flags, curr_finished], axis=1) return utils.compute_topk_scores_and_seq( curr_finished_seq, curr_finished_scores, curr_finished_scores, curr_finished_flags, beam_size, batch_size, "grow_finished") def grow_alive(curr_seq, curr_scores, curr_log_probs, curr_finished, states): """Given sequences and scores, will gather the top k=beam size sequences.""" curr_scores += tf.to_float(curr_finished) * -INF return utils.compute_topk_scores_and_seq(curr_seq, curr_scores, curr_log_probs, curr_finished, beam_size, batch_size, "grow_alive", states) def grow_topk(i, alive_seq, alive_log_probs, states): """Inner beam search loop.""" flat_ids = tf.reshape(alive_seq, [batch_size * beam_size, -1]) # (batch_size * beam_size, decoded_length) if states: flat_states = nest.map_structure(utils.merge_beam_dim, states) flat_logits, flat_states = symbols_to_logits_fn( flat_ids, i, flat_states) states = nest.map_structure( lambda t: utils.unmerge_beam_dim(t, batch_size, beam_size), flat_states) else: flat_logits = symbols_to_logits_fn(flat_ids) logits = tf.reshape(flat_logits, [batch_size, beam_size, -1]) candidate_log_probs = utils.log_prob_from_logits(logits) log_probs = candidate_log_probs + tf.expand_dims(alive_log_probs, axis=2) length_penalty = tf.pow(((5. + tf.to_float(i + 1)) / 6.), alpha) curr_scores = log_probs / length_penalty flat_curr_scores = tf.reshape(curr_scores, [-1, beam_size * vocab_size]) topk_scores, topk_ids = tf.nn.top_k(flat_curr_scores, k=beam_size * 2) topk_log_probs = topk_scores * length_penalty topk_beam_index = topk_ids // vocab_size topk_ids %= vocab_size # Unflatten the ids batch_pos = utils.compute_batch_indices(batch_size, beam_size * 2) topk_coordinates = tf.stack([batch_pos, topk_beam_index], axis=2) topk_seq = tf.gather_nd(alive_seq, topk_coordinates) if states: states = nest.map_structure( lambda state: tf.gather_nd(state, topk_coordinates), states) topk_seq = tf.concat( [topk_seq, tf.expand_dims(topk_ids, axis=2)], axis=2) topk_finished = tf.equal(topk_ids, eos_id) return topk_seq, topk_log_probs, topk_scores, topk_finished, states def inner_loop(i, alive_seq, alive_log_probs, finished_seq, finished_scores, finished_flags, states): """Inner beam search loop.""" topk_seq, topk_log_probs, topk_scores, topk_finished, states = grow_topk( i, alive_seq, alive_log_probs, states) alive_seq, alive_log_probs, _, states = grow_alive( topk_seq, topk_scores, topk_log_probs, topk_finished, states) finished_seq, finished_scores, finished_flags, _ = grow_finished( finished_seq, finished_scores, finished_flags, topk_seq, topk_scores, topk_finished) return (i + 1, alive_seq, alive_log_probs, finished_seq, finished_scores, finished_flags, states) def _is_finished(i, unused_alive_seq, alive_log_probs, unused_finished_seq, finished_scores, unused_finished_in_finished, unused_states): """Checking termination condition. """ max_length_penalty = tf.pow( ((5. + tf.to_float(decode_length)) / 6.), alpha) lower_bound_alive_scores = alive_log_probs[:, 0] / max_length_penalty if not stop_early: lowest_score_of_finished_in_finished = tf.reduce_min( finished_scores) else: lowest_score_of_finished_in_finished = tf.reduce_max( finished_scores, axis=1) bound_is_met = tf.reduce_all( tf.greater(lowest_score_of_finished_in_finished, lower_bound_alive_scores)) return tf.logical_and(tf.less(i, decode_length), tf.logical_not(bound_is_met)) inner_shape = tf.TensorShape([None, None, None]) state_struc = nest.map_structure(utils.get_state_shape_invariants, states) (_, alive_seq, alive_log_probs, finished_seq, finished_scores, finished_flags, states) = tf.while_loop( _is_finished, inner_loop, [ tf.constant(0), alive_seq, alive_log_probs, finished_seq, finished_scores, finished_flags, states ], shape_invariants=[ tf.TensorShape([]), inner_shape, alive_log_probs.get_shape(), inner_shape, finished_scores.get_shape(), finished_flags.get_shape(), state_struc ], parallel_iterations=1, back_prop=False) alive_seq.set_shape((None, beam_size, None)) finished_seq.set_shape((None, beam_size, None)) finished_seq = tf.where(tf.reduce_any(finished_flags, 1), finished_seq, alive_seq) finished_scores = tf.where(tf.reduce_any(finished_flags, 1), finished_scores, alive_log_probs) return finished_seq, finished_scores, states
def compute_output_shape(self, input_shape): return tf.TensorShape([input_shape[0], self.cell_dim * 2])
def get_state_shape_invariants(tensor): """Returns the shape of the tensor but sets middle dims to None.""" shape = tensor.shape.as_list() for i in range(1, len(shape) - 1): shape[i] = None return tf.TensorShape(shape)
def feature_spec(self): class_num = self.taskconf['classes']['num'] if self.input_type == 'samples': # wavforms output_shapes = ( tf.TensorShape([self.example_len]), tf.TensorShape([self.max_text_len]), tf.TensorShape([]), tf.TensorShape([]), tf.TensorShape([]), tf.TensorShape([class_num]), # soft_label ) else: # features output_shapes = ( tf.TensorShape(self.feature_shape), # audio_feat (3000, 40, 3) tf.TensorShape([self.max_text_len]), # text tf.TensorShape([]), # label tf.TensorShape([]), # filename tf.TensorShape([]), # clip_id tf.TensorShape([class_num]), # soft_label ) output_types = ( tf.float32, tf.int32, tf.int32, tf.string, tf.int32, tf.float32, ) return output_shapes, output_types