TensorFlow 版本:1.14.0
第 3 章 p41 页代码:
>>> with g1.as_default():
... v = tf.get_variable("v", initializer=tf.zeros_initializer(shape=[1]))
报错内容:
…
Traceback (most recent call last):
File “”, line 2, in
File “/Users/******/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py”, line 507, in new_func
return func(*args, **kwargs)
TypeError: init() got an unexpected keyword argument ‘shape’
查看 TensorFlow 官方 API:https://tensorflow.google.cn/api_docs/python/tf/zeros_initializer
中说明了 zeros_initializer 通过 __call__方法得到可调用对象(callable),并不能直接传入参数使用,因此 tf.zeros_initializer(shape=[1]) 会报错!
通过 __call__方法得到可调用对象的使用方法:
# 通过在可调用对象实例后面添加一对括号使用
>>> initializer = tf.zeros_initializer()
>>> initializer(shape=[1])
<tf.Tensor 'zeros:0' shape=(1,) dtype=float32>
同时,该实例的正确使用方法:
>>> with g1.as_default():
... v = tf.get_variable("v", shape=[1], initializer=tf.zeros_initializer())
当然该书的官方源代码已更正该错误:https://github.com/caicloud/tensorflow-tutorial/blob/master/Deep_Learning_with_TensorFlow/1.4.0/Chapter03/1.%20图,张量及会话.ipynb