我正在尝试从以下Dense
图层获取权重:
x = Dense(1024)(Flatten()(previous_layer))
如果我尝试执行以下操作:
x = Dense(1024)
weights = x.get_weights()
这很好用,但我的理解是这些权重将无用,因为我们没有向图层提供任何输入。
但是,如果我尝试执行以下操作:
x = Dense(1024)(Flatten()(previous_layer))
weights = x.get_weights()
这不起作用,因为x
现在是Tensor
对象,并且没有get_weights
方法:
'Tensor' object has no attribute 'get_weights'
我做错了什么?
答案 0 :(得分:1)
将图层应用于某个输入张量(Dense(n)
)时,图层(Dense(n)(input)
)与输出张量之间存在差异。
您需要将图层存储在变量中,而不仅仅是输出张量:
>>> import keras
>>> input_layer = keras.layers.Input((2,))
>>> layer = keras.layers.Dense(3) # create a layer
>>> print(layer)
<keras.layers.core.Dense object at 0x7f03ca9d4d68>
>>> print(layer.get_weights()) # the layer does not have weights yet
[]
>>> output_tensor = layer(input_layer) # apply the layer to the input tensor
>>> print(output_tensor)
Tensor("dense_1/BiasAdd:0", shape=(?, 3), dtype=float32)
>>> print(layer.get_weights()) # now get the weights
[array([[-0.84973848, -0.19682372, -0.14602524],
[ 0.70318353, -0.1578933 , -0.94751853]], dtype=float32),
array([ 0., 0., 0.], dtype=float32)]
答案 1 :(得分:0)
lstm_output = keras.layers.LSTM(100,return_sequences = True)(x_5)
output_location_final = Dense(2,activation='tanh')
#lstm_output is a tensor
result = output_location_final(lstm_output)
#result is a layer
weight = output_location_final.get_weights()
print(weight)