【发布时间】:2019-11-11 06:47:23
【问题描述】:
我考虑过的一些方法:
从模型类继承 Sampled softmax in tensorflow keras
继承自 Layers 类 How can I use TensorFlow's sampled softmax loss function in a Keras model?
在这两种方法中,模型方法更简洁,因为分层方法有点老套——它将目标作为输入的一部分推入,然后再见多输出模型。
我需要一些帮助来继承 Model 类 - 具体来说: 1)与第一种方法不同 - 我想像我们在指定标准 keras 模型时一样采用任意数量的层。例如,
class LanguageModel(tf.keras.Model):
def __init__(self, **kwargs)
2)我希望将以下代码合并到模型类中 - 但希望让模型类认识到这一点
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))
weights = tf.Variable(tf.float64))
biases = tf.Variable(tf.float64)
loss = tf.nn.sampled_softmax_loss(
weights=weights,
biases=biases,
labels=labels,
inputs=inputs,
...,
partition_strategy="div")
logits = tf.matmul(inputs, tf.transpose(weights))
logits = tf.nn.bias_add(logits, biases)
y_predis = tf.nn.softmax_cross_entropy_with_logits_v2(
labels=inputs[1],
logits=logits)
3 我想我需要一些指针来指出我应该在功能 API 中处理模型类的哪些部分 - 知道我必须编写一个像上面这样的自定义损失函数。 我想问题是访问 tf.nn.sampledsoftmax 函数中的权重
【问题讨论】:
标签: python tensorflow keras sampled-softmax