Keras连接稀疏矩阵InvalidArgumentError

时间:2018-09-05 17:55:15

标签: tensorflow scikit-learn keras concatenation sparse-matrix

我正在尝试连接多个输入张量。一些输入是密集的,有些是稀疏矩阵。

    input_shipping = Input(shape=[x_train['shipping'].shape[1]], name='shipping')
    input_condition = Input(shape=[x_train['condition'].shape[1]], name='condition')
    input_subcat1 = Input(shape=[x_train['subcat1'].shape[1]], name='subcat1', sparse=True)
    input_subcat2 = Input(shape=[x_train['subcat2'].shape[1]], name='subcat2', sparse=True)
    input_subcat3 = Input(shape=[x_train['subcat3'].shape[1]], name='subcat3', sparse=True)
    input_brand = Input(shape=[x_train['brand'].shape[1]], name='brand', sparse=True)
    input_description = Input(shape=[x_train['description'].shape[1]], name='description', sparse=True)

    input_list = [input_shipping, input_condition, input_subcat1, input_subcat2, input_subcat3, input_brand, input_description]
    input_conc = concatenate(input_list, axis=-1)

所有稀疏矩阵均由sklearn.preprocessing.LabelBinarizer生成,但最后一个(x_train ['description'])由sklearn.feature_extraction.text.TfidfVectorizer创建。创建完最后一个后,将执行 sort_indices() sum_duplicates(),因此矩阵获得“规范”形式。其属性 _has_canonical_format _has_sorted_indices 均为True。我也是手动检查过的。

尽管如此,培训运行从一开始就中断了

Caused by op 'concatenate_2/SparseToDense_4', defined at:
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevconsole.py", line 511, in <module>
    pydevconsole.start_server(host, int(port), int(client_port), client_host)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevconsole.py", line 336, in start_server
    process_exec_queue(interpreter)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevconsole.py", line 192, in process_exec_queue
    more = interpreter.add_exec(code_fragment)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\_pydev_bundle\pydev_console_utils.py", line 281, in add_exec
    more = self.do_add_exec(code_fragment)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevconsole.py", line 102, in do_add_exec
    command.run()
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevconsole.py", line 63, in run
    self.more = self.interpreter.runsource(text, '<input>', symbol)
  File "C:\Users\Aleksandar\Miniconda3\envs\modelling\lib\code.py", line 75, in runsource
    self.runcode(code)
  File "C:\Users\Aleksandar\Miniconda3\envs\modelling\lib\code.py", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 18, in <module>
  File "C:\Users\Aleksandar\Miniconda3\envs\modelling\lib\site-packages\keras\layers\merge.py", line 641, in concatenate
    return Concatenate(axis=axis, **kwargs)(inputs)
  File "C:\Users\Aleksandar\Miniconda3\envs\modelling\lib\site-packages\keras\engine\base_layer.py", line 457, in __call__
    output = self.call(inputs, **kwargs)
  File "C:\Users\Aleksandar\Miniconda3\envs\modelling\lib\site-packages\keras\layers\merge.py", line 155, in call
    return self._merge_function(inputs)
  File "C:\Users\Aleksandar\Miniconda3\envs\modelling\lib\site-packages\keras\layers\merge.py", line 357, in _merge_function
    return K.concatenate(inputs, axis=self.axis)
  File "C:\Users\Aleksandar\Miniconda3\envs\modelling\lib\site-packages\keras\backend\tensorflow_backend.py", line 1934, in concatenate
    return tf.concat([to_dense(x) for x in tensors], axis)
  File "C:\Users\Aleksandar\Miniconda3\envs\modelling\lib\site-packages\keras\backend\tensorflow_backend.py", line 1934, in <listcomp>
    return tf.concat([to_dense(x) for x in tensors], axis)
  File "C:\Users\Aleksandar\Miniconda3\envs\modelling\lib\site-packages\keras\backend\tensorflow_backend.py", line 353, in to_dense
    return tf.sparse_tensor_to_dense(tensor)
  File "C:\Users\Aleksandar\Miniconda3\envs\modelling\lib\site-packages\tensorflow\python\ops\sparse_ops.py", line 996, in sparse_tensor_to_dense
    name=name)
  File "C:\Users\Aleksandar\Miniconda3\envs\modelling\lib\site-packages\tensorflow\python\ops\sparse_ops.py", line 776, in sparse_to_dense
    name=name)
  File "C:\Users\Aleksandar\Miniconda3\envs\modelling\lib\site-packages\tensorflow\python\ops\gen_sparse_ops.py", line 2898, in sparse_to_dense
    name=name)
  File "C:\Users\Aleksandar\Miniconda3\envs\modelling\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\Aleksandar\Miniconda3\envs\modelling\lib\site-packages\tensorflow\python\util\deprecation.py", line 454, in new_func
    return func(*args, **kwargs)
  File "C:\Users\Aleksandar\Miniconda3\envs\modelling\lib\site-packages\tensorflow\python\framework\ops.py", line 3155, in create_op
    op_def=op_def)
  File "C:\Users\Aleksandar\Miniconda3\envs\modelling\lib\site-packages\tensorflow\python\framework\ops.py", line 1717, in __init__
    self._traceback = tf_stack.extract_stack()
InvalidArgumentError (see above for traceback): indices[1] = [0,111568] is out of order
     [[Node: concatenate_2/SparseToDense_4 = SparseToDense[T=DT_FLOAT, Tindices=DT_INT64, validate_indices=true, _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_description/indices_1_0_4, _arg_description/shape_1_0_5, _arg_description/values_1_0_6, concatenate_2/SparseToDense/default_value)]]

我确定问题出在这个矩阵中,因为如果忽略它,一切都会按预期进行。另外,我确定问题出在连接层,因为当input_description直接连接到下一层时,就没有问题。

我在这里错过了什么吗?除了将所有输入矩阵连接到一个稀疏矩阵之外,还有其他解决方案吗?

我正在使用Keras v2.2.2和TensorFlow-GPU v1.10.0和scikit-learn v0.19.1。 并在Windows 10上运行Python v3.6.6。

0 个答案:

没有答案