Keras 时间分布LSTM结果的Top k分类精度

Keras 时间分布LSTM结果的Top k分类精度,keras,deep-learning,lstm,Keras,Deep Learning,Lstm,我试图使用top\k\u category\u accurity评估LSTM的结果 对于每一个热编码令牌,我尝试预测下一个令牌。为了做到这一点,我使用TimeDistributed层包装器获取序列中每个实例的输出,并将其传递到一个密集层,以将结果重新编码为同一个热编码 当使用内置精度指标metrics=['accurity']时,使用top\k\u categorical\u accurity失败,给我错误消息: ValueError: Shape must be rank 2 but is r

我试图使用
top\k\u category\u accurity
评估LSTM的结果

对于每一个热编码令牌,我尝试预测下一个令牌。为了做到这一点,我使用
TimeDistributed
层包装器获取序列中每个实例的输出,并将其传递到一个密集层,以将结果重新编码为同一个热编码

当使用内置精度指标
metrics=['accurity']
时,使用
top\k\u categorical\u accurity
失败,给我错误消息:

ValueError: Shape must be rank 2 but is rank 3 for 'metrics/my_acc/in_top_k/InTopKV2' (op: 'InTopKV2') with input shapes: [?,?,501], [?,?], [].
要使此度量值正常工作,我需要更改什么

我的代码如下:

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()

非常感谢。

您可以在自定义度量中以二维张量变换数据,使其适合所需形状,同时保持最后一个轴不变:

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)

您想在哪个轴上进行排序?当你有一个3D张量时,你认为什么是“最伟大的”?我的轴是:[序列号,步数,内边,序列,令牌]。我想,对于每个序列,对于序列中的每个步骤,知道基本事实是否在预测的前5个结果之内。也就是说,排序是沿着最后一个轴,代币轴进行的。太好了,谢谢!出于完整性考虑,您可能希望添加
import keras.backend as K
,我知道这是一种常见的约定,但仍然提高了可读性。