创建秩为1的张量
# create a rank1 tensor object import tensorflow as tf mystr = tf.Variable(["Hello"], tf.string) cool_numbers = tf.Variable([3.14159, 2.71828], tf.float32) first_primes = tf.Variable([2, 3, 5, 7, 11], tf.int32) its_very_complicated = tf.Variable([12.3 - 4.85j, 7.5 - 6.23j], tf.complex64) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) print(sess.run(mystr)) print(sess.run(cool_numbers)) print(sess.run(first_primes)) print(sess.run(its_very_complicated))
下面是上面的结果:
2018-02-16 21:31:32.599557: I C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2 [b'Hello'] [ 3.14159012 2.71828008] [ 2 3 5 7 11] [ 12.3-4.85j 7.5-6.23j]
创建秩为二的张量
# create rank2 tensor import tensorflow as tf mymat = tf.Variable([[7], [11]], tf.int16) myxor = tf.Variable([[False, True], [True, False]], tf.bool) linear_squares = tf.Variable([[4], [9], [16], [25]], tf.int32) squarish_squares = tf.Variable([ [4, 9], [16, 25] ], tf.int32) rank_of_squares = tf.rank(linear_squares) mymatC = tf.Variable([[7], [11]], tf.int32) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) print(sess.run(rank_of_squares))
下面是秩为二的张量的结果:
2018-02-16 21:33:53.407399: I C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2 2
创建维度更高的张量
# create higher rank tensors , # consist of an n-dimensional array import tensorflow as tf my_image = tf.zeros([10, 299, 299, 3]) # Getting a tf.Tensor object's rank r = tf.rank(my_image) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) print(sess.run(r))
下面是维度更高的张量的结果:
2018-02-16 21:34:57.278721: I C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2 4