我正在尝试在Tensorflow中编写核密度估计算法。
在拟合KDE模型时,我正在遍历当前批处理中的所有数据,并且对于每个数据,我使用Function ExciseBracketedText(ByVal Text As String, Optional TrimSpaces AS Boolean = False) As String
Dim lBracketCount As Long, sExcise As String, lCounter As Long
'How many pairs of Open/Close brackets are there?
'lBracketCount = Len(Text) - WorksheetFunction.Max(Len(Replace(Text, "(", "")), Len(Replace(Text, ")", "")))
'This does not distinguish between correct & misordered pairs
'New code works in VB environments other than Excel VBA
lBracketCount = Len(Replace(Text, "(", ""))
If lBracketCount < Len(Replace(Text, ")", "")) Then lBracketCount = Len(Replace(Text, ")", ""))
lBracketCount = Len(Text) - lBracketCount
' e.g. ")()(" will return 2, even though only 1 will be trimmed
ExciseBracketedText = Text
sExcise = "" 'This is the text to cut out in each loop
While lBracketCount > 0 'Once for each bracket pair
For lCounter = 1 To Len(ExciseBracketedText)
If Mid(ExciseBracketedText, lCounter, 1) = "(" Then
sExcise = "(" 'Reset the text-to-excise every time we hit an open bracket
ElseIf Mid(ExciseBracketedText, lCounter, 1) = ")" Then
If Len(sExcise) > 0 Then
'Replace the text in brackets when we hit a close bracket
ExciseBracketedText = Replace(ExciseBracketedText, sExcise & ")", "")
Exit For
End If
ElseIf Len(sExcise) > 0 Then
'If we already have an open bracket, at to the text we will remove
sExcise = sExcise & Mid(ExciseBracketedText, lCounter, 1)
End If
Next lCounter
lBracketCount = lBracketCount - 1 'Next pair
sExcise = "" 'Reset the text to excise
'If the first open bracket is AFTER the last close bracket, then we can exit early
If InStr(ExciseBracketedText, "(") > InStrRev(ExciseBracketedText, ")") Then lBracketCount = 0
Wend
If TrimSpaces Then ExciseBracketedText=Application.Trim(ExciseBracketedText)
End Function
对象创建内核:
tensorflow.contrib.distributions.MultivariateNormalDiag
后来,当我试图预测数据点相对于上面拟合的模型的可能性时,对于我正在评估的每个数据点,我将上面每个内核给出的概率加在一起:
self.kernels = [MultivariateNormalDiag(loc=data, scale=bandwidth) for data in X]
这种方法仅在
tf.reduce_sum([kernel._prob(X) for kernel in self.kernels], axis=0)
是一个numpy数组时才有效,因为TF不允许你迭代Tensor。我的问题是,是否有一种方法可以使上述算法与X
或X
一起使用tf.Tensor
?
答案 0 :(得分:0)
我在这个问题上找到的一个答案解决了适应KDE并一举预测概率的问题。但是,实施有点过时了。
def fit_predict(self, data):
return tf.map_fn(lambda x: \
tf.div(tf.reduce_sum(
tf.map_fn(lambda x_i: self.kernel_dist(x_i, self.bandwidth).prob(x), self.fit_X)),
tf.multiply(tf.cast(data.shape[0], dtype=tf.float64), self.bandwidth[0])), self.X)
第一个tf.map_fn
遍历我们正在计算可能性的数据,将每个单独内核的概率加在一起。
第二个tf.map_fn
遍历我们用于调整模型的所有数据,并创建tf.contrib.distributions.Distribution
(此处由kernel_dist
参数化)。
self.X
和self.fit_X
是初始化KernelDensity
对象时创建的占位符。