tensorflow 2におけるカスタムレイヤまたはネットワーク
29520 ワード
カスタムネットワークを介して、既存のネットワークと独自のネットワークを直列に接続することで、さまざまなネットワークを構築することができます.
その使い方はこのブログを参照してください.
tensorflow 2でネットワーク層をカスタマイズする最良の方法はtfを継承することである.keras.layers.Layerクラスは、実際の状況に応じて次のいくつかのクラスメソッドを書き換えます. __init__: クラスを初期化するには、ここでネットワーク層に必要なパラメータを構成したり、tfをインスタンス化したりすることができます.kerasが提供するいくつかの基礎演算子、例えばDepthwiseConv 2 Dは、call方法での応用に便利である. build:この方法は入力されたshape次元を取得することができ、Poolやinput shapeに基づいて重みを構築するなどの必要な演算子を動的に構築するのに便利である. call:ネットワーク層による順方向推理の実現方法;
カスタムレイヤーの実装例:
カスタムモデルを実装するには、モデルクラスがkerasを継承する必要があります.layers.Modelクラスでは、compile、fit、evaluateなど、私たちのモデルクラスで親クラスの多くの方法を使用することができます.
そして、モデルクラスで__を実装するinit__()とcall()の2つの方法.その原理はカスタムレイヤと同じです.
カスタムモデルモデルの実装例:
1、keras.Sequential
その使い方はこのブログを参照してください.
network = Sequential([layers.Dense(256, activation='relu'),
layers.Dense(128, activation='relu'),
layers.Dense(64, activation='relu'),
layers.Dense(32, activation='relu'),
layers.Dense(10)])
network.build(input_shape=(None, 28*28))
network.summary()
2、カスタム層またはネットワーク
(1)、カスタムネットワーク層の実現
tensorflow 2でネットワーク層をカスタマイズする最良の方法はtfを継承することである.keras.layers.Layerクラスは、実際の状況に応じて次のいくつかのクラスメソッドを書き換えます.
カスタムレイヤーの実装例:
class MyDense(tf.keras.layers.Layer):
def __init__(self, inp_dim, out_dim):
super(MyDense, self).__init__()
self.kernel = self.add_variable('w', [inp_dim, outp_dim])
self.bias = self.add_variable('b', [outp_dim])
def call(self, inputs, training = None):
out = inputs @ self.kernel + self.bias
return out
(2)カスタムモデルの実装
カスタムモデルを実装するには、モデルクラスがkerasを継承する必要があります.layers.Modelクラスでは、compile、fit、evaluateなど、私たちのモデルクラスで親クラスの多くの方法を使用することができます.
そして、モデルクラスで__を実装するinit__()とcall()の2つの方法.その原理はカスタムレイヤと同じです.
カスタムモデルモデルの実装例:
class MyModel(tf.keras.Model):
def __init__(self):
super(MyModel, self).__init__()
self.fc1 = MyDense(28*28, 256)
self.fc2 = MyDense(256, 128)
self.fc3 = MyDense(128, 64)
self.fc4 = MyDense(64, 32)
self.fc5 = MyDense(32, 10)
def call(self, inputs, training = None):
x = self.fc1(inputs)
x = tf.nn.relu(x)
x = self.fc2(x)
x = tf.nn.relu(x)
x = self.fc3(x)
x = tf.nn.relu(x)
x = self.fc4(x)
x = tf.nn.relu(x)
x = self.fc5(x)
return x
3、カスタム層またはネットワーク実戦(順方向伝播)
import tensorflow as tf
from tensorflow.keras import datasets, layers, optimizers, Sequential, metrics
from tensorflow import keras
def preprocess(x, y):
"""
x is a simple image, not a batch
"""
x = tf.cast(x, dtype=tf.float32) / 255.
x = tf.reshape(x, [28 * 28])
y = tf.cast(y, dtype=tf.int32)
y = tf.one_hot(y, depth=10)
return x, y
batchsz = 128
(x, y), (x_val, y_val) = datasets.mnist.load_data()
print('datasets:', x.shape, y.shape, x.min(), x.max())
db = tf.data.Dataset.from_tensor_slices((x, y))
db = db.map(preprocess).shuffle(60000).batch(batchsz)
ds_val = tf.data.Dataset.from_tensor_slices((x_val, y_val))
ds_val = ds_val.map(preprocess).batch(batchsz)
sample = next(iter(db))
print(sample[0].shape, sample[1].shape)
network = Sequential([layers.Dense(256, activation='relu'),
layers.Dense(128, activation='relu'),
layers.Dense(64, activation='relu'),
layers.Dense(32, activation='relu'),
layers.Dense(10)])
network.build(input_shape=(None, 28 * 28))
network.summary()
class MyDense(layers.Layer):
def __init__(self, inp_dim, outp_dim):
super(MyDense, self).__init__()
self.kernel = self.add_variable('w', [inp_dim, outp_dim])
self.bias = self.add_variable('b', [outp_dim])
def call(self, inputs, training=None):
out = inputs @ self.kernel + self.bias
return out
class MyModel(keras.Model):
def __init__(self):
super(MyModel, self).__init__()
self.fc1 = MyDense(28 * 28, 256)
self.fc2 = MyDense(256, 128)
self.fc3 = MyDense(128, 64)
self.fc4 = MyDense(64, 32)
self.fc5 = MyDense(32, 10)
def call(self, inputs, training=None):
x = self.fc1(inputs)
x = tf.nn.relu(x)
x = self.fc2(x)
x = tf.nn.relu(x)
x = self.fc3(x)
x = tf.nn.relu(x)
x = self.fc4(x)
x = tf.nn.relu(x)
x = self.fc5(x)
return x
network = MyModel()
network.compile(optimizer=optimizers.Adam(lr=0.01),
loss=tf.losses.CategoricalCrossentropy(from_logits=True),
metrics=['accuracy']
)
network.fit(db, epochs=5, validation_data=ds_val,
validation_freq=2)
network.evaluate(ds_val)
sample = next(iter(ds_val))
x = sample[0]
y = sample[1] # one-hot
pred = network.predict(x) # [b, 10]
# convert back to number
y = tf.argmax(y, axis=1)
pred = tf.argmax(pred, axis=1)
print(pred)
print(y)