本文整理汇总了Python中mxnet.gluon.SymbolBlock方法的典型用法代码示例。如果您正苦于以下问题:Python gluon.SymbolBlock方法的具体用法?Python gluon.SymbolBlock怎么用?Python gluon.SymbolBlock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mxnet.gluon
的用法示例。
在下文中一共展示了gluon.SymbolBlock方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: model_fn
# 需要导入模块: from mxnet import gluon [as 别名]
# 或者: from mxnet.gluon import SymbolBlock [as 别名]
def model_fn(model_dir):
"""Load the gluon model. Called once when hosting service starts.
Args:
model_dir: The directory where model files are stored.
Returns:
a model (in this case a Gluon network)
"""
symbol = mx.sym.load("%s/model.json" % model_dir)
outputs = mx.symbol.softmax(data=symbol, name="softmax_label")
inputs = mx.sym.var("data")
param_dict = gluon.ParameterDict("model_")
net = gluon.SymbolBlock(outputs, inputs, param_dict)
net.load_params("%s/model.params" % model_dir, ctx=mx.cpu())
return net
示例2: test_sparse_symbol_block
# 需要导入模块: from mxnet import gluon [as 别名]
# 或者: from mxnet.gluon import SymbolBlock [as 别名]
def test_sparse_symbol_block():
data = mx.sym.var('data')
weight = mx.sym.var('weight', stype='row_sparse')
bias = mx.sym.var('bias')
out = mx.sym.broadcast_add(mx.sym.dot(data, weight), bias)
# an exception is expected when creating a SparseBlock w/ sparse param
net = gluon.SymbolBlock(out, data)
示例3: test_import
# 需要导入模块: from mxnet import gluon [as 别名]
# 或者: from mxnet.gluon import SymbolBlock [as 别名]
def test_import():
ctx = mx.context.current_context()
net1 = gluon.model_zoo.vision.resnet18_v1(
prefix='resnet', ctx=ctx, pretrained=True)
net1.hybridize()
data = mx.nd.random.normal(shape=(1, 3, 32, 32))
out1 = net1(data)
net1.export('net1', epoch=1)
net2 = gluon.SymbolBlock.imports(
'net1-symbol.json', ['data'], 'net1-0001.params', ctx)
out2 = net2(data)
assert_almost_equal(out1.asnumpy(), out2.asnumpy())
示例4: test_symbol_block_save_load
# 需要导入模块: from mxnet import gluon [as 别名]
# 或者: from mxnet.gluon import SymbolBlock [as 别名]
def test_symbol_block_save_load():
class Net(gluon.HybridBlock):
def __init__(self):
super(Net, self).__init__()
with self.name_scope():
backbone = gluon.model_zoo.vision.resnet18_v1()
data = mx.sym.var('data')
featnames = ['stage1_activation0', 'stage2_activation0', 'stage3_activation0']
out_names = ['_'.join([backbone.name, featname, 'output']) for featname in featnames]
internals = backbone(data).get_internals()
outs = [internals[out_name] for out_name in out_names]
self.backbone = gluon.SymbolBlock(outs, data, params=backbone.collect_params())
self.body = nn.Conv2D(3, 1)
def hybrid_forward(self, F, x):
x = self.body(x)
return self.backbone(x)
net1 = Net()
net1.initialize(mx.init.Normal())
net1.hybridize()
net1(mx.nd.random.normal(shape=(1, 3, 32, 32)))
net1.save_parameters('./test_symbol_block_save_load.params')
net2 = Net()
net2.load_parameters('./test_symbol_block_save_load.params', ctx=mx.cpu())
示例5: test_legacy_save_params
# 需要导入模块: from mxnet import gluon [as 别名]
# 或者: from mxnet.gluon import SymbolBlock [as 别名]
def test_legacy_save_params():
net = gluon.nn.HybridSequential(prefix='')
with net.name_scope():
net.add(gluon.nn.Conv2D(10, (3, 3)))
net.add(gluon.nn.Dense(50))
net.initialize()
net(mx.nd.ones((1,1,50,50)))
a = net(mx.sym.var('data'))
a.save('test.json')
net.save_params('test.params')
model = gluon.nn.SymbolBlock(outputs=mx.sym.load_json(open('test.json', 'r').read()),
inputs=mx.sym.var('data'))
model.load_params('test.params', ctx=mx.cpu())
示例6: load_model
# 需要导入模块: from mxnet import gluon [as 别名]
# 或者: from mxnet.gluon import SymbolBlock [as 别名]
def load_model(model_uri, ctx):
"""
Load a Gluon model from a local file or a run.
:param model_uri: The location, in URI format, of the MLflow model. For example:
- ``/Users/me/path/to/local/model``
- ``relative/path/to/local/model``
- ``s3://my_bucket/path/to/model``
- ``runs:/<mlflow_run_id>/run-relative/path/to/model``
- ``models:/<model_name>/<model_version>``
- ``models:/<model_name>/<stage>``
For more information about supported URI schemes, see
`Referencing Artifacts <https://www.mlflow.org/docs/latest/concepts.html#
artifact-locations>`_.
:param ctx: Either CPU or GPU.
:return: A Gluon model instance.
.. code-block:: python
:caption: Example
# Load persisted model as a Gluon model, make inferences against an NDArray
model = mlflow.gluon.load_model("runs:/" + gluon_random_data_run.info.run_id + "/model")
model(nd.array(np.random.rand(1000, 1, 32)))
"""
local_model_path = _download_artifact_from_uri(artifact_uri=model_uri)
model_arch_path = os.path.join(local_model_path, "data", _MODEL_SAVE_PATH) + "-symbol.json"
model_params_path = os.path.join(local_model_path, "data", _MODEL_SAVE_PATH) + "-0000.params"
symbol = sym.load(model_arch_path)
inputs = sym.var('data', dtype='float32')
net = gluon.SymbolBlock(symbol, inputs)
net.collect_params().load(model_params_path, ctx)
return net
示例7: test_symbol_block
# 需要导入模块: from mxnet import gluon [as 别名]
# 或者: from mxnet.gluon import SymbolBlock [as 别名]
def test_symbol_block():
model = nn.HybridSequential()
model.add(nn.Dense(128, activation='tanh'))
model.add(nn.Dropout(0.5))
model.add(nn.Dense(64, activation='tanh'),
nn.Dense(32, in_units=64))
model.add(nn.Activation('relu'))
model.initialize()
inputs = mx.sym.var('data')
outputs = model(inputs).get_internals()
smodel = gluon.SymbolBlock(outputs, inputs, params=model.collect_params())
assert len(smodel(mx.nd.zeros((16, 10)))) == 14
out = smodel(mx.sym.var('in'))
assert len(out) == len(outputs.list_outputs())
class Net(nn.HybridBlock):
def __init__(self, model):
super(Net, self).__init__()
self.model = model
def hybrid_forward(self, F, x):
out = self.model(x)
return F.add_n(*[i.sum() for i in out])
net = Net(smodel)
net.hybridize()
assert isinstance(net(mx.nd.zeros((16, 10))), mx.nd.NDArray)
inputs = mx.sym.var('data')
outputs = model(inputs)
smodel = gluon.SymbolBlock(outputs, inputs, params=model.collect_params())
net = Net(smodel)
net.hybridize()
assert isinstance(net(mx.nd.zeros((16, 10))), mx.nd.NDArray)