tensorflow+Python ValueError以及解决方法(后续继续更新)

本文解析了在使用TensorFlow过程中遇到的几种常见的ValueError,包括类型转换错误、形状不匹配及输入维度错误,并提供了相应的代码修改建议。
部署运行你感兴趣的模型镜像

使用python 怎么可能没有ValueError,下面罗列我见到的几种错误,虽然很低级,但是不试过还是很难找到错误的

ValueError: Incompatible type conversion requested to type ‘float32’ for variable of type ‘int32_ref’

使用tensorflow时,出现在卷积层,原因在于初始化卷积权重或者偏置时, 采用整数进行初始化,导致卷积权重或者偏置为整形变量。
下面贴出我错误的代码

def bias_variable(shape, name):
    with tf.name_scope('Bias'):
        initial = tf.constant(0, shape=shape)
        biases = tf.Variable(initial)
        tf.summary.histogram(name+'/Biases_function', biases)
    return biases

修改结果

def bias_variable(shape, name):
    with tf.name_scope('Bias'):
        initial = tf.constant(0.0, shape=shape)
        biases = tf.Variable(initial)
        tf.summary.histogram(name+'/Biases_function', biases)
        return biases

也就是0于0.0的问题,在修改初始化参数时,一不留神就改错了,然后还不知道发生了什么。

ValueError: Incompatible shapes between op input and calculated input gradient.

我错误的原因在于使用tensorflow的反卷积tf.nn.conv2d_transpose是制定的output_shape出错了,其实函数根据输入和卷积核是可以计算输出尺寸的,也就是tf.nn.conv2d_transpose的信息是冗余的,因此,如果冗余的信息对应不上,导致误差无法反向传递。

ValueError: ‘images’ must have either 3 or 4 dimensions.

错误的代码为:

 level_map = tf.image.resize_images(gt_level, size)

说明输入的变量gt_level不是一个3-D或者4-D的tensor。查找修改即可。tf.image.resize_images的定义如下:
Signature:
tf.image.resize_images(images, size, method=0, align_corners=False)
Args:

  1. images: 4-D Tensor of shape [batch, height, width, channels] or
    3-D Tensor of shape [height, width, channels].
  2. size: A 1-D int32 Tensor of 2 elements: new_height, new_width. The
    new size for the images.
  3. method: ResizeMethod. Defaults to ResizeMethod.BILINEAR.
  4. align_corners: bool. If True, the centers of the 4 corner pixels of the
    input and output tensors are aligned, preserving the values at the
    corner pixels. Defaults to False.

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值