我应该在Keras Functional API中使用哪些输入参数?

fae0ux8s  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(95)

我正在尝试使用函数式API为Keras编写机器学习代码。我不知道应该使用什么输入参数。

# first input model
numbers_input = Input(shape=(,5,1))
conv11 = Conv2D(32, kernel_size=4, activation='relu')(visible1)
pool11 = MaxPooling2D(pool_size=(2, 2))(conv11)
conv12 = Conv2D(16, kernel_size=4, activation='relu')(pool11)
pool12 = MaxPooling2D(pool_size=(2, 2))(conv12)
flat1 = Flatten()(pool12)
# second input model
visible2 = Input(shape=(32,32,3))
conv21 = Conv2D(32, kernel_size=4, activation='relu')(visible2)
pool21 = MaxPooling2D(pool_size=(2, 2))(conv21)
conv22 = Conv2D(16, kernel_size=4, activation='relu')(pool21)
pool22 = MaxPooling2D(pool_size=(2, 2))(conv22)
flat2 = Flatten()(pool22)

字符串
它使用两种类型的输入,数字和布尔(1和0)。如何在此代码中使用选定的行和列?数值输入是从行1到行44044和列5到10。布尔型输入的范围是从行1到行44044和列1到4。
我尝试使用以下语法

# first input model
numbers_input = Input(shape=(1:44044,5:10,1))
conv11 = Conv2D(32, kernel_size=4, activation='relu')(visible1)
pool11 = MaxPooling2D(pool_size=(2, 2))(conv11)
conv12 = Conv2D(16, kernel_size=4, activation='relu')(pool11)
pool12 = MaxPooling2D(pool_size=(2, 2))(conv12)
flat1 = Flatten()(pool12)
# second input model
visible2 = Input(shape=(1:44044,1:4,3))
conv21 = Conv2D(32, kernel_size=4, activation='relu')(visible2)
pool21 = MaxPooling2D(pool_size=(2, 2))(conv21)
conv22 = Conv2D(16, kernel_size=4, activation='relu')(pool21)
pool22 = MaxPooling2D(pool_size=(2, 2))(conv22)
flat2 = Flatten()(pool22)

gcmastyq

gcmastyq1#

在Keras Functional API中,Input shape参数需要定义输入数据结构的元组。“但是,不能直接在形状参数中指定值的范围。相反,您需要在将输入数据提供给模型之前,预先行程输入数据,以选择所需的数据列和数据行。
以下是修改代码以选择特定行和列的方法:

import numpy as np
from keras.layers import Input, Conv2D, MaxPooling2D, Flatten
from keras.models import Model

# Numerical input data preprocessing
numerical_input = np.random.random((44044, 10))  # Assuming you have numerical data of shape (44044, 10)
numerical_input = numerical_input[:, 5:10]  # Selecting columns 5 to 10
numerical_input = numerical_input.reshape((-1, 1, 1, 5))  # Reshape to match Conv2D input shape

# Boolean input data preprocessing
boolean_input = np.random.randint(0, 2, (44044, 4))  # Assuming you have boolean data of shape (44044, 4)
boolean_input = boolean_input.reshape((-1, 1, 4, 1))  # Reshape to match Conv2D input shape

# First input model
numbers_input = Input(shape=(1, 5, 1))
conv11 = Conv2D(32, kernel_size=4, activation='relu')(numbers_input)
pool11 = MaxPooling2D(pool_size=(2, 2))(conv11)
conv12 = Conv2D(16, kernel_size=4, activation='relu')(pool11)
pool12 = MaxPooling2D(pool_size=(2, 2))(conv12)
flat1 = Flatten()(pool12)

# Second input model
boolean_input = Input(shape=(1, 4, 1))
conv21 = Conv2D(32, kernel_size=4, activation='relu')(boolean_input)
pool21 = MaxPooling2D(pool_size=(2, 2))(conv21)
conv22 = Conv2D(16, kernel_size=4, activation='relu')(pool21)
pool22 = MaxPooling2D(pool_size=(2, 2))(conv22)
flat2 = Flatten()(pool22)

# Merge the two input models
merged = concatenate([flat1, flat2])

# Rest of your model architecture
# ...

# Create the model
model = Model(inputs=[numbers_input, boolean_input], outputs=output)

字符串
在上面的代码中,我假设您的数字输入数据的大小为(44044,10),而您的布尔输入数据的大小为(44044,4)。您可以将这些值替换为实际的数据大小。

相关问题