Keras 连接的卷积层:每个卷积层所需的数据格式

Keras Connected Convolutional Layers: Data Format Needed on Every Conv Layer

在 Keras 中使用连续的 Conv2D 层,我是否需要在每一层上设置 data_format,还是仅在第一层上设置?我的数据采用 NCHW(频道优先)格式。

为了提供一些上下文,我有一个 Keras 网络,它由多个连续连接的 Conv2D 层组成。我的图像是:

换句话说,每个样本的形状都是(4, 84, 84)。这是我的模型,它是一个 Deep-q 网络实现:

import numpy as np
import tensorflow as tf

'''
 ' Huber loss: https://en.wikipedia.org/wiki/Huber_loss
'''
def huber_loss(y_true, y_pred):
  error = y_true - y_pred
  cond  = tf.keras.backend.abs(error) < 1.0

  squared_loss = 0.5 * tf.keras.backend.square(error)
  linear_loss  = tf.keras.backend.abs(error) - 0.5

  return tf.where(cond, squared_loss, linear_loss)

'''
 ' Importance Sampling weighted huber loss.
'''
def huber_loss_mean_weighted(y_true, y_pred, is_weights):
  error = huber_loss(y_true, y_pred)

  return tf.keras.backend.mean(error * is_weights)


# The observation input.
in_obs = tf.keras.layers.Input(shape=(4, 84, 84))

# The importance sampling weights are used with the custom loss function,
# and correct for the non-uniform distribution of the samples.
in_is_weights = tf.keras.layers.Input(shape=(1,))

# Expectations when training (the output is qualities for actions).
in_actual = tf.keras.layers.Input(shape=(4,))

# Normalize the observation to the range of [0, 1].
norm = tf.keras.layers.Lambda(lambda x: x / 255.0)(in_obs)

# Convolutional layers per the Nature paper on DQN.
conv1 = tf.keras.layers.Conv2D(filters=32, kernel_size=8, strides=4,
  activation="relu", data_format="channels_first")(norm)
conv2 = tf.keras.layers.Conv2D(filters=64, kernel_size=4, strides=2,
  activation="relu", data_format="channels_first")(conv1)
conv3 = tf.keras.layers.Conv2D(filters=64, kernel_size=3, strides=1,
  activation="relu", data_format="channels_first")(conv2)

# Flatten, and move to the fully-connected part of the network.
flatten = tf.keras.layers.Flatten()(conv3)
dense1  = tf.keras.layers.Dense(512, activation="relu")(flatten)

# Output prediction.
out_pred = tf.keras.layers.Dense(4, activation="linear")(dense1)

# Using Adam optimizer, RMSProp's successor.
opt = tf.keras.optimizers.Adam(lr=5e-5, decay=0.0)

# This network is used for training.
train_network = tf.keras.models.Model(
  inputs=[in_obs, in_actual, in_is_weights],
  outputs=out_pred)

# The custom loss, which is Huber Loss weighted by IS weights.
train_network.add_loss(
  huber_loss_mean_weighted(out_pred, in_actual, in_is_weights))

train_network.compile(optimizer=opt, loss=None)

在此先感谢您的帮助。

你在每一层都需要它,或者你可以在你的keras配置文件中设置它:

  • Linux: ~/.keras/keras.json
  • Windows: C:\users\<yourusername>\.keras\keras.json

但老实说,您最好从数据中交换轴,因为其他 keras 函数往往总是在最后一个轴上工作。因此,在最后一个轴上设置通道可能会为您节省很多额外的工作。

要更改您的数据:

np.moveaxis(data,1,-1)