Tf.shape()およびx.get_shape()。as_list()
Tf Shape X Get_shape
これは簡単な説明、x.get_shape()であり、テンソルのみがこのメソッドを使用してタプルを返すことができます。
import tensorflow as tf import numpy as np a_array=np.array([[1,2,3],[4,5,6]]) b_list=[[1,2,3],[3,4,5]] c_tensor=tf.constant([[1,2,3],[4,5,6]]) print(c_tensor.get_shape()) print(c_tensor.get_shape().as_list()) with tf.Session() as sess: print(sess.run(tf.shape(a_array))) print(sess.run(tf.shape(b_list))) print(sess.run(tf.shape(c_tensor)))
結果:Visibleは、テンソルが形状を返すためにのみ使用できますが、as_list()の操作によってリストに変換する必要があるタプルです。
操作するタプルでない場合は、エラーが報告されます。
上記のコードにa_array.get_shape()を追加すると、次のエラーが報告されます。
print(a_array.get_shape()) AttributeError: 'numpy.ndarray' object has no attribute 'get_shape'
テンソルだけがそのような特権を持っていることがわかります!
ここにいくつかの警告があります:
最初のポイント:tensor.get_shape()はタプルを返しますが、これはsess.run()に配置できません。これは操作とテンソルのみを置くことができます
2番目のポイント:tf.shape()はテンソルを返します。それがいくらであるかを知るには、sess.run()を渡す必要があります
import tensorflow as tf import numpy as np a_array=np.array([[1,2,3],[4,5,6]]) b_list=[[1,2,3],[3,4,5]] c_tensor=tf.constant([[1,2,3],[4,5,6]]) with tf.Session() as sess: a_array_shape=tf.shape(a_array) print('a_array_shape:',a_array_shape) print('a_array_shape:',sess.run(a_array_shape)) c_tensor_shape=c_tensor.get_shape().as_list() print('c_tensor_shape:',c_tensor_shape) #print(sess.run(c_tensor_shape)) #Report Error
結果:
コメントアウトを解放すると、エラーは次のようになります。明らかに、テンソルまたは操作ではありません。