![](/img/trans.png)
[英]How can just apply forward propagation for single-layer-feed-forward or multi-layer-feed-forward models by Keras?
[英]How to apply (call) a single layer on data in Keras?
是否有一種簡單的方法可以將數據提供給 Keras 中的層(通過 TF)並查看返回值以進行測試,而無需實際構建完整模型並為其擬合數據?
如果沒有,如何測試他們開發的自定義層?
您可以為此目的定義和使用后端函數:
from keras import backend as K
# my_layer could be a layer from a previously built model, like:
# my_layer = model.layers[3]
func = K.function(model.inputs, [my_layer.output])
# or it is a layer with customized weights, like:
# my_layer = Dense(...)
# my_layer.set_weights(...)
# out = my_layer(input_data)
input_data = Input(shape=...)
func = K.function([input_data], [my_layer.output])
# to use the function:
layer_output = func(layer_input) # layer_input is a list of numpy array(s)
這是此答案中建議的完整示例。
import numpy as np
import tensorflow as tf
# import tensorflow_probability as tfp
def main():
input_data = tf.keras.layers.Input(shape=(1,))
layer1 = tf.keras.layers.Dense(1)
out1 = layer1(input_data)
# Get weights only returns a non-empty list after we need the input_data
print("layer1.get_weights() =", layer1.get_weights())
# This is actually the required object for weights.
new_weights = [np.array([[1]]), np.array([0])]
layer1.set_weights(new_weights)
out1 = layer1(input_data)
print("layer1.get_weights() =", layer1.get_weights())
func1 = tf.keras.backend.function([input_data], [layer1.output])
#layer2 = tfp.layers.DenseReparameterization(1)
#out2 = layer2(input_data)
#func2 = tf.keras.backend.function([input_data], [layer2.output])
# The input to the layer.
data = np.array([[1], [3], [4]])
print(data)
# The output of layer1
layer1_output = func1(data)
print("layer1_output =", layer1_output)
# The output of layer2
# layer2_output = func2(data)
# print("layer2_output =", layer2_output)
if __name__ == "__main__":
main()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.