最近开始接触TensorFlow 2.0,进行深度学习MRI图像重建、医学图像分类方面的研究。之前使用的是TensorFlow 1.4,和2.0差别非常大,因此把一些学习心得记录下来。
前言 TensorFlow 2.0的模型构建已经转向以keras为中心,非常方便,并且默认Eager execution方便调试,和以前的静态图差别很大。以前的编程思想是先构造Graph,然后在Session中运行。
模型构建分为两种:Sequential model和keras functional API:
Sequential model 【深度学习|TensorFlow 2.0学习笔记-段曹辉】Sequential model是最简单的一种序列模型,由层和层堆叠而成,使用tf.keras.Sequential API:
The most common type of model is a stack of layers: the tf.keras.Sequential model.
model = tf.keras.Sequential([
layers.Dense(64, activation='relu', input_shape=(32,)),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')])model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])
keras functional API keras functional API用来构建更复杂、更灵活的网络结构,例如残差网络:
The Functional API a set of tools for building graphs of layers.
inputs = keras.Input(shape=(784,), name='img')
x = layers.Dense(64, activation='relu')(inputs)
x = layers.Dense(64, activation='relu')(x)
outputs = layers.Dense(10, activation='softmax')(x)model = keras.Model(inputs=inputs, outputs=outputs, name='mnist_model'
通过确定输入和输出来确定一个网络模型。
模型训练也分为两种:model.fit和custom train
model.fit
model.compile(optimizer=tf.keras.optimizers.RMSprop(0.01),
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=[tf.keras.metrics.CategoricalAccuracy()])model.fit(data, labels, epochs=10, batch_size=32)
custom train
Use the tf.GradientTape context to calculate the gradients used to optimize our model.
- Iterate each epoch. An epoch is one pass through the dataset.
- Within an epoch, iterate over each example in the training Dataset grabbing its features (x) and label (y).
- Using the example’s features, make a prediction and compare it with the label. Measure the inaccuracy of the prediction and use that
to calculate the model’s loss and gradients.- Use an optimizer to update the model’s variables.
- Keep track of some stats for visualization.
- Repeat for each epoch.
train_loss_results = []
train_accuracy_results = []
num_epochs = 201for epoch in range(num_epochs):
epoch_loss_avg = tf.keras.metrics.Mean()
epoch_accuracy = tf.keras.metrics.SparseCategoricalAccuracy()for x, y in train_dataset:
# Optimize the model
loss_value, grads = grad(model, x, y)
optimizer.apply_gradients(zip(grads, model.trainable_variables))# Track progress
epoch_loss_avg(loss_value)# add current batch loss
# compare predicted label to actual label
epoch_accuracy(y, model(x))# end epoch
train_loss_results.append(epoch_loss_avg.result())
train_accuracy_results.append(epoch_accuracy.result())if epoch % 50 == 0:
print("Epoch {:03d}: Loss: {:.3f}, Accuracy: {:.3%}".format(epoch,
epoch_loss_avg.result(),
epoch_accuracy.result()))
推荐阅读
- 圆桌实录 | 为什么不约而同选择了大 Kernel
- 影像组学|医学影像组学之病理切片分割(免费训练数据,标注数据,免费代码,免费教程)三天走完影像组学全部流程
- 自然语言处理|纵览200大规模机器学习研究!道翰天琼认知智能机器人平台API接口大脑为您揭秘-3。
- 论文学习|Hierarchical Transformer Model for Scientific Named Entity Recognition 论文总结
- pytorch|pytorch实现手写数字识别 | MNIST数据集(全连接神经网络)
- 技巧tips|图像风格迁移实战
- FCN论文复现|resnet50代码前向对齐
- 一点就分享系列|一点就分享系列(实践篇4-上篇)深度学习部署之Tensorrt转换思路(“授人与鱼不如授人与渔”)
- 一点就分享系列|一点就分享系列(理解篇5)补更必看(SwintransformerV2.0版本的改进以及使用理解(上篇))