设置tensorflow conv2d操作的权重和偏置张量 [英] Set weight and bias tensors of tensorflow conv2d operation

查看:237
本文介绍了设置tensorflow conv2d操作的权重和偏置张量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Torch 中获得了一个训练有素的神经网络,我需要在 tensorflow 中准确地重建它.我相信我已经在 tensorflow 中正确定义了网络的架构,但是我在传输权重和偏置张量时遇到了麻烦.使用第三方包,我将来自 Torch 网络的所有权重和偏置张量转换为 numpy 数组,然后将它们写入磁盘.我可以将它们加载回我的 python 程序,但我无法找到一种方法将它们分配给我的 tensorflow 网络中的相应层.

I have been given a trained neural network in torch and I need to rebuild it exactly in tensorflow. I believe I have correctly defined the network's architecture in tensorflow but I am having trouble transferring the weight and bias tensors. Using a third party package, I converted all the weight and bias tensors from the torch network to numpy arrays then wrote them to disk. I can load them back into my python program but I cannot figure out a way to assign them to the corresponding layers in my tensorflow network.

例如,我在 tensorflow 中定义了一个卷积层

For instance, I have a convolution layer defined in tensorflow as

kernel_1 = tf.Variable(tf.truncated_normal([11,11,3,64], stddev=0.1))
conv_kernel_1 = tf.nn.conv2d(input, kernel_1, [1,4,4,1], padding='SAME')
biases_1 = tf.Variable(tf.zeros[64])
bias_layer_1 = tf.nn_add(conv_kernel_1, biases_1)

根据 tensorflow 文档,tf.nn.conv2d 操作使用 kernel_1 变量中定义的形状来构造权重张量.但是,我无法弄清楚如何访问该权重张量以将其设置为我从文件加载的权重数组.

According to the tensorflow documentation, the tf.nn.conv2d operation uses the shape defined in the kernel_1 variable to construct the weight tensor. However, I cannot figure out how to access that weight tensor to set it to the weight array I have loaded from file.

是否可以明确设置权重张量?如果是这样,如何?

(同样的问题适用于偏置张量.)

(The same question applies to bias tensor.)

推荐答案

如果您在 NumPy 数组中有权重和偏差,那么将它们连接到您的 TensorFlow 网络应该很容易:

If you have the weights and biases in a NumPy array, it should be easy to connect them into your TensorFlow network:

weights_1_array = ...  # ndarray of weights for layer 1
biases_1_array = ...  # ndarray of biases for layer 1

conv_kernel_1 = tf.nn.conv2d(input, weights_1_array, [1, 4, 4, 1], padding='SAME')
bias_layer_1 = tf.nn.bias_add(conv_kernel_1, biases_1_array)

请注意,您必须确保 weights_1_arraybiases_1_array 的数据格式正确.请参阅 tf.nn.conv2d()<的文档/code> 解释所需的过滤器形状.

Note that you must ensure that weights_1_array and biases_1_array are in the correct data format. See the documentation for tf.nn.conv2d() for an explanation of the required filter shape.

这篇关于设置tensorflow conv2d操作的权重和偏置张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆