Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/nnet3/nnet-normalize-component.cc
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,24 @@ void BatchNormComponent::Write(std::ostream &os, bool binary) const {
WriteToken(os, binary, "</BatchNormComponent>");
}

CuVector<BaseFloat> BatchNormComponent::Mean() {
CuVector<BaseFloat> mean(stats_sum_);
if (count_ != 0) {
mean.Scale(1.0 / count_);
}
return mean;
}

CuVector<BaseFloat> BatchNormComponent::Var() {
CuVector<BaseFloat> mean(stats_sum_), var(stats_sumsq_);
if (count_ != 0) {
mean.Scale(1.0 / count_);
var.Scale(1.0 / count_);
var.AddVecVec(-1.0, mean, mean, 1.0);
}
return var;
}

void BatchNormComponent::Scale(BaseFloat scale) {
if (scale == 0) {
count_ = 0.0;
Expand Down
4 changes: 2 additions & 2 deletions src/nnet3/nnet-normalize-component.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ class BatchNormComponent: public Component {
const CuVector<BaseFloat> &Offset() const { return offset_; }
const CuVector<BaseFloat> &Scale() const { return scale_; }

CuVector<double> &StatsSum() { return stats_sum_; }
CuVector<double> &StatsSumsq() { return stats_sumsq_; }
CuVector<BaseFloat> Mean();
CuVector<BaseFloat> Var();
double Count() { return count_; }
BaseFloat Eps() { return epsilon_; }

Expand Down
Empty file modified src/pybind/nnet3/nnet3_pybind.cc
100755 → 100644
Empty file.
Empty file modified src/pybind/nnet3/nnet_chain_example_pybind_test.py
100755 → 100644
Empty file.
Empty file modified src/pybind/nnet3/nnet_component_itf_pybind.cc
100755 → 100644
Empty file.
Empty file modified src/pybind/nnet3/nnet_component_itf_pybind.h
100755 → 100644
Empty file.
Empty file modified src/pybind/nnet3/nnet_convolutional_component_pybind.cc
100755 → 100644
Empty file.
Empty file modified src/pybind/nnet3/nnet_convolutional_component_pybind.h
100755 → 100644
Empty file.
Empty file modified src/pybind/nnet3/nnet_nnet_pybind.cc
100755 → 100644
Empty file.
48 changes: 24 additions & 24 deletions src/pybind/nnet3/nnet_nnet_pybind_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ def test_nnet_nnet(self):
elif comp_name == 'tdnn1.batchnorm':
self.assertEqual(comp_type, 'BatchNormComponent')
component.SetTestMode(True)
offset = from_dlpack(component.Offset().to_dlpack())
scale = from_dlpack(component.Scale().to_dlpack())
self.assertEqual(offset.shape, (16,))
self.assertEqual(scale.shape, (16,))
mean = from_dlpack(component.Mean().to_dlpack())
var = from_dlpack(component.Var().to_dlpack())
self.assertEqual(mean.shape, (16,))
self.assertEqual(var.shape, (16,))
elif comp_name == 'tdnnf2.linear':
self.assertEqual(comp_type, 'TdnnComponent')
linear_params = from_dlpack(
Expand All @@ -102,10 +102,10 @@ def test_nnet_nnet(self):
elif comp_name == 'tdnnf2.batchnorm':
self.assertEqual(comp_type, 'BatchNormComponent')
component.SetTestMode(True)
offset = from_dlpack(component.Offset().to_dlpack())
scale = from_dlpack(component.Scale().to_dlpack())
self.assertEqual(offset.shape, (16,))
self.assertEqual(scale.shape, (16,))
mean = from_dlpack(component.Mean().to_dlpack())
var = from_dlpack(component.Var().to_dlpack())
self.assertEqual(mean.shape, (16,))
self.assertEqual(var.shape, (16,))
elif comp_name == 'prefinal-l':
self.assertEqual(comp_type, 'LinearComponent')
params = from_dlpack(component.Params().to_dlpack())
Expand All @@ -120,21 +120,21 @@ def test_nnet_nnet(self):
elif comp_name == 'prefinal-chain.batchnorm1':
self.assertEqual(comp_type, 'BatchNormComponent')
component.SetTestMode(True)
offset = from_dlpack(component.Offset().to_dlpack())
scale = from_dlpack(component.Scale().to_dlpack())
self.assertEqual(offset.shape, (16,))
self.assertEqual(scale.shape, (16,))
mean = from_dlpack(component.Mean().to_dlpack())
var = from_dlpack(component.Var().to_dlpack())
self.assertEqual(mean.shape, (16,))
self.assertEqual(var.shape, (16,))
elif comp_name == 'prefinal-chain.linear':
self.assertEqual(comp_type, 'LinearComponent')
params = from_dlpack(component.Params().to_dlpack())
self.assertEqual(linear_params.shape, (16, 4))
elif comp_name == 'prefinal-chain.batchnorm2':
self.assertEqual(comp_type, 'BatchNormComponent')
component.SetTestMode(True)
offset = from_dlpack(component.Offset().to_dlpack())
scale = from_dlpack(component.Scale().to_dlpack())
self.assertEqual(offset.shape, (4,))
self.assertEqual(scale.shape, (4,))
mean = from_dlpack(component.Mean().to_dlpack())
var = from_dlpack(component.Var().to_dlpack())
self.assertEqual(mean.shape, (4,))
self.assertEqual(var.shape, (4,))
elif comp_name == 'output.affine':
self.assertEqual(comp_type, 'NaturalGradientAffineComponent')
linear_params = from_dlpack(
Expand All @@ -152,21 +152,21 @@ def test_nnet_nnet(self):
elif comp_name == 'prefinal-xent.batchnorm1':
self.assertEqual(comp_type, 'BatchNormComponent')
component.SetTestMode(True)
offset = from_dlpack(component.Offset().to_dlpack())
scale = from_dlpack(component.Scale().to_dlpack())
self.assertEqual(offset.shape, (16,))
self.assertEqual(scale.shape, (16,))
mean = from_dlpack(component.Mean().to_dlpack())
var = from_dlpack(component.Var().to_dlpack())
self.assertEqual(mean.shape, (16,))
self.assertEqual(var.shape, (16,))
elif comp_name == 'prefinal-xent.linear':
self.assertEqual(comp_type, 'LinearComponent')
params = from_dlpack(component.Params().to_dlpack())
self.assertEqual(linear_params.shape, (16, 4))
elif comp_name == 'prefinal-xent.batchnorm2':
self.assertEqual(comp_type, 'BatchNormComponent')
component.SetTestMode(True)
offset = from_dlpack(component.Offset().to_dlpack())
scale = from_dlpack(component.Scale().to_dlpack())
self.assertEqual(offset.shape, (4,))
self.assertEqual(scale.shape, (4,))
mean = from_dlpack(component.Mean().to_dlpack())
var = from_dlpack(component.Var().to_dlpack())
self.assertEqual(mean.shape, (4,))
self.assertEqual(var.shape, (4,))
elif comp_name == 'output-xent.affine':
self.assertEqual(comp_type, 'NaturalGradientAffineComponent')
linear_params = from_dlpack(
Expand Down
6 changes: 2 additions & 4 deletions src/pybind/nnet3/nnet_normalize_component_pybind.cc
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ using namespace kaldi::nnet3;
void pybind_nnet_normalize_component(py::module& m) {
using PyClass = kaldi::nnet3::BatchNormComponent;
py::class_<PyClass, Component>(m, "BatchNormComponent")
.def("StatsSum", &PyClass::StatsSum, py::return_value_policy::reference)
.def("StatsSumsq", &PyClass::StatsSumsq,
py::return_value_policy::reference)
.def("Mean", &PyClass::Mean, py::return_value_policy::reference)
.def("Mean", &PyClass::Mean)
.def("Var", &PyClass::Var)
.def("Count", &PyClass::Count)
.def("Eps", &PyClass::Eps)
.def("SetTestMode", &PyClass::SetTestMode, py::arg("test_mode"))
Expand Down
Empty file modified src/pybind/nnet3/nnet_normalize_component_pybind.h
100755 → 100644
Empty file.
Empty file modified src/pybind/nnet3/nnet_simple_component_pybind.cc
100755 → 100644
Empty file.
Empty file modified src/pybind/nnet3/nnet_simple_component_pybind.h
100755 → 100644
Empty file.