Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 训练中的损失总是零_Python_Tensorflow_Machine Learning - Fatal编程技术网

Python 训练中的损失总是零

Python 训练中的损失总是零,python,tensorflow,machine-learning,Python,Tensorflow,Machine Learning,我想复制Andrew Ng课程中的房价预测模型,但在生成训练结果后,损失始终为零,权重变量也为零。有人能帮忙吗 import pandas as pd import numpy as np def normalize_feature(df): return df.apply(lambda column: (column - column.mean()) / column.std()) df = normalize_feature(pd.read_csv('data1.csv',

我想复制Andrew Ng课程中的房价预测模型,但在生成训练结果后,损失始终为零,权重变量也为零。有人能帮忙吗

import pandas as pd
import numpy as np

def normalize_feature(df):
    return df.apply(lambda column: (column - column.mean()) / column.std())


df = normalize_feature(pd.read_csv('data1.csv',
                                   names=['square', 'bedrooms', 'price']))

ones = pd.DataFrame({'ones': np.ones(len(df))})
df = pd.concat([ones, df], axis=1)
df.head()X_data = np.array(df[df.columns[0:3]])
y_data = np.array(df[df.columns[-1]]).reshape(len(df), 1)

print(X_data.shape, type(X_data))
print(y_data.shape, type(y_data))import tensorflow as tf

alpha = 0.1 
epoch = 500 

X = tf.placeholder(tf.float32, X_data.shape)
y = tf.placeholder(tf.float32, y_data.shape)

W = tf.get_variable("weights", (X_data.shape[1], 1), initializer=tf.constant_initializer())
y_pred = tf.matmul(X, W)


loss_op = 1 / (2 * len(X_data)) * tf.matmul((y_pred - y), (y_pred - y), transpose_a=True)
opt = tf.train.GradientDescentOptimizer(learning_rate=alpha)
train_op = opt.minimize(loss_op)with tf.Session() as sess:

    sess.run(tf.global_variables_initializer())

    for e in range(1, epoch + 1):
        sess.run(train_op, feed_dict={X:X_data, y:y_data})
        if e % 10 == 0:
            loss, w = sess.run([loss_op, W], feed_dict={X: X_data, y: y_data})
            log_str = "Epoch %d \t Loss=%.4g \t Model: y = %.4gx1 + %.4gx2 + %.4g"
            print(log_str % (e, loss, w[1], w[2], w[0]))
结果是

Epoch 10     Loss=0      Model: y = 0x1 + 0x2 + 0
Epoch 20     Loss=0      Model: y = 0x1 + 0x2 + 0
Epoch 30     Loss=0      Model: y = 0x1 + 0x2 + 0
Epoch 40     Loss=0      Model: y = 0x1 + 0x2 + 0
Epoch 50     Loss=0      Model: y = 0x1 + 0x2 + 0
...
Epoch 500    Loss=0      Model: y = 0x1 + 0x2 + 0

loss_op变量的结果很可能是自动四舍五入的。尝试用显式浮点替换分子,如下所示

loss_op = 1.0 / (2 * len(X_data)) * tf.matmul((y_pred - y), (y_pred - y), transpose_a=True)

通过这种方式,您可以显式强制输出为float类型,loss_op变量的结果很可能会自动四舍五入。尝试用显式浮点替换分子,如下所示

loss_op = 1.0 / (2 * len(X_data)) * tf.matmul((y_pred - y), (y_pred - y), transpose_a=True)

通过这种方式,您可以显式强制输出为键入float

好听,然后您可以将答案作为问题的解决方案吗?谢谢接受,而不是except@Joeswooddd欢迎来到SO;既然答案解决了您的问题,请接受它-很高兴听到,那么您能将答案作为您问题的解决方案吗?谢谢接受,而不是except@Joeswooddd欢迎来到SO;既然答案解决了您的问题,请接受它-参见