본문 바로가기
AI & 머신러닝 coding skill

[INTRODUCTION TO TENSORFLOW FOR ARTIFICIAL INTELLIGENCE, MACHINE LEARNING, AND DEEP LEARNING] Housing Prices

by 호빵님 2020. 11. 16.
반응형

코세라의   deeplearning.AI tensorflow developer 전문가 자격증 과정내에

Introduction to TensorFlow for Artificial Intelligence, Machine Learning, and Deep Learning

과정의 1주차 A New Programming Paradigm 챕터의 코드 예제입니다.

Your First Neural Network

1) ys에 따른 xs 데이터를 준비

2) Dense unit 1개의 뉴럴 네트워크 모델 생성

3) medel.predict로 새로운 y 7.0 에 대응하는 x를 예측한다.

#!/usr/bin/env python
# coding: utf-8

# In this exercise you'll try to build a neural network that predicts the price of a house according to a simple formula.
# 
# So, imagine if house pricing was as easy as a house costs 50k + 50k per bedroom, so that a 1 bedroom house costs 100k, a 2 bedroom house costs 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.

# In[1]:


import tensorflow as tf
import numpy as np
from tensorflow import keras


# In[2]:


# GRADED FUNCTION: house_model
def house_model(y_new):
    ys = np.array([1.0,  1.5, 2.0, 2.5, 3.0, 3.5], dtype=float)
    xs = np.array([1.0,  2.0, 3.0, 4.0, 5.0, 6.0], dtype=float)
    model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
    model.compile(optimizer='sgd', loss='mean_squared_error')
    model.fit(xs, ys, epochs=500)
    return model.predict(y_new)[0]


# In[3]:


prediction = house_model([7.0])
print(prediction)




반응형