当使用完整数据集进行梯度下降时,TensorFlow权重会增加

时间:2017-03-26 09:30:28

标签: python numpy tensorflow

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

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

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

我从leboncoin.fr检索数据,我的数据集由大约9k辆汽车组成(仅限宝马系列1)。我将数据标准化,使价格介于[0,1]之间,燃料类型采用二进制编码,年龄和公里数使用均值和标准差进行标准化。

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

### 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]

我不明白为什么。

我的张量流版本现​​在是以下内容:

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)按预期工作。

1 个答案:

答案 0 :(得分:3)

问题不在于优化器,而是你的损失。它应该返回平均损失,而不是总和。例如,如果您正在进行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),而不必担心符号更改,这会在复杂平面中产生结果。