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

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

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

  1. # first input model
  2. numbers_input = Input(shape=(,5,1))
  3. conv11 = Conv2D(32, kernel_size=4, activation='relu')(visible1)
  4. pool11 = MaxPooling2D(pool_size=(2, 2))(conv11)
  5. conv12 = Conv2D(16, kernel_size=4, activation='relu')(pool11)
  6. pool12 = MaxPooling2D(pool_size=(2, 2))(conv12)
  7. flat1 = Flatten()(pool12)
  8. # second input model
  9. visible2 = Input(shape=(32,32,3))
  10. conv21 = Conv2D(32, kernel_size=4, activation='relu')(visible2)
  11. pool21 = MaxPooling2D(pool_size=(2, 2))(conv21)
  12. conv22 = Conv2D(16, kernel_size=4, activation='relu')(pool21)
  13. pool22 = MaxPooling2D(pool_size=(2, 2))(conv22)
  14. flat2 = Flatten()(pool22)

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

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

gcmastyq

gcmastyq1#

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

  1. import numpy as np
  2. from keras.layers import Input, Conv2D, MaxPooling2D, Flatten
  3. from keras.models import Model
  4. # Numerical input data preprocessing
  5. numerical_input = np.random.random((44044, 10)) # Assuming you have numerical data of shape (44044, 10)
  6. numerical_input = numerical_input[:, 5:10] # Selecting columns 5 to 10
  7. numerical_input = numerical_input.reshape((-1, 1, 1, 5)) # Reshape to match Conv2D input shape
  8. # Boolean input data preprocessing
  9. boolean_input = np.random.randint(0, 2, (44044, 4)) # Assuming you have boolean data of shape (44044, 4)
  10. boolean_input = boolean_input.reshape((-1, 1, 4, 1)) # Reshape to match Conv2D input shape
  11. # First input model
  12. numbers_input = Input(shape=(1, 5, 1))
  13. conv11 = Conv2D(32, kernel_size=4, activation='relu')(numbers_input)
  14. pool11 = MaxPooling2D(pool_size=(2, 2))(conv11)
  15. conv12 = Conv2D(16, kernel_size=4, activation='relu')(pool11)
  16. pool12 = MaxPooling2D(pool_size=(2, 2))(conv12)
  17. flat1 = Flatten()(pool12)
  18. # Second input model
  19. boolean_input = Input(shape=(1, 4, 1))
  20. conv21 = Conv2D(32, kernel_size=4, activation='relu')(boolean_input)
  21. pool21 = MaxPooling2D(pool_size=(2, 2))(conv21)
  22. conv22 = Conv2D(16, kernel_size=4, activation='relu')(pool21)
  23. pool22 = MaxPooling2D(pool_size=(2, 2))(conv22)
  24. flat2 = Flatten()(pool22)
  25. # Merge the two input models
  26. merged = concatenate([flat1, flat2])
  27. # Rest of your model architecture
  28. # ...
  29. # Create the model
  30. model = Model(inputs=[numbers_input, boolean_input], outputs=output)

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

展开查看全部

相关问题