自定义层
根据自己的需要自定义层,该类必须继承layers.Layer
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])
def call(self, inputs, training=None):
x = inputs @ self.kernel
return x
自定义模型
自定义层数以及使用的激活函数等,该类必须继承keras.Model
class MyNetwork(keras.Model):
def __init__(self):
super(MyNetwork, self).__init__()
self.fc1 = MyDense(32*32*3, 256)
self.fc2 = MyDense(256,128)
self.fc3 = MyDense(128,128)
self.fc4 = MyDense(128,128)
self.fc5 = MyDense(128,10)
def call(self, inputs, training=None):
x = tf.reshape(inputs, [-1, 32*32*3])
x = self.fc1(x)
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
model.compile配置网络
配置网络优化器,损失函数,性能指标
model.compile( optimizer = 优化器,
loss = 损失函数,
metrics = ["准确率”]
)
model.fit训练网络
设置相关参数,训练网络
model.fit( 训练集的输入特征,
训练集的标签,
batch_size, #每一个batch的大小
epochs, #迭代次数
validation_data = (测试集的输入特征,测试集的标签),
validation_split = 从测试集中划分多少比例给训练集,
validation_freq = 测试的epoch间隔数)
自定义完整网络
代码如下
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers,datasets,optimizers
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
def preprocess(x, y):
x = 2*tf.cast(x, dtype=tf.float32)/255.-1
y = tf.cast(y, dtype=tf.int32)
return x,y
batchs = 128
(x, y), (x_val,y_val) = datasets.cifar10.load_data() # 加载数据
print(x.shape,y.shape,x_val.shape,y_val.shape) # (50000, 32, 32, 3) (50000, 1) (10000, 32, 32, 3) (10000, 1)
y = tf.squeeze(y) # 降维
y_val = tf.squeeze(y_val)
y = tf.one_hot(y,depth=10) # 50k 10
y_val = tf.one_hot(y_val,depth=10) # 10k 10
print('datases:',x.shape,y.shape,x_val.shape,y_val.shape)
train_db = tf.data.Dataset.from_tensor_slices((x,y))
train_db = train_db.map(preprocess).shuffle(10000).batch(batchs)
test_db = tf.data.Dataset.from_tensor_slices((x_val,y_val))
test_db = test_db.map(preprocess).batch(batchs)
sample = next(iter(train_db))
print('batch:',sample[0].shape,sample[1].shape)
# 自定义层:未加偏置
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])
def call(self, inputs, training=None):
x = inputs @ self.kernel
return x
# 网络模型
class MyNetwork(keras.Model):
def __init__(self):
super(MyNetwork, self).__init__()
self.fc1 = MyDense(32*32*3, 256)
self.fc2 = MyDense(256,128)
self.fc3 = MyDense(128,128)
self.fc4 = MyDense(128,128)
self.fc5 = MyDense(128,10)
def call(self, inputs, training=None):
x = tf.reshape(inputs, [-1, 32*32*3])
x = self.fc1(x)
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 = MyNetwork() # 创建网络
network.compile( # 配置网络
optimizer=optimizers.Adam(lr=1e-3)
,loss=tf.losses.CategoricalCrossentropy(from_logits=True) # 交叉熵做为损失函数
,metrics=['accuracy'] # 标注网络评价指标
)
network.fit(train_db
, epochs=5
, validation_data=test_db
, validation_freq=1
, batch_size=32
)
# 评估模型 损失值和选定指标值
loss, accuracy = network.evaluate(test_db)
print(loss,accuracy)
小白,望大佬指点
参考:https://blog.csdn.net/yunfeather/article/details/106461754?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162459670116780262593351%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=162459670116780262593351&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2blogfirst_rank_v2~rank_v29-1-106461754.pc_v2_rank_blog_default&utm_term=compile&spm=1018.2226.3001.4450