无法将 <class 'tuple'> 类型的对象转换为 Tensor

时间:2021-07-23 06:21:16

标签: python tensorflow tuples

如何修复以下错误。它由5层组成。前三个后面是 GCN 层。第四层是注意力层。错误似乎来自第 4 层,即注意力层。

class SimpleAttLayer():
    def __init__(self, attention_size, time_major=False):
        self.attention_size = attention_size
        self.time_major = time_major
        self.vars = {}

    def __call__(self, inputs):
        if isinstance(inputs, tuple):
            # In case of Bi-RNN, concatenate the forward and the backward RNN outputs.
            inputs = tf.concat(inputs, 2)
        
        if self.time_major:
            # (T,B,D) => (B,T,D)
            inputs = tf.transpose(inputs, [1, 0, 2])
       
        
        hidden_size = inputs.shape[2].value  # D value - hidden size of the RNN layer
        
        # Trainable parameters

        with tf.variable_scope('v'):
            # Applying fully connected layer with non-linear activation to each of the B*T timestamps;
            #  the shape of `v` is (B,T,D)*(D,A)=(B,T,A), where A=attention_size
            w_omega = tf.get_variable(initializer=tf.random_normal([hidden_size, self.attention_size], stddev=0.1), name='w_omega')
            self.vars['w_omega'] = w_omega

以下是错误消息:我试图将输入更改为 tf.shape(inputs)[0]。总的来说,我是 GCN 和神经网络的新手。我该如何解决这个问题。

Tensor("features:0", shape=(2136, 2136), dtype=float32)
WARNING:tensorflow:From /content/ACKRec/layers.py:126: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.
[<tf.Tensor 'first/MatMul_1:0' shape=<unknown> dtype=float32>]
Tensor("first/Relu:0", dtype=float32)
1
Tensor("first/Relu_1:0", dtype=float32)
[<tf.Tensor 'graphconvolution_1/MatMul_1:0' shape=<unknown> dtype=float32>]
Tensor("graphconvolution_1/Relu:0", dtype=float32)
2
Tensor("graphconvolution_1/Relu_1:0", dtype=float32)
[<tf.Tensor 'graphconvolution_2/MatMul_1:0' shape=<unknown> dtype=float32>]
Tensor("graphconvolution_2/Relu:0", dtype=float32)
3
Tensor("graphconvolution_2/Relu_1:0", dtype=float32)
<unknown>
Tensor("transpose:0", shape=(?, ?, ?), dtype=float32)
(?, ?, ?)
None
WARNING:tensorflow:From /content/ACKRec/layers.py:191: The name tf.random_normal is deprecated. Please use tf.random.normal instead.

Traceback (most recent call last):
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/framework/tensor_util.py", line 541, in make_tensor_proto
    str_values = [compat.as_bytes(x) for x in proto_values]
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/framework/tensor_util.py", line 541, in <listcomp>
    str_values = [compat.as_bytes(x) for x in proto_values]
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/util/compat.py", line 71, in as_bytes
    (bytes_or_text,))
TypeError: Expected binary or unicode string, got None

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "train.py", line 40, in <module>
    model = GCN(placeholders=placeholders, input_dim=features.shape[1], num_support=num_support)
  File "/content/ACKRec/models.py", line 129, in __init__
    self.build()
  File "/content/ACKRec/models.py", line 59, in build
    hidden = self.layers[i](self.activations[-1])
  File "/content/ACKRec/layers.py", line 191, in __call__
    w_omega = tf.get_variable(initializer=tf.random_normal([hidden_size, self.attention_size], stddev=0.1), name='w_omega')
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/ops/random_ops.py", line 69, in random_normal
    shape_tensor = tensor_util.shape_tensor(shape)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/framework/tensor_util.py", line 964, in shape_tensor
    return ops.convert_to_tensor(shape, dtype=dtype, name="shape")
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/framework/ops.py", line 1184, in convert_to_tensor
    return convert_to_tensor_v2(value, dtype, preferred_dtype, name)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/framework/ops.py", line 1242, in convert_to_tensor_v2
    as_ref=False)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/framework/ops.py", line 1297, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/framework/constant_op.py", line 286, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/framework/constant_op.py", line 227, in constant
    allow_broadcast=True)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/framework/constant_op.py", line 265, in _constant_impl
    allow_broadcast=allow_broadcast))
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/framework/tensor_util.py", line 545, in make_tensor_proto
    "supported type." % (type(values), values))
TypeError: Failed to convert object of type <class 'tuple'> to Tensor. Contents: (None, 32). Consider casting elements to a supported type.

0 个答案:

没有答案