数值错误:维度必须为4,但实际为3。

3
使用tensorflow Keras实现自注意力机制,并进行了一些修改(例如,残差(加法连接))。
我的输入形状如下:
myinput:KerasTensor(type_spec=TensorSpec(shape=(None, 8, 6, 64), dtype=tf.float32, name=None), name='multiply/mul:0', description="created by layer 'multiply'") 我的目标是通过自注意力机制处理TensorSpec(shape=(None, 8, 6, 64)中的8个时间戳(逐个处理6 * 64)并获取每个时间戳的自注意力特征图,然后将其再次连接到输出张量形状(None, 8, 6, 64)中。
实现的代码如下:
import tensorflow as tf
from tensorflow.keras.layers import Permute


def conv1d(channels, ks=1, strides=1, padding='same'):
    conv = tf.keras.layers.Conv1D(channels, ks, strides, padding, activation='relu', use_bias=False,
                                  kernel_initializer='HeNormal')
    return conv


class my_self_attention(tf.keras.layers.Layer):
    def __init__(self, channels):
        super(my_self_attention, self).__init__()
        self.query = conv1d(channels)
        self.key = conv1d(channels)
        self.value = conv1d(channels)
        self.gamma = tf.compat.v1.get_variable("gamma", [1], initializer=tf.constant_initializer(0.0))

    def call(self, x):
        x = tf.reshape(x, shape=[-1, x.shape[2], x.shape[3]])
        f = self.query(x),
        g = self.key(x)
        h = self.value(x)
        attention_weights = tf.keras.activations.softmax(
            tf.matmul(g, Permute((2, 1))(f)))  # query multiply with key and then softmax on it
        sensor_att_fm = tf.matmul(attention_weights, h)
        o = self.gamma * sensor_att_fm + x
        # return tf.reshape(o, shape = [-1, 1, x.shape[1], x.shape[2]])
        return tf.reshape(o, shape=[-1, 1, x.shape[1], x.shape[2]])


 sa = my_self_attention(channels)
 refined_fm = tf.concat([sa(tf.expand_dims(my_input[:, t, :, :], 1)) for t in   range(my_input.shape[1])], 1)

遇到以下错误

ValueError: Dimension must be 4 but is 3 for '{{node my_self_attention/permute/transpose}} = Transpose[T=DT_FLOAT, Tperm=DT_INT32](my_self_attention/permute/transpose/a, my_self_attention/permute/transpose/perm)' with input shapes: [1,?,6,64], [3].

我该如何解决这个问题?

1个回答

2

请确保通道数与输入数据的最后一个维度相同。在这里添加逗号:f = self.query(x),将创建一个元组,您可能不需要它:以下是一个可行的示例:

import tensorflow as tf
from tensorflow.keras.layers import Permute

def conv1d(channels, ks=1, strides=1, padding='same'):
  conv = tf.keras.layers.Conv1D(channels, ks, strides, padding, activation='relu', use_bias=False,
                                kernel_initializer='HeNormal')
  return conv

class my_self_attention(tf.keras.layers.Layer):
  def __init__(self, channels):
      super(my_self_attention, self).__init__()
      self.query = conv1d(channels)
      self.key = conv1d(channels)
      self.value = conv1d(channels)
      self.gamma = tf.compat.v1.get_variable("gamma", [1], initializer=tf.constant_initializer(0.0))

  def call(self, x):
      x = tf.reshape(x, shape=[-1, x.shape[2], x.shape[3]])
      f = self.query(x)
      g = self.key(x)
      h = self.value(x)

      attention_weights = tf.keras.activations.softmax(
          tf.matmul(g, Permute((2, 1))(f)))  # query multiply with key and then softmax on it
      sensor_att_fm = tf.matmul(attention_weights, h)
      o = self.gamma * sensor_att_fm + x
      return tf.reshape(o, shape=[-1, 1, x.shape[1], x.shape[2]])


my_input = tf.random.normal((50, 8, 6, 64))
sa = my_self_attention(64)
refined_fm = tf.concat([sa(tf.expand_dims(my_input[:, t, :, :], 1)) for t in   range(my_input.shape[1])], 1)

哦,我犯了多傻的错误。通道数与输入的最后一个维度相似。但是在“f=self.query(x),”处打错了逗号。只需删除逗号即可解决问题。非常感谢。 - Ahmad
然而,现在我正在收到警告:“WARNING:absl:发现未跟踪的函数,例如conv1d_layer_call_and_return_conditional_losses、conv1d_layer_call_fn、conv1d_1_layer_call_and_return_conditional_losses、conv1d_1_layer_call_fn、conv1d_2_layer_call_and_return_conditional_losses,在保存时显示(显示15个中的5个)。” - Ahmad
1
嗯,看起来是一个新问题,因为使用您提供的代码我没有遇到这个错误。也许可以发一篇新的帖子,提供更多信息? - AloneTogether
这里是:https://stackoverflow.com/questions/70924722/warningabslfound-untraced-functions-such-as-conv1d-layer-call-and-return-condi - Ahmad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接