def extract(cls, node): pb = node.parameters collect_until_token(pb, b'<MaxChange>') max_change = read_binary_float_token(pb) collect_until_token(pb, b'<L2Regularize>') collect_until_token(pb, b'<LearningRate>') collect_until_token(pb, b'<TimeOffsets>') time_offsets = read_binary_vector(pb, False, np.int32) collect_until_token(pb, b'<LinearParams>') weights, weights_shape = read_binary_matrix(pb) collect_until_token(pb, b'<BiasParams>') bias_params = read_binary_vector(pb) collect_until_token(pb, b'<OrthonormalConstraint>') orthonormal_constraint = read_binary_float_token( pb) # used only on training collect_until_token(pb, b'<UseNaturalGradient>') use_natural_grad = read_binary_bool_token(pb) # used only on training collect_until_token(pb, b'<NumSamplesHistory>') num_samples_hist = read_binary_float_token(pb) collect_until_token(pb, b'<AlphaInOut>') alpha_in_out = read_binary_float_token(pb), read_binary_float_token( pb) # for training, usually (4, 4) # according to Kaldi documentation http://kaldi-asr.org/doc/classkaldi_1_1nnet3_1_1TdnnComponent.html#details # it looks like it's used only during training (but not 100% sure) collect_until_token(pb, b'<RankInOut>') rank_in_out = read_binary_integer32_token( pb), read_binary_integer32_token(pb) biases = np.array(bias_params) if len(bias_params) != 0 else None attrs = { 'weights': np.reshape(weights, weights_shape), 'biases': biases, 'time_offsets': time_offsets, } TdnnComponent.update_node_stat(node, attrs) return cls.enabled
def extract(cls, node): pb = node.parameters try: collect_until_token(pb, b'<Dim>') except Error: try: pb.seek(0) collect_until_token(pb, b'<InputDim>') except Error: raise Error("Neither <Dim> nor <InputDim> were found") in_dim = read_binary_integer32_token(pb) try: collect_until_token(pb, b'<TargetRms>') target_rms = read_binary_float_token(pb) except Error: # model does not contain TargetRms target_rms = 1.0 try: collect_until_token(pb, b'<AddLogStddev>') add_log = read_binary_bool_token(pb) except Error: # model does not contain AddLogStddev add_log = False if add_log is not False: raise Error( "AddLogStddev True in Normalize component is not supported") scale = target_rms * np.sqrt(in_dim) attrs = { 'eps': 0.00000001, 'across_spatial': 0, 'channel_shared': 1, 'in_dim': in_dim, } embed_input(attrs, 1, 'weights', [scale]) NormalizeOp.update_node_stat(node, attrs) return cls.enabled
def extract(node): pb = node.parameters collect_until_token(pb, b'<Dim>') dim = read_binary_integer32_token(pb) collect_until_token(pb, b'<BlockDim>') block_dim = read_binary_integer32_token(pb) if block_dim != dim: raise Error( "Dim is not equal BlockDim for BatchNorm is not supported") collect_until_token(pb, b'<Epsilon>') eps = read_binary_float_token(pb) collect_until_token(pb, b'<TargetRms>') target_rms = read_binary_float_token(pb) collect_until_token(pb, b'<TestMode>') test_mode = read_binary_bool_token(pb) if test_mode is not False: raise Error("Test mode True for BatchNorm is not supported") collect_until_token(pb, b'<StatsMean>') mean = read_binary_vector(pb) collect_until_token(pb, b'<StatsVar>') var = read_binary_vector(pb) scale = target_rms / np.sqrt(var + eps) shift = -target_rms * mean / np.sqrt(var + eps) attrs = {} embed_input(attrs, 1, 'weights', scale) embed_input(attrs, 2, 'biases', shift) ScaleShiftOp.update_node_stat(node, attrs) return __class__.enabled
def test_read_binary_bool_token(self): true_value = self.bytesio_from(self.pack_value(True, self.bool_fmt)) false_value = self.bytesio_from(self.pack_value(False, self.bool_fmt)) self.assertTrue(read_binary_bool_token(true_value)) self.assertFalse(read_binary_bool_token(false_value))