Python TensorFlow使目标最大化而不是最小化

Python TensorFlow使目标最大化而不是最小化,python,tensorflow,Python,Tensorflow,我正在研究加州住房数据集。我在TensorFlow中使用梯度下降优化器 tf.reset_default_graph() X = tf.placeholder(tf.float32, shape=(None,n+1)) y = tf.placeholder(tf.float32, shape=(None,1)) theta = tf.Variable(tf.random_uniform([n+1,1], -1.0, 1.0)) y_pred = tf.matmul(X, theta) error

我正在研究加州住房数据集。我在
TensorFlow
中使用梯度下降优化器

tf.reset_default_graph()
X = tf.placeholder(tf.float32, shape=(None,n+1))
y = tf.placeholder(tf.float32, shape=(None,1))
theta = tf.Variable(tf.random_uniform([n+1,1], -1.0, 1.0))
y_pred = tf.matmul(X, theta)
error = y_pred - y
mse = tf.reduce_mean(tf.square(error))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
training_op = optimizer.minimize(mse)

def fetch_batch(epoch, batch_index, batch_size):
    np.random.seed(epoch * n_batches + batch_index)
    indices = np.random.randint(m, size=batch_size)
    X_batch = housing_scaled_bias[indices]
    y_batch = housing.target.reshape(-1,1)[indices]
    return X_batch, y_batch

batch_size = 100
n_batches = int(np.ceil(m/batch_size))
init = tf.global_variables_initializer()
n_epochs = 10
learning_rate=0.01

with tf.Session() as sess:
    sess.run(init)
    for epoch in range(n_epochs):        
        for batch_index in range(n_batches):
            X_batch, y_batch = fetch_batch(epoch, batch_index, batch_size)
            sess.run(training_op, feed_dict={X: X_batch, y:y_batch} )
        print("Epoch:", epoch, "MSE:", mse.eval(feed_dict={X: X_batch, y:y_batch}))

> Epoch: 0 MSE: 0.46619293 Epoch: 1 MSE: 43.83843 Epoch: 2 MSE:
> 11674116.0 Epoch: 3 MSE: 1745939000.0 Epoch: 4 MSE: 5442349600000.0 Epoch: 5 MSE: 3.392655e+17 Epoch: 6 MSE: 7.45177e+18 Epoch: 7 MSE:
> 1.9520157e+29 Epoch: 8 MSE: inf Epoch: 9 MSE: inf

为什么这是最大化MSE?有什么想法吗

我不明白为什么它会出现在代码中,但是如果你把你的损失fn乘以-1,它会训练吗?它应该,如果你是对的,它最大化了mse当我第一次训练模型时,它最大化了mse。第二次,它最小化了。这种情况每次都会发生,你是说一半的时间损失最大,另一半的时间损失最小?或者你是说当你把损失乘以-1时,它的训练是正确的?我是说当我重新启动内核并运行代码时,它第一次就最大化了。当我在wards上第二次运行代码时,它最小化了.hmm。我不知道tensorflow。在Pytorch中,需要在每一步将梯度归零。也许退一步,仔细研究示例网络,以确保您完成了所有基本步骤。sess.run中隐藏了大量工作