Kerasでニューラルネットワークを定義する

Introduction to TensorFlow in Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

手話文字の分類

この画像は手話の文字Aを示す。

この画像は手話の文字Bを示す。

この画像は手話の文字Cを示す。

この画像は手話の文字Dを示す。

Introduction to TensorFlow in Python

Sequential API

この画像は、最初の中間層に16ノード、2番目に8、出力層に4ノードのニューラルネットワークを示す。

Introduction to TensorFlow in Python

Sequential API

  • 入力層
  • 中間層
  • 出力層
  • 逐次に配置
Introduction to TensorFlow in Python

Sequentialモデルの構築

# Import tensorflow
from tensorflow import keras

# Define a sequential model
model = keras.Sequential()
# Define first hidden layer
model.add(keras.layers.Dense(16, activation='relu', input_shape=(28*28,)))
Introduction to TensorFlow in Python

Sequentialモデルの構築

# Define second hidden layer
model.add(keras.layers.Dense(8, activation='relu'))
# Define output layer
model.add(keras.layers.Dense(4, activation='softmax'))
# Compile the model
model.compile('adam', loss='categorical_crossentropy')
# Summarize the model
print(model.summary())
Introduction to TensorFlow in Python

Functional API

図は2つの入力をもつニューラルネットワークを示す。

Introduction to TensorFlow in Python

Functional APIの使用

# Import tensorflow
import tensorflow as tf

# Define model 1 input layer shape
model1_inputs = tf.keras.Input(shape=(28*28,))

# Define model 2 input layer shape
model2_inputs = tf.keras.Input(shape=(10,))
# Define layer 1 for model 1
model1_layer1 = tf.keras.layers.Dense(12, activation='relu')(model1_inputs)

# Define layer 2 for model 1
model1_layer2 = tf.keras.layers.Dense(4, activation='softmax')(model1_layer1)
Introduction to TensorFlow in Python

Functional APIの使用

# Define layer 1 for model 2
model2_layer1 = tf.keras.layers.Dense(8, activation='relu')(model2_inputs)

# Define layer 2 for model 2
model2_layer2 = tf.keras.layers.Dense(4, activation='softmax')(model2_layer1)
# Merge model 1 and model 2
merged = tf.keras.layers.add([model1_layer2, model2_layer2])
# Define a functional model
model = tf.keras.Model(inputs=[model1_inputs, model2_inputs], outputs=merged)

# Compile the model
model.compile('adam', loss='categorical_crossentropy')
Introduction to TensorFlow in Python

練習しましょう!

Introduction to TensorFlow in Python

Preparing Video For Download...