如何在keras中使用可学习的权重进行矩阵乘法?
How to do matrix multiplication with learnable weights in keras?
我有一个像下面这样的模型。我想在最后添加一个可学习权重矩阵,它被初始化为我传递给函数 create_model.
的变量 matrix
为了直观地了解我想做什么,假设 矩阵 应该是我传递给模型的那个,但我觉得它可以还是在训练的时候进行微调。因此,我希望它被初始化为我传递的值,然后在训练过程中进行细化。
下面的代码有效,但正如您从 model.summary() 输出中看到的那样,矩阵乘法不包含可学习的权重,这让我认为权重矩阵未微调。
我做错了什么?
def create_model(num_columns, matrix):
inp_layer = tfl.Input((num_columns,))
dense = tfl.Dense(512, activation = 'relu')(inp_layer)
dense = tfl.Dense(256, activation = 'relu')(dense)
dense = tfl.Dense(128, activation = 'relu')(dense)
va = tf.Variable(matrix, dtype = tf.float32)
dense = K.dot(dense, va )
model = tf.keras.Model(inputs = inp_layer, outputs = dense)
model.compile(optimizer='adam', loss=['binary_crossentropy'])
model.summary()
return model
matrix = np.random.randint(0,2,(128, 206)) # In reality, this is not random, but it has sensed values
num_columns = 750
model = create_model(num_columns,matrix)
你可以简单地使用一个没有偏差的密集层来做这个乘法。建立模型后,我用您提供的矩阵更改了感兴趣的权重
def create_model(num_columns, matrix):
inp_layer = Input((num_columns,))
x = Dense(512, activation = 'relu')(inp_layer)
x = Dense(256, activation = 'relu')(x)
x = Dense(128, activation = 'relu')(x)
dense = Dense(206, use_bias=False)(x)
model = Model(inputs = inp_layer, outputs = dense)
model.compile(optimizer='adam', loss=['binary_crossentropy'])
model.set_weights(model.get_weights()[:-1] + [matrix])
model.summary()
return model
matrix = np.random.randint(0,2,(128, 206)) # In reality, this is not random, but it has sensed values
num_columns = 750
model = create_model(num_columns,matrix)
检查
(model.get_weights()[-1] == matrix).all() # True
这样权重就可以fine-tuned
我有一个像下面这样的模型。我想在最后添加一个可学习权重矩阵,它被初始化为我传递给函数 create_model.
的变量 matrix为了直观地了解我想做什么,假设 矩阵 应该是我传递给模型的那个,但我觉得它可以还是在训练的时候进行微调。因此,我希望它被初始化为我传递的值,然后在训练过程中进行细化。
下面的代码有效,但正如您从 model.summary() 输出中看到的那样,矩阵乘法不包含可学习的权重,这让我认为权重矩阵未微调。
我做错了什么?
def create_model(num_columns, matrix):
inp_layer = tfl.Input((num_columns,))
dense = tfl.Dense(512, activation = 'relu')(inp_layer)
dense = tfl.Dense(256, activation = 'relu')(dense)
dense = tfl.Dense(128, activation = 'relu')(dense)
va = tf.Variable(matrix, dtype = tf.float32)
dense = K.dot(dense, va )
model = tf.keras.Model(inputs = inp_layer, outputs = dense)
model.compile(optimizer='adam', loss=['binary_crossentropy'])
model.summary()
return model
matrix = np.random.randint(0,2,(128, 206)) # In reality, this is not random, but it has sensed values
num_columns = 750
model = create_model(num_columns,matrix)
你可以简单地使用一个没有偏差的密集层来做这个乘法。建立模型后,我用您提供的矩阵更改了感兴趣的权重
def create_model(num_columns, matrix):
inp_layer = Input((num_columns,))
x = Dense(512, activation = 'relu')(inp_layer)
x = Dense(256, activation = 'relu')(x)
x = Dense(128, activation = 'relu')(x)
dense = Dense(206, use_bias=False)(x)
model = Model(inputs = inp_layer, outputs = dense)
model.compile(optimizer='adam', loss=['binary_crossentropy'])
model.set_weights(model.get_weights()[:-1] + [matrix])
model.summary()
return model
matrix = np.random.randint(0,2,(128, 206)) # In reality, this is not random, but it has sensed values
num_columns = 750
model = create_model(num_columns,matrix)
检查
(model.get_weights()[-1] == matrix).all() # True
这样权重就可以fine-tuned