报错:‘Dense’ object has no attribute ‘kernel’
问题关键:没有调用dense函数,无法获取参数
try:
net = tf.keras.layers.Dense(10)
net.kernel.shape
except Exception as e:
print(e)
# output:'Dense' object has no attribute 'kernel'
正确使用方法:
import tensorflow as tf
#自动生成数据
x = tf.random.normal([4,784])
#构建512神经元的全连接层
#[4,784]==>[4,512]
net = tf.keras.layers.Dense(512)
out = net(x)
print(f'net的属性为{net.kernel.shape},net的bias的属性{net.bias.shape}')
# output:net的属性为(784, 512),net的bias的属性(512,)