forked from NVIDIA-NeMo/Speech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pytorch_trainers.py
More file actions
76 lines (65 loc) · 3.21 KB
/
Copy pathtest_pytorch_trainers.py
File metadata and controls
76 lines (65 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# ! /usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2019 NVIDIA. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================
import nemo
from tests.common_setup import NeMoUnitTest
logging = nemo.logging
class TestPytorchTrainers(NeMoUnitTest):
def test_simple_train(self):
logging.info("Simplest train test")
data_source = nemo.backends.pytorch.tutorials.RealFunctionDataLayer(n=10000, batch_size=128)
trainable_module = nemo.backends.pytorch.tutorials.TaylorNet(dim=4)
loss = nemo.backends.pytorch.tutorials.MSELoss()
x, y = data_source()
y_pred = trainable_module(x=x)
loss_tensor = loss(predictions=y_pred, target=y)
optimizer = nemo.backends.pytorch.actions.PtActions()
optimizer.train(
tensors_to_optimize=[loss_tensor], optimizer="sgd", optimization_params={"lr": 0.0003, "num_epochs": 1},
)
def test_simple_train_named_output(self):
logging.info('Simplest train test with using named output.')
data_source = nemo.backends.pytorch.tutorials.RealFunctionDataLayer(n=10000, batch_size=128,)
trainable_module = nemo.backends.pytorch.tutorials.TaylorNet(dim=4)
loss = nemo.backends.pytorch.tutorials.MSELoss()
data = data_source()
self.assertEqual(
first=type(data).__name__,
second='RealFunctionDataLayerOutput',
msg='Check output class naming coherence.',
)
y_pred = trainable_module(x=data.x)
loss_tensor = loss(predictions=y_pred, target=data.y)
optimizer = nemo.backends.pytorch.actions.PtActions()
optimizer.train(
tensors_to_optimize=[loss_tensor], optimizer="sgd", optimization_params={"lr": 0.0003, "num_epochs": 1},
)
def test_simple_chained_train(self):
logging.info("Chained train test")
data_source = nemo.backends.pytorch.tutorials.RealFunctionDataLayer(n=10000, batch_size=32)
trainable_module1 = nemo.backends.pytorch.tutorials.TaylorNet(dim=4)
trainable_module2 = nemo.backends.pytorch.tutorials.TaylorNet(dim=2)
trainable_module3 = nemo.backends.pytorch.tutorials.TaylorNet(dim=2)
loss = nemo.backends.pytorch.tutorials.MSELoss()
x, y = data_source()
y_pred1 = trainable_module1(x=x)
y_pred2 = trainable_module2(x=y_pred1)
y_pred3 = trainable_module3(x=y_pred2)
loss_tensor = loss(predictions=y_pred3, target=y)
optimizer = nemo.backends.pytorch.actions.PtActions()
optimizer.train(
tensors_to_optimize=[loss_tensor], optimizer="sgd", optimization_params={"lr": 0.0003, "num_epochs": 1},
)