在基于注意力的模型中如何为配置设置参数?
How are parameters set for the config in attention-based models?
配置中有一些参数,尤其是当我更改 max_len
、hidden_size
或 embedding_size
时。
config = {
"max_len": 64,
"hidden_size": 64,
"vocab_size": vocab_size,
"embedding_size": 128,
"n_class": 15,
"learning_rate": 1e-3,
"batch_size": 32,
"train_epoch": 20
}
我收到一个错误:
"ValueError: Cannot feed value of shape (32, 32) for Tensor 'Placeholder:0', which has shape '(?, 64)'"
下面的张量流图是我理解有问题的地方。有没有办法了解需要设置哪些相对 max_len
、hidden_size
或 embedding_size
参数以避免出现上述错误?
embeddings_var = tf.Variable(tf.random_uniform([self.vocab_size, self.embedding_size], -1.0, 1.0),
trainable=True)
batch_embedded = tf.nn.embedding_lookup(embeddings_var, self.x)
# multi-head attention
ma = multihead_attention(queries=batch_embedded, keys=batch_embedded)
# FFN(x) = LN(x + point-wisely NN(x))
outputs = feedforward(ma, [self.hidden_size, self.embedding_size])
outputs = tf.reshape(outputs, [-1, self.max_len * self.embedding_size])
logits = tf.layers.dense(outputs, units=self.n_class)
self.loss = tf.reduce_mean(
tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=self.label))
self.prediction = tf.argmax(tf.nn.softmax(logits), 1)
# optimization
loss_to_minimize = self.loss
tvars = tf.trainable_variables()
gradients = tf.gradients(loss_to_minimize, tvars, aggregation_method=tf.AggregationMethod.EXPERIMENTAL_TREE)
grads, global_norm = tf.clip_by_global_norm(gradients, 1.0)
self.global_step = tf.Variable(0, name="global_step", trainable=False)
self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate)
self.train_op = self.optimizer.apply_gradients(zip(grads, tvars), global_step=self.global_step,
name='train_step')
print("graph built successfully!")
max_len
是训练集中最长 sentence/document 标记的长度。它是输入张量的第二维(第一个是批处理)。
每个句子都会被填充到这个长度。注意力模型需要预定义最长的句子,因为每个标记都有其各自的权重。
hidden_size
是隐藏的RNN单元格的大小,可以设置为每个时间步输出的任何值。
embedding_size
定义标记表示的维度(例如 300 是 word2vec 的标准,1024 是 BERT 嵌入等)。
配置中有一些参数,尤其是当我更改 max_len
、hidden_size
或 embedding_size
时。
config = {
"max_len": 64,
"hidden_size": 64,
"vocab_size": vocab_size,
"embedding_size": 128,
"n_class": 15,
"learning_rate": 1e-3,
"batch_size": 32,
"train_epoch": 20
}
我收到一个错误:
"ValueError: Cannot feed value of shape (32, 32) for Tensor 'Placeholder:0', which has shape '(?, 64)'"
下面的张量流图是我理解有问题的地方。有没有办法了解需要设置哪些相对 max_len
、hidden_size
或 embedding_size
参数以避免出现上述错误?
embeddings_var = tf.Variable(tf.random_uniform([self.vocab_size, self.embedding_size], -1.0, 1.0),
trainable=True)
batch_embedded = tf.nn.embedding_lookup(embeddings_var, self.x)
# multi-head attention
ma = multihead_attention(queries=batch_embedded, keys=batch_embedded)
# FFN(x) = LN(x + point-wisely NN(x))
outputs = feedforward(ma, [self.hidden_size, self.embedding_size])
outputs = tf.reshape(outputs, [-1, self.max_len * self.embedding_size])
logits = tf.layers.dense(outputs, units=self.n_class)
self.loss = tf.reduce_mean(
tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=self.label))
self.prediction = tf.argmax(tf.nn.softmax(logits), 1)
# optimization
loss_to_minimize = self.loss
tvars = tf.trainable_variables()
gradients = tf.gradients(loss_to_minimize, tvars, aggregation_method=tf.AggregationMethod.EXPERIMENTAL_TREE)
grads, global_norm = tf.clip_by_global_norm(gradients, 1.0)
self.global_step = tf.Variable(0, name="global_step", trainable=False)
self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate)
self.train_op = self.optimizer.apply_gradients(zip(grads, tvars), global_step=self.global_step,
name='train_step')
print("graph built successfully!")
max_len
是训练集中最长 sentence/document 标记的长度。它是输入张量的第二维(第一个是批处理)。
每个句子都会被填充到这个长度。注意力模型需要预定义最长的句子,因为每个标记都有其各自的权重。
hidden_size
是隐藏的RNN单元格的大小,可以设置为每个时间步输出的任何值。
embedding_size
定义标记表示的维度(例如 300 是 word2vec 的标准,1024 是 BERT 嵌入等)。