tensorflow - 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
解决方案
推荐阅读
- r - 将时钟时间从 H:MM 转换(格式化)为 HH:MM
- apache - 在 DirectAdmin 上安装 OpenLiteSpeed
- android - 暂停协程函数直到它收到结果然后继续
- python - Groupby 列名并将结果添加为附加列
- php - 为什么 is_null 在未设置的变量上抛出异常
- python - 在值而不是时间/合并数据帧上重新采样数据帧
- excel - 在 application.visible=false 时执行 workbook.close 时出现意外的工作簿显示
- c - 两个定时器产生相同的频率,但其中一个是循环 67 次
- google-cloud-data-fusion - GCP Datafusion上传自定义插件太慢
- spring-boot - 使用 SAML sso、keycloak idp、委托用户身份的 springboot 后端到后端