1. 程式人生 > 實用技巧 >2、tensorflow 變數的初始化

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]]