我在Torch进行了一年的卷积神经网络训练,用于图像分类。我刚刚开始使用Tensorflow。如何在Tensorflow中重复使用经过培训的Torch CNN模型进行转移学习?是否有从Torch到Tensorflow的参数传输库?或者是否有任何关于Torch和Tensorflow之间共享参数格式的建议?
我尝试将Torch模型的每一层的参数保存到文本文件中,并将它们作为常量初始化复制到Tensorflow。
的示例:
conv_weights = tf.constant([0.17347207665443,0.11592379212379,0.026265326887369,0.12255407869816,-0.15566548705101,0.18516965210438,-0.045260231941938,0.13446696102619,0.15107157826424,0.0024442630819976,-0.03807720541954,-0.10220601409674,-0.17906428873539,0.10029320418835,-0.085617743432522,-0.015723343938589,-0.15337321162224,0.16704897582531,0.18761920928955,0.16804780066013,0.18608762323856,0.0048886127769947,0.12103436142206,0.088970154523849,0.050000578165054,0.092202171683311,0.11841697990894])
# Torch conv weights shape (output_dim, input_dim, width, height)
# Tensorflow conv weights shape (width, height, input_dim, output_dim )
# reshape to Tensorflow shape
conv_weights = tf.reshape(conv_weights, [1,3,3,3])
conv_weights = tf.transpose(conv_weights, [2,3,1,0])
kernel = tf.get_variable('weights', initializer=conv_weights, dtype=dtype)
conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='SAME')
如果你想检查Tensorflow和Torch的结果是否匹配(使用小模型和手动复制权重),这可能就足够了。但这不是正确转学的最佳方式。