Theano的这一行是做什么的?

What is this line of Theano doing?

我不明白我们是否正在迭代 y,因为它看起来对 y 中的值做了什么?他们是 T.log 的一部分吗?它们是否以某种方式与 p_y_given_x 相结合?

result = -T.mean(T.log(p_y_given_x)[T.arange(y1.shape[0]), y1])
print ("result1", result.eval())


print("_________________________")
print("y ", y2)
print("y.shape[0] ", y2.shape[0])

temp =  (y2.shape[0], y2)
print("y.shape[0], y", temp)

temp2 = [T.arange(2), y2]
print("T.arange(y rows)", T.arange(2).eval())
print("[t.arange(2), y]    [[0, 1], [1, 2]]")
print("T.log(p_y_given_x) ", (T.log(p_y_given_x)).eval())
print(-T.mean(T.log(p_y_given_x)).eval())
print("#########################")


result1 1.022485096286888
_________________________
y  <TensorType(int64, matrix)>
y.shape[0]  Subtensor{int64}.0
y.shape[0], y (Subtensor{int64}.0, <TensorType(int64, matrix)>)
T.arange(y rows) [0 1]
[t.arange(2), y]    [[0, 1], [1, 2]]
T.log(p_y_given_x)  [[-1.11190143 -0.91190143 -1.31190143]
 [-1.13306876 -1.03306876 -1.13306876]]
1.10581842962
#########################

我没有足够的声誉来发表评论,所以我将 post 作为答案。

逐字引用 here

y.shape[0] is (symbolically) the number of rows in y, i.e.,
number of examples (call it n) in the minibatch
T.arange(y.shape[0]) is a symbolic vector which will contain
[0,1,2,... n-1] T.log(self.p_y_given_x) is a matrix of
Log-Probabilities (call it LP) with one row per example and
one column per class LP[T.arange(y.shape[0]),y] is a vector
v containing [LP[0,y[0]], LP[1,y[1]], LP[2,y[2]], ...,
LP[n-1,y[n-1]]] and T.mean(LP[T.arange(y.shape[0]),y]) is
the mean (across minibatch examples) of the elements in v,
i.e., the mean log-likelihood across the minibatch.

y 中的值是小批量中示例的标签。例如,让三个示例的小批量将 y(标签)向量设为 [0,6,9](考虑手写数字示例)。 所以,[LP[0,y[0]], LP[1,y[1]], LP[2,y[2]], ...,LP[n-1,y[n-1]]] 将是 LP[1,0], L[2,6], LP[3,9] 现在,我们为什么对这些数字感兴趣? 那是因为您需要这些数字来计算可能性,可能性被定义为小批量中示例的对数概率的平均值。例如 LP[1,0] 包含第一个示例属于 class 0 的对数概率。您希望这个数字尽可能高,因为这是事实。然后取平均值以找到这些数字的平均值。负号是因为损失是可能性的负数。这有帮助吗?