本文整理汇总了Python中paddle.fluid方法的典型用法代码示例。如果您正苦于以下问题:Python paddle.fluid方法的具体用法?Python paddle.fluid怎么用?Python paddle.fluid使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类paddle
的用法示例。
在下文中一共展示了paddle.fluid方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: conv_bn_layer
# 需要导入模块: import paddle [as 别名]
# 或者: from paddle import fluid [as 别名]
def conv_bn_layer(input, num_filters, filter_size, stride=1,dilation=1,
groups=1,act='relu'):
conv = fluid.layers.conv2d(
input=input,
num_filters=num_filters,
filter_size=filter_size,
stride=stride,
padding=dilation*(int((filter_size - 1) / 2)),
dilation=dilation,
groups=groups,
act=act,
param_attr=fluid.initializer.Xavier(uniform=False),
bias_attr=False)
# param_attr=fluid.initializer.Normal(loc=0.0, scale=2.0),
outconv = fluid.layers.batch_norm(input=conv)
return outconv
示例2: load
# 需要导入模块: import paddle [as 别名]
# 或者: from paddle import fluid [as 别名]
def load(self, param_state_pairs, optim_state):
if self._executor is None:
executor = fluid.Executor(fluid.CPUPlace())._default_executor
else:
executor = self._executor._default_executor
# restore parameter states
fluid.core._create_loaded_parameter(
[param for param, state in param_state_pairs],
global_scope(), executor)
for param, state in param_state_pairs:
self._set_var(param, state)
# restore optimizer states
# FIXME what if a different optimizer is used?
if not self.model._optimizer or not optim_state:
return
self._load_optimizer(optim_state, executor)
示例3: rnn_net
# 需要导入模块: import paddle [as 别名]
# 或者: from paddle import fluid [as 别名]
def rnn_net(ipt, input_dim):
emb = fluid.layers.embedding(input=ipt, size=[input_dim, 128], is_sparse=True)
sentence = fluid.layers.fc(input=emb, size=128, act='tanh')
rnn = fluid.layers.DynamicRNN()
with rnn.block():
word = rnn.step_input(sentence)
prev = rnn.memory(shape=[128])
hidden = fluid.layers.fc(input=[word, prev], size=128, act='relu')
rnn.update_memory(prev, hidden)
rnn.output(hidden)
last = fluid.layers.sequence_last_step(rnn())
out = fluid.layers.fc(input=last, size=2, act='softmax')
return out
# 定义长短期记忆网络
示例4: lstm_net
# 需要导入模块: import paddle [as 别名]
# 或者: from paddle import fluid [as 别名]
def lstm_net(ipt, input_dim):
# 以数据的IDs作为输入
emb = fluid.layers.embedding(input=ipt, size=[input_dim, 128], is_sparse=True)
# 第一个全连接层
fc1 = fluid.layers.fc(input=emb, size=128)
# 进行一个长短期记忆操作
lstm1, _ = fluid.layers.dynamic_lstm(input=fc1, size=128)
# 第一个最大序列池操作
fc2 = fluid.layers.sequence_pool(input=fc1, pool_type='max')
# 第二个最大序列池操作
lstm2 = fluid.layers.sequence_pool(input=lstm1, pool_type='max')
# 以softmax作为全连接的输出层,大小为2,也就是正负面
out = fluid.layers.fc(input=[fc2, lstm2], size=2, act='softmax')
return out
# 定义输入数据, lod_level不为0指定输入数据为序列数据
示例5: infer
# 需要导入模块: import paddle [as 别名]
# 或者: from paddle import fluid [as 别名]
def infer(save_dirname=None):
place = fluid.CPUPlace()
exe = fluid.Executor(place)
inference_scope = fluid.core.Scope()
with fluid.scope_guard(inference_scope):
[inference_program, feed_target_names, fetch_targets] = (
fluid.io.load_inference_model(save_dirname, exe))
test_reader = paddle.batch(paddle.dataset.uci_housing.test(), batch_size=20)
test_data = six.next(test_reader())
test_feat = numpy.array(list(map(lambda x: x[0], test_data))).astype("float32")
test_label = numpy.array(list(map(lambda x: x[1], test_data))).astype("float32")
results = exe.run(inference_program,
feed={feed_target_names[0]: numpy.array(test_feat)},
fetch_list=fetch_targets)
print("infer results: ", results[0])
print("ground truth: ", test_label)
# Run train and infer.
示例6: ChannelSE
# 需要导入模块: import paddle [as 别名]
# 或者: from paddle import fluid [as 别名]
def ChannelSE(self, input, num_channels, reduction_ratio=16):
"""
Squeeze and Excitation block, reimplementation inspired by
https://github.com/Cadene/pretrained-models.pytorch/blob/master/pretrainedmodels/models/senet.py
"""
pool = fluid.layers.pool2d(
input=input, pool_size=0, pool_type='avg', global_pooling=True)
stdv = 1.0 / math.sqrt(pool.shape[1] * 1.0)
squeeze = fluid.layers.fc(input=pool,
size=num_channels // reduction_ratio,
act='relu',
param_attr=fluid.param_attr.ParamAttr(
initializer=fluid.initializer.Uniform(
-stdv, stdv)))
stdv = 1.0 / math.sqrt(squeeze.shape[1] * 1.0)
excitation = fluid.layers.fc(input=squeeze,
size=num_channels,
act='sigmoid',
param_attr=fluid.param_attr.ParamAttr(
initializer=fluid.initializer.Uniform(
-stdv, stdv)))
scale = fluid.layers.elementwise_mul(x=input, y=excitation, axis=0)
return scale
示例7: multi_res_block
# 需要导入模块: import paddle [as 别名]
# 或者: from paddle import fluid [as 别名]
def multi_res_block(self, inputs,filter_size1,filter_size2,filter_size3,filter_size4):
conv1 = conv_bn_layer(
input=inputs, num_filters=filter_size1, filter_size=3, stride=1, act='relu')
conv2 = conv_bn_layer(
input=conv1, num_filters=filter_size2, filter_size=3, stride=1, act='relu')
conv3 = conv_bn_layer(
input=conv2, num_filters=filter_size3, filter_size=3, stride=1, act='relu')
conv = conv_bn_layer(
input=inputs, num_filters=filter_size4, filter_size=1, stride=1, act='relu')
concat = fluid.layers.concat([conv1, conv2, conv3], axis = 1) #merge in channel
add = fluid.layers.elementwise_add(concat,y=conv)
return add
示例8: res_path
# 需要导入模块: import paddle [as 别名]
# 或者: from paddle import fluid [as 别名]
def res_path(self, inputs,filter_size,path_number):
def block(x,fl):
conv1 = conv_bn_layer(
input=inputs, num_filters=filter_size, filter_size=3, stride=1, act='relu')
conv2 = conv_bn_layer(
input=inputs, num_filters=filter_size, filter_size=1, stride=1, act='relu')
add = fluid.layers.elementwise_add(conv1,conv2)
return add
cnn = block(inputs, filter_size)
if path_number <= 3:
cnn = block(cnn,filter_size)
if path_number <= 2:
cnn = block(cnn,filter_size)
if path_number <= 1:
cnn = block(cnn,filter_size)
return cnn
示例9: DenseBlock
# 需要导入模块: import paddle [as 别名]
# 或者: from paddle import fluid [as 别名]
def DenseBlock(self, inputs, outdim):
input_shape = inputs.shape
bn = fluid.layers.batch_norm(input=inputs, epsilon=2e-05,fuse_with_relu=True)
conv1 = conv_bn_layer(
input=bn, num_filters=outdim, filter_size=3, stride=1, act='relu')
if input_shape[1] != outdim:
shortcut = conv_bn_layer(
input=inputs, num_filters=outdim, filter_size=1, stride=1, act='relu')
else:
shortcut = inputs
result1 = fluid.layers.elementwise_add(conv1, shortcut)
bn = fluid.layers.batch_norm(input=result1, epsilon=2e-05,fuse_with_relu=True)
conv2 = conv_bn_layer(
input=bn, num_filters=outdim, filter_size=3, stride=1, act='relu')
result = fluid.layers.elementwise_add(result1, conv2)
result = fluid.layers.elementwise_add(result, shortcut)
result = fluid.layers.relu(result)
return result
示例10: Inception_dilation
# 需要导入模块: import paddle [as 别名]
# 或者: from paddle import fluid [as 别名]
def Inception_dilation(self, inputs, channels):
conv3 = conv_bn_layer(input=inputs, num_filters=channels,
filter_size=3, stride=1, dilation=1, act='relu')
print("conv3.shape----------",conv3.shape)
conv5 = conv_bn_layer(input=inputs, num_filters=channels,
filter_size=3, stride=1, dilation=2, act='relu')
print("conv5.shape----------",conv5.shape)
conv7 = conv_bn_layer(input=inputs, num_filters=channels,
filter_size=3, stride=1, dilation=4, act='relu')
print("conv7.shape----------",conv7.shape)
conv9 = conv_bn_layer(input=inputs, num_filters=channels,
filter_size=3, stride=1, dilation=6, act='relu')
print("conv9.shape----------",conv9.shape)
merge2 = fluid.layers.concat([conv3, conv5, conv7, conv9], axis = 1)
return merge2
示例11: GlobalAttentionUpsample
# 需要导入模块: import paddle [as 别名]
# 或者: from paddle import fluid [as 别名]
def GlobalAttentionUpsample(self, inputs_low, inputs_high, channels):
#inputs_low:低层次信息输入
#inputs_high:高层次信息输入
print('inputs_high.shape---------',inputs_high.shape)
conv3 = conv_bn_layer(input=inputs_low, num_filters=3*channels,
filter_size=3, stride=1,dilation=1, act='relu')
gap = fluid.layers.pool2d(inputs_high,pool_type='avg',global_pooling=True)
print('gap.shape------------', gap.shape)
h = conv3.shape[2]
w = conv3.shape[3]
gap = fluid.layers.resize_bilinear(input = gap,out_shape = [h,w] )
conv1conv3 = fluid.layers.elementwise_mul(gap, conv3)
'''
conv1 = conv_bn_layer(input=gap, num_filters=3*channels,
filter_size=1, stride=1,dilation=1, act='relu')
print("conv1.shape---------",conv1.shape)
'''
#out = fluid.layers.sequence_concat(input=[conv1conv3, inputs_high])
out = fluid.layers.concat([conv1conv3, inputs_high], axis = 1)
return out
示例12: __init__
# 需要导入模块: import paddle [as 别名]
# 或者: from paddle import fluid [as 别名]
def __init__(self,):
self.name_scope = ""
self.decode_channel = 48
self.encode_channel = 256
self.label_number = 9
self.bn_momentum = 0.99
self.dropout_keep_prop = 0.9
self.is_train = True
self.op_results = {}
self.default_epsilon = 1e-3
self.default_norm_type = 'bn'
self.default_group_number = 32
self.depthwise_use_cudnn = False
self.bn_regularizer = fluid.regularizer.L2DecayRegularizer(regularization_coeff=0.0)
self.depthwise_regularizer = fluid.regularizer.L2DecayRegularizer(
regularization_coeff=0.0)
self.clean()
示例13: group_norm
# 需要导入模块: import paddle [as 别名]
# 或者: from paddle import fluid [as 别名]
def group_norm(self,input, G, eps=1e-5, param_attr=None, bias_attr=None):
N, C, H, W = input.shape
if C % G != 0:
# print "group can not divide channle:", C, G
for d in range(10):
for t in [d, -d]:
if G + t <= 0: continue
if C % (G + t) == 0:
G = G + t
break
if C % G == 0:
# print "use group size:", G
break
assert C % G == 0
x = fluid.layers.group_norm(
input,
groups=G,
param_attr=param_attr,
bias_attr=bias_attr,
name=self.name_scope + 'group_norm')
return x
示例14: decoder
# 需要导入模块: import paddle [as 别名]
# 或者: from paddle import fluid [as 别名]
def decoder(self,encode_data, decode_shortcut):
with self.scope('decoder'):
with self.scope('concat'):
decode_shortcut = self.bn_relu(
self.conv(
decode_shortcut, self.decode_channel, 1, 1, groups=1, padding=0))
encode_data = fluid.layers.resize_bilinear(
encode_data, decode_shortcut.shape[2:])
encode_data = fluid.layers.concat(
[encode_data, decode_shortcut], axis=1)
self.append_op_result(encode_data, 'concat')
with self.scope("separable_conv1"):
encode_data = self.seq_conv(
encode_data, self.encode_channel, 1, 3, dilation=1, act=self.relu)
with self.scope("separable_conv2"):
encode_data = self.seq_conv(
encode_data, self.encode_channel, 1, 3, dilation=1, act=self.relu)
return encode_data
示例15: xception_downsample_block
# 需要导入模块: import paddle [as 别名]
# 或者: from paddle import fluid [as 别名]
def xception_downsample_block(self,x,channels,top_relu=False):
if top_relu:
x = fluid.layers.relu(x)
x0_d = depthwise_bn_layer(input=x, filter_size=3, dilation=1,
act=None)
x0_p = conv_bn_layer(input=x0_d, num_filters=channels, filter_size=1,
act='relu')
x1_d = depthwise_bn_layer(input=x0_p, filter_size=3, dilation=1,
act=None)
x1_p = conv_bn_layer(input=x1_d, num_filters=channels, filter_size=1,
act='relu')
x2_d = depthwise_bn_layer(input=x1_p, filter_size=3, stride=2, dilation=1,
act=None)
x2_p = conv_bn_layer(input=x2_d, num_filters=channels, filter_size=1,
act=None)
return x2_p