ฉันต้องการวิธีรับรูปร่างของเอาท์พุตเทนเซอร์สำหรับเลเยอร์ทุกประเภท (เช่น Dense, Conv2D ฯลฯ) ใน TensorFlow ตามเอกสารประกอบ มีคุณสมบัติ output_shape
ที่ช่วยแก้ปัญหาได้ อย่างไรก็ตาม ทุกครั้งที่ฉันเข้าถึงมัน ฉันจะได้รับ AttributedError
นี่คือตัวอย่างโค้ดที่แสดงปัญหา:
import numpy as np
import tensorflow as tf
x = np.arange(0, 8, dtype=np.float32).reshape((1, 8))
x = tf.constant(value=x, dtype=tf.float32, verify_shape=True)
dense = tf.layers.Dense(units=2)
out = dense(x)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
res = sess.run(fetches=out)
print(res)
print(dense.output_shape)
คำสั่ง print(dense.output_shape)
จะสร้างข้อความแสดงข้อผิดพลาด:
AttributeError: The layer has never been called and thus has no defined output shape.
หรือ print(dense.output)
จะผลิต:
AttributeError('Layer ' + self.name + ' has no inbound nodes.')
AttributeError: Layer dense_1 has no inbound nodes.
มีวิธีแก้ไขข้อผิดพลาดหรือไม่?
ป.ล.: ฉันรู้ว่าในตัวอย่างด้านบน ฉันสามารถรับรูปร่างของเอาท์พุตเทนเซอร์ได้ทาง out.get_shape()
อย่างไรก็ตาม ฉันต้องการทราบว่าเหตุใดคุณสมบัติ output_shape
จึงไม่ทำงาน และฉันจะแก้ไขได้อย่างไร