tensorflow踩坑

  • List item

在对Tensorflow(1.5)构建的模型使用Pytorch进行复现的过程中,遇到了一些关于Tensorflow的问题,这里整合记录一下。

权重初始化

复现的前人的论文
论文:https://daooshee.github.io/BMVC2018website/
代码:https://github.com/weichen582/RetinexNet
首先,贴出来我要使用的模型,我想要在Pytorch使用相同的模型,需要先理解并且测试一下模型的运行情况,我需要打印一些张量数值用来确定两种框架的模型可以输出相同的结果。

import tensorflow as tf

A = tf.random_uniform((3, 2, 3, 4), minval=0.0, maxval=4.0, seed=0)
B = tf.ones((3, 2, 3, 4))


def concat(layers):
    return tf.concat(layers, axis=3)


def DecomNet(input_im, layer_num, channel=64, kernel_size=3):
    input_max = tf.reduce_max(input_im, axis=3, keepdims=True)
    input_im = concat([input_max, input_im])
    with tf.variable_scope('DecomNet', reuse=tf.AUTO_REUSE):
        conv = tf.layers.conv2d(input_im, channel, kernel_size * 3, padding='same', activation=None,
                                name="shallow_feature_extraction")
        for idx in range(layer_num):
            conv = tf.layers.conv2d(conv, channel, kernel_size, padding='same', activation=tf.nn.relu,
                                    name='activated_layer_%d' % idx)
        conv = tf.layers.conv2d(conv, 4, kernel_size, padding='same', activation=None, name='recon_layer')

    R = tf.sigmoid(conv[:, :, :, 0:3])
    L = tf.sigmoid(conv[:, :, :, 3:4])

    return R, L


with tf.Session():
    print(A.eval())
# init = tf.global_variables_initializer()
with tf.Session() as sess:

    [R_low, I_low] = DecomNet(A, layer_num=5)
    # sess.run(tf.global_variables_initializer())
    print(R_low.eval())


但是这里运行报错,错误代码为:FailedPreconditionError (see above for traceback): Attempting to use uninitialized value
也就是在运行模型之前需要初始化网络的权重。

解决方法

使用sess.run(tf.global_variables_initializer())进行初始化权重,切记要在使用紧贴着使用模型语句之后,不报错的代码如下

import tensorflow as tf

A = tf.random_uniform((3, 2, 3, 4), minval=0.0, maxval=4.0, seed=0)
B = tf.ones((3, 2, 3, 4))


def concat(layers):
    return tf.concat(layers, axis=3)


def DecomNet(input_im, layer_num, channel=64, kernel_size=3):
    input_max = tf.reduce_max(input_im, axis=3, keepdims=True)
    input_im = concat([input_max, input_im])
    with tf.variable_scope('DecomNet', reuse=tf.AUTO_REUSE):
        conv = tf.layers.conv2d(input_im, channel, kernel_size * 3, padding='same', activation=None,
                                name="shallow_feature_extraction")
        for idx in range(layer_num):
            conv = tf.layers.conv2d(conv, channel, kernel_size, padding='same', activation=tf.nn.relu,
                                    name='activated_layer_%d' % idx)
        conv = tf.layers.conv2d(conv, 4, kernel_size, padding='same', activation=None, name='recon_layer')

    R = tf.sigmoid(conv[:, :, :, 0:3])
    L = tf.sigmoid(conv[:, :, :, 3:4])

    return R, L


with tf.Session():
    print(A.eval())
# init = tf.global_variables_initializer()
with tf.Session() as sess:

    [R_low, I_low] = DecomNet(A, layer_num=5)
    sess.run(tf.global_variables_initializer()) # 这是不报错的关键语句
    print(R_low.eval())


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值