在tensorflow框架下,模型为GNN模型,要解决图着色(图论)问题,中间大致过程经过编码输入,RNN循环,聚合更新节点,颜色的嵌入,经固定次数嵌入更新,得到最后的节点和颜色的特征,输入MLP,进行预测任务:该图是否接受C中颜色着色?(二分类问题)
我想在最后一层激活函数sigmoid之前加入BN层,部分段代码如下:
# Get the last embeddings
last_states = gnn(
{ "M": M_matrix, "VC": VC_matrix, 'chrom_number': chrom_number },
{ "V": vertex_initial_embeddings, "C": colors_initial_embeddings },
time_steps = time_steps
)
GNN["last_states"] = last_states
V_n = last_states['V'].h
C_n = last_states['C'].h
# Compute a vote for each embedding
V_vote = tf.reshape(V_vote_MLP(V_n), [-1])
# Add Batch Normalization layer before activation function
V_vote = tf.keras.layers.BatchNormalization()(V_vote)
# Compute the number of problems in the batch
num_problems = tf.shape(n_vertices)[0]
pred_logits = tf.while_loop(
lambda i, pred_logits: tf.less(i, num_problems),
lambda i, pred_logits:
(
(i + 1),
pred_logits.write(
i,
tf.reduce_mean(V_vote[tf.reduce_sum(n_vertices[0:i]):tf.reduce_sum(n_vertices[0:i]) + n_vertices[i]])
)
),
[0, tf.TensorArray(size=num_problems, dtype=tf.float32)]
)[1].stack()
# Add sigmoid activation function
pred_logits = tf.keras.activations.sigmoid(pred_logits)
# Assign predictions to dictionary
GNN['predictions'] = pred_logits
出现了如下报错:ValueError: ('Input has undefined axis
dimension. Input shape: ', TensorShape([None]))
我应该怎么修改代码呢?
同时我想在如下代码添加梯度裁剪该怎么添加呢?
# Define loss
GNN['loss'] = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=cn_exists, logits=pred_logits))
# Define optimizer
optimizer = tf.train.AdamOptimizer(name='Adam', learning_rate=learning_rate)
# Compute cost relative to L2 normalization
vars_cost = tf.add_n([ tf.nn.l2_loss(var) for var in tf.trainable_variables() ])
# Define gradients and train step
grads, _ = tf.clip_by_global_norm(tf.gradients(GNN['loss'] + tf.multiply(vars_cost, l2norm_scaling),tf.trainable_variables()),global_norm_gradient_clipping_ratio)
GNN['train_step'] = optimizer.apply_gradients(zip(grads, tf.trainable_variables()))
GNN['C_n'] = C_n