2、tensorflow 变量的初始化
https://blog.csdn.net/mzpmzk/article/details/78636137
关于张量tensor的介绍
import tensorflow as tf import numpy as np x = tf.convert_to_tensor([[0,1,2],[3,4,5],[6,7,8]]) y = tf.constant([1,2,3,4,5,6], shape = [2,3]) z = tf.constant(-1, shape=[3,3]) a1 = tf.fill([2,3], 9) #可忽略 a2 = tf.tile([[1,2],[3,4],[5,6]] , [2,3])
# tf.range(2,10,2)
# xx = tf.cast(x,tf.int32) #强制数据类型转换 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print("x:",sess.run(x)) print("y:",sess.run(y)) print("z:",sess.run(z)) print("a1:",sess.run(a1)) print("a2:",sess.run(a2))
('x:', '/n', array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=int32)) ('y:', array([[1, 2, 3], [4, 5, 6]], dtype=int32)) ('z:', array([[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]], dtype=int32)) ('a1:', array([[9, 9, 9], [9, 9, 9]], dtype=int32)) ('a2:', array([[1, 2, 1, 2, 1, 2], [3, 4, 3, 4, 3, 4], [5, 6, 5, 6, 5, 6], [1, 2, 1, 2, 1, 2], [3, 4, 3, 4, 3, 4], [5, 6, 5, 6, 5, 6]], dtype=int32))
其他:
1. tf.pad
tf.pad(tensor, paddings, mode='CONSTANT', name=None) paddings: is an integer tensor with shape [n, 2],n是 tensor 的维度 For example: # 't' is [[1, 2, 3], [4, 5, 6]]. # 'paddings' is [[1, 1,], [2, 2]]. # paddings[0, 0/1]: 沿着第 0 维(x轴)在 tensor 上方/下方补 1 圈零 # paddings[1, 0/1]: 沿着第 1 维(y轴)在 tensor 左方/右方补 2 圈零 tf.pad(t, paddings, "CONSTANT") ==> [[0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 2, 3, 0, 0], [0, 0, 4, 5, 6, 0, 0], [0, 0, 0, 0, 0, 0, 0]]