tensorflow+Python 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.

你可能感兴趣的:(python,tensorflow,ValueError,python)