Keras_错误:“无法导入名称'\u time_distributed_dense”

2024-09-27 23:25:42 发布

您现在位置:Python中文网/ 问答频道 /正文

由于Keras包装器还不支持注意力模型,我想引用下面的定制注意。在

https://github.com/datalogue/keras-attention/blob/master/models/custom_recurrents.py

但问题是,当我运行上面的代码时,它返回以下错误:

ImportError: cannot import name '_time_distributed_dense'

2.0.0以上的keras似乎不再支持“时间”分布的“密集”了

唯一使用_time_distributed_dense module的部分是以下部分:

^{pr2}$

我应该用哪种方式改变“时间”的分布密度(自我…)部分?在


Tags: https模型githubmastercomtime时间blob
1条回答
网友
1楼 · 发布于 2024-09-27 23:25:42

我刚从An Chen's answer of the GitHub issue发帖(该页或他的答案将来可能会被删除)

def _time_distributed_dense(x, w, b=None, dropout=None,
                        input_dim=None, output_dim=None,
                        timesteps=None, training=None):
"""Apply `y . w + b` for every temporal slice y of x.
# Arguments
    x: input tensor.
    w: weight matrix.
    b: optional bias vector.
    dropout: wether to apply dropout (same dropout mask
        for every temporal slice of the input).
    input_dim: integer; optional dimensionality of the input.
    output_dim: integer; optional dimensionality of the output.
    timesteps: integer; optional number of timesteps.
    training: training phase tensor or boolean.
# Returns
    Output tensor.
"""
if not input_dim:
    input_dim = K.shape(x)[2]
if not timesteps:
    timesteps = K.shape(x)[1]
if not output_dim:
    output_dim = K.shape(w)[1]

if dropout is not None and 0. < dropout < 1.:
    # apply the same dropout pattern at every timestep
    ones = K.ones_like(K.reshape(x[:, 0, :], (-1, input_dim)))
    dropout_matrix = K.dropout(ones, dropout)
    expanded_dropout_matrix = K.repeat(dropout_matrix, timesteps)
    x = K.in_train_phase(x * expanded_dropout_matrix, x, training=training)

# collapse time dimension and batch dimension together
x = K.reshape(x, (-1, input_dim))
x = K.dot(x, w)
if b is not None:
    x = K.bias_add(x, b)
# reshape to 3D tensor
if K.backend() == 'tensorflow':
    x = K.reshape(x, K.stack([-1, timesteps, output_dim]))
    x.set_shape([None, None, output_dim])
else:
    x = K.reshape(x, (-1, timesteps, output_dim))
return x

您只需在Python代码中添加这个。在

相关问题 更多 >

    热门问题