简体   繁体   English

不知道为什么我的损失值在各个时期都在增加(张量流中的linreg)

[英]Not sure why my loss values are increasing across epochs (linreg in tensorflow)

I know, TF is overkill for this sort of problem but this is just my way of introducing myself to the syntax and TFs training process. 我知道,对于这种问题,TF是矫kill过正,但这只是我向语法和TFs培训过程自我介绍的方式。

Here is the code: 这是代码:

data = pd.read_excel("/Users/madhavthaker/Downloads/Reduced_Car_Data.xlsx")

train = np.random.rand(len(data)) < 0.8

data_train = data[train]
data_test = data[~train]


x_train = data_train.ix[:,0:3].values
y_train = data_train.ix[:,-1].values
x_test = data_test.ix[:,0:3].values
y_test = data_test.ix[:,-1].values

# Build inference graph.
# Create Variables W and b that compute y_data = W * x_data + b
W = tf.Variable(tf.random_normal([3,1]), name='weights')
b = tf.Variable(tf.random_normal([1]), name='bias')

# Uncomment the following lines to see what W and b are.
# print(W)
# print(b)

# Create a placeholder we'll use later to feed x's into the graph for training and eval.
# shape=[None] means we can put in any number of examples. 
# This is used for minibatch training, and to evaluate a lot of examples at once.
x = tf.placeholder(tf.float32,shape=[x_train.shape[0],3], name='x')

# Uncomment this line to see what x is
# print(x)

# This is the same as tf.add(tf.mul(W, x), b), but looks nicer
y = tf.matmul(x,W) + b

# Create a placeholder we'll use later to feed the correct y value into the graph
y_label = tf.placeholder(shape=[y_train.shape[0],], dtype=tf.float32, name='y_label')
# print (y_label)

# Build training graph.
loss = tf.reduce_mean(tf.square(y - y_label))  # Create an operation that calculates loss.
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.00001)  # Create an optimizer.
train = optimizer.minimize(loss)  # Create an operation that minimizes loss.

# Uncomment the following 3 lines to see what 'loss', 'optimizer' and 'train' are.
# print("loss:", loss)
# print("optimizer:", optimizer)
# print("train:", train)
init = tf.global_variables_initializer()

# Launch the graph
with tf.Session() as sess:
    sess.run(init)

    # Fit all training data
    for epoch in range(1000):

        # Display logs per epoch step
        if (epoch+1) % 50 == 0:
            cost_val, hy_val, _ = sess.run([loss, y, train], feed_dict={x: x_train, y_label: y_train})
            print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(cost_val))

    print("Optimization Finished!")
    training_cost = sess.run(loss, feed_dict={x: x_train, y_label: y_train})
    print("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')

With a result of: 结果:

Epoch: 0050 cost= 12377621.000000000
Epoch: 0100 cost= 455768801280.000000000
Epoch: 0150 cost= 16799577747226624.000000000
Epoch: 0200 cost= 619229115796003225600.000000000
Epoch: 0250 cost= 22824834360245537040498688.000000000
Epoch: 0300 cost= 841322078804629437979012628480.000000000
Epoch: 0350 cost= 31011140748122347114388001285734400.000000000
Epoch: 0400 cost= inf
Epoch: 0450 cost= inf
Epoch: 0500 cost= inf
Epoch: 0550 cost= inf
Epoch: 0600 cost= inf
Epoch: 0650 cost= inf
Epoch: 0700 cost= inf
Epoch: 0750 cost= inf
Epoch: 0800 cost= inf
Epoch: 0850 cost= nan
Epoch: 0900 cost= nan
Epoch: 0950 cost= nan
Epoch: 1000 cost= nan
Optimization Finished!
Training cost= nan W= [[ nan]
 [ nan]
 [ nan]] b= [ nan] 

I've been staring at this for a while and I can't seem to figure out what is going on. 我凝视了一段时间,似乎无法弄清楚发生了什么。 Any help would be much appreciated. 任何帮助将非常感激。

I think it is due to the shape of your cost function. 我认为这是由于您的成本函数的形状。 Actually it can happen that the cost increases, see the answer there for a mathematical explanation: https://datascience.stackexchange.com/questions/15962/why-is-learning-rate-causing-my-neural-networks-weights-to-skyrocket 实际上,成本可能会增加,请参见答案以获取数学解释: https : //datascience.stackexchange.com/questions/15962/why-is-learning-rate-causing-my-neural-networks-weights-到扶摇直上

Maybe try to decrease the learning rate to see if it help. 也许尝试降低学习率以查看是否有帮助。

PS: is it normal that you don't call "sess.run(...)" at every epoch ? PS:在每个时期不调用“ sess.run(...)”是否正常?

I think the model is too small to approximate the desired mapping. 我认为模型太小,无法近似所需的映射。 I have run your code on random data as it is, the loss didn't improve. 我已经在随机数据上运行了您的代码,损失没有改善。 It did improve only when I added one more layer to the model. 仅当我在模型中再增加一层时,它的确改善了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的损失函数随着每个 epoch 增加? - Why is my loss function increasing with each epoch? 为什么我的张量流模型输出在x epochs之后变为NaN? - Why my tensorflow model outputs become NaN after x epochs? 验证损失在3个时期后增加,但验证准确性不断提高 - Validation loss increases after 3 epochs but validation accuracy keeps increasing Keras:骰子系数损失函数为负且随时间增加 - Keras: Dice coefficient loss function is negative and increasing with epochs 为什么我的 val_loss 波动并具有巨大的价值,而 val_categorical_accuracy 在所有时期都或多或少是恒定的? - Why do my val_loss fluctuate and have enormous values while val_categorical_accuracy are more or less constant throughout all epochs? 为什么在Tensorflow Nan中丢失我的简单NN? - Why is the loss of my simple NN in Tensorflow nan? 为什么我的损失计算正确地作为指标而不是 TensorFlow 中的损失? - Why is my loss computing correctly as a metric but not as a loss in TensorFlow? 为什么我的 Tensorflow Keras model output 在训练时会出现奇怪的损失和准确度值? - Why does my Tensorflow Keras model output weird loss and accuracy values while training? tensorflow Triplet_semihard_loss 在多个时期后不会改变 - tensorflow triplet_semihard_loss doesnt change after multiple epochs TensorFlow多个损失值 - TensorFlow multiple values for loss
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM