335 Star 1.5K Fork 862

MindSpore / docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
initConst.md 2.33 KB
一键复制 编辑 原始数据 按行查看 历史
luojianing 提交于 2023-07-21 15:16 . replace target=blank

Function Differences with tf.keras.initializers.Constant

View Source On Gitee

tf.keras.initializers.Constant

tf.keras.initializers.Constant(value=0)

For more information, see tf.keras.initializers.Constant.

mindspore.common.initializer.Constant

mindspore.common.initializer.Constant(value)

For more information, see mindspore.common.initializer.Constant.

Usage

TensorFlow: The function input parameter value supports scalar, list, tuple, and array types. Suppose you need to create a tensor of the specified shape, and the input parameter value of this interface is a list or an array, the number of elements contained in value must be less than or equal to the number of elements with the specified shape. If the number of elements contained in value must be less than the number of elements with the specified shape, the last element of value is used to fill the remaining positions.

MindSpore: The function input parameter value supports scalar and array types. When value is an array, only a tensor with the same shape as value can be generated.

Code Example

As an example, if the input is an array, the code sample is as follows:

TensorFlow:

import numpy as np
import tensorflow as tf

value = np.array([0, 1, 2, 3, 4, 5, 6, 7])
value = value.reshape([2, 4])

init = tf.keras.initializers.Constant(value)

x = init(shape=(2, 4))
y = init(shape=(3, 4))

with tf.Session() as sess:
    print(x.eval(), "\n")
    print(y.eval())

# out:
# [[0. 1. 2. 3.]
#  [4. 5. 6. 7.]]

# [[0. 1. 2. 3.]
#  [4. 5. 6. 7.]
#  [7. 7. 7. 7.]]

MindSpore:

import numpy as np
import mindspore as ms
from mindspore.common.initializer import initializer, Constant

value = np.array([0, 1, 2, 3, 4, 5, 6, 7])
value = value.reshape([2, 4])

x = initializer(Constant(value), shape=[2, 4], dtype=ms.float32)

print(x)

# out:
# [[0. 1. 2. 3.]
#  [4. 5. 6. 7.]]
1
https://gitee.com/mindspore/docs.git
git@gitee.com:mindspore/docs.git
mindspore
docs
docs
r2.0

搜索帮助

53164aa7 5694891 3bd8fe86 5694891