日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Tensorflow自定義模型與訓(xùn)練超詳細(xì)講解_python

作者:沐兮Krystal ? 更新時(shí)間: 2022-12-19 編程語(yǔ)言

前言

Tensorflow的核心與NumPy非常相似,但具有GPU支持;

Tensorflow支持分布式計(jì)算(跨多個(gè)設(shè)備和服務(wù)器)。

像NumPy一樣使用TensorFlow

@運(yùn)算符是在Python 3.5 中添加的,用于矩陣乘法,等效于 tf.matmul() 函數(shù)。

Keras的底層API

Keras API在keras.backend中有自己的底層API,如果要編寫(xiě)可移植到其他Keras實(shí)現(xiàn)中的代碼,則應(yīng)使用這些Keras函數(shù)。

from tensorflow import keras
K = keras.backend
K.square(K.transpose(t)) + 10

當(dāng)從NumPy數(shù)組創(chuàng)建張量時(shí),需設(shè)置 dtype=tf.float32;

定制模型和訓(xùn)練算法

自定義損失函數(shù)

實(shí)現(xiàn)Huber損失:

def huber_fn(y_true, y_pred):
	error = y_true - y_pred
	is_small_error = tf.abs(error) < 1
	squared_loss = tf.square(error) / 2
	linear_loss = tf.abs(error) - 0.5
	return tf.where(is_small_error, squared_loss, linear_loss)

編譯Keras模型,訓(xùn)練模型:

model.compile(loss=huber_fn, optimizer="nadam")
model.fit(X_train, y_train, [...])

保存和加載包含自定義組件的模型

當(dāng)加載包含自定義對(duì)象的模型時(shí),需要將名稱(chēng)映射到對(duì)象。

model = keras.models.load_model("my_model_with_a_custom_loss.h5",custom_objects={<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->"huber_fn": huber_fn})

創(chuàng)建一個(gè)函數(shù),該函數(shù)創(chuàng)建已配置的損失函數(shù):

def create_huber(threshold=1.0):
	def huber_fn(y_true, y_pred):
		error = y_true - y_pred
		is_small_error = tf.abs(error) < threshold
		squared_loss = tf.square(error) / 2
		linear_loss = threshold * tf.abs(error) - threshold**2 / 2
		return tf.where(is_small_error, squared_loss, linear_loss)
	return huber_fn
model.compile(loss=create_huber(2.0), optimizer="nadam")

在加載模型的時(shí)候必須指定閾值:

model = keras.models.load_model("my_model_with_a_custom_loss.h5",custom_objects={"huber_fn": create_huber(2.0)})

通過(guò)創(chuàng)建 keras.losses.Loss 類(lèi)的子類(lèi),然后實(shí)現(xiàn)其 get_config() 方法來(lái)解決此問(wèn)題:

class HuberLoss(keras.losses.Loss):
	def __init__(self, threshold=1.0, **kwargs):
		self.threshold = threshold
		super().__init__(**kwargs)
	def call(self, y_true, y_pred):
		error = y_true - y_pred
		is_small_error = tf.abs(error) < self.threshold
		squared_loss = tf.square(error) / 2
		linear_loss = self.threshold * tf.abs(error) - self.threshold**2 / 2
		return tf.where(is_small_error, squared_loss, linear_loss)
	def get_config(self):
		base_config = super().get_config()
		return {**base_config, "threshold": self.threshold}

以上父類(lèi)構(gòu)造函數(shù)處理標(biāo)準(zhǔn)超參數(shù):損失的name和用于聚合單個(gè)實(shí)例損失的reduction算法。

get_config() 方法返回一個(gè)字典,將每個(gè)超參數(shù)映射到其值。首先調(diào)用父類(lèi)的get_config() 方法,然后將新的超參數(shù)添加到此字典中。

可在編譯模型時(shí)使用此類(lèi)的任何實(shí)例:

model.compile(loss=HuberLoss(2.),optimizer="nadam")

當(dāng)保存模型的時(shí)候,閾值會(huì)同時(shí)一起保存。在加載模型時(shí),只需要將類(lèi)名映射到類(lèi)本身:

model = keras.models.load_model("my_model_with_a_custom_loss.h5",custom_objects={<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->"HuberLoss": HuberLoss})

保存模型時(shí),Keras會(huì)調(diào)用損失實(shí)例的 get_config() 方法,并將配置以 JSON 格式保存到 HDF5 文件中。

自定義激活函數(shù)與初始化與正則化和約束

編寫(xiě)簡(jiǎn)單的函數(shù)進(jìn)行自定義

def my_softplus(z):
	return tf.math.log(tf.exp(z)+1.0)
def my_glorot_initializer(shape, dtype=tf.float32):
	stddev = tf.sqrt(2. / (shape[0] + shape[1]))
	return tf.random.normal(shape, stddev=stddev, dtype=dtype)
def my_l1_regularizer(weights):
	return tf.reduce_sum(tf.abs(0.01 * weights))
def my_positive_weights(weights): # return value is just tf.nn.relu(weights)
	return tf.where(weights < 0., tf.zeros_like(weights), weights)

使用這些自定義函數(shù)

layer = keras.layers.Dense(30, activation=my_softplus,
							kernel_initializer=my_glorot_initializer,
							kenel_regularizer=my_l1_regularizer,
							kenel_constraint=my_positive_weights)

在每個(gè)訓(xùn)練步驟中,權(quán)重將傳遞給正則化函數(shù),以計(jì)算正則化損失,并將其添加到主要損失中得到用于訓(xùn)練的最終損失。

必須為損失、層(包括激活函數(shù))和模型實(shí)現(xiàn) call() 方法,或者為正則化、初始化和約束實(shí)現(xiàn) __call__()方法。

自定義指標(biāo)

可以將創(chuàng)建的損失函數(shù)作為指標(biāo)。

model.compile(loss="mse", optimizer="nadam", metrics=[create_huber(2.0)])

流式指標(biāo)(狀態(tài)指標(biāo))是逐批次更新的。

自定義層

創(chuàng)建不帶任何權(quán)重的自定義層:

exponential_layer = keras.layers.Lambda(lambda x: tf.exp(x))
  • 當(dāng)要預(yù)測(cè)的值具有非常不同的標(biāo)度(例如0.001、10、1000)時(shí),有時(shí)會(huì)在回歸模型的輸出層中使用指數(shù)層。
  • 構(gòu)建自定義的有狀態(tài)層(即具有權(quán)重的層):
class MyDense(keras.layers.Layer):
	# 將所有超參數(shù)用作參數(shù)
	def __init__(self, units, activation=None, **kwargs):
		super().__init__(**kwargs)
		self.units = units
		self.activation = keras.activations.get(activation)
	# 創(chuàng)建層的變量
	def build(self, batch_input_shape):
		self.kernel = self.add_weight(
			name="kernel", shape=[batch_input_shape[-1], self.units],
			initializer="glorot_normal")
		self.bias = self.add_weight(
			name="bias", shape=[self.units], initializer="zeros")
		super().build(batch_input_shape) # must be at the end
		# 調(diào)用父類(lèi)方法,告訴keras這一層被構(gòu)建了,設(shè)置 self.built=true
	def call(self, X):
	 	return self.activation(X @ self.kernel + self.bias)
	def compute_output_shape(self, batch_input_shape):
		return tf.TensorShape(batch_input_shape.as_list()[:-1] + [self.units])
	def get_config(self):
		base_config = super().get_config()
		return {**base_config, "units": self.units,
				"activation": keras.activation.serialize(self.activation) }

創(chuàng)建多輸入,多輸出的層:

class MyMultiLayer(keras.layers.Layer):
	def call(self, X):
		X1, X2 = X
		return [X1 + X2, X1 * X2, X1 / X2]
	def compute_output_shape(self, batch_input_shape):
		b1, b2 = batch_input_shape
		return [b1, b1, b1]

如果層在訓(xùn)練期間和測(cè)試期間需要具有不同的行為,比如,創(chuàng)建一個(gè)在訓(xùn)練期間(用于正則化)添加高斯噪聲,但在測(cè)試期間不執(zhí)行任何操作:

class MyGaussianNoise(keras.layers.Layer):
	def __init__(self, stddev, **kwargs):
		super().__init__(**kwargs)
		self.stddev = stddev
	def call(self, X, training=None):
		if training:
			noise = tf.random.normal(tf.shape(X), stddev=self.stddev)
			return X + noise
		else:
			return X
	def compute_output_shape(self, batch_input_shape):
		return batch_input_shape

自定義模型

首先創(chuàng)建一個(gè) ResidualBlock 層:

class ResidualBlock(keras.layers.Layer):
	def __init__(self, n_layers, n_nerons, **kwargs):
		super().__init__(**kwargs)
		self.hidden = [keras.layers.Dense(n_nerons, activation="elu",
										  kenel_initializer="he_normal")
					   for _ in range(n_layers)]
	def call(self, inputs):
		Z = inputs
		for layer in self.hidden:
			Z = layer(Z)
		return inputs + Z

使用子類(lèi)API定義模型:

class ResidualRegressor(keras.Model):
	def __init__(self, output_dim, **kwargs):
		super().__init__(**kwargs)
		self.hidden1 = keras.layers.Dense(30, activation="elu",
										  kernel_initializer="he_normal")
		self.block1 = ResidualBlock(2, 30)
		self.block2 = ResidualBlock(2, 30)
		self.out = keras.layers.Dense(output_dim)
	def call(self, inputs):
		Z = self.hidden1(inputs)
		for _ in range(1 + 3):
			Z = self.block1(Z)
		Z = self.block2(Z)
		return self.out(Z)
  • 在構(gòu)造函數(shù)中創(chuàng)建層,在call()方法中使用它們。
  • 帶有自定義重建損失的自定義模型,此自定義模型在上部隱藏層的頂部有輔助輸出,與該輔助輸出相關(guān)的損失稱(chēng)為重建損失。
class ReconstructingRegressor(keras.Model):
	def __init__(self, output_dim, **kwargs):
		super().__init__(**kwargs)
		self.hidden = [keras.layers.Dense(30, activation="selu",
										  kernel_initializer = "lecun_normal")
					   for _ in range(5)]
		self.out = keras.layers.Dense(output_dim)
	def build(self, batch_input_shape):
		n_inputs = batch_input_shape[-1]
		self.reconstruct = keras.layers.Dense(n_inputs) # 該層用于重建模型的輸入
		super().build(batch_input_shape)
	def call(self, inputs):
		Z = inputs
		for layer in self.hidden:
			Z = layer(Z)
		reconstruction = self.reconstruct(Z)
		recon_loss = tf.reduce_mean(tf.square(reconstruction - inputs))
		self.add_loss(0.05 * recon_loss)
		return self.out(Z)

正則化,在輸入變化不大時(shí)懲罰那些變化很大的激活。

原文鏈接:https://blog.csdn.net/GW_Krystal/article/details/127788879

欄目分類(lèi)
最近更新