1

I am trying to implement the code for Unsupervised Aspect Extraction from the code available here. Link to the paper
While implementing Attention class in ml_layers.py, i am getting error in call function at line

y = K.repeat_elements(y, self.steps, axis=1)    

Complete code of the function is given below:

def call(self, input_tensor, mask=None):
    x = input_tensor[0]
    y = input_tensor[1]
    mask = mask[0]

    y = K.transpose(K.dot(self.W, K.transpose(y)))
    y = K.expand_dims(y, axis=-2)
    y = K.repeat_elements(y, self.steps, axis=1)
    eij = K.sum(x*y, axis=-1)

    if self.bias:
        b = K.repeat_elements(self.b, self.steps, axis=0)
        eij += b

    eij = K.tanh(eij)
    a = K.exp(eij)

    if mask is not None:
        a *= K.cast(mask, K.floatx())

    a /= K.cast(K.sum(a, axis=1, keepdims=True) + K.epsilon(), K.floatx())
    return a

The error is as follows Traceback (most recent call last):

File "", line 1, in model = create_model(ortho_reg, neg_size, emb_dim, aspect_size, emb_path, maxlen, vocab)

File "/home/fractaluser/Projects/workspace/UnsupervisedAspectExtraction/code/model.py", line 32, in create_model att_weights = Attention(name='att_weights')([e_w, y_s])

文件“/home/fractaluser/anaconda3/envs/venv_keras/lib/python3.5/site-packages/keras/engine/base_layer.py”,第 457 行,调用 output = self.call(inputs, **kwargs)

文件“/home/fractaluser/Projects/workspace/UnsupervisedAspectExtraction/code/my_layers.py”,第 58 行,调用 y = K.repeat_elements(y, self.steps, axis=1)

文件“/home/fractaluser/anaconda3/envs/venv_keras/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py”,第 2093 行,repeat_elements 返回连接(x_rep,轴)

文件“/home/fractaluser/anaconda3/envs/venv_keras/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py”,第 1954 行,串联返回 tf.sparse_concat(axis, tensors)

文件“/home/fractaluser/.local/lib/python3.5/site-packages/tensorflow/python/util/deprecation.py”,第 488 行,在 new_func 返回 func(*args, **kwargs)

文件“/home/fractaluser/.local/lib/python3.5/site-packages/tensorflow/python/ops/sparse_ops.py”,第 316 行,在 sparse_concat gen_sparse_ops.sparse_concat(inds,vals,shapes,axis,name=姓名))

文件“/home/fractaluser/.local/lib/python3.5/site-packages/tensorflow/python/ops/gen_sparse_ops.py”,第 911 行,在 sparse_concat concat_dim=concat_dim, name=name)

_apply_op_helper 中的文件“/home/fractaluser/.local/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py”,第 570 行(input_name,op_type_name,len(values),num_attr.minimum) )

ValueError:将参数 'indices' 列出到 'SparseConcat' Op,长度 0 比最小长度 2 短。

在互联网上找不到任何解决方案。请帮忙

4

1 回答 1

-1

我曾经有这个问题

AttributeError: module 'keras.backend' has no attribute 'image_dim_ordering',

所以我必须修改
K.image_dim_ordering() == 'th'('tf') ==> K.image_data_format() == 'channels_first'(channels_last)

在那之后,我遇到了和你一样的问题。但我的问题是仍有一些地方不正确。在我修改所有地方之后。问题消失了。我希望这可以帮助你。

于 2019-10-08T08:35:28.503 回答