python - 如何将 Keras 中多个输入的标签赋予 model.fit() 函数?
问题描述
我正在使用 Keras 的功能 API。我想创建一个适用于多个输入的模型,其中输入具有不同的标签。下面是我要创建的模型:
这是我尝试过的:
import numpy as np
from keras import backend as K
from keras import losses
from keras.models import Model
from keras.utils import to_categorical, plot_model
from keras.layers import Input, Dense, Conv1D, Flatten
from keras.layers.merge import concatenate
# Create data
NUM_TRAINING_SAMPLES_INPUT_1 = 100
NUM_TRAINING_SAMPLES_INPUT_2 = 100
NUM_FEATURES_INPUT_1 = 144
NUM_FEATURES_INPUT_2 = 145
NUM_CLASSES = 15
# Create train data
train_X_1 = np.random.randint(low=0, high=100, size=(NUM_TRAINING_SAMPLES_INPUT_1, NUM_FEATURES_INPUT_1, 1), dtype='int64')
train_X_2 = np.random.randint(low=0, high=100, size=(NUM_TRAINING_SAMPLES_INPUT_2, NUM_FEATURES_INPUT_2, 1), dtype='int64')
# Create labels
train_Y_1 = np.random.randint(low=0, high=NUM_CLASSES, size=(NUM_TRAINING_SAMPLES_INPUT_1, 1), dtype='int64')
train_Y_2 = np.random.randint(low=0, high=NUM_CLASSES, size=(1, 5), dtype='int64')
# Convert labels to categorical
train_Y_1 = to_categorical(train_Y_1, num_classes=NUM_CLASSES)
train_Y_2 = to_categorical(train_Y_2, num_classes=NUM_CLASSES)
# Create model architecture
input_1 = Input(shape=(train_X_1.shape[1], train_X_1.shape[2]))
convl1_input_1 = Conv1D(filters=96, kernel_size=12, strides=2, padding='valid', activation='relu')(input_1)
flat1 = Flatten()(convl1_input_1)
input_2 = Input(shape=(train_X_2.shape[1], train_X_2.shape[2]))
convl1_input_2 = Conv1D(filters=96, kernel_size=13, strides=2, padding='valid', activation='relu')(input_2)
flat2 = Flatten()(convl1_input_2)
# Merge inputs
merge = concatenate([flat1, flat2])
# interpretation model
hidden = Dense(10, activation='relu')(merge)
output = Dense(NUM_CLASSES, activation='sigmoid')(hidden)
model = Model(inputs=[input_1, input_2], outputs=output)
# Plot model
plot_model(model, to_file='multiple_inputs_dummy.png')
# Compile model
model.compile(loss=losses.categorical_crossentropy, optimizer='adam', metrics=["accuracy"])
# Fit model
BATCH_SIZE = 32
EPOCHS = 3
SHUFFLE = False
VERBOSE = 1
model.fit([train_X_1, train_X_2], [train_Y_1, train_Y_2], epochs=EPOCHS, batch_size=BATCH_SIZE, verbose=VERBOSE, shuffle=SHUFFLE)
这给了我以下错误,
ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 arrays but instead got the following list of 2 arrays: [array([[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
...,
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0...
在功能中给出标签的正确方法是什么model.fit()
?
解决方案
您的模型只有一个输出。这意味着每对输入只产生一个“标签”。使用标准损失函数,这个标签只能与单个真实标签进行比较,因此每对输入只能有一个标签。
要解决此问题,您有多种选择,但没有一个是快速简便的解决方法。
- 弄清楚如何将每对真实标签组合成一个标签,然后将这些组合标签传递给
fit()
. - 修改您的模型以具有两个输出。
- 我还没有尝试过,但您也许可以制作一个自定义损失函数,将每个输出标签与一对真实标签进行比较。
其中哪一个效果最好取决于您的数据是什么,以及为什么尽管有两个标签,但您只需要一个输出。
推荐阅读
- ios - SWIFT 5 - 调用“显示”方法更改情节提要后导航栏消失
- google-cloud-platform - 如何以编程方式为 Google Cloud 项目启用身份和访问管理 (IAM) API?
- wordpress - Navwalker 不接受菜单中定义的 CSS 类
- javascript - JavaScript 继承:在 HTML 文档中调用原型对象 - 如何使用模块
- mono - 如何使用 mono.cecil 注入 HTTP 调用
- javascript - 在Javascript中读取CSV文件并将名称写入一个数组并将值写入另一个数组
- amazon-web-services - 如何从 Azure Devops 连接 Amazon RDS SQL 以实现数据库自动化
- java - Spring OAuth2 从访问令牌字符串中提取主体
- excel - 用VBA提取趋势线斜率
- javascript - 如何将数据写入一个csv文件?