Keras 輸入層與全連接層

使用 Keras 的進階深度學習

Zach Deane-Mayer

Data Scientist

課程大綱

  • 第 1 章:Keras 函式式 API 入門(複習)
  • 第 2 章:含 2 個輸入的模型
  • 第 3 章:含 3 個輸入的模型
  • 第 4 章:多重輸出
使用 Keras 的進階深度學習

課程資料集:大學籃球,1989-2017

資料集 1:例行賽

  • 球隊 ID 1
  • 球隊 ID 2
  • 主場 vs 客場
  • 分差(球隊 1 − 球隊 2)
  • 球隊 1 得分
  • 球隊 2 得分
  • 勝負

資料集 2:錦標賽

  • 與資料集 1 相同
  • 另含種子差
使用 Keras 的進階深度學習

課程資料集:大學籃球,1989-2017

import pandas as pd
games_season = pd.read_csv('datasets/games_season.csv')
games_season.head()

Out[1]:
   season  team_1  team_2  home  score_diff  score_1  score_2  won
0    1985    3745    6664     0          17       81       64    1
1    1985     126    7493     1           7       77       70    1
2    1985     288    3593     1           7       63       56    1
3    1985    1846    9881     1          16       70       54    1
4    1985    2675   10298     1          12       86       74    1

games_tourney = pd.read_csv('datasets/games_tourney.csv')
games_tourney.head()

Out[2]:
   season  team_1  team_2  home  seed_diff  score_diff  score_1  score_2  won
0    1985     288      73     0          -3          -9       41       50    0
1    1985    5929      73     0          4           6       61       55    1
2    1985    9884      73     0          5          -4       59       63    0
3    1985      73     288     0          3           9       50       41    1
4    1985    3920     410     0          1          -9       54       63    0
使用 Keras 的進階深度學習

輸入與輸出

兩個基本部分:

  • 輸入層
  • 輸出層
使用 Keras 的進階深度學習

輸入層

from tensorflow.keras.layers import Input
input_tensor = Input(shape=(1,))

使用 Keras 的進階深度學習

輸入層

from tensorflow.keras.layers import Input
input_tensor = Input(shape=(1,))
print(input_tensor)

KerasTensor(type_spec=TensorSpec(shape=(None, 1), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'")

使用 Keras 的進階深度學習

輸出層

from tensorflow.keras.layers import Dense
output_layer = Dense(1)

使用 Keras 的進階深度學習

輸出層

from tensorflow.keras.layers import Dense
output_layer = Dense(1)
print(output_layer)

<keras.layers.core.dense.Dense object at 0x15df15e80>

使用 Keras 的進階深度學習

連結輸入到輸出

from tensorflow.keras.layers import Input, Dense
input_tensor = Input(shape=(1,))
output_layer = Dense(1)
output_tensor = output_layer(input_tensor)

使用 Keras 的進階深度學習

連結輸入到輸出

print(output_tensor)

KerasTensor(type_spec=TensorSpec(shape=(None, 1), dtype=tf.float32, name=None), name='dense_1/BiasAdd:0', description="created by layer 'dense_1'")

使用 Keras 的進階深度學習

一起來練習吧!

使用 Keras 的進階深度學習

Preparing Video For Download...