当我尝试使用 tf.contrib.layers.convolution2d 时出现 Tensorflow 错误

Tensorflow error when I try to use tf.contrib.layers.convolution2d

当我调用 tf.contrib.layers.convolution2d 时,tensorflow 执行终止并出现有关所用参数之一的错误

got an unexpected keyword argument 'weight_init'

传递的参数如下:

layer_one = tf.contrib.layers.convolution2d(
    float_image_batch,
    num_output_channels=32,     
    kernel_size=(5,5),          
    activation_fn=tf.nn.relu,
    weight_init=tf.random_normal,
    stride=(2, 2),
    trainable=True)

这正是我正在阅读的书中所描述的。我怀疑直接在调用中写入 weight_init=tf.random_normal 可能存在语法问题,但我不知道如何解决。我正在使用 Tensorflow 0.12.0

当权重张量的初始值通过 weight_init 参数传递时,您正在阅读的书(您没有提到是哪本书)可能使用的是旧版本的 TensorFlow。在您使用的 TensorFlow 库版本中(您没有提到您的 TF 版本),可能该参数已替换为 weight_initializertf.contrib.layers.convolution2d 的最新 (TensorFlow v0.12.0) 文档是 here

要解决您的问题,您可以更改代码中的以下行:

weight_init=tf.random_normal

weight_initializer=tf.random_normal_initializer()

根据 documentation,默认情况下,tf.random_normal_initialier 使用 0.0 均值,1.0 标准差和数据类型 tf.float32。您可以根据需要使用此行更改参数: weight_initializer=tf.random_normal_initializer(mean=0.0, stddev=1.0, seed=None, dtype=tf.float32)