在TensorFlow 2.0中使用tf.keras.optimizers.apply_gradients方法时出现TypeError

时间:2019-04-25 17:40:07

标签: python-3.x tensorflow tensorflow2.0 tf.keras

当我执行以下代码时,错误消息 TypeError:zip参数#2必须支持迭代

theta = tf.Variable(tf.zeros(100), dtype=tf.float32, name='theta')

@tf.function
def p(x):
    N = tf.cast(tf.shape(x)[0], tf.int64)
    softmax = tf.ones([N, 1]) * tf.math.softmax(theta)
    idx_x = tf.stack([tf.range(N, dtype=tf.int64), x-1], axis=1)
    return tf.gather_nd(softmax, idx_x)


@tf.function
def softmaxLoss(x):
    return tf.reduce_mean(-tf.math.log(p(x)))


train_dset = tf.data.Dataset.from_tensor_slices(data_train).\
                                repeat(1).batch(BATCH_SIZE)


# Create the metrics
loss_metric = tf.keras.metrics.Mean(name='train_loss')
val_loss_metric = tf.keras.metrics.Mean(name='val_loss')
optimizer = tf.keras.optimizers.Adam(0.001)

@tf.function
def train_step(inputs):
    with tf.GradientTape() as tape:
        log_loss = softmaxLoss(inputs)
    gradients = tape.gradient(log_loss,theta)
    optimizer.apply_gradients(zip(gradients, theta))
    # Update the metrics
    loss_metric.update_state(log_loss)


for epoch in range(NUM_EPOCHS):
    # Reset the metrics
    loss_metric.reset_states()

    # Shuffle dataset before each training epoch
    train_dset = train_dset.shuffle(buffer_size=10000)
    for inputs in train_dset:
        train_step(inputs)


检查后,我发现麻烦来自此行代码:

optimizer.apply_gradients(zip(gradients, theta))

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

您可以通过列出theta来解决此问题,因为zip要求参数可以迭代(而单个tf.Variable则不能迭代)。

因此:

optimizer.apply_gradients(zip(gradients, [theta]))