python - 使用完整数据集进行梯度下降时 TensorFlow 权重增加

标签 python numpy tensorflow

我写了一篇文章,从头开始深入解释神经网络的工作原理。

为了说明博文,我在 python using numpy 中编写了神经网络我用 TensorFlow 写了一个版本.我在 Github 上上传了代码来说明这个问题,但这不是一个干净的版本。

网络的目标是根据汽车的三个特征(公里数、燃料类型、车龄)预测汽车价格,这是我从头开始创建的玩具示例。

我从 leboncoin.fr 中检索了数据,我的数据集由大约 9k 辆汽车组成(仅 BMW serie 1)。我对数据进行了归一化处理,使价格在 [0, 1] 之间,燃料类型采用二进制编码,使用均值和标准差对使用年限和公里数进行归一化处理。

神经网络架构非常简单,我只使用了三个汽车属性,但我的非 tensorflow 网络的结果非常好。验证测试集给出:

### Testing summary ###
Iteration: 2000, Loss 0.001066
RMSE: 0.0567967802161
MAE: 0.00757498877216
R2: 0.198448957215

我在梯度下降优化期间使用了整个数据集。我的问题出现在 TensorFlow 版本中,如果我在梯度下降过程中只使用 20 个输入,损失会正确减少:

I tensorflow/core/kernels/logging_ops.cc:79] loss[0.6057564]
I tensorflow/core/kernels/logging_ops.cc:79] loss[0.45724705]
I tensorflow/core/kernels/logging_ops.cc:79] loss[0.35986084]
I tensorflow/core/kernels/logging_ops.cc:79] loss[0.29016402]
I tensorflow/core/kernels/logging_ops.cc:79] loss[0.23823617]
I tensorflow/core/kernels/logging_ops.cc:79] loss[0.1986042]
I tensorflow/core/kernels/logging_ops.cc:79] loss[0.16779649]
I tensorflow/core/kernels/logging_ops.cc:79] loss[0.14347225]
I tensorflow/core/kernels/logging_ops.cc:79] loss[0.12400422]
I tensorflow/core/kernels/logging_ops.cc:79] loss[0.10823684]

但如果我使用整个数据集,即 9k 个示例,我的损失会显示出不稳定的行为。

I tensorflow/core/kernels/logging_ops.cc:79] loss[226.40295]
I tensorflow/core/kernels/logging_ops.cc:79] loss[6130.1694]
I tensorflow/core/kernels/logging_ops.cc:79] loss[8629.668]
I tensorflow/core/kernels/logging_ops.cc:79] loss[9219.1445]
I tensorflow/core/kernels/logging_ops.cc:79] loss[9217.1855]
I tensorflow/core/kernels/logging_ops.cc:79] loss[9211.8428]
I tensorflow/core/kernels/logging_ops.cc:79] loss[9209.2715]
I tensorflow/core/kernels/logging_ops.cc:79] loss[9212.22]
I tensorflow/core/kernels/logging_ops.cc:79] loss[9204.3613]
I tensorflow/core/kernels/logging_ops.cc:79] loss[9182.3125]
I tensorflow/core/kernels/logging_ops.cc:79] loss[9171.9746]
I tensorflow/core/kernels/logging_ops.cc:79] loss[9200.2207]

我不明白为什么。

目前我的 tensorflow 版本如下:

import csv
import numpy as np
import tensorflow as tf

reader = csv.reader(open("normalized_car_features.csv", "rb"), delimiter=",")
x = list(reader)
features = np.array(x[1:]).astype("float")
np.random.shuffle(features)

data_x = features[:, :3]
data_y = features[:, 3:]

m = float(features.shape[0])
threshold = int(m * 0.8)

x_data, x_test = data_x[:threshold, :], data_x[threshold:, :]
y_data, y_test = data_y[:threshold, :], data_y[threshold:, :]

x = tf.placeholder("float")
y = tf.placeholder("float")

w1 = np.matrix([
    [0.01, 0.05, 0.07],
    [0.2, 0.041, 0.11],
    [0.04, 0.56, 0.13]
])

w2 = np.matrix([
    [0.04, 0.78],
    [0.4, 0.45],
    [0.65, 0.23]
])

w3 = np.matrix([
    [0.04],
    [0.41]
])

w1 = tf.Variable(w1, dtype=tf.float32)
w2 = tf.Variable(w2, dtype=tf.float32)
w3 = tf.Variable(w3, dtype=tf.float32)

b1 = tf.Variable(np.matrix([0.1, 0.1, 0.1]), dtype=tf.float32)
b2 = tf.Variable(np.matrix([0.1, 0.1]), dtype=tf.float32)
b3 = tf.Variable(np.matrix([0.1]), dtype=tf.float32)

layer_1 = tf.nn.tanh(tf.add(tf.matmul(x, w1), b1))
layer_2 = tf.nn.tanh(tf.add(tf.matmul(layer_1, w2), b2))
layer_3 = tf.nn.tanh(tf.add(tf.matmul(layer_2, w3),  b3))

loss = tf.reduce_sum(tf.square(layer_3 - y))
loss = tf.Print(loss, [loss], "loss")

train_op = tf.train.GradientDescentOptimizer(1/m * 0.01).minimize(loss)

init = tf.global_variables_initializer()

with tf.Session() as session:
    session.run(init)
    for i in range(10000):
        session.run(train_op, feed_dict={x: x_data, y: y_data})

预测值为[-1, -1, ..., -1, -1]

更新:使用 tf.train.GradientDescentOptimizer(1/m * 0.01) 它按预期工作。

最佳答案

问题不在于优化器,而是你的损失。它应该返回平均损失,而不是总和。例如,如果您正在进行 L2 回归,它应该如下所示:

l_value = tf.pow(tf.abs(ground_truth - predict), 2) # distance for each individual position of the output matrix of shape = (n_examples, example_data_size)
regression_loss = tf.reduce_sum(l_value, axis=1) # distance per example, shape = (n_examples, 1)
total_regression_loss = tf.reduce_mean(regression_loss) # mean distance of all examples, shape = (1)

PS:tf.abs 是为了方便而使用的,因此您可以将 L2 损失替换为另一个损失(如 L1),而不必担心符号变化,这会在复平面上产生结果.

关于python - 使用完整数据集进行梯度下降时 TensorFlow 权重增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43027109/

相关文章:

python - 如何在 python 中替换'&' to ' &'?

python - 如何关联 Pandas 中的序数分类列?

python - 如何从 MySQL 日期时间转换为 numpy datetime64?

python - numpy.sum 的内部结构

python - 卡住图到 Tflite 转换错误 -> ValueError - 为输入数组 'wav data' 提供输入形状

python - Tensorflow 中的 Seq2Seq,但我收到 ValueError : Input 0 of layer gru_cell_3 is incompatible with the layer:

python - 从 SVN pip 安装 [错误 2]

python - Selenium 通过 xpath 查找所有元素

python - Tensorflowcompute_gradients 和 apply_gradients 内存不足

python - 获取 3D 空间中某个点的 26 个最近邻点 - 矢量化