Python tensorflow keras中的采样softmax
我想做tf keras中的最大损耗采样。我通过子类化keras模型定义了自己的模型。在init中,我指定了我需要的层,包括最后一个密集投影层。但是这个密集层不应该在训练中被调用,因为我想做采样的softmax,并且只使用它的权重和偏差。然后我定义损失函数如下:Python tensorflow keras中的采样softmax,python,tensorflow,keras,Python,Tensorflow,Keras,我想做tf keras中的最大损耗采样。我通过子类化keras模型定义了自己的模型。在init中,我指定了我需要的层,包括最后一个密集投影层。但是这个密集层不应该在训练中被调用,因为我想做采样的softmax,并且只使用它的权重和偏差。然后我定义损失函数如下: class SampledSoftmax: def init( self, num_sampled, num_classes, projectio
class SampledSoftmax:
def init( self,
num_sampled,
num_classes,
projection,
bias,
hidden_size):
self.weights = tf.transpose(projection)
self.bias = bias
self.num_classes = num_classes
self.num_sampled = num_sampled
self.hidden_size = hidden_size
def call(self, y_true, input):
""" reshaping of y_true and input to make them fit each other """
input = tf.reshape(input, (-1,self.hidden_size))
y_true = tf.reshape(y_true, (-1,1))
return tf.nn.sampled_softmax_loss(
weights=self.weights,
biases=self.bias,
labels=y_true,
inputs=input,
num_sampled=self.num_sampled,
num_classes=self.num_classes,
partition_strategy='div')
它接受必要的参数进行初始化,类调用将是所需的采样softmax loss函数。问题是,为了给模型编译增加损失,我需要最后一个密度的权重等。但是1)在training中,稠密层不包含在模型中,2)即使它包含在模型中,稠密层也只会与输入连接,从而在调用我的自定义模型时获得其输入维度等。简言之,在编译模型之前,权重等将不可用。有人能帮我指出正确的方向吗
现在是导致它失败的代码。第一个子类模型如下:
class LanguageModel(tf.keras.Model):
def __init__(self,
vocal_size=15003,
embedding_size=512
input_len=64)
self.embedding = Embedding(vocal_size, embedding_size,
input_length=input_len)
self.lstm = LSTM(hidden_size, return_sequences=True)
self.dense = Dense(vocal_size, activation='softmax')
def call(self, inputs, training=False):
emb_out = self.embedding(inputs)
lstm_out = self.lstm(embrace_out)
res = self.dense(lstm_out)
if (training)
''' shouldn't use the last dense as we want to do sampling'''
return lstm_out
return res
然后对模型进行如下训练
sampled_loss = SampledSoftmax(num_sampled, vocal_size,
model.dense.kernel, model.dense.bias,
hidden_size)
model.compile(optimizer=tf.train.RMSPropOptimizer(lr),
loss=sampled_loss)
它将失败,但我会玩弄它,因为model.dense.kernel是不可访问的,因为在编译模型时,稠密层还没有在call方法中初始化。错误消息如下:
Traceback (most recent call last):
File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/wuxinyu/workspace/nlu/lm/main.py", line 72, in <module>
train_main()
File "/home/wuxinyu/workspace/nlu/lm/main.py", line 64, in train_main
train_model.build_lm_model()
File "/home/wuxinyu/workspace/nlu/lm/main.py", line 26, in build_lm_model
self.model.dense.kernel,
AttributeError: 'Dense' object has no attribute 'kernel'
什么情况下丢失函数不起作用?我想创建一个自定义模型子类化模型。根据tf.keras指南,我在init方法中指定了层,并在call方法中将它们与输入连接到整个网络中。编译模型时需要使用损失函数。捕获的是模型编译发生在init之后但在调用之前。这意味着最后一个致密层还没有得到它的尺寸,因此权重矩阵不可用。因此,在编译模型之前,我无法向SampledSoftmax传递正确的参数以获得损失函数。它形成了一个我无法打破的循环。在编译时必须知道密集层的维度(这使得权重张量可用)。您是否有这样一个示例,其中此丢失会产生错误?添加了失败的案例您是否可以同时包含错误消息?
x = Input(shape=(10,), name='input_x')
emb_out = Embedding(10000,200,input_length=10)(x)
lstm_out = LSTM(200, return_sequences=True)(emb_out)
dense = Dense(10000, activation='sigmoid')
output = dense(lstm_out)
sl = SampledSoftmax(10, 10000, dense.kernel, dense.bias)
model = Model(inputs=x, outputs=lstm_out)
model.compile(optimizer='adam', loss=sl)
model.summary()
model.fit(dataset, epochs=20, steps_per_epoch=5)