TensorFlow 2.0学习笔记-段曹辉

最近开始接触TensorFlow 2.0,进行深度学习MRI图像重建、医学图像分类方面的研究。之前使用的是TensorFlow 1.4,和2.0差别非常大,因此把一些学习心得记录下来。

前言

TensorFlow 2.0的模型构建已经转向以keras为中心,非常方便,并且默认Eager execution方便调试,和以前的静态图差别很大。以前的编程思想是先构造Graph,然后在Session中运行。

模型构建分为两种:Sequential modelkeras functional API:

Sequential model

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.

  1. Iterate each epoch. An epoch is one pass through the dataset.
  2. Within an epoch, iterate over each example in the training Dataset grabbing its features (x) and label (y).
  3. 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.
  4. Use an optimizer to update the model’s variables.
  5. Keep track of some stats for visualization.
  6. Repeat for each epoch.
train_loss_results = []
train_accuracy_results = []
num_epochs = 201

for 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()))

你可能感兴趣的:(深度学习,TensorFlow,keras)