mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 00:21:07 +01:00
72 lines
2.9 KiB
Python
72 lines
2.9 KiB
Python
# Copyright (c) 2016-present, Facebook, Inc.
|
|
#
|
|
# 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.
|
|
##############################################################################
|
|
|
|
from __future__ import absolute_import
|
|
from __future__ import division
|
|
from __future__ import print_function
|
|
from __future__ import unicode_literals
|
|
|
|
import unittest
|
|
from caffe2.python import brew, model_helper, workspace
|
|
from caffe2.python.modeling.initializers import (
|
|
Initializer, PseudoFP16Initializer)
|
|
|
|
|
|
class InitializerTest(unittest.TestCase):
|
|
def test_fc_initializer(self):
|
|
model = model_helper.ModelHelper(name="test")
|
|
data = model.net.AddExternalInput("data")
|
|
fc1 = brew.fc(model, data, "fc1", dim_in=1, dim_out=1)
|
|
|
|
# no operator name set, will use default
|
|
fc2 = brew.fc(model, fc1, "fc2", dim_in=1, dim_out=1,
|
|
WeightInitializer=Initializer)
|
|
|
|
# no operator name set, will use custom
|
|
fc3 = brew.fc(model, fc2, "fc3", dim_in=1, dim_out=1,
|
|
WeightInitializer=Initializer,
|
|
weight_init=("ConstantFill", {}),
|
|
)
|
|
|
|
# operator name set, no initializer class set
|
|
fc4 = brew.fc(model, fc3, "fc4", dim_in=1, dim_out=1,
|
|
WeightInitializer=None,
|
|
weight_init=("ConstantFill", {})
|
|
)
|
|
|
|
@unittest.skipIf(not workspace.has_gpu_support, 'No GPU support')
|
|
def test_fc_fp16_initializer(self):
|
|
model = model_helper.ModelHelper(name="test")
|
|
data = model.net.AddExternalInput("data")
|
|
fc1 = brew.fc(model, data, "fc1", dim_in=1, dim_out=1)
|
|
|
|
# default operator, PseudoFP16Initializer
|
|
fc2 = brew.fc(model, fc1, "fc2", dim_in=1, dim_out=1,
|
|
WeightInitializer=PseudoFP16Initializer
|
|
)
|
|
|
|
# specified operator, PseudoFP16Initializer
|
|
fc3 = brew.fc(model, fc2, "fc3", dim_in=1, dim_out=1,
|
|
weight_init=("ConstantFill", {}),
|
|
WeightInitializer=PseudoFP16Initializer
|
|
)
|
|
|
|
def test_fc_external_initializer(self):
|
|
model = model_helper.ModelHelper(name="test", init_params=False)
|
|
data = model.net.AddExternalInput("data")
|
|
fc1 = brew.fc(model, data, "fc1", dim_in=1, dim_out=1) # noqa
|
|
self.assertEqual(len(model.net.Proto().op), 1)
|
|
self.assertEqual(len(model.param_init_net.Proto().op), 0)
|