在Keras中,如何避免在模型编译时需要形状知识?

时间:2019-01-10 12:33:32

标签: python keras

我在Python中具有以下功能

def mlpg_postprocessing(mfcc, bins_1, scaler_sp):
    """
    Takes the static and delta features, converts back and unnormalises

    Parameters:
    ----------
    mfcc: (N x T X bins_1) array
    bins_1: number of static + delta coefficients + power 
    scaler_sp: normalisation object

    Return:
    -------
    MFCC features after maximum likelihood parameter generations
    """

    windows = [
        (0, 0, np.array([1.0])),            # static
        (1, 1, np.array([-0.5, 0.0, 0.5])), # delta
    ]

    N = mfcc.shape[0]
    T = mfcc.shape[1]

    mlpg_generated = np.zeros((N,T,bins_1))

    for i in range(N):
        mlpg_generated[i,:,:] = mlpg(mfcc[i,:,:],
                                     np.ones((T,bins_1 * 2)),
                                     windows)
    for i in range(len(scaler_sp)):
        mlpg_generated[:,:,i] = scaler_sp[i].inverse_transform(mlpg_generated[:,:,i])

    return mlpg_generated

我想将此功能用作Keras模型的最后一层,我认为最简单的方法是使用Lambda层:

    mlpg_layer = Lambda(lambda mfcc:
                        proc.mlpg_postprocessing(mfcc,
                                                    bins_1,
                                                    scaler_sp))(
                                                        dense)

但是形状参数N,T在编译时未知,因此创建函数时遇到麻烦。

TypeError: __index__ returned non-int (type NoneType)

如何更改代码,以便在编译时不需要形状?

0 个答案:

没有答案