Keras 连接顺序模型和密集模型
Keras concatenate Sequential and Dense models
我有以下型号:
密集模型:
def dense_model(num_features):
inputs = Input(shape=(num_features,), dtype=tf.float32)
layers = Dense(32, activation='relu')(inputs)
model = Model(inputs=inputs, outputs=layers)
return model
LSTM模型:
def lstm_model(num_features):
inputs = Input(shape=[None, num_features], dtype=tf.float32, ragged=True)
layers = LSTM(16, activation='tanh')(
inputs.to_tensor(), mask=tf.sequence_mask(inputs.row_lengths()))
layers = BatchNormalization()(layers)
layers = Dense(16,activation='relu')(layers)
layers = Dense(1, activation='sigmoid')(layers)
model = Model(inputs=inputs, outputs=layers)
model.compile(loss='mse', optimizer='adam', metrics=['mse'])
return model
我正在尝试像这样连接两个网络:
def concatenate_model(num_features):
model_1 = dense_model(10)
inputs = Input(shape=[None, num_features], dtype=tf.float32, ragged=True)
layers = LSTM(16, activation='tanh')(
inputs.to_tensor(), mask=tf.sequence_mask(inputs.row_lengths()))
layers = BatchNormalization()(layers)
layers = Dense(16,activation='relu')(concatenate([layers,model_1]))
layers = Dense(1, activation='sigmoid')(layers)
model = Model(inputs=inputs, outputs=layers)
model.compile(loss='mse', optimizer='adam', metrics=['mse'])
return model
得到如下结果:
但是我得到一个错误:
ValueError: as_list() is not defined on an unknown TensorShape.
Here is a Colab 证明了我的问题。我错过了什么?
试试这个:
import tensorflow as tf
def dense_model(num_features):
inputs = tf.keras.layers.Input(shape=(num_features,), dtype=tf.float32)
layers = tf.keras.layers.Dense(32, activation='relu')(inputs)
model = tf.keras.Model(inputs=inputs, outputs=layers)
return model
def lstm_model(num_features):
model_1 = dense_model(10)
inputs = tf.keras.layers.Input(shape=[None, num_features], dtype=tf.float32, ragged=True)
layers = tf.keras.layers.LSTM(16, activation='tanh')(
inputs.to_tensor(), mask=tf.sequence_mask(inputs.row_lengths()))
layers = tf.keras.layers.BatchNormalization()(layers)
concatenated = tf.keras.layers.Concatenate(axis=-1)([layers, model_1.output])
layers = tf.keras.layers.Dense(16,activation='relu')(concatenated)
layers = tf.keras.layers.Dense(1, activation='sigmoid')(layers)
model = tf.keras.Model(inputs=[inputs, model_1.input], outputs=layers)
model.compile(loss='mse', optimizer='adam', metrics=['mse'])
return model
model = lstm_model(20)
dot_img_file = '/model.png'
tf.keras.utils.plot_model(model, to_file=dot_img_file, show_shapes=True)
我有以下型号:
密集模型:
def dense_model(num_features):
inputs = Input(shape=(num_features,), dtype=tf.float32)
layers = Dense(32, activation='relu')(inputs)
model = Model(inputs=inputs, outputs=layers)
return model
LSTM模型:
def lstm_model(num_features):
inputs = Input(shape=[None, num_features], dtype=tf.float32, ragged=True)
layers = LSTM(16, activation='tanh')(
inputs.to_tensor(), mask=tf.sequence_mask(inputs.row_lengths()))
layers = BatchNormalization()(layers)
layers = Dense(16,activation='relu')(layers)
layers = Dense(1, activation='sigmoid')(layers)
model = Model(inputs=inputs, outputs=layers)
model.compile(loss='mse', optimizer='adam', metrics=['mse'])
return model
我正在尝试像这样连接两个网络:
def concatenate_model(num_features):
model_1 = dense_model(10)
inputs = Input(shape=[None, num_features], dtype=tf.float32, ragged=True)
layers = LSTM(16, activation='tanh')(
inputs.to_tensor(), mask=tf.sequence_mask(inputs.row_lengths()))
layers = BatchNormalization()(layers)
layers = Dense(16,activation='relu')(concatenate([layers,model_1]))
layers = Dense(1, activation='sigmoid')(layers)
model = Model(inputs=inputs, outputs=layers)
model.compile(loss='mse', optimizer='adam', metrics=['mse'])
return model
得到如下结果:
但是我得到一个错误:
ValueError: as_list() is not defined on an unknown TensorShape.
Here is a Colab 证明了我的问题。我错过了什么?
试试这个:
import tensorflow as tf
def dense_model(num_features):
inputs = tf.keras.layers.Input(shape=(num_features,), dtype=tf.float32)
layers = tf.keras.layers.Dense(32, activation='relu')(inputs)
model = tf.keras.Model(inputs=inputs, outputs=layers)
return model
def lstm_model(num_features):
model_1 = dense_model(10)
inputs = tf.keras.layers.Input(shape=[None, num_features], dtype=tf.float32, ragged=True)
layers = tf.keras.layers.LSTM(16, activation='tanh')(
inputs.to_tensor(), mask=tf.sequence_mask(inputs.row_lengths()))
layers = tf.keras.layers.BatchNormalization()(layers)
concatenated = tf.keras.layers.Concatenate(axis=-1)([layers, model_1.output])
layers = tf.keras.layers.Dense(16,activation='relu')(concatenated)
layers = tf.keras.layers.Dense(1, activation='sigmoid')(layers)
model = tf.keras.Model(inputs=[inputs, model_1.input], outputs=layers)
model.compile(loss='mse', optimizer='adam', metrics=['mse'])
return model
model = lstm_model(20)
dot_img_file = '/model.png'
tf.keras.utils.plot_model(model, to_file=dot_img_file, show_shapes=True)