Week 1 Assignment: Housing Prices
In this exercise you'll try to build a neural network that predicts the price of a house according to a simple formula.
Imagine that house pricing is as easy as:
A house has a base cost of 50k, and every additional bedroom adds a cost of 50k. This will make a 1 bedroom house cost 100k, a 2 bedroom house cost 150k etc.
How would you create a neural network that learns this relationship so that it would predict a 7 bedroom house as costing close to 400k etc.
Hint: Your network might work better if you scale the house price down. You don't have to give the answer 400...it might be better to create something that predicts the number 4, and then your answer is in the 'hundreds of thousands' etc.
import tensorflow as tf
import numpy as np
# GRADED FUNCTION: house_model
def house_model():
### START CODE HERE
# Define input and output tensors with the values for houses with 1 up to 6 bedrooms
# Hint: Remember to explictly set the dtype as float
xs = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], dtype=float)
ys = np.array([1.0, 1.5, 2.0, 2.5, 3.0, 3.5], dtype=float)
# Define your model (should be a model with 1 dense layer and 1 unit)
model = tf.keras.Sequential([tf.keras.layers.Dense(units=1, input_shape=[1])])
# Compile your model
# Set the optimizer to Stochastic Gradient Descent
# and use Mean Squared Error as the loss function
model.compile(optimizer='sgd', loss='mean_squared_error')
# Train your model for 1000 epochs by feeding the i/o tensors
model.fit(xs, ys, epochs=5)
### END CODE HERE
return model
Now that you have a function that returns a compiled and trained model when invoked, use it to get the model to predict the price of houses:
new_y = 7.0
prediction = model.predict([new_y])[0]
print(prediction)
# 1/1 [==============================] - 0s 71ms/step
# [3.218548]
If everything went as expected you should see a prediction value very close to 4. If not, try adjusting your code before submitting the assignment. Notice that you can play around with the value of new_y to get different predictions. In general you should see that the network was able to learn the linear relationship between x and y, so if you use a value of 8.0 you should get a prediction close to 4.5 and so on.
Congratulations on finishing this week's assignment!
You have successfully coded a neural network that learned the linear relationship between two variables. Nice job!
Keep it up!
'Programing 프로그래밍 > Python 파이썬' 카테고리의 다른 글
[DeepLearning.AI TensorFlow Developer] C1W3-Assignment: Improve MNIST with Convolutions (0) | 2023.07.24 |
---|---|
[DeepLearning.AI TensorFlow Developer] C1W2-Assignment: Implementing Callbacks in TensorFlow using the MNIST Dataset (0) | 2023.07.24 |
[나도코딩] Python 코딩 무료 강의 (기본편) 정리 (3/3) (0) | 2022.01.17 |
[나도코딩] Python 코딩 무료 강의 (기본편) 정리 (2/3) (0) | 2022.01.14 |
[나도코딩] Python 코딩 무료 강의 (기본편) 정리 (1/3) (0) | 2022.01.13 |