时间分布LSTM结果的前k个分类准确性 [英] Top k categorical accuracy for Time Distributed LSTM results

查看:111
本文介绍了时间分布LSTM结果的前k个分类准确性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用top_k_categorical_accuracy评估LSTM的结果.

I'm trying to evaluate the results of an LSTM using top_k_categorical_accuracy.

对于每个一键编码"令牌,我尝试预测下一个令牌.为此,我使用TimeDistributed层包装器获取序列中每个实例的输出,并将其传递到Dense层以将结果重新编码为相同的一键编码.

For each One-Hot encoded token, I try to predict the next token. In order to do this I take the output for each instance in the sequence by using the TimeDistributed layer wrapper, and pass it to a Dense layer to re-encode the results into the same One-Hot encoding.

虽然使用内置精度度量标准metrics=['accuracy']可以顺利进行,但是使用top_k_categorical_accuracy失败,出现错误消息:

While using the built in accuracy metric metrics=['accuracy'] works without a hitch, using top_k_categorical_accuracy fails, giving me the error message:

ValueError: Shape must be rank 2 but is rank 3 for 'metrics/my_acc/in_top_k/InTopKV2' (op: 'InTopKV2') with input shapes: [?,?,501], [?,?], [].

要使此指标有效,我需要更改什么?

What do I need to change in order to make this metric work?

我的代码如下:

import numpy as np
import glob

import keras
from keras.models import Sequential
from keras.layers import LSTM, Dense, TimeDistributed,Lambda, Dropout, Activation
from keras.metrics import top_k_categorical_accuracy




train_val_split=0.2 # portion to be placed in validation


train_control_number=0
val_control_number=0


def my_acc(y_true, y_pred):
    return top_k_categorical_accuracy(y_true, y_pred, k=5)


def basic_LSTM(features_num):
    model = Sequential()
    model.add(LSTM(40, return_sequences=True, input_shape=(None, features_num)))
    model.add(LSTM(40, return_sequences=True))
    model.add(LSTM(40, return_sequences=True))

    model.add(TimeDistributed(Dense(features_num)))
    model.add(Activation('linear')) 

    print(model.summary())
    model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=[my_acc])   
    return (model)


def main ():
    input_files=glob.glob('*npy')
    data_list,dim=loader(input_files)
    train_list,val_list=data_spliter(data_list)
    model=basic_LSTM(dim)
    model.fit_generator(train_generator(train_list), steps_per_epoch=len(train_list), epochs=10, verbose=1,validation_data=val_generator(val_list),validation_steps=len(val_list))




def train_generator(data_list):
    while True:
        global train_control_number
        train_control_number=cycle_throught(len(data_list),train_control_number)    
        this=data_list[train_control_number]
        x_train = this [:,:-1,:] # all but the last 1
        y_train = this [:,1:,:] # all but the first 1

        yield (x_train, y_train)




def val_generator(data_list):
    while True:
        global val_control_number
        val_control_number=cycle_throught(len(data_list),val_control_number)    
        this=data_list[val_control_number]
        x_train = this [:,:-1,:] # all but the last 1
        y_train = this [:,1:,:] # all but the first 1

        yield (x_train, y_train)



def cycle_throught (total,current):
    current+=1
    if (current==total):
        current=0
    return (current)


def loader(input_files):

    data_list=[]

    for input_file in input_files:
        a=np.load (input_file)
        incoming_shape=list(a.shape)
        requested_shape=[1]+incoming_shape
        a=a.reshape(requested_shape)
        data_list.append(a)


    return (data_list,incoming_shape[-1])


def data_spliter(input_list):
    val_num=int(len(input_list)*train_val_split)
    validation=input_list[:val_num]
    train=input_list[val_num:]

    return (train,validation)



main()

非常感谢.

推荐答案

您可以在自定义指标内以2D张量变换数据,使其适合所需的形状,而使最后一个轴保持不变:

You can transform your data in a 2D tensor within a custom metric so it suits to the required shape, keeping the last axis untouched:

import keras.backend as K #or tf.keras.backend as K    

def 3D_top_k(true, pred):
    true = K.reshape(true, (-1, features_num))   
    pred = K.reshape(pred, (-1, features_num))
    return top_k_categorical_accuracy(true, pred, k=5)

这篇关于时间分布LSTM结果的前k个分类准确性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆