시작하기무료로 시작하기

선형 회귀 설정하기

단변량 선형 회귀는 하나의 특성과 타깃 텐서 사이의 관계를 찾습니다. 이번 연습에서는 부동산의 대지 면적과 가격을 사용해 볼게요. 영상에서 다룬 것처럼, 두 텐서의 자연로그를 취해 사용할 것이며, 이는 price_logsize_log로 제공됩니다.

이 연습에서는 모델과 손실 함수를 정의합니다. 그런 다음 서로 다른 두 개의 interceptslope 값에 대해 손실 함수를 평가해 볼 거예요. 예측 값은 intercept + features*slope로 주어진다는 점을 기억하세요. 또한 keras.losses.mse()를 사용할 수 있다는 점에 유의하세요. 아울러 slopeintercept는 변수로 정의되어 있습니다.

이 연습은 강의의 일부입니다

Python으로 시작하는 TensorFlow

강의 보기

연습 안내

  • intercept, features, slope를 사용하고 add()multiply()를 쓰지 않는 선형 회귀의 예측 값을 반환하는 함수를 정의하세요.
  • 모델의 변수인 interceptslope를 인수로 추가하여 loss_function()을 완성하세요.
  • targetspredictions를 사용해 평균제곱오차를 계산하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Define a linear regression model
def linear_regression(intercept, slope, features = size_log):
	return ____

# Set loss_function() to take the variables as arguments
def loss_function(____, ____, features = size_log, targets = price_log):
	# Set the predicted values
	predictions = linear_regression(intercept, slope, features)
    
    # Return the mean squared error loss
	return keras.losses.____

# Compute the loss for different slope and intercept values
print(loss_function(0.1, 0.1).numpy())
print(loss_function(0.1, 0.5).numpy())
코드 편집 및 실행