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

时间:2018-09-02 00:42:36

标签: python tensorflow

我正在研究加利福尼亚州的住房数据集。我在TensorFlow中使用了Gradient Descent优化器。

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?有什么想法吗?

0 个答案:

没有答案