python - 最大似然线性回归 tensorflow

标签 python machine-learning tensorflow

在我使用梯度下降法对一个简单的线性回归问题实现 LS 估计后,我现在尝试使用最大似然法执行相同的操作。 我使用了 wikipedia 中的这个方程。必须找到最大值。

train_X = np.random.rand(100, 1) # all values [0-1)
train_Y = train_X
X = tf.placeholder("float", None)
Y = tf.placeholder("float", None)
theta_0 = tf.Variable(np.random.randn())
theta_1 = tf.Variable(np.random.randn())
var = tf.Variable(0.5)

hypothesis = tf.add(theta_0, tf.mul(X, theta_1))
lhf = 1 * (50 * np.log(2*np.pi) + 50 * tf.log(var) + (1/(2*var)) * tf.reduce_sum(tf.pow(hypothesis - Y, 2)))
op = tf.train.GradientDescentOptimizer(0.01).minimize(lhf)

这段代码可以工作,但我仍然有一些疑问:

  • 如果我将 lhf 函数从 1 * 更改为 -1 * 并最小化 -lhf (根据等式),它会不行。但为什么呢?
  • lhf 的值在优化过程中会上下波动。难道不应该只朝一个方向改变吗?
  • 在优化过程中,lhf 的值有时是 NaN。我怎样才能避免这种情况?
  • 在方程中,σ² 是误差的方差(对吗?)。我的值(value)观完全在一条线上。为什么我得到的 var 值高于 100?

最佳答案

您问题中的症状表明了一个常见问题:学习率或步长对于该问题来说可能太高。

当学习率太高时,锯齿形行为(即要最大化的函数上下波动)很常见。特别是当你得到 NaN 时。

最简单的解决方案是降低学习率,将当前学习率除以 10,直到学习曲线平滑并且不存在 NaN 或上下行为。

当您使用 TensorFlow 时,您还可以尝试 AdamOptimizer,因为它会在您训练时动态调整学习率。

关于python - 最大似然线性回归 tensorflow ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41885665/

相关文章:

python - 简单利用 "import as"加载多场景数据?

python - 谷歌 tensorflow 速成类(class)。表示问题 :Programming exercises Task 2: Make Better Use of Latitude

python - TensorFlow 相当于 PyTorch 的 transforms.Normalize()

带有 RNNCell 的 TensorFlow attention_decoder(state_is_tuple=True)

python - python 新手的网页抓取

python - 在 matplotlib 中显示毫秒

python - 如何在 Python 中使用部分旋转实现 LU 分解?

machine-learning - 正确解释余弦角距离相似度和欧氏距离相似度

machine-learning - 使用 cross_validate() 获取预测值

python - 如何在 Tensorflow 中使用 SWA 实现 Batch Norm?