始める無料で始める

線形回帰をセットアップする

単回帰(univariate linear regression)は、1つの特徴量と目的のテンソルとの関係を捉えます。この演習では、物件の敷地面積と価格を使います。動画で説明したとおり、両方のテンソルの自然対数を取り、price_logsize_log として用意されています。

この演習では、モデルと損失関数を定義します。その後、interceptslope の2つの異なる値に対して損失関数を評価します。予測値は intercept + features*slope で与えられる点を思い出してください。さらに、keras.losses.mse() が利用可能です。また、slopeintercept は変数として定義済みです。

この演習はコースの一部です

Introduction to TensorFlow in Python

コースを見る

演習の手順

  • interceptfeaturesslope を使い、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())
コードを編集して実行