【问题标题】:Change this Python Keras code to DeepLearning4j code将此 Python Keras 代码更改为 DeepLearning4j 代码
【发布时间】:2021-07-19 18:37:10
【问题描述】:

所以看起来 Python 的 tensorflow 和当前发布的 Jython 版本不兼容,所以我正在用 Java 编写我的 AI 模型(GAN)。我正在为我的 GAN 模型遵循 Keras 和 Tensorflow 中的 python 指南。我已经想出了如何使用 DeepLearning4j 在 Java 中设置我的 python 神经网络代码。问题是,我无法在 DeepLearning4j 中设置训练功能。

这是我所遵循的 Python 培训代码:

generator_optimizer = tf.keras.optimizers.Adam(generator_lr) # learning rate for generator
discriminator_optimizer = tf.keras.optimizers.Adam(discriminator_lr) # learning rate for discriminator


seed = tf.random.normal([num_examples_to_generate, noise_dim]) 

# ignore this; I already have dataset code set up
train_dataset = tf.data.Dataset.from_tensor_slices(train_images_scaled).shuffle(BUFFER_SIZE).batch(BATCH_SIZE)

@tf.function
def train_step(images):
    noise = tf.random.normal([BATCH_SIZE, noise_dim])

    with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:

      generated_images = generator(noise, training=True) #generator model is already set up under the name "generator"

      
      real_output = discriminator(images, training=True) #discriminator model is already set up as "discriminator"
      fake_output = discriminator(generated_images, training=True)

      
      gen_loss = generator_loss(fake_output) #predefined function that calculates the loss of the generator as a decimal value using the loss of the discriminator; uses crossentropy
      disc_loss = discriminator_loss(real_output, fake_output) #predefined function that calculates the loss of the discriminator as a decimal value

    
    gradients_of_generator = gen_tape.gradient(gen_loss, generator.trainable_variables)
    gradients_of_discriminator = disc_tape.gradient(disc_loss, discriminator.trainable_variables)

    generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))
    discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator, discriminator.trainable_variables))

def train(dataset, epochs):

  for epoch in range(epochs):
    start = time.time() # time module of course

    for structure_batch in dataset:
      train_step(structure_batch)
    

    print ('Time for epoch {} is {} sec'.format(epoch + 1, time.time()-start))

关于 GAN 的目的是什么,它是通过对不同块使用 one-hot 编码算法在 Minecraft 中生成结构,但我认为这不是必要的信息。

我只是希望使用 DeepLearning4j 库将上述 Python 代码“翻译”成 Java。 (我也有 Java 的 tensorflow 库,但我认为它与 DL4J 不直接兼容)

【问题讨论】:

    标签: tensorflow artificial-intelligence minecraft generative-adversarial-network deeplearning4j


    【解决方案1】:

    使用 keras 导入。

    此处为外部链接: https://deeplearning4j.konduit.ai/v/en-1.0.0-m1/deeplearning4j/how-to-guides/keras-import/api-reference

    对于后人来说,执行此操作的方法取决于您是否有功能模型:

    public static ComputationGraph importKerasModelAndWeights(InputStream modelHdf5Stream) throws IOException, UnsupportedKerasConfigurationException, InvalidKerasConfigurationException
    
    

    或顺序模型:

    public static MultiLayerNetwork importKerasSequentialModelAndWeights(InputStream modelHdf5Stream,
                                                                             boolean enforceTrainingConfig)
                throws IOException, InvalidKerasConfigurationException, UnsupportedKerasConfigurationException
    

    将模型另存为 H5 文件。 使用上述方法将模型加载到 deeplearning4j,然后您可以使用 deeplearning4j 数据集迭代器来训练您的模型。

    对于输入数据管道,如果可能的话,我会将其放在一个单独的问题中。如果您想了解更多详情,请在 cmets 中询问。

    【讨论】:

    • 嗨,实际上,当我尝试在 python 中制作模型并将其导入 Java 时,DL4J 提出了我使用“Conv3DTranspose”层的问题。它拒绝识别该层,因此我无法导入它,这就是为什么我决定使用 DL4J 自己的库在 Java 中构建整个模型的原因。可以,就是不知道怎么写这个训练函数
    • 首先,下次发生这种情况时请提出问题。 (反馈对于确保我们支持用户需要的功能至关重要。(对于每个开源项目都是如此,不管是什么)解决这个问题的方法是添加一个自定义层。您可以使用定义自定义层samediff(我们相当于 tensorflow)——从这样做开始——你可以在这里找到更多相关信息:deeplearning4j.konduit.ai/keras-import/custom-layers
    • 现有的 DL4J 层与 Conv3DTranspose 功能相同,称为“Deconvolution3D”。有没有办法在导入时注册要映射到名称“Conv3DTranspose”的Deconvolution3D层?
    猜你喜欢
    • 1970-01-01
    • 2021-05-13
    • 2021-04-30
    • 2022-01-14
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多