Reputation: 3
I want to duplicate the house price prediction model in Andrew Ng's course, but after the training result is generated, the loss is always zero and the weight variables are zero too. Can anyone please assist with this?
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]))
The result is
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
Upvotes: 0
Views: 67
Reputation: 237
The result for your loss_op variable is most likely automatically rounded. Try replacing the numerator with an explicit float, like this
loss_op = 1.0 / (2 * len(X_data)) * tf.matmul((y_pred - y), (y_pred - y), transpose_a=True)
This way you explicitly coerce the output to type float
Upvotes: 2