Scope的问题
收藏
0
收藏
全部评论(9)
paddle可以通过scope_guard来管理作用域,不同的scope可以拥有相同的参数名。
如:
import paddle.fluid as fluid
import numpy
new_scope = fluid.Scope()
with fluid.scope_guard(new_scope):
fluid.global_scope().var("data").get_tensor().set(numpy.ones((1, 2)), fluid.CPUPlace())
data = numpy.array(new_scope.find_var("data").get_tensor())
print(data) # [[1. 1.]]
0
网络定义如下,最终会报错,应该还是不同scope中即使名字相同,也会有冲突
def net(self, input, class_dim=1000):
scope_s = fluid.Scope( )
with fluid.scope_guard(scope_s):
student = ResNet34_vd()
out_student = student.net( input, class_dim=class_dim )
scope_t = fluid.Scope( )
with fluid.scope_guard(scope_t):
teacher = ResNet50_vd()
out_teacher = teacher.net( input, class_dim=class_dim )
out_teacher.stop_gradient = True
错误信息:
ValueError: Variable res2a_branch2a_weights has been created before. the previous shape is (64L, 64L, 3L, 3L); the new shape is (64, 64, 1, 1). They are not matched.
0
我简单组了个网络,如下:
import paddle.fluid as fluid
class Net1(object):
def __init__(self):
pass
def net(self, input, class_num):
fc1 = fluid.layers.fc(input, 100, act='tanh', name='fc1')
fc2 = fluid.layers.fc(fc1, size=class_num, act='softmax', name='fc2')
return fc2
class Net2(object):
def __init__(self):
pass
def net(self, input, class_num):
fc1 = fluid.layers.fc(input, 50, act='tanh', name='fc1')
fc2 = fluid.layers.fc(fc1, size=class_num, act='softmax', name='fc2')
return fc2
def final_net(input, class_num):
scope_s = fluid.Scope()
with fluid.scope_guard(scope_s):
student = Net1()
out_student = student.net(input, class_dim=class_num)
scope_t = fluid.Scope()
with fluid.scope_guard(scope_t):
teacher = Net2()
out_teacher = teacher.net(input, class_dim=class_num)
out_teacher.stop_gradient = True
0
这个跟我组的网其实是一样的呀,你这个是因为不同scope内,相同名字参数的的shape相同,所以才没有报错,这也验证了不同scope内的参数其实共享的?
或者说要实现scope内不同参数的管理,必须要保证相同名字的参数的shape完全一致?
0
或者说要实现scope内不同参数的管理,必须要保证相同名字的参数的shape完全一致?
应该是没有这个限制的。我把上述fc层的shape改为不一致,也是可以的。想问下你的网络有什么特殊之处么?
或者提供一下简单的可复现代码也可以的
0
或者说要实现scope内不同参数的管理,必须要保证相同名字的参数的shape完全一致?
应该是没有这个限制的。我把上述fc层的shape改为不一致,也是可以的。想问下你的网络有什么特殊之处么?
或者提供一下简单的可复现代码也可以的
ResNet_vd调用的就是模型库里的ResNet50vd代码
https://github.com/PaddlePaddle/models/blob/develop/PaddleCV/image_classification/models/resnet_vd.py
0
整个网络的定义为
class ResNet50_vd_distill_ResNet34_vd():
def __init__(self):
self.params = train_parameters
self.width = 16
def net(self, input, class_dim=1000):
scope_s = fluid.Scope( )
with fluid.scope_guard(scope_s):
student = ResNet34_vd()
out_student = student.net( input, class_dim=class_dim )
scope_t = fluid.Scope( )
with fluid.scope_guard(scope_t):
teacher = ResNet50_vd()
out_teacher = teacher.net( input, class_dim=class_dim )
out_teacher.stop_gradient = True
return out_teacher, out_student
0
请登录后评论
目前paddle能否实现,在同一个program内,通过设置不同的scope,即使相同的参数名,也可以是不同的参数呢?(保存时是保存在不同的文件夹内),我自己使用with scope("tesst1")这种方式目前好像对于相同参数名,不同shape的参数会报错。