在 Keras 中将顺序转换为函数式
Convert Sequential to Functional in Keras
我有一个以 Sequential 风格编写的 keras 代码。但是我正在尝试切换 Functional mode
因为我想使用 merge
功能。但是我在声明 Model(x, out)
时遇到了下面的错误。我的 API 功能代码有什么问题?
# Sequential, this is working
# out_size==16, seq_len==1
model = Sequential()
model.add(LSTM(128,
input_shape=(seq_len, input_dim),
activation='tanh',
return_sequences=True))
model.add(TimeDistributed(Dense(out_size, activation='softmax')))
# Functional API
x = Input((seq_len, input_dim))
lstm = LSTM(128, return_sequences=True, activation='tanh')(x)
td = TimeDistributed(Dense(out_size, activation='softmax'))(lstm)
out = merge([td, Input((seq_len, out_size))], mode='mul')
model = Model(input=x, output=out) # error below
RuntimeError: Graph disconnected: cannot obtain value for tensor
Tensor("input_40:0", shape=(?, 1, 16), dtype=float32) at layer
"input_40". The following previous layers were accessed without issue:
['input_39', 'lstm_37']
已更新
谢谢@Marcin Możejko。我终于做到了。
x = Input((seq_len, input_dim))
lstm = LSTM(128, return_sequences=True, activation='tanh')(x)
td = TimeDistributed(Dense(out_size, activation='softmax'))(lstm)
second_input = Input((seq_len, out_size)) # object instanciated and hold as a var.
out = merge([td, second_input], mode='mul')
model = Model(input=[x, second_input], output=out) # second input provided to model.compile(...)
# then I add two inputs
model.fit([trainX, filter], trainY, ...)
人们可能会注意到,对由 Input((seq_len, out_size))
调用创建的对象的引用只能从 merge
函数调用环境访问。此外 - 它没有添加到 Model
定义中 - 是什么导致图形断开连接。您需要做的是:
x = Input((seq_len, input_dim))
lstm = LSTM(128, return_sequences=True, activation='tanh')(x)
td = TimeDistributed(Dense(out_size, activation='softmax'))(lstm)
second_input = Input((seq_len, out_size)) # object instanciated and hold as a var.
out = merge([td, second_input], mode='mul')
model = Model(input=[x, second_input], output=out) # second input provided to model
我有一个以 Sequential 风格编写的 keras 代码。但是我正在尝试切换 Functional mode
因为我想使用 merge
功能。但是我在声明 Model(x, out)
时遇到了下面的错误。我的 API 功能代码有什么问题?
# Sequential, this is working
# out_size==16, seq_len==1
model = Sequential()
model.add(LSTM(128,
input_shape=(seq_len, input_dim),
activation='tanh',
return_sequences=True))
model.add(TimeDistributed(Dense(out_size, activation='softmax')))
# Functional API
x = Input((seq_len, input_dim))
lstm = LSTM(128, return_sequences=True, activation='tanh')(x)
td = TimeDistributed(Dense(out_size, activation='softmax'))(lstm)
out = merge([td, Input((seq_len, out_size))], mode='mul')
model = Model(input=x, output=out) # error below
RuntimeError: Graph disconnected: cannot obtain value for tensor Tensor("input_40:0", shape=(?, 1, 16), dtype=float32) at layer "input_40". The following previous layers were accessed without issue: ['input_39', 'lstm_37']
已更新
谢谢@Marcin Możejko。我终于做到了。
x = Input((seq_len, input_dim))
lstm = LSTM(128, return_sequences=True, activation='tanh')(x)
td = TimeDistributed(Dense(out_size, activation='softmax'))(lstm)
second_input = Input((seq_len, out_size)) # object instanciated and hold as a var.
out = merge([td, second_input], mode='mul')
model = Model(input=[x, second_input], output=out) # second input provided to model.compile(...)
# then I add two inputs
model.fit([trainX, filter], trainY, ...)
人们可能会注意到,对由 Input((seq_len, out_size))
调用创建的对象的引用只能从 merge
函数调用环境访问。此外 - 它没有添加到 Model
定义中 - 是什么导致图形断开连接。您需要做的是:
x = Input((seq_len, input_dim))
lstm = LSTM(128, return_sequences=True, activation='tanh')(x)
td = TimeDistributed(Dense(out_size, activation='softmax'))(lstm)
second_input = Input((seq_len, out_size)) # object instanciated and hold as a var.
out = merge([td, second_input], mode='mul')
model = Model(input=[x, second_input], output=out) # second input provided to model