Tensorflow 什么';“get_variable()”中的“tf.zeros_初始值设定项”有什么问题吗?
我想用CW算法训练一些对抗性的例子,我使用了来自的例子和来自的CW实现。但是我遇到了一个关于Tensorflow 什么';“get_variable()”中的“tf.zeros_初始值设定项”有什么问题吗?,tensorflow,Tensorflow,我想用CW算法训练一些对抗性的例子,我使用了来自的例子和来自的CW实现。但是我遇到了一个关于tf.zeros\u初始值设定项的错误: ValueError: The initializer passed is not valid. It should be a callable with no arguments and the shape should not be provided or an instance of 'tf.keras.initializers.*' and `shape
tf.zeros\u初始值设定项的错误:
ValueError: The initializer passed is not valid. It should be a callable with no arguments and the shape should not be provided or an instance of
'tf.keras.initializers.*' and `shape` should be fully defined.
编辑:似乎未完全定义的形状与使用初始值设定项冲突。我怎样才能修好它
下面是一段代码:
# ... omitted
with tf.variable_scope('model', reuse=tf.AUTO_REUSE):
# CW
_, env.adv_cw, _ = cw.cw(model, env.x)
以下是env.x
:
env.x = tf.placeholder(tf.float32, (None, width, height, channels), name='x')
运行代码时,我会收到错误消息:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-39-712c8b007d37> in <module>()
8 with tf.variable_scope('model', reuse=tf.AUTO_REUSE):
9 # CW
---> 10 _, env.adv_cw, _ = cw.cw(model, env.x)
5 frames
/content/cw.py in cw(model, x, y, eps, ord_, T, optimizer, alpha, min_prob, clip)
50 """
51 xshape = x.get_shape().as_list()
---> 52 noise = tf.get_variable('noise', shape=xshape, dtype=tf.float32,
53 initializer=tf.zeros_initializer)
54
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variable_scope.py in get_variable(name, shape, dtype, initializer, regularizer, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter, constraint, synchronization, aggregation)
1494 constraint=constraint,
1495 synchronization=synchronization,
-> 1496 aggregation=aggregation)
1497
1498
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variable_scope.py in get_variable(self, var_store, name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter, constraint, synchronization, aggregation)
1237 constraint=constraint,
1238 synchronization=synchronization,
-> 1239 aggregation=aggregation)
1240
1241 def _get_partitioned_variable(self,
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variable_scope.py in get_variable(self, name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter, constraint, synchronization, aggregation)
560 constraint=constraint,
561 synchronization=synchronization,
--> 562 aggregation=aggregation)
563
564 def _get_partitioned_variable(self,
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variable_scope.py in _true_getter(name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource, constraint, synchronization, aggregation)
512 constraint=constraint,
513 synchronization=synchronization,
--> 514 aggregation=aggregation)
515
516 synchronization, aggregation, trainable = (
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variable_scope.py in _get_single_variable(self, name, shape, dtype, initializer, regularizer, partition_info, reuse, trainable, collections, caching_device, validate_shape, use_resource, constraint, synchronization, aggregation)
906 variable_dtype = None
907 else:
--> 908 raise ValueError("The initializer passed is not valid. It should "
909 "be a callable with no arguments and the "
910 "shape should not be provided or an instance of "
ValueError: The initializer passed is not valid. It should be a callable with no arguments and the shape should not be provided or an instance of `tf.keras.initializers.*' and `shape` should be fully defined.
环境:Google Colab、TensorFlow 1.14.0-rc1、Python 3.6只需根据占位符维度进行更改,让我以占位符变量为例
** x = placeholder(t f. float 32, (None, width, height, channels), name='x')**.
它有4个维度:[无,宽度,高度,通道],但未定义宽度,高度,通道,这意味着,对于图像宽度=6,高度=6,通道=3,因此定义了张量维度[6 x 6 x 3]
您可以做的是,读取图像,获取不同变量中的所有三维值,并将其传递给占位符变量。
前任。
图像A=32 x 32 x 3
宽度=A.形状[0]
高度=A.形状[1]
通道=A.shape[2]
或者,您可以通过这种方式直接向宽度、高度、通道(如果您知道输入数据形状)提供值,从而定义占位符。为什么要初始化占位符?你应该向它输入数据。添加()
,这样它就可以调用这样的函数…,initializer=tf.zeros.initializer()
@ImperishableNight我不想初始化占位符。该错误是由初始化变量noise
引起的。tf.placeholder
,shape
的第二个参数没有完全定义((无,…)
),因此我收到了错误消息。@yolo如果初始值设定项
是一个类,TensorFlow似乎会首先创建一个实例。
** x = placeholder(t f. float 32, (None, width, height, channels), name='x')**.