Skip to content

Commit

Permalink
Merge pull request mravanelli#170 from shuttle1987/master
Browse files Browse the repository at this point in the history
[MRG] Clean up source code formatting
  • Loading branch information
mravanelli authored Oct 23, 2019
2 parents 5374de9 + e5e759e commit f9e7065
Show file tree
Hide file tree
Showing 17 changed files with 6,136 additions and 4,868 deletions.
890 changes: 523 additions & 367 deletions core.py

Large diffs are not rendered by default.

1,616 changes: 922 additions & 694 deletions data_io.py

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions kaldi_decoding_scripts/utils/filt.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/env python

# Apache 2.0
from __future__ import print_function

import sys

vocab=set()
vocab = set()
with open(sys.argv[1]) as vocabfile:
for line in vocabfile:
vocab.add(line.strip())

with open(sys.argv[2]) as textfile:
for line in textfile:
print " ".join(map(lambda word: word if word in vocab else '<UNK>', line.strip().split()))
print(" ".join(map(lambda word: word if word in vocab else "<UNK>", line.strip().split())))
50 changes: 25 additions & 25 deletions kaldi_decoding_scripts/utils/nnet/gen_dct_mat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
# limitations under the License.

# ./gen_dct_mat.py
# script generates matrix with DCT transform, which is sparse
# and takes into account that data-layout is along frequency axis,
# script generates matrix with DCT transform, which is sparse
# and takes into account that data-layout is along frequency axis,
# while DCT is done along temporal axis.
from __future__ import print_function

from math import *
import sys
Expand All @@ -27,41 +28,40 @@
from optparse import OptionParser

parser = OptionParser()
parser.add_option('--fea-dim', dest='dim', help='feature dimension')
parser.add_option('--splice', dest='splice', help='applied splice value')
parser.add_option('--dct-basis', dest='dct_basis', help='number of DCT basis')
parser.add_option("--fea-dim", dest="dim", help="feature dimension")
parser.add_option("--splice", dest="splice", help="applied splice value")
parser.add_option("--dct-basis", dest="dct_basis", help="number of DCT basis")
(options, args) = parser.parse_args()

if(options.dim == None):
if options.dim == None:
parser.print_help()
sys.exit(1)

dim=int(options.dim)
splice=int(options.splice)
dct_basis=int(options.dct_basis)
dim = int(options.dim)
splice = int(options.splice)
dct_basis = int(options.dct_basis)

timeContext=2*splice+1
timeContext = 2 * splice + 1


#generate the DCT matrix
# generate the DCT matrix
M_PI = 3.1415926535897932384626433832795
M_SQRT2 = 1.4142135623730950488016887


#generate sparse DCT matrix
print '['
# generate sparse DCT matrix
print("[")
for k in range(dct_basis):
for m in range(dim):
for n in range(timeContext):
if(n==0):
print m*'0 ',
else:
print (dim-1)*'0 ',
print str(sqrt(2.0/timeContext)*cos(M_PI/timeContext*k*(n+0.5))),
if(n==timeContext-1):
print (dim-m-1)*'0 ',
print
print

print ']'

if n == 0:
print(m * "0 ", end=" ")
else:
print((dim - 1) * "0 ", end=" ")
print(str(sqrt(2.0 / timeContext) * cos(M_PI / timeContext * k * (n + 0.5))), end=" ")
if n == timeContext - 1:
print((dim - m - 1) * "0 ", end=" ")
print()
print()

print("]")
33 changes: 16 additions & 17 deletions kaldi_decoding_scripts/utils/nnet/gen_hamm_mat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

# ./gen_hamm_mat.py
# script generates diagonal matrix with hamming window values
from __future__ import print_function

from math import *
import sys
Expand All @@ -25,33 +26,31 @@
from optparse import OptionParser

parser = OptionParser()
parser.add_option('--fea-dim', dest='dim', help='feature dimension')
parser.add_option('--splice', dest='splice', help='applied splice value')
parser.add_option("--fea-dim", dest="dim", help="feature dimension")
parser.add_option("--splice", dest="splice", help="applied splice value")
(options, args) = parser.parse_args()

if(options.dim == None):
if options.dim == None:
parser.print_help()
sys.exit(1)

dim=int(options.dim)
splice=int(options.splice)
dim = int(options.dim)
splice = int(options.splice)


#generate the diagonal matrix with hammings
# generate the diagonal matrix with hammings
M_2PI = 6.283185307179586476925286766559005

dim_mat=(2*splice+1)*dim
timeContext=2*splice+1
print '['
dim_mat = (2 * splice + 1) * dim
timeContext = 2 * splice + 1
print("[")
for row in range(dim_mat):
for col in range(dim_mat):
if col!=row:
print '0',
if col != row:
print("0", end=" ")
else:
i=int(row/dim)
print str(0.54 - 0.46*cos((M_2PI * i) / (timeContext-1))),
print

print ']'

i = int(row / dim)
print(str(0.54 - 0.46 * cos((M_2PI * i) / (timeContext - 1))), end=" ")
print()

print("]")
33 changes: 19 additions & 14 deletions kaldi_decoding_scripts/utils/nnet/gen_splice.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

# ./gen_splice.py
# generates <splice> Component
from __future__ import print_function

from math import *
import sys
Expand All @@ -25,27 +26,31 @@
from optparse import OptionParser

parser = OptionParser()
parser.add_option('--fea-dim', dest='dim_in', help='feature dimension')
parser.add_option('--splice', dest='splice', help='number of frames to concatenate with the central frame')
parser.add_option('--splice-step', dest='splice_step', help='splicing step (frames dont need to be consecutive, --splice 3 --splice-step 2 will select offsets: -6 -4 -2 0 2 4 6)', default='1' )
parser.add_option("--fea-dim", dest="dim_in", help="feature dimension")
parser.add_option("--splice", dest="splice", help="number of frames to concatenate with the central frame")
parser.add_option(
"--splice-step",
dest="splice_step",
help="splicing step (frames dont need to be consecutive, --splice 3 --splice-step 2 will select offsets: -6 -4 -2 0 2 4 6)",
default="1",
)
(options, args) = parser.parse_args()

if(options.dim_in == None):
if options.dim_in == None:
parser.print_help()
sys.exit(1)

dim_in=int(options.dim_in)
splice=int(options.splice)
splice_step=int(options.splice_step)
dim_in = int(options.dim_in)
splice = int(options.splice)
splice_step = int(options.splice_step)

dim_out=(2*splice+1)*dim_in
dim_out = (2 * splice + 1) * dim_in

print '<splice>', dim_out, dim_in
print '[',
print("<splice>", dim_out, dim_in)
print("[", end=" ")

splice_vec = range(-splice*splice_step, splice*splice_step+1, splice_step)
splice_vec = range(-splice * splice_step, splice * splice_step + 1, splice_step)
for idx in range(len(splice_vec)):
print splice_vec[idx],

print ']'
print(splice_vec[idx], end=" ")

print("]")
105 changes: 67 additions & 38 deletions kaldi_decoding_scripts/utils/nnet/make_blstm_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,61 +16,90 @@
# limitations under the License.

# Generated Nnet prototype, to be initialized by 'nnet-initialize'.
from __future__ import print_function

import sys

###
### Parse options
###
from optparse import OptionParser
usage="%prog [options] <feat-dim> <num-leaves> >nnet-proto-file"

usage = "%prog [options] <feat-dim> <num-leaves> >nnet-proto-file"
parser = OptionParser(usage)
#
parser.add_option('--num-cells', dest='num_cells', type='int', default=800,
help='Number of LSTM cells [default: %default]');
parser.add_option('--num-recurrent', dest='num_recurrent', type='int', default=512,
help='Number of LSTM recurrent units [default: %default]');
parser.add_option('--num-layers', dest='num_layers', type='int', default=2,
help='Number of LSTM layers [default: %default]');
parser.add_option('--lstm-stddev-factor', dest='lstm_stddev_factor', type='float', default=0.01,
help='Standard deviation of initialization [default: %default]');
parser.add_option('--param-stddev-factor', dest='param_stddev_factor', type='float', default=0.04,
help='Standard deviation in output layer [default: %default]');
parser.add_option('--clip-gradient', dest='clip_gradient', type='float', default=5.0,
help='Clipping constant applied to gradients [default: %default]');
parser.add_option(
"--num-cells", dest="num_cells", type="int", default=800, help="Number of LSTM cells [default: %default]"
)
parser.add_option(
"--num-recurrent",
dest="num_recurrent",
type="int",
default=512,
help="Number of LSTM recurrent units [default: %default]",
)
parser.add_option(
"--num-layers", dest="num_layers", type="int", default=2, help="Number of LSTM layers [default: %default]"
)
parser.add_option(
"--lstm-stddev-factor",
dest="lstm_stddev_factor",
type="float",
default=0.01,
help="Standard deviation of initialization [default: %default]",
)
parser.add_option(
"--param-stddev-factor",
dest="param_stddev_factor",
type="float",
default=0.04,
help="Standard deviation in output layer [default: %default]",
)
parser.add_option(
"--clip-gradient",
dest="clip_gradient",
type="float",
default=5.0,
help="Clipping constant applied to gradients [default: %default]",
)
#
(o,args) = parser.parse_args()
if len(args) != 2 :
parser.print_help()
sys.exit(1)
(o, args) = parser.parse_args()
if len(args) != 2:
parser.print_help()
sys.exit(1)

(feat_dim, num_leaves) = map(int,args);
(feat_dim, num_leaves) = list(map(int, args))

# Original prototype from Jiayu,
#<NnetProto>
#<Transmit> <InputDim> 40 <OutputDim> 40
#<LstmProjectedStreams> <InputDim> 40 <OutputDim> 512 <CellDim> 800 <ParamScale> 0.01 <NumStream> 4
#<AffineTransform> <InputDim> 512 <OutputDim> 8000 <BiasMean> 0.000000 <BiasRange> 0.000000 <ParamStddev> 0.04
#<Softmax> <InputDim> 8000 <OutputDim> 8000
#</NnetProto>
# <NnetProto>
# <Transmit> <InputDim> 40 <OutputDim> 40
# <LstmProjectedStreams> <InputDim> 40 <OutputDim> 512 <CellDim> 800 <ParamScale> 0.01 <NumStream> 4
# <AffineTransform> <InputDim> 512 <OutputDim> 8000 <BiasMean> 0.000000 <BiasRange> 0.000000 <ParamStddev> 0.04
# <Softmax> <InputDim> 8000 <OutputDim> 8000
# </NnetProto>

print "<NnetProto>"
print("<NnetProto>")
# normally we won't use more than 2 layers of LSTM
if o.num_layers == 1:
print "<BLstmProjectedStreams> <InputDim> %d <OutputDim> %d <CellDim> %s <ParamScale> %f <ClipGradient> %f" % \
(feat_dim, 2*o.num_recurrent, o.num_cells, o.lstm_stddev_factor, o.clip_gradient)
print(
"<BLstmProjectedStreams> <InputDim> %d <OutputDim> %d <CellDim> %s <ParamScale> %f <ClipGradient> %f"
% (feat_dim, 2 * o.num_recurrent, o.num_cells, o.lstm_stddev_factor, o.clip_gradient)
)
elif o.num_layers == 2:
print "<BLstmProjectedStreams> <InputDim> %d <OutputDim> %d <CellDim> %s <ParamScale> %f <ClipGradient> %f" % \
(feat_dim, 2*o.num_recurrent, o.num_cells, o.lstm_stddev_factor, o.clip_gradient)
print "<BLstmProjectedStreams> <InputDim> %d <OutputDim> %d <CellDim> %s <ParamScale> %f <ClipGradient> %f" % \
(2*o.num_recurrent, 2*o.num_recurrent, o.num_cells, o.lstm_stddev_factor, o.clip_gradient)
print(
"<BLstmProjectedStreams> <InputDim> %d <OutputDim> %d <CellDim> %s <ParamScale> %f <ClipGradient> %f"
% (feat_dim, 2 * o.num_recurrent, o.num_cells, o.lstm_stddev_factor, o.clip_gradient)
)
print(
"<BLstmProjectedStreams> <InputDim> %d <OutputDim> %d <CellDim> %s <ParamScale> %f <ClipGradient> %f"
% (2 * o.num_recurrent, 2 * o.num_recurrent, o.num_cells, o.lstm_stddev_factor, o.clip_gradient)
)
else:
sys.stderr.write("make_lstm_proto.py ERROR: more than 2 layers of LSTM, not supported yet.\n")
sys.exit(1)
print "<AffineTransform> <InputDim> %d <OutputDim> %d <BiasMean> 0.0 <BiasRange> 0.0 <ParamStddev> %f" % \
(2*o.num_recurrent, num_leaves, o.param_stddev_factor)
print "<Softmax> <InputDim> %d <OutputDim> %d" % \
(num_leaves, num_leaves)
print "</NnetProto>"


print(
"<AffineTransform> <InputDim> %d <OutputDim> %d <BiasMean> 0.0 <BiasRange> 0.0 <ParamStddev> %f"
% (2 * o.num_recurrent, num_leaves, o.param_stddev_factor)
)
print("<Softmax> <InputDim> %d <OutputDim> %d" % (num_leaves, num_leaves))
print("</NnetProto>")
Loading

0 comments on commit f9e7065

Please sign in to comment.