使用DNN出现交叉熵损失报错
768326003 发布于2020-07 浏览:5279 回复:4
1
收藏

参加《百度顶会论文复现营》其中作业《车牌识别》部分,运行代码出现错误。

定义DNN网络如下:

#定义DNN网络
class MyDNN(fluid.dygraph.Layer):
'''
DNN网络
'''
def __init__(self):
super(MyDNN,self).__init__()
self.hidden1 = Linear(20*20,200,act='relu')
self.hidden2 = Linear(200,100,act='relu')
self.hidden3 = Linear(100,100,act='relu')
self.hidden4 = Linear(100,60,act='softmax') 
def forward(self,input): # forward 定义执行实际运行时网络的执行逻辑
'''前向计算'''
x=fluid.layers.reshape(input,shape=[-1,20*20])
x=self.hidden1(x)
x=self.hidden2(x)
x=self.hidden3(x)
y=self.hidden4(x)
return y


训练模型没有修改,代码如下:

with fluid.dygraph.guard():
    model=MyDNN() #模型实例化
    model.train() #训练模式
    opt=fluid.optimizer.SGDOptimizer(learning_rate=train_parameters['learning_strategy']['lr'], parameter_list=model.parameters())#优化器选用SGD随机梯度下降,学习率为0.001.
    epochs_num=train_parameters['num_epochs'] #迭代次数
    
    for pass_num in range(epochs_num):
        for batch_id,data in enumerate(train_reader()):
            images=np.array([x[0].reshape(1,20,20) for x in data],np.float32)
            labels = np.array([x[1] for x in data]).astype('int64')
            labels = labels[:, np.newaxis]
            image=fluid.dygraph.to_variable(images)
            label=fluid.dygraph.to_variable(labels)

            predict=model(image) #数据传入model
            
            loss=fluid.layers.cross_entropy(predict,label)
            avg_loss=fluid.layers.mean(loss)#获取loss值
            
            acc=fluid.layers.accuracy(predict,label)#计算精度
            
            if batch_id!=0 and batch_id%50==0:
                Batch = Batch+50 
                Batchs.append(Batch)
                all_train_loss.append(avg_loss.numpy()[0])
                all_train_accs.append(acc.numpy()[0])
                
                print("train_pass:{},batch_id:{},train_loss:{},train_acc:{}".format(pass_num,batch_id,avg_loss.numpy(),acc.numpy()))
            
            avg_loss.backward()       
            opt.minimize(avg_loss)    #优化器对象的minimize方法对参数进行更新 
            model.clear_gradients()   #model.clear_gradients()来重置梯度
    fluid.save_dygraph(model.state_dict(),'MyDNN')#保存模型

draw_train_acc(Batchs,all_train_accs)
draw_train_loss(Batchs,all_train_loss)


显示第17行报错,错误信息如下:

---------------------------------------------------------------------------EnforceNotMet                             Traceback (most recent call last) in 
     15             predict=model(image) #数据传入model
     16 
---> 17             loss=fluid.layers.cross_entropy(predict,label)
     18             avg_loss=fluid.layers.mean(loss)#获取loss值
     19 
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/loss.py in cross_entropy(input, label, soft_label, ignore_index)
    237     """
    238     if not soft_label:
--> 239         return cross_entropy2(input, label, ignore_index)
    240 
    241     if in_dygraph_mode():
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/loss.py in cross_entropy2(input, label, ignore_index)
    258     if in_dygraph_mode():
    259         loss, _, _ = core.ops.cross_entropy2(input, label, 'ignore_index',
--> 260                                              ignore_index)
    261         return loss
    262 
EnforceNotMet: 

--------------------------------------------
C++ Call Stacks (More useful to developers):
--------------------------------------------
0   std::string paddle::platform::GetTraceBackString(char const*&&, char const*, int)
1   paddle::platform::EnforceNotMet::EnforceNotMet(std::__exception_ptr::exception_ptr, char const*, int)
2   paddle::operators::CrossEntropyOpKernel2::Compute(paddle::framework::ExecutionContext const&) const
3   std::_Function_handler, paddle::operators::CrossEntropyOpKernel2 >::operator()(char const*, char const*, int) const::{lambda(paddle::framework::ExecutionContext const&)#1}>::_M_invoke(std::_Any_data const&, paddle::framework::ExecutionContext const&)
4   paddle::imperative::PreparedOp::Run(std::map, std::allocator > >, std::less, std::allocator, std::allocator > > > > > const&, std::map, std::allocator > >, std::less, std::allocator, std::allocator > > > > > const&, std::unordered_map >, std::vector >, std::vector >, bool, std::vector >, paddle::framework::BlockDesc*, long, std::vector >, std::vector >, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_>, std::hash, std::equal_to, std::allocator >, std::vector >, std::vector >, bool, std::vector >, paddle::framework::BlockDesc*, long, std::vector >, std::vector >, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> > > > const&)
5   paddle::imperative::OpBase::Run(paddle::framework::OperatorBase const&, std::map, std::allocator > >, std::less, std::allocator, std::allocator > > > > > const&, std::map, std::allocator > >, std::less, std::allocator, std::allocator > > > > > const&, std::unordered_map >, std::vector >, std::vector >, bool, std::vector >, paddle::framework::BlockDesc*, long, std::vector >, std::vector >, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_>, std::hash, std::equal_to, std::allocator >, std::vector >, std::vector >, bool, std::vector >, paddle::framework::BlockDesc*, long, std::vector >, std::vector >, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> > > > const&, paddle::platform::Place const&)
6   paddle::imperative::Tracer::TraceOp(std::string const&, std::map, std::allocator > >, std::less, std::allocator, std::allocator > > > > > const&, std::map, std::allocator > >, std::less, std::allocator, std::allocator > > > > > const&, std::unordered_map >, std::vector >, std::vector >, bool, std::vector >, paddle::framework::BlockDesc*, long, std::vector >, std::vector >, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_>, std::hash, std::equal_to, std::allocator >, std::vector >, std::vector >, bool, std::vector >, paddle::framework::BlockDesc*, long, std::vector >, std::vector >, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> > > >, paddle::platform::Place const&, bool)
7   paddle::imperative::Tracer::TraceOp(std::string const&, std::map, std::allocator > >, std::less, std::allocator, std::allocator > > > > > const&, std::map, std::allocator > >, std::less, std::allocator, std::allocator > > > > > const&, std::unordered_map >, std::vector >, std::vector >, bool, std::vector >, paddle::framework::BlockDesc*, long, std::vector >, std::vector >, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_>, std::hash, std::equal_to, std::allocator >, std::vector >, std::vector >, bool, std::vector >, paddle::framework::BlockDesc*, long, std::vector >, std::vector >, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> > > >)

----------------------
Error Message Summary:
----------------------
Error: Variable value (label) of OP(fluid.layers.cross_entropy) expected >= 0 and < 60, but got 60. Please check label value. at (/paddle/paddle/fluid/operators/cross_entropy_op.h:173)
收藏
点赞
1
个赞
共4条回复 最后由156******37回复于2022-07
#5干嘛呢啊那呢的回复于2020-08
#4 干嘛呢啊那呢的回复
self.hidden4 = Linear(100,65,act='softmax')也会出现此问题,我的解决方法:self.hidden4 = Linear(100,66,act='softmax') 我认为:标签由0开始,到65结束,共计66个标签
展开

原因如下,飞桨有个默认的文件夹被记入标签中,查看readme.json会发现有个.ipynb_checkpoints文件夹,所以导致标签多了一位

0
#4干嘛呢啊那呢的回复于2020-08

self.hidden4 = Linear(100,65,act='softmax')也会出现此问题,我的解决方法:self.hidden4 = Linear(100,66,act='softmax')

我认为:标签由0开始,到65结束,共计66个标签

0
#3李勇华的老巢回复于2020-07

试试

0
#2李勇华的老巢回复于2020-07

self.hidden4 = Linear(100,60,act='softmax')

改成

self.hidden4 = Linear(100,65,act='softmax')

0
TOP
切换版块