tensorflow2.0 创建张量
2 创建张量 Tensorflow中可以通过多种方式创建张量,如从python list对象创建,从numpy数组创建,创建采样自某种已知分布的张量。 2.1 从numpy,python List对象创建 Numpy Array数组和Python List是python程序中间非常重要的数据载体容器,很多数据都是通过Python语言将数据加载至Array或者List容器,再转换到Tensor类型,通过Tensorflow运算处理后导出到Array或者List容器,方便其他模块调用。 通过tf.convert_to_tensor可以创建新Tensor,并将保存在Python List对象或者Numpy Array对象中的数据导入到新的Tensor中: tf.convert_to_tensor([1, 2.]) tf.convert_to_tensor(np.array([[1,2 ], [3, 4]])) tf.constant()和tf.convert_to_tensor()都能够自动的把Numpy数组或者Python List数据类型转换为Tensor类型,使用其一即可。 2.2 创建全0,全1张量(tf.zeros(), tf.ones()) 将张量创建为全0或者全1数据是非常常见的张量初始化手段。 考虑线性变换 ,将权值矩阵W初始化为全1矩阵,偏置b初始化为全0向量