个人理解--batch_dot函数

本文详细解析了Keras库中的batch_dot函数,该函数用于计算批次数据的内积,特别适用于处理多维矩阵。文章通过实例展示了如何使用该函数,并解释了其内部运作原理。

在keras中有batch_dot函数,用于计算两个多维矩阵,官方注释如下:

def batch_dot(x, y, axes=None):
    """Batchwise dot product.

    `batch_dot` is used to compute dot product of `x` and `y` when
    `x` and `y` are data in batches, i.e. in a shape of
    `(batch_size, :)`.
    `batch_dot` results in a tensor or variable with less dimensions
    than the input. If the number of dimensions is reduced to 1,
    we use `expand_dims` to make sure that ndim is at least 2.

    这个函数是用于计算批次数据‘x’和‘y'的内积,两个数据的batch_size必须相同。
    函数的输出张量的维度数量会少于输入的维度数量和。如果输出的维度数量减少到1,就会使用
    ’expand_dim‘函数来确保维度数量至少为2。

    # Arguments
        x: Keras tensor or variable with `ndim >= 2`.  维度数量 >= 2
        y: Keras tensor or variable with `ndim >= 2`.  维度数量 >= 2
        axes: int or tuple(int, int). Target dimensions to be reduced.  
        要减少的目标维度。理论上从0开始(即shape首位),但batch_size是忽略的,故从1开始。若是一
        个整数,则表示两个输入的shape的同一位。若是一个tuple或list,则分别指向不同位置。
        注意:无论axes是那种类型,指向的两个位置上的数值必须一致。

    # Returns
        A tensor with shape equal to the concatenation of `x`'s shape
        (less the dimension that was summed over) and `y`'s shape
        (less the batch dimension and the dimension that was summed over).
        If the final rank is 1, we reshape it to `(batch_size, 1)`.
        
    """

下面对例子进行说明。

 >>> x_batch = K.ones(shape=(32, 20, 1))
 >>> y_batch = K.ones(shape=(32, 30, 20))
 >>> xy_batch_dot = K.batch_dot(x_batch, y_batch, axes=(1, 2))
 >>> K.int_shape(xy_batch_dot)
 (32, 1, 30)

首先我认为,该函数进行还是普通的矩阵乘法,但是两个输入矩阵的格式明显不符合,所以进行了类似reshape的操作,具体就是将左边的矩阵的目标位移动到末尾,将右边矩阵的目标位移动到首位。如上例,去掉batch_size,原规格为(20,1)和(30,20),因为axes=(1,2),故变为(1,20)和(20,30)的矩阵乘法,结果为(1,30),加上batch_size即为(32,1,30)。

在 `class LinearRegression(object)` 代码的 `__init__` 方法里将学习率设置为 `1e - 2`(即 0.01),可能有以下几个原因: ### 平衡收敛速度与稳定性 学习率决定了在梯度下降过程中参数更新的步长。如果学习率设置得过大,参数更新的步长会很长,可能会导致算法跳过最优解,使得损失函数无法收敛,甚至出现损失函数值不断增大的情况。相反,如果学习率设置得过小,参数更新的步长会非常短,算法收敛速度会变得很慢,需要更多的迭代次数才能达到最优解。`1e - 2` 是一个相对适中的学习率,它在保证收敛稳定性的同时,能让算法以合理的速度向最优解逼近。 ### 经验选择 在许多机器学习实践中,`1e - 2` 是一个常用的初始学习率。经过大量的实验和实践验证,这个值在很多线性回归问题中都能取得较好的效果。当面对一个新的问题时,先尝试使用这个经验值是一种常见的做法,后续可以根据具体的实验结果再进行调整。 ### 代码示例 以下是一个简单的 `LinearRegression` 类的示例,其中学习率设置为 `1e - 2`: ```python import numpy as np class LinearRegression(object): def __init__(self, learning_rate=1e-2, num_iterations=1000): self.learning_rate = learning_rate self.num_iterations = num_iterations self.weights = None self.bias = None def fit(self, X, y): num_samples, num_features = X.shape self.weights = np.zeros(num_features) self.bias = 0 for _ in range(self.num_iterations): y_pred = np.dot(X, self.weights) + self.bias dw = (1 / num_samples) * np.dot(X.T, (y_pred - y)) db = (1 / num_samples) * np.sum(y_pred - y) self.weights -= self.learning_rate * dw self.bias -= self.learning_rate * db def predict(self, X): return np.dot(X, self.weights) + self.bias ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值