我嘗試用另一層擴展我已經構建的模型之一,以將模型先前輸出的減法和附加輸入相加。我所有的嘗試都可以使用 Lambda 層來總結:
new_input = Input(shape=(20, 3))
sub_layer = Lambda(lambda x: Reshape((20,1))(backend.sum(x[0] - x[1], axis=-1)))((model.output,new_input))
model = Model(inputs=[model.input, new_input], outputs=sub_layer.output)
但無論我做什么,我總是收到這個錯誤:
Could not build a TypeSpec for KerasTensor(type_spec=TensorSpec(shape=(None, 20, 1), dtype=tf.float32, name=None), name='tf.reshape/Reshape:0', description="created by layer 'tf.reshape'") of unsupported type <class 'keras.engine.keras_tensor.KerasTensor'>.
uj5u.com熱心網友回復:
不確定您的預訓練模型是什么樣的,但您可以嘗試以下方法:
import tensorflow as tf
inputs = tf.keras.layers.Input((10,))
outputs = tf.keras.layers.Dense(20, activation='relu')(inputs)
outputs = tf.keras.layers.RepeatVector(3)(outputs)
outputs = tf.keras.layers.Reshape((20,3))(outputs)
model1 = tf.keras.Model(inputs, outputs)
new_input = tf.keras.layers.Input(shape=(20, 3))
sub_layer = tf.keras.layers.Lambda(lambda x: tf.keras.layers.Reshape((20,1))(tf.keras.backend.sum(x[0] - x[1], axis=-1)))((model1.output,new_input))
model2 = tf.keras.Model(inputs=[model1.input, new_input], outputs=sub_layer)
print(model2.summary())
Model: "model_4"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_7 (InputLayer) [(None, 10)] 0 []
dense_3 (Dense) (None, 20) 220 ['input_7[0][0]']
repeat_vector_1 (RepeatVector) (None, 3, 20) 0 ['dense_3[0][0]']
reshape (Reshape) (None, 20, 3) 0 ['repeat_vector_1[0][0]']
input_8 (InputLayer) [(None, 20, 3)] 0 []
lambda_3 (Lambda) (None, 20, 1) 0 ['reshape[0][0]',
'input_8[0][0]']
==================================================================================================
Total params: 220
Trainable params: 220
Non-trainable params: 0
__________________________________________________________________________________________________
None
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/426830.html
標籤:Python 张量流 喀拉斯 张量流2.0 tf.keras
上一篇:如何從張量流資料集中洗掉單個特征,如何在單個特征上使用應用?
下一篇:時間序列資料的自定義損失函式
