简体   繁体   English

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

[英]How to give labels of multiple inputs in Keras to model.fit() function?

I am using functional API of Keras.我正在使用 Keras 的功能 API。 I want to create a model that will work on multiple inputs where inputs have different labels.我想创建一个 model 将在输入具有不同标签的多个输入上工作。 Below is the model that I want to create:下面是我要创建的 model:

模型

Here is what I have tried:这是我尝试过的:

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)

This gives me the below error,这给了我以下错误,

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...

What is the proper way of giving labels in model.fit() function?model.fit() function 中给出标签的正确方法是什么?

Your model only has one output.您的 model 只有一个 output。 That means that only one "label" is produced for each pair of inputs.这意味着每对输入只产生一个“标签”。 Using standard loss functions, this label can only be compared to a single ground truth label, so each pair of inputs can only have one label.使用标准损失函数,这个 label 只能与单个 ground truth label 进行比较,因此每对输入只能有一个 label。

To fix this, you have several options, but none are a quick and easy fix.要解决此问题,您有多种选择,但没有一个是快速简便的解决方法。

  1. Figure out how to combine each pair of ground truth labels into a single label, and then pass those combined labels into fit() .弄清楚如何将每对真实标签组合成一个 label,然后将这些组合标签传递给fit()
  2. Modify your model to have two outputs.修改您的 model 以具有两个输出。
  3. I haven't tried this, but you might be able to make a custom loss function that compares each output label to a pair of ground truth labels.我还没有尝试过,但是您可以自定义损失 function 将每个 output label 与一对基本事实标签进行比较。

Which one of these works best depends on what your data is, and why you want only a single output despite having two labels.其中哪一个最有效取决于您的数据是什么,以及为什么您只需要一个 output 尽管有两个标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Keras:model.fit()在siamese_model中出现多个输入错误 - Keras: model.fit() getting error for multiple inputs in siamese_model 在 Keras model.fit 中将训练数据指定为元组 (x, y) 的正确方法,具有多个输入和输出 - Correct way to specify training data as tuple (x, y) in Keras model.fit with multiple inputs and outputs 在Keras中调用model.fit输入不同形状的输入吗? - Call model.fit in Keras for inputs of different shapes? In Tensorflow.keras 2.0, when a model has multiple outputs, how to define a flexible loss function for model.fit()? - In Tensorflow.keras 2.0, when a model has multiple outputs, how to define a flexible loss function for model.fit()? model.fit() Keras 分类多输入-单输出给出错误:AttributeError: 'NoneType' 对象没有属性 'fit' - model.fit() Keras Classification Multiple Inputs-Single Output gives error: AttributeError: 'NoneType' object has no attribute 'fit' Keras model.fit UnboundLocalError - Keras model.fit UnboundLocalError 带有 Model.Fit() 的 Keras InvalidArgumentError - Keras InvalidArgumentError With Model.Fit() 我可以在 keras 中多次调用 model.fit 吗? - Can i call model.fit multiple times in keras? keras model.fit函数需要哪种数据格式? - Which data format does keras model.fit function need? 为什么keras中的model.fit函数会显着增加RAM内存? - Why model.fit function in keras significantly increase RAM memory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM