前言:
Keras是一个由Python编写的开源人工神经网络库,可以作为Tensorflow、Microsoft-CNTK和Theano的高阶应用程序接口,进行深度学习模型的设计、调试、评估、应用和可视化。
Theano于2008年诞生于蒙特利尔理工学院,其派生出了大量的深度学习Python软件包,最著名的包括Blocks和Keras。Theano的核心是一个数学表达式的编译器,它知道如何获取你的结构,并使之成为一个使用numpy、高效本地库的高效代码,如BLAS和本地代码(C++)在CPU或GPU上尽可能快地运行。它是为深度学习中处理大型神经网络算法所需的计算而专门设计,是这类库的首创之一(发展始于2007年),被认为是深度学习研究和开发的行业标准。
TensorFlow是一个基于数据流编程(dataflow programming)的符号数学系统,被广泛应用于各类机器学习(machine learning)算法的编程实现,其前身是谷歌的神经网络算法库DistBelief。
Tensorflow拥有多层级结构,可部署于各类服务器、PC终端和网页并支持GPU和TPU高性能数值计算,被广泛应用于谷歌内部的产品开发和各领域的科学研究。
代码:
</pre><pre code_snippet_id="1947416" snippet_file_name="blog_20161025_1_3331239" name="code" class="python">
# coding:utf-8 """ If you want to load pre-trained weights that include convolutions (layers Convolution2D or Convolution1D), be mindful of this: Theano and TensorFlow implement convolution in different ways (TensorFlow actually implements correlation, much like Caffe), and thus, convolution kernels trained with Theano (resp. TensorFlow) need to be converted before being with TensorFlow (resp. Theano). """ from keras import backend as K from keras.utils.np_utils import convert_kernel from text_classifier import keras_text_classifier import sys def th3tf( model): import tensorflow as tf ops = [] for layer in model.layers: if layer.__class__.__name__ in ['Convolution1D', 'Convolution2D']: original_w = K.get_value(layer.W) converted_w = convert_kernel(original_w) ops.append(tf.assign(layer.W, converted_w).op) K.get_session().run(ops) return model def tf2th(model): for layer in model.layers: if layer.__class__.__name__ in ['Convolution1D', 'Convolution2D']: original_w = K.get_value(layer.W) converted_w = convert_kernel(original_w) K.set_value(layer.W, converted_w) return model def conv_layer_converted(tf_weights, th_weights, m = 0): """ :param tf_weights: :param th_weights: :param m: 0-tf2th, 1-th3tf :return: """ if m == 0: # tf2th tc = keras_text_classifier(weights_path=tf_weights) model = tc.loadmodel() model = tf2th(model) model.save_weights(th_weights) elif m == 1: # th3tf tc = keras_text_classifier(weights_path=th_weights) model = tc.loadmodel() model = th3tf(model) model.save_weights(tf_weights) else: print("0-tf2th, 1-th3tf") return if __name__ == '__main__': if len(sys.argv) < 4: print("python tf_weights th_weights <0|1>\n0-tensorflow to theano\n1-theano to tensorflow") sys.exit(0) tf_weights = sys.argv[1] th_weights = sys.argv[2] m = int(sys.argv[3]) conv_layer_converted(tf_weights, th_weights, m)
名称栏目:keras实现tensorflow与theano相互转换的方法-创新互联
网址分享:https://www.cdcxhl.com/article14/hhoge.html
成都网站建设公司_创新互联,为您提供响应式网站、用户体验、商城网站、面包屑导航、App设计、网站建设
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联