基本上,我想计算精度的最高k @ n并回忆keras神经网络中每个时期的@n . 经过长时间的搜索,我找到了自定义功能......
def precision(y_true,y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
return precision
def召回(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)))
recall = true_positives / (possible_positives + K.epsilon())
return recall
//这是用于模型编译
model.compile(optimizer = RMSprop(lr = learning_rate),loss ='binary_crossentropy',metrics = [precision,recall])
对于xrange(epochs)中的纪元:t1 = time()user_input,item_input,labels = get_train_instances(train,num_negatives)
用于培训
hist = model.fit([np.array(user_input), np.array(item_input)], np.array(labels), batch_size=batch_size, nb_epoch=1, verbose=0, shuffle=True) loss,precision,recall = hist.history['loss'][0],hist.history["precision"][0] , hist.history["recall"][0] print('Iteration %d [%.1f s]: loss = %.4f , precision= %.4f , recall=%.4f [%.1f s]' % (epoch, t2-t1, loss,precision,recall, time()-t2))
//这是我的输出,看起来不对... enter image description here
我希望你现在明白了......
1 回答
你最好的选择是使用classification report capability in SciKitLearn . 这是一般的分类评估功能,但我经常与Keras一起使用 .
对分类报告的调用将为您提供每个类的精确度和召回统计信息,如下所示: