当前位置:网站首页>4 custom model training
4 custom model training
2022-06-26 15:58:00 【X1996_】
Build the model ( Forward propagation of neural networks ) --> Define the loss function --> Define optimization functions --> Definition tape --> The model gets the predicted value --> Forward propagation gets loss --> Back propagation --> Use the optimization function to update the calculated gradient to the variable
Custom model training No evaluation function
import numpy as np
import tensorflow as tf
data = np.random.random((1000, 32))
labels = np.random.random((1000, 10))
class MyModel(tf.keras.Model):
def __init__(self, num_classes=10):
super(MyModel, self).__init__(name='my_model')
self.num_classes = num_classes
# Define the layers you need
self.dense_1 = tf.keras.layers.Dense(32, activation='relu')
self.dense_2 = tf.keras.layers.Dense(num_classes)
def call(self, inputs):
# Define forward propagation
# Use in (in `__init__`) Defined layer
x = self.dense_1(inputs)
return self.dense_2(x)
model = MyModel(num_classes=10)
# Instantiate an optimizer.
optimizer = tf.keras.optimizers.SGD(learning_rate=1e-3)
# Instantiate a loss function.
loss_fn = tf.keras.losses.CategoricalCrossentropy()
# Prepare the training dataset.
batch_size = 64
train_dataset = tf.data.Dataset.from_tensor_slices((data, labels))
train_dataset = train_dataset.shuffle(buffer_size=1024).batch(batch_size)
# epoch
#batch_size
#tape Find gradient Gradient update
# Training
epochs = 10
for epoch in range(epochs):
#print('Start of epoch %d' % (epoch,))
# Traversing the data set batch_size
for step, (x_batch_train, y_batch_train) in enumerate(train_dataset):
# open GradientTape To record the operations that were run during the forward pass , This will enable automatic discrimination .
with tf.GradientTape() as tape:
# Run the forward propagation of the model . The operation of the model applied to its input will be recorded in GradientTape On .
logits = model(x_batch_train, training=True) # This minibatch The predicted value of
# Compute the minibatch The loss value of
loss_value = loss_fn(y_batch_train, logits)
# Use GradientTape Automatically obtain the gradient of the trainable variable relative to the loss .
grads = tape.gradient(loss_value, model.trainable_weights)
# Minimize the loss by updating the value of the variable , This performs a step of gradient descent .
optimizer.apply_gradients(zip(grads, model.trainable_weights))
# Every time 200 batches Print once .
print('Training loss %s epoch: %s' % (epoch, float(loss_value)))
Add evaluation function
import numpy as np
import tensorflow as tf
x_train = np.random.random((1000, 32))
y_train = np.random.random((1000, 10))
x_val = np.random.random((200, 32))
y_val = np.random.random((200, 10))
x_test = np.random.random((200, 32))
y_test = np.random.random((200, 10))
class MyModel(tf.keras.Model):
def __init__(self, num_classes=10):
super(MyModel, self).__init__(name='my_model')
self.num_classes = num_classes
# Define the layers you need
self.dense_1 = tf.keras.layers.Dense(32, activation='relu')
self.dense_2 = tf.keras.layers.Dense(num_classes)
def call(self, inputs):
# Define forward propagation
# Use in (in `__init__`) Defined layer
x = self.dense_1(inputs)
return self.dense_2(x)
# Optimizer
optimizer = tf.keras.optimizers.SGD(learning_rate=1e-3)
# Loss function
loss_fn = tf.keras.losses.CategoricalCrossentropy(from_logits=True)
# Get ready metrics function
train_acc_metric = tf.keras.metrics.CategoricalAccuracy()
val_acc_metric = tf.keras.metrics.CategoricalAccuracy()
# Prepare the training data set
batch_size = 64
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
train_dataset = train_dataset.shuffle(buffer_size=1024).batch(batch_size)
# Prepare the test data set
val_dataset = tf.data.Dataset.from_tensor_slices((x_val, y_val))
val_dataset = val_dataset.batch(64)
model = MyModel(num_classes=10)
epochs = 10
for epoch in range(epochs):
print('Start of epoch %d' % (epoch,))
# Traversing the data set batch_size
for step, (x_batch_train, y_batch_train) in enumerate(train_dataset):
# One batch
with tf.GradientTape() as tape:
logits = model(x_batch_train)
loss_value = loss_fn(y_batch_train, logits)
grads = tape.gradient(loss_value, model.trainable_weights)
optimizer.apply_gradients(zip(grads, model.trainable_weights))####
# Update the of the training set metrics
train_acc_metric(y_batch_train, logits)
# At every epoch Show at the end metrics.
train_acc = train_acc_metric.result()
# print('Training acc over epoch: %s' % (float(train_acc),))
# At every epoch Reset the training indicator at the end
train_acc_metric.reset_states()#!!!!!!!!!!!!!!!
# At every epoch Run a validation set at the end .
for x_batch_val, y_batch_val in val_dataset:
val_logits = model(x_batch_val)
# Update validation set merics
val_acc_metric(y_batch_val, val_logits)
val_acc = val_acc_metric.result()
# print('Validation acc: %s' % (float(val_acc),))
val_acc_metric.reset_states()
print('Training_losses: %s Training_acc: %s Validation_acc: %s' % (float(loss_value), float(train_acc), float(val_acc)))
边栏推荐
猜你喜欢
随机推荐
El dialog drag and drop, the boundary problem is completely corrected, and the bug of the online version is fixed
现在券商的优惠开户政策是什么?现在在线开户安全么?
Ansible自动化的运用
js文本滚动分散动画js特效
NFT Platform Security Guide (1)
Particle filter PF -- Application in maneuvering target tracking (particle filter vs extended Kalman filter)
selenium将元素保存为图片
NFT transaction principle analysis (1)
6 自定义层
Svg capital letter a animation JS effect
High frequency interview 𞓜 Flink Shuangliu join
Evaluation - TOPSIS
5000字解析:实战化场景下的容器安全攻防之道
【问题解决】新版webots纹理等资源文件加载/下载时间过长
CNN优化trick
STEPN 新手入门及进阶
Solana扩容机制分析(1):牺牲可用性换取高效率的极端尝试 | CatcherVC Research
How to identify contractual issues
AUTO sharding policy will apply DATA sharding policy as it failed to apply FILE sharding policy
【leetcode】331. 验证二叉树的前序序列化





![[CEPH] Lock Notes of cephfs](/img/9a/b68e7b07b1187794c0dbed36eea749.png)


