使用CNN对不平衡数据集进行图像分类。 我对tensorflow后端是全新的。 这是多种问题,我有16种。 类是热码。 在
我想计算每个日历元的宏观指标。 F1、准确度和召回率。 在类方法(callback ) :上
efon_train_Begin(self,logs={} ) :
self.val_f1s=[]
self.val_recalls=[]
self.val_precisions=[]
efon_epoch_end(self,epoch,logs={} ) :
val_predict=(NP.asarray ) self.model.predict (self.validation _ data [0] ) ) ) ).round ) )
val _ targ=self.validation _ data [1]
_val_F1=F1_score(val_targ,val_predict,average='macro ' )
_ val _ recall=recall _ score (val _ targ,val_predict,average='macro ' )
_ val _ precision=precision _ score (val _ targ,val_predict,average='macro ' )
self.val_F1s.append(_val_F1 ) ) ) ) ) ) ) )。
self.val _ recalls.append (val _ recall ) )。
self.val _ precisions.append (val _ precision ) )。
print (-val _ f 1: % f-val _ precision : % f-val _ recall % f ) (val_f1,_val_precision,_val_precision )
返回
metrics=Metrics (
因为我们在用,所以我甚至不知道这个代码是否真的有效
^{pr2}$
如果分为多个类别,舍入会出错吗?
使用此代码打印训练集中的度量。 (我只记得对我来说重要的度量。 )也可以在验证集中进行计算。 这是因为它用于模型.计算。 )
代码修订: custommacroforrecallinkerasdefrecall (y _ true,y_pred ) :
true_positives=k.sum(k.round ) k.clip ) y_true*y_pred,0,1 ) )
possible _ positives=k.sum (k.round ) k.clip(y_true,0,1 ) )
return true _ positives/(possible _ positivesk.epsilon () )
efunweightedrecall(y_true,y_pred ) :
return (recall (y _ true [ :0 ],y _ pred [ :0 ] ) recall ) y _ true [ :1 ],y _ pred [ 3360,1 ]
recall (y _ true [ :4 ],y _ pred [ :4 ] ) recall (y _ true [ :5 ],y _ pred [ 3360,5 ] ) )
recall (y _ true [ :6 ],y _ pred [ :6 ] ) recall (y _ true [ :7 ],y _ pred [ 3360,7 ] ) )
recall (y _ true [ :8 ],y _ pred [ :8 ] ) recall (y _ true [ :9 ],y _ pred [ 3360,9 ] ) )
recall (y _ true [ :10 ],y _ pred [ :10 ] ) recall (y _ true [ :11 ],y _ pred [ 3360,11 ] )
recall (y _ true [ :12 ],y _ pred [ :12 ] ) recall (y _ true [ :13 ],y _ pred [ 3360,13 ] )
recall (y _ true [ :14 ],y _ pred [ :14 ] ) recall (y _ true [ :15 ],y _ pred [ 3360,15 ] )
我的模型model.compile(optimizer='Adam ',loss='categorical_crossentropy ',metrics=[unweightedRecall,' accurall] )
train=model.fit _ generator (image _ gen.flow ) train_x、train_label、batch_size=64 )、epochs=100、vepoch class_weight=class_weights,callbacks=[metrics],steps _ per _ epoch=len (train _ x (/64 ) #run the model
验证宏调用与两个不同的代码不同。 在
也就是说,(看到val峰unweightedRecall和无心的香水回忆) Epoch 10/100
19/18 [==================]-13s 703 ms/step-loss :1.5167-unweighted recall
—val _ f 1:168833—val _ precision :197502—val _ recall 0.15636
为什么两个不同代码的宏验证调用有不同的值? 在
奖金问题:对于已经尝试过它的人来说,基于我们感兴趣的指标(例如回忆)使用定制损失或使用加权分类交叉熵产生相同的结果真的值得吗? 在