gpt4 book ai didi

python - 如何将 Keras 中的多个输入的标签赋予 model.fit() 函数?

转载 作者:行者123 更新时间:2023-12-01 00:27:08 24 4
gpt4 key购买 nike

我正在使用 Keras 的功能 API。我想创建一个可以处理多个输入的模型,其中输入具有不同的标签。以下是我要创建的模型:

model

这是我尝试过的:

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() 函数中给出标签的正确方法是什么?

最佳答案

您的模型只有一个输出。这意味着只为每一对输入生成一个“标签”。使用标准损失函数,该标签只能与单个真实标签进行比较,因此每一对输入只能有一个标签。

要解决此问题,您有多种选择,但没有一个可以快速轻松地解决。

  1. 弄清楚如何将每对真实标签组合成一个标签,然后将这些组合标签传递给 fit()
  2. 修改您的模型以具有两个输出。
  3. 我还没有尝试过这一点,但您也许可以创建一个自定义损失函数,将每个输出标签与一对真实标签进行比较。

其中哪一个最有效取决于您的数据是什么,以及为什么尽管有两个标签但您只需要一个输出。

关于python - 如何将 Keras 中的多个输入的标签赋予 model.fit() 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58488172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com