本文整理汇总了Python中mxnet.gluon.nn.HybridBlock方法的典型用法代码示例。如果您正苦于以下问题:Python nn.HybridBlock方法的具体用法?Python nn.HybridBlock怎么用?Python nn.HybridBlock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mxnet.gluon.nn
的用法示例。
在下文中一共展示了nn.HybridBlock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_constant
# 需要导入模块: from mxnet.gluon import nn [as 别名]
# 或者: from mxnet.gluon.nn import HybridBlock [as 别名]
def test_constant():
class Test(gluon.HybridBlock):
def __init__(self, **kwargs):
super(Test, self).__init__(**kwargs)
self.value = np.asarray([[1,2], [3,4]])
self.const = self.params.get_constant('const', self.value)
def hybrid_forward(self, F, x, const):
return x + const
test = Test()
test.initialize()
trainer = gluon.Trainer(test.collect_params(), 'sgd',
{'learning_rate': 1.0, 'momentum': 0.5})
with mx.autograd.record():
x = mx.nd.ones((2,2))
x.attach_grad()
y = test(x)
y.backward()
trainer.step(1)
assert (test.const.data().asnumpy() == test.value).all()
assert (x.grad.asnumpy() == 1).all()
示例2: test_conv2d_16c
# 需要导入模块: from mxnet.gluon import nn [as 别名]
# 或者: from mxnet.gluon.nn import HybridBlock [as 别名]
def test_conv2d_16c():
chn_list = [16, 256]
kernel_list = [1, 3]
kernel_list.append(224)
batch_size = 4
class Net(gluon.HybridBlock):
def __init__(self,
chn_num,
kernel,
**kwargs):
super(Net, self).__init__(**kwargs)
with self.name_scope():
self.conv0 = gluon.nn.Conv2D(chn_num, (kernel, kernel))
def hybrid_forward(self, F, x):
out = self.conv0(x)
return out
x = mx.nd.random.uniform(-1.0, 1.0, shape=(batch_size, 3, 224, 224))
for i in range(len(chn_list)):
for j in range(len(kernel_list)):
net = Net(chn_list[i], kernel_list[j])
check_layer_forward_withinput(net, x)
示例3: test_deconv2d_16c
# 需要导入模块: from mxnet.gluon import nn [as 别名]
# 或者: from mxnet.gluon.nn import HybridBlock [as 别名]
def test_deconv2d_16c():
in_chn_list = [1024, 512, 256, 128, 64, 32, 16]
out_chn_list = [512, 256, 128, 64, 32, 16, 3]
kernel_list = [1, 3, 5, 7]
in_shape = [4, 8, 16, 32, 64, 224]
batch_size = 4
class Net(gluon.HybridBlock):
def __init__(self, chn_num, kernel, **kwargs):
super(Net, self).__init__(**kwargs)
with self.name_scope():
self.deconv0 = gluon.nn.Conv2DTranspose(chn_num, (kernel, kernel))
def hybrid_forward(self, F, x):
out = self.deconv0(x)
return out
for i in range(len(in_shape)):
x = mx.nd.random.uniform(-1.0, 1.0, shape=(batch_size, in_chn_list[i], in_shape[i], in_shape[i]))
for j in range(len(kernel_list)):
net = Net(out_chn_list[i], kernel_list[j])
check_layer_forward_withinput(net, x)
示例4: test_reshape_conv_reshape_conv
# 需要导入模块: from mxnet.gluon import nn [as 别名]
# 或者: from mxnet.gluon.nn import HybridBlock [as 别名]
def test_reshape_conv_reshape_conv():
class Net(gluon.HybridBlock):
def __init__(self, **kwargs):
super(Net, self).__init__(**kwargs)
with self.name_scope():
self.conv0 = nn.Conv2D(64, (3, 3))
self.conv1 = nn.Conv2D(128, (3, 3))
def hybrid_forward(self, F, x):
x_reshape = x.reshape((0, 0, 128, 32))
y = self.conv0(x_reshape)
"spatial shape of y is (62, 62)"
y_reshape = y.reshape((0, 0, 124, 31))
out = self.conv1(y_reshape)
return out
x = mx.nd.random.uniform(shape=(4, 3, 64, 64))
net = Net()
check_layer_forward_withinput(net, x)
示例5: test_slice_conv_slice_conv
# 需要导入模块: from mxnet.gluon import nn [as 别名]
# 或者: from mxnet.gluon.nn import HybridBlock [as 别名]
def test_slice_conv_slice_conv():
class Net(gluon.HybridBlock):
def __init__(self, **kwargs):
super(Net, self).__init__(**kwargs)
with self.name_scope():
self.conv0 = nn.Conv2D(32, (3, 3))
self.conv1 = nn.Conv2D(16, (1, 1))
def hybrid_forward(self, F, x):
x_slice = x.slice(begin=(0, 0, 0, 0), end=(4, 16, 16, 16))
y = self.conv0(x_slice)
"shape of y is (4, 32, 14, 14)"
y_slice = y.slice(begin=(0, 0, 0, 0), end=(4, 16, 3, 3))
out = self.conv1(y_slice)
return out
x = mx.nd.random.uniform(shape=(4, 32, 32, 32))
net = Net()
check_layer_forward_withinput(net, x)
示例6: test_slice_conv_reshape_conv
# 需要导入模块: from mxnet.gluon import nn [as 别名]
# 或者: from mxnet.gluon.nn import HybridBlock [as 别名]
def test_slice_conv_reshape_conv():
class Net(gluon.HybridBlock):
def __init__(self, **kwargs):
super(Net, self).__init__(**kwargs)
with self.name_scope():
self.conv0 = nn.Conv2D(64, (3, 3))
self.conv1 = nn.Conv2D(128, (3, 3))
def hybrid_forward(self, F, x):
x_slice = x.slice(begin=(0, 0, 1, 1), end=(4, 16, 33, 33))
y = self.conv0(x_slice)
"shape of y is (4, 64, 30, 30)"
y_reshape = y.reshape((0, 0, 60, 15))
out = self.conv1(y_reshape)
return out
x = mx.nd.random.uniform(shape=(4, 32, 64, 64))
net = Net()
check_layer_forward_withinput(net, x)
示例7: test_reshape_dense
# 需要导入模块: from mxnet.gluon import nn [as 别名]
# 或者: from mxnet.gluon.nn import HybridBlock [as 别名]
def test_reshape_dense():
class Net(gluon.HybridBlock):
def __init__(self, **kwargs):
super(Net, self).__init__(**kwargs)
with self.name_scope():
channel0 = np.random.randint(1, 17)
self.dense0 = nn.Dense(channel0)
def hybrid_forward(self, F, x):
x_reshape = x.reshape((8, 64, 128, -1))
out = self.dense0(x_reshape)
return out
x = mx.nd.random.uniform(shape=(4, 32, 64, 64))
net = Net()
check_layer_forward_withinput(net, x)
示例8: test_slice_dense
# 需要导入模块: from mxnet.gluon import nn [as 别名]
# 或者: from mxnet.gluon.nn import HybridBlock [as 别名]
def test_slice_dense():
class Net(gluon.HybridBlock):
def __init__(self, slice, **kwargs):
super(Net, self).__init__(**kwargs)
with self.name_scope():
channel0 = np.random.randint(1, 17)
self.dense0 = nn.Dense(channel0)
self.slice = slice
def hybrid_forward(self, F, x):
x_slice = x.slice(begin=tuple(self.slice[0]),
end=tuple(self.slice[1]))
out = self.dense0(x_slice)
return out
x = mx.nd.random.uniform(shape=(16, 32, 64, 64))
slice = [[0, 16, 0, 0], [4, 32, 32, 32]]
net = Net(slice)
check_layer_forward_withinput(net, x)
示例9: test_slice_dense_slice_dense
# 需要导入模块: from mxnet.gluon import nn [as 别名]
# 或者: from mxnet.gluon.nn import HybridBlock [as 别名]
def test_slice_dense_slice_dense():
class Net(gluon.HybridBlock):
def __init__(self, slice, **kwargs):
super(Net, self).__init__(**kwargs)
with self.name_scope():
channel0 = 32
channel1 = np.random.randint(1, 17)
self.dense0 = nn.Dense(channel0)
self.dense1 = nn.Dense(channel1)
self.slice = slice
def hybrid_forward(self, F, x):
x_slice = x.slice(begin=tuple(self.slice[0]), end=tuple(self.slice[1]))
y = self.dense0(x_slice)
y_slice = y.slice(begin=(1, 0), end=(3, 10))
out = self.dense1(y_slice)
return out
x = mx.nd.random.uniform(shape=(16, 32, 64, 64))
slice = [[0, 16, 0, 0], [4, 32, 32, 32]]
net = Net(slice)
check_layer_forward_withinput(net, x)
示例10: test_reshape_dense_reshape_dense
# 需要导入模块: from mxnet.gluon import nn [as 别名]
# 或者: from mxnet.gluon.nn import HybridBlock [as 别名]
def test_reshape_dense_reshape_dense():
class Net(gluon.HybridBlock):
def __init__(self, **kwargs):
super(Net, self).__init__(**kwargs)
with self.name_scope():
channel0 = np.random.randint(1, 17)
channel1 = np.random.randint(1, 33)
self.dense0 = nn.Dense(channel0)
self.dense1 = nn.Dense(channel1)
def hybrid_forward(self, F, x):
x_reshape = x.reshape((4, 16, 128, 32))
y = self.dense0(x_reshape)
y_reshape = y.reshape((1, -1))
out = self.dense1(y_reshape)
return out
x = mx.nd.random.uniform(shape=(4, 16, 64, 64))
net = Net()
check_layer_forward_withinput(net, x)
示例11: test_slice_dense_reshape_dense
# 需要导入模块: from mxnet.gluon import nn [as 别名]
# 或者: from mxnet.gluon.nn import HybridBlock [as 别名]
def test_slice_dense_reshape_dense():
class Net(gluon.HybridBlock):
def __init__(self, slice, **kwargs):
super(Net, self).__init__(**kwargs)
with self.name_scope():
channel0 = np.random.randint(1, 17)
channel1 = np.random.randint(1, 17)
self.dense0 = nn.Dense(channel0)
self.dense1 = nn.Dense(channel1)
self.slice = slice
def hybrid_forward(self, F, x):
x_slice = x.slice(begin=tuple(self.slice[0]), end=tuple(self.slice[1]))
y = self.dense0(x_slice)
y_reshape = y.reshape((1, -1))
out = self.dense1(y_reshape)
return out
x = mx.nd.random.uniform(shape=(16, 32, 64, 64))
slice = [[0, 16, 0, 0], [4, 32, 32, 32]]
net = Net(slice)
check_layer_forward_withinput(net, x)
示例12: test_reshape_batchnorm
# 需要导入模块: from mxnet.gluon import nn [as 别名]
# 或者: from mxnet.gluon.nn import HybridBlock [as 别名]
def test_reshape_batchnorm():
class Net(gluon.HybridBlock):
def __init__(self, shape, **kwargs):
super(Net, self).__init__(**kwargs)
with self.name_scope():
self.conv0 = nn.Conv2D(96, (1, 1))
self.bn0 = nn.BatchNorm()
self.reshape = shape
def hybrid_forward(self, F, x):
x_in = self.conv0(x)
x_reshape = x_in.reshape(self.reshape)
out = self.bn0(x_reshape)
return out
x = mx.nd.random.uniform(shape=(4, 32, 64, 64))
shape = (4, 64, 64, -1)
net = Net(shape)
check_layer_forward_withinput(net, x)
示例13: test_slice_batchnorm
# 需要导入模块: from mxnet.gluon import nn [as 别名]
# 或者: from mxnet.gluon.nn import HybridBlock [as 别名]
def test_slice_batchnorm():
class Net(gluon.HybridBlock):
def __init__(self, slice, **kwargs):
super(Net, self).__init__(**kwargs)
with self.name_scope():
self.conv0 = nn.Conv2D(128, (1, 1))
self.bn0 = nn.BatchNorm()
self.slice = slice
def hybrid_forward(self, F, x):
x_in = self.conv0(x)
x_slice = x_in.slice(begin=tuple(self.slice[0]),
end=tuple(self.slice[1]))
out = self.bn0(x_slice)
return out
x = mx.nd.random.uniform(shape=(16, 128, 256, 256))
slice = [[0, 0, 0, 0], [4, 32, 32, 32]]
net = Net(slice)
check_layer_forward_withinput(net, x)
示例14: test_slice_batchnorm_slice_batchnorm
# 需要导入模块: from mxnet.gluon import nn [as 别名]
# 或者: from mxnet.gluon.nn import HybridBlock [as 别名]
def test_slice_batchnorm_slice_batchnorm():
class Net(gluon.HybridBlock):
def __init__(self, slice, **kwargs):
super(Net, self).__init__(**kwargs)
with self.name_scope():
self.conv0 = nn.Conv2D(128, (1, 1))
self.bn0 = nn.BatchNorm()
self.bn1 = nn.BatchNorm()
self.slice = slice
def hybrid_forward(self, F, x):
x_in = self.conv0(x)
x_slice = x_in.slice(begin=tuple(self.slice[0][0]), end=tuple(self.slice[0][1]))
y = self.bn0(x_slice)
y_slice = y.slice(begin=tuple(self.slice[1][0]), end=tuple(self.slice[1][1]))
out = self.bn1(y_slice)
return out
x = mx.nd.random.uniform(shape=(16, 128, 256, 256))
slice = [[[0, 0, 0, 0], [4, 32, 32, 32]], [[0, 0, 0, 0], [2, 64, 16, 16]]]
net = Net(slice)
check_layer_forward_withinput(net, x)
示例15: test_reshape_batchnorm_reshape_batchnorm
# 需要导入模块: from mxnet.gluon import nn [as 别名]
# 或者: from mxnet.gluon.nn import HybridBlock [as 别名]
def test_reshape_batchnorm_reshape_batchnorm():
class Net(gluon.HybridBlock):
def __init__(self, shape, **kwargs):
super(Net, self).__init__(**kwargs)
with self.name_scope():
self.conv0 = nn.Conv2D(128, (1, 1))
self.bn0 = nn.BatchNorm()
self.bn1 = nn.BatchNorm()
self.reshape = shape
def hybrid_forward(self, F, x):
x_in = self.conv0(x)
x_reshape = x_in.reshape(self.reshape[0])
y = self.bn0(x_reshape)
y_reshape = y.reshape(self.reshape[1])
out = self.bn1(y_reshape)
return out
x = mx.nd.random.uniform(shape=(4, 32, 64, 64))
shape = [(4, 64, 64, -1), (4, 128, -1, 32)]
net = Net(shape)
check_layer_forward_withinput(net, x)