张量的维度(秩)
tensor属性
tensor和numpy类似,具有类似的属性,例如:
数据类型dtpye
形状shape
几种tensor
constant
值不能改变的一种tensor
tf.constant( value, dtype=None, shape=None, name='Const' )
不能够直接输出,需要使用session会话
placeholder
先占一个固定的位置,等后续再添加值的一种tensor
tf.compat.v1.placeholder( dtype, shape=None, name=None )
注意:
x = tf.compat.v1.placeholder(tf.float32, shape=(1024, 1024)) y = tf.matmul(x, x) with tf.compat.v1.Session() as sess: print(sess.run(y)) # ERROR: will fail because x was not fed. rand_array = np.random.rand(1024, 1024) print(sess.run(y, feed_dict={x: rand_array})) # Will succeed.
Variable
值可以改变的一种tensor
__init__( initial_value=None, trainable=None, validate_shape=True, caching_device=None, name=None, variable_def=None, dtype=None, import_scope=None, constraint=None, synchronization=tf.VariableSynchronization.AUTO, aggregation=tf.compat.v1.VariableAggregation.NONE, shape=None )
SparseTensor
一种稀疏tensor,类似线性代数中稀疏矩阵的概念
spare = tf.SparseTensor(indices=[[0, 0], [1, 2]], values=[1, 2], dense_shape=[3, 4]) with tf.Session() as sess: print(sess.run(tf.sparse_tensor_to_dense(spare)))
运行结果:
[[1 0 0 0] [0 0 2 0] [0 0 0 0]]