from sklearn.datasets import fetch_california_housing
import numpy as np
from sklearn.preprocessing import StandardScaler
n_epochs = 1000
learning_rate = 0.01
housing = fetch_california_housing()
# m is length of dataset, n is # of atts
m, n = housing.data.shape
# Scale Data with StandardScalar()
scaler = StandardScaler()
scaled_housing_data = scaler.fit_transform(housing.data)
# m is length of dataset, n is # of atts
m, n = housing.data.shape
batch_size = 100
num_batches = int(np.ceil(m/batch_size))
# Just appends a a column of '1.'s to beginnging of table
scaled_housing_data_plus_bias = np.c_[np.ones((m, 1)), scaled_housing_data]
X = tf.placeholder(tf.float32, shape=(None, n+1), name="X")
y = tf.placeholder(tf.float32, shape=(None, 1), name="y")
theta = tf.Variable(tf.random_uniform([n+1, 1], -1, 1), name="theta")
# Formulas for preducing predictions and error metrics
y_pred = tf.matmul(X, theta)
error = y_pred - y
mse = tf.reduce_mean(tf.square(error), name="mse")
# gradients = 2/m * tf.matmul(tf.transpose(X), error)
gradients = tf.gradients(mse, [theta])[0] # Calculate gradients with built in function for efficiency
# Operation that adjusts theta values based on gradients & learning rate
# training_op = theta.assign(theta - learning_rate * gradients)
# optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=0.9)
training_op = optimizer.minimize(mse)
def fetch_batch(epoch, batch_index, batch_size):
np.random.seed(epoch * num_batches + batch_index)
indices = np.random.randint(m, size=batch_size)
X_batch = scaled_housing_data_plus_bias[indices]
y_batch = housing.target.reshape(-1, 1)[indices]
return X_batch, y_batch
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for epoch in range(n_epochs):
print("Epoch: " + str(epoch))
for batch_index in range(num_batches):
X_batch, y_batch = fetch_batch(epoch, batch_index, batch_size)
print(X_batch.shape)
print(y_batch.shape)
sess.run(training_op, feed_dict={X: X_batch, y: y_batch})
if epoch % 100 == 0:
print("Mean Squared Error: " + str(mse.eval()))
print(theta.eval())
输出:纪元:0,(100,9),(100,1)
Error Message:
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'y' with dtype float and shape [?,1]
[[Node: y = Placeholder[dtype=DT_FLOAT, shape=[?,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
(我也得到类似'X'的东西) 提前感谢任何建议或帮助!!!
答案 0 :(得分:0)
我明白了。尝试评估'mse'会导致占位符值出错。
# Correct Code
with tf.Session() as sess:
sess.run(init)
best_theta = None
best_mse = None
for epoch in range(n_epochs):
for batch_index in range(num_batches):
X_batch, y_batch = fetch_batch(epoch, batch_index, batch_size)
_, best_mse = sess.run([training_op,mse], feed_dict={X: X_batch, y: y_batch})
if epoch % 100 == 0:
print("Epoch: " + str(epoch))
print("MSE " + str(best_mse))
print(theta.eval())