【Tensorflow API】新版 tf.zeros_initializer()

原型:

class Zeros(Initializer):
  """Initializer that generates tensors initialized to 0."""

  def __init__(self, dtype=dtypes.float32):
    self.dtype = dtypes.as_dtype(dtype)

  def __call__(self, shape, dtype=None, partition_info=None):
    if dtype is None:
      dtype = self.dtype
    return array_ops.zeros(shape, dtype)

  def get_config(self):
    return {"dtype": self.dtype.name}

新版zeros_initializer()的构造函数默认指定数据类型,没有shape相关内容

import tensorflow as tf

x = tf.zeros_initializer()
y = x([2])

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(y))

输出:

[0. 0.]

在tf.get_variable()中使用tf.zeros_initializer()中的正确用法:

import tensorflow as tf

x = tf.zeros_initializer()
y = x([2])
v = tf.get_variable("v", shape=[3], initializer=tf.zeros_initializer())

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(y))
    print(sess.run(v))

输出:

[0. 0.]
[0. 0. 0.]

猜你喜欢

转载自blog.csdn.net/oMoDao1/article/details/82083083
今日推荐