バッチで線形モデルを学習する
この演習では、前の演習の続きとして、バッチ処理で線形回帰モデルを学習します。データセットをバッチごとに順に処理し、各ステップの後でモデルの変数 intercept と slope を更新していきます。これにより、メモリに載せきれないような大きなデータセットでも学習できるようになります。
損失関数 loss_function(intercept, slope, targets, features) は用意されています。また、keras はインポート済みで、numpy は np として利用できます。学習可能な変数は、損失関数の引数の並び順に合わせて var_list に入力してください。
この演習はコースの一部です
Introduction to TensorFlow in Python
演習の手順
.Adam()オプティマイザを使用します。'kc_house_data.csv'をchunksizeを 100 にしてバッチで読み込みます。batchからprice列を取り出し、32 ビット浮動小数点のnumpy配列に変換して、price_batchに代入します。- 損失関数を完成させ、学習可能な変数のリストを埋めて、最小化を実行します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Initialize Adam optimizer
opt = keras.optimizers.____
# Load data in batches
for batch in pd.read_csv('____', ____=____):
size_batch = np.array(batch['sqft_lot'], np.float32)
# Extract the price values for the current batch
price_batch = np.array(batch['____'], np.____)
# Complete the loss, fill in the variable list, and minimize
opt.minimize(lambda: loss_function(____, slope, price_batch, size_batch), var_list=[intercept, ____])
# Print trained parameters
print(intercept.numpy(), slope.numpy())