首页 > 解决方案 > reshape 的输入是一个有 61440 个值的张量,但请求的形状有 1024 个

问题描述

我正在创建一个带有 GRU 和注意力机制的分类器。训练数据集有 255 个训练特征向量,并具有二进制 (0/1) 标签数据。

火车 = (1000, 255)

标签 = (1000, ) ->{0/1}

关于型号

GRU 将训练数据作为输入(作为编码器工作),注意力机制将通过在每个时间步获取编码器的所有隐藏单元来执行,然后上下文向量(注意力 + 编码器的输出隐藏状态)通过用于预测的 softmax 层。

max_len = 255

BATCH_SIZE = 60
embedding_dim = 256
units = 1024
vocab_train = len(train_token.word_index)+1


class Attention(tf.keras.layers.Layer):
    def __init__(self, units):
        super(Attention, self).__init__()

        self.s = tf.keras.layers.Dense(units, kernel_regularizer=tf.keras.regularizers.L2(0.001))
        self.t = tf.keras.layers.Dense(units, kernel_regularizer=tf.keras.regularizers.L2(0.001))
        self.a = tf.keras.layers.Dense(1, kernel_regularizer=tf.keras.regularizers.L2(0.001))

    def call(self, query, value):
        #query: (None, 255, 1024)
        #value: (None, 1024)
        
        query = query[0, :]
        value = tf.expand_dims(value, 1)[0, :]
        #prev: (255, 1024)
        #value: (1, 1024)
        
        score = self.a(tf.keras.activations.relu(self.s(query) + self.t(value)))
        
        attention_weights = tf.keras.activations.softmax(score, axis=1) 

        attention = attention_weights * value
        
        attention = tf.reduce_sum(attention, axis=0) 

        return attention, attention_weights

attention = Attention(units)


# encoder 
encoder = tf.keras.Input(shape=(max_len, ))
enc_embd = tf.keras.layers.Embedding(vocab_train, embedding_dim)(encoder)
encoder_gru = tf.keras.layers.GRU(units, return_sequences=True, return_state=True, kernel_regularizer=tf.keras.regularizers.L2(0.001))
output_e, hidden_e = encoder_gru(enc_embd)

# attention
attention_value, attention_weights = attention(output_e, hidden_e)

# reshaping to concatenate the attention and hidden_e layer
attention_value = tf.reshape(attention_value, (1, attention_value.shape[0]))
hidden_e = tf.reshape(hidden_e, (1, hidden_e.shape[1]))

context_vector = tf.concat([attention_value, hidden_e], axis=1)

attention_layer = tf.keras.layers.Dense(1*context_vector.shape[1], activation='relu')
attention_ = attention_layer(context_vector)

# final output layer
final_output = tf.keras.layers.Dense(1, activation='softmax', kernel_regularizer=tf.keras.regularizers.L2(0.001))
output_f = final_output(attention_)



model = tf.keras.Model(encoder, output_f)

print(model.summary())

model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])

model.fit(train, label, batch_size=60, epochs=10, verbose=1)

错误

InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument:  Input to reshape is a tensor with 61440 values, but the requested shape has 1024
     [[node model_11/tf.reshape_51/Reshape (defined at <ipython-input-336-3c6515bf3e82>:1) ]]
     [[gradient_tape/model_11/embedding_42/embedding_lookup/Reshape/_38]]
  (1) Invalid argument:  Input to reshape is a tensor with 61440 values, but the requested shape has 1024
     [[node model_11/tf.reshape_51/Reshape (defined at <ipython-input-336-3c6515bf3e82>:1) ]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_51709]

Function call stack:
train_function -> train_function

标签: tensorflowmachine-learningkerasdeep-learningnlp

解决方案


推荐阅读