MINI-BATCH 完整代码

  1. import numpy as np
  2. from sklearn.datasets import fetch_california_housing
  3. import tensorflow as tf
  4. from sklearn.preprocessing import StandardScaler
  5. housing = fetch_california_housing()
  6. m, n = housing.data.shape
  7. print("数据集:{}行,{}列".format(m,n))
  8. housing_data_plus_bias = np.c_[np.ones((m, 1)), housing.data]
  9. scaler = StandardScaler()
  10. scaled_housing_data = scaler.fit_transform(housing.data)
  11. scaled_housing_data_plus_bias = np.c_[np.ones((m, 1)), scaled_housing_data]
  12. n_epochs = 1000
  13. learning_rate = 0.01
  14. X = tf.placeholder(tf.float32, shape=(None, n + 1), name="X")
  15. y = tf.placeholder(tf.float32, shape=(None, 1), name="y")
  16. theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0, seed=42), name="theta")
  17. y_pred = tf.matmul(X, theta, name="predictions")
  18. error = y_pred - y
  19. mse = tf.reduce_mean(tf.square(error), name="mse")
  20. optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
  21. training_op = optimizer.minimize(mse)
  22. init = tf.global_variables_initializer()
  23. n_epochs = 10
  24. batch_size = 100
  25. n_batches = int(np.ceil(m / batch_size)) # ceil() 方法返回 x 的值上限 - 不小于 x 的最小整数。
  26. def fetch_batch(epoch, batch_index, batch_size):
  27. know = np.random.seed(epoch * n_batches + batch_index) # not shown in the book
  28. print("我是know:",know)
  29. indices = np.random.randint(m, size=batch_size) # not shown
  30. X_batch = scaled_housing_data_plus_bias[indices] # not shown
  31. y_batch = housing.target.reshape(-1, 1)[indices] # not shown
  32. return X_batch, y_batch
  33. with tf.Session() as sess:
  34. sess.run(init)
  35. for epoch in range(n_epochs):
  36. for batch_index in range(n_batches):
  37. X_batch, y_batch = fetch_batch(epoch, batch_index, batch_size)
  38. sess.run(training_op, feed_dict={X: X_batch, y: y_batch})
  39. best_theta = theta.eval()
  40. print(best_theta)