Machine learning 有人能解释为什么这个标量层是不可训练的吗?

Machine learning 有人能解释为什么这个标量层是不可训练的吗?,machine-learning,keras,keras-layer,Machine Learning,Keras,Keras Layer,我不知道为什么我把标量模型放在外面。它仍然可以训练 原始模型可在此处找到: 我尝试使用Keras实现。我知道标量有它自己的学习率,但使用相同的损失函数和分数模型,我怎么能做到这一点。请告诉我最简单的方法,我是新手 query_word_ids = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="query_word_ids") query_mask = tf.keras.Input(shape=(max_

我不知道为什么我把标量模型放在外面。它仍然可以训练

原始模型可在此处找到: 我尝试使用Keras实现。我知道标量有它自己的学习率,但使用相同的损失函数和分数模型,我怎么能做到这一点。请告诉我最简单的方法,我是新手

  query_word_ids = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="query_word_ids")
  query_mask = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="query_mask")
  query_segment_ids = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="query_segment_ids")

  pooled_output, sequence_output = bert_layer([query_word_ids, query_mask, query_segment_ids])
  query_dense_embeddings = sequence_output[:, 0, :] # [None, 768]


  candidates_dense_embeddings = tf.keras.Input(shape=(topk,768,), dtype=tf.float32, name="candidates_dense_embeddings")


  candidates_dense_score = keras.layers.Dot(axes=(2,1),name="dense_score")([candidates_dense_embeddings,query_dense_embeddings])

  batch_size = candidates_dense_score.shape[0] 
  candidate_sparse_score = tf.keras.Input(shape=(batch_size,), dtype=tf.float32, name="candidate_sparse_score")

  scaling_sparse_score = Scalar()(candidate_sparse_score)

  score = scaling_sparse_score + candidates_dense_score

  model =  tf.keras.models.Model(inputs = [[query_word_ids, query_mask, query_segment_ids],candidates_dense_embeddings, candidate_sparse_score], outputs=score )
  model.compile(optimizer=tf.keras.optimizers.Adam(lr=1e-5),loss=marginal_loss)

  return model  

  class Scalar(Layer):
    def __init__(self):
      super(Scalar, self).__init__()
    def build(self,input_shape) :
      self.W = K.variable(2)
      self._trainable_weights=[self.W]
    def call(self,inputs):
      return self.W*inputs