Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSError:could not get source code 动态图训练 + 模型&参数存储出错 #660

Open
HuAn-508 opened this issue Mar 18, 2021 · 0 comments

Comments

@HuAn-508
Copy link

在安装了paddle之后,根据:使用教程-模型开发-3.1.2 动态图训练 + 模型&参数存储给出的示例代码,测试模型&参数的存储。执行到保存时报错OSError: could not get source code,请问是什么原因引起的,如何解决
代码过程:

import numpy as np
import paddle
import paddle.nn as nn
import paddle.optimizer as opt
from paddle.static import InputSpec

BATCH_SIZE = 16
BATCH_NUM = 4
EPOCH_NUM = 4

IMAGE_SIZE = 784
CLASS_NUM = 10

define a random dataset

class RandomDataset(paddle.io.Dataset):
def init(self, num_samples):
self.num_samples = num_samples
def getitem(self, idx):
image = np.random.random([IMAGE_SIZE]).astype('float32')
label = np.random.randint(0, CLASS_NUM - 1, (1, )).astype('int64')
return image, label
def len(self):
return self.num_samples

class LinearNet(nn.Layer):
def init(self):
super(LinearNet, self).init()
self._linear = nn.Linear(IMAGE_SIZE, CLASS_NUM)
def forward(self, x):
return self._linear(x)

def train(layer, loader, loss_fn, opt):
for epoch_id in range(EPOCH_NUM):
for batch_id, (image, label) in enumerate(loader()):
out = layer(image)
loss = loss_fn(out, label)
loss.backward()
opt.step()
opt.clear_grad()
print("Epoch {} batch {}: loss = {}".format(
epoch_id, batch_id, np.mean(loss.numpy())))

create network

layer = LinearNet()
loss_fn = nn.CrossEntropyLoss()
adam = opt.Adam(learning_rate=0.001, parameters=layer.parameters())

create data loader

dataset = RandomDataset(BATCH_NUM * BATCH_SIZE)
loader = paddle.io.DataLoader(dataset,
batch_size=BATCH_SIZE,
shuffle=True,
drop_last=True,
num_workers=2)

train

train(layer, loader, loss_fn, adam)

save

path = "/home/pymleg/data/example.dy_model/linear"
paddle.jit.save(
layer=layer,
path=path,
input_spec=[InputSpec(shape=[None, 784], dtype='float32')])

报错:
/home/pymleg/anaconda3/lib/python3.7/site-packages/paddle/fluid/layers/utils.py:77: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working
return (isinstance(seq, collections.Sequence) and
Traceback (most recent call last):
File "", line 4, in
File "</home/pymleg/anaconda3/lib/python3.7/site-packages/decorator.py:decorator-gen-60>", line 2, in save
File "/home/pymleg/anaconda3/lib/python3.7/site-packages/paddle/fluid/wrapped_decorator.py", line 25, in impl
return wrapped_func(*args, **kwargs)
File "/home/pymleg/anaconda3/lib/python3.7/site-packages/paddle/fluid/dygraph/base.py", line 39, in impl
return func(*args, **kwargs)
File "/home/pymleg/anaconda3/lib/python3.7/site-packages/paddle/fluid/dygraph/jit.py", line 686, in save
concrete_program = static_forward.concrete_program
File "/home/pymleg/anaconda3/lib/python3.7/site-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py", line 454, in concrete_program
return self.concrete_program_specify_input_spec(input_spec=None)
File "/home/pymleg/anaconda3/lib/python3.7/site-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py", line 488, in concrete_program_specify_input_spec
*desired_input_spec)
File "/home/pymleg/anaconda3/lib/python3.7/site-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py", line 402, in get_concrete_program
concrete_program, partial_program_layer = self._program_cache[cache_key]
File "/home/pymleg/anaconda3/lib/python3.7/site-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py", line 711, in getitem
self._caches[item] = self._build_once(item)
File "/home/pymleg/anaconda3/lib/python3.7/site-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py", line 702, in _build_once
class_instance=cache_key.class_instance)
File "</home/pymleg/anaconda3/lib/python3.7/site-packages/decorator.py:decorator-gen-58>", line 2, in from_func_spec
File "/home/pymleg/anaconda3/lib/python3.7/site-packages/paddle/fluid/wrapped_decorator.py", line 25, in impl
return wrapped_func(*args, **kwargs)
File "/home/pymleg/anaconda3/lib/python3.7/site-packages/paddle/fluid/dygraph/base.py", line 39, in impl
return func(*args, **kwargs)
File "/home/pymleg/anaconda3/lib/python3.7/site-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py", line 620, in from_func_spec
static_func = convert_to_static(dygraph_function)
File "/home/pymleg/anaconda3/lib/python3.7/site-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py", line 141, in convert_to_static
static_func = _FUNCTION_CACHE.convert_with_cache(function)
File "/home/pymleg/anaconda3/lib/python3.7/site-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py", line 78, in convert_with_cache
static_func = self._convert(func)
File "/home/pymleg/anaconda3/lib/python3.7/site-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py", line 105, in _convert
source_code = func_to_source_code(func)
File "/home/pymleg/anaconda3/lib/python3.7/site-packages/paddle/fluid/dygraph/dygraph_to_static/utils.py", line 544, in func_to_source_code
source_code = inspect.getsource(function)
File "/home/pymleg/anaconda3/lib/python3.7/inspect.py", line 973, in getsource
lines, lnum = getsourcelines(object)
File "/home/pymleg/anaconda3/lib/python3.7/inspect.py", line 955, in getsourcelines
lines, lnum = findsource(object)
File "/home/pymleg/anaconda3/lib/python3.7/inspect.py", line 786, in findsource
raise OSError('could not get source code')
OSError: could not get source code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant