“无法转换对象keras”:如何正确实现此层?

时间:2018-07-30 16:31:25

标签: lambda keras combinations lstm

我正在尝试使用Lambda层来实现keras层:

enter image description here

NN是biLSTM层, Uc 是可训练向量。 目的是计算以下函数,该函数结合了biLSTM的不同输出(输出矩阵的列):

enter image description here

其中 Uiz 是不同的输出 hij

我尝试了以下3个输出列的代码示例:

val = np.array([1, 2, 3])
in1 = Input(shape=(3,10), dtype="int32")
in2 = Input(shape=(3,1), dtype="int32")
x = K.repeat_elements(in2, rep=10, axis=2)
res = Multiply()([in1, x])
kvar = K.variable(val, dtype='float64', name='weights')
r_kvar = Reshape((None, None, len(val)))
sum_layer = Lambda(lambda x: K.exp(K.transpose(x)*K.cast(r_kvar, dtype="float64"))/K.sum(K.exp(K.transpose(x)), axis=1), dtype='float64')(K.cast(res, dtype="float64"))

但是我遇到了以下错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_util.py", line 518, in make_tensor_proto
    str_values = [compat.as_bytes(x) for x in proto_values]
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_util.py", line 518, in <listcomp>
    str_values = [compat.as_bytes(x) for x in proto_values]
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/util/compat.py", line 68, in as_bytes
    (bytes_or_text,))
TypeError: Expected binary or unicode string, got <keras.layers.core.Reshape object at 0x7f36fff9a128>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py", line 2910, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-113-4b33b49398bd>", line 1, in <module>
    sum_layer = Lambda(lambda x: K.exp(K.transpose(x)*K.cast(r_kvar, dtype="float64"))/K.sum(K.exp(K.transpose(x)), axis=1), dtype='float64')(K.cast(res, dtype="float64"))
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py", line 619, in __call__
    output = self.call(inputs, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/keras/layers/core.py", line 663, in call
    return self.function(inputs, **arguments)
  File "<ipython-input-113-4b33b49398bd>", line 1, in <lambda>
    sum_layer = Lambda(lambda x: K.exp(K.transpose(x)*K.cast(r_kvar, dtype="float64"))/K.sum(K.exp(K.transpose(x)), axis=1), dtype='float64')(K.cast(res, dtype="float64"))
  File "/usr/local/lib/python3.5/dist-packages/keras/backend/tensorflow_backend.py", line 947, in cast
    return tf.cast(x, dtype)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/math_ops.py", line 779, in cast
    x = ops.convert_to_tensor(x, name="x")
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 950, in convert_to_tensor
    as_ref=False)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1040, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/constant_op.py", line 235, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/constant_op.py", line 214, in constant
    value, dtype=dtype, shape=shape, verify_shape=verify_shape))
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_util.py", line 522, in make_tensor_proto
    "supported type." % (type(values), values))
TypeError: Failed to convert object of type <class 'keras.layers.core.Reshape'> to Tensor. Contents: <keras.layers.core.Reshape object at 0x7f36fff9a128>. Consider casting elements to a supported type.

预先感谢

1 个答案:

答案 0 :(得分:1)

图层不能输入到其他图层。

必须输入“张量”:

#i'm not sure if r_kvar should be "kvar" reshaped, if not, change the input    
r_kvar = Reshape((None, None, len(val)))(kvar) 

def lambdaFunc(x):
    x = K.cast(x[0], dtype='float64')
    r_kvar = K.cast(x[1], dtype='float64')

    xTrans = K.transpose(x)
    K.exp(xTrans*r_kvar)/K.sum(K.exp(xTrans), axis=1)

sum_layer_output = Lambda(lambdaFunc)([res,r_kvar])

您还需要进行很多更改,因为您无法在外部进行操作的keras模型。

val = np.array([1, 2, 3])

in1 = Input(shape=(3,10), dtype="int32")
in2 = Input(shape=(3,1), dtype="int32")

x = Lambda(lambda x: K.repeat_elements(x, rep=10, axis=2))(in2)
res = Multiply()([in1, x])

#or create this inside a layer instead of make it an input
kvar = Input(tensor=K.variable(val, dtype='float64', name='weights'))

r_kvar = Reshape((None, None, len(val)))(kvar)
sum_layer_output = Lambda(lambdaFunc)([res,r_kvar])