【问题标题】:Keras Network with 2d Array具有二维阵列的 Keras 网络
【发布时间】:2020-04-21 22:34:50
【问题描述】:

我有以下输入数组,我想将其提供给 CNN

import numpy as np 
from keras.layers import Input, Dense, Conv2D
#dummy data for 1d array
width = 5
levels = 4
inputArray =np.random.randint(low=0, high=levels, size=20)
inputArray_ = inputArray.reshape(-1,width) #reshape to 2d array

网络的第一层是:

x = Conv2D(16, (3, 3), activation='relu', padding='same')(inputArray_)

我收到以下错误:

ValueError: Layer conv2d_13 was called with an input that isn't a symbolic tensor.   Received type: <class 'numpy.ndarray'>. Full input: [array([[3, 1, 1, 1, 1],
   [0, 1, 3, 1, 3],
   [0, 0, 2, 0, 0],
   [1, 3, 2, 2, 0],
   [1, 1, 2, 1, 1],
   [0, 3, 1, 3, 0],
   [3, 1, 2, 3, 2],
   [1, 2, 0, 2, 2],
   [3, 2, 2, 1, 0],
   [1, 2, 1, 3, 1],
   [0, 3, 2, 3, 0],
   [0, 3, 1, 0, 0],
   [2, 2, 0, 0, 2],
   [2, 2, 1, 0, 1],
   [3, 3, 0, 3, 1],
   [0, 0, 3, 1, 0],
   [1, 3, 1, 2, 2],
   [1, 0, 3, 2, 2],
   [3, 1, 2, 1, 2],
   [3, 0, 3, 3, 1]])]. All inputs to the layer should be tensors.

【问题讨论】:

    标签: python keras keras-layer


    【解决方案1】:

    您需要将数组转换为张量。

    import numpy as np
    import tensorflow.keras.backend as K
    #dummy data for 1d array
    width = 5
    levels = 4
    inputArray =np.random.randint(low=0, high=levels, size=20)
    inputArray_ = inputArray.reshape(-1,width)
    print(type(inputArray_))
    
    
    
    inputTensor_ = K.constant(inputArray_)
    
    print(type(inputTensor_))
    print(inputTensor_)
    

    【讨论】:

    • 这会返回以下错误 ValueError: Input 0 is incompatible with layer conv2d_15: expected ndim=4, found ndim=2
    【解决方案2】:

    您应该将Input 层而不是inputArray_ 传递给Conv2D 层。在定义神经网络的架构时,我们只传递占位符而不传递实际值。

    inputArray =np.random.randint(low=0, high=levels, size=20)
    inputArray_ = inputArray.reshape(-1,width, 1) #Conv2D accepts 3D array
    
    input_data = Input(name='the_input', shape=(None, width, 1), dtype='float32')
    x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_data) 
    

    【讨论】:

    • 使用如下布局:x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_data) x = MaxPooling2D((2, 2), padding='same')(x) x = Conv2D(8, (3, 3), activation='relu', padding='same')(x) x = MaxPooling2D((2, 2), padding='same ')(x) x = Conv2D(8, (3, 3), activation='relu', padding='same')(x) 编码 = MaxPooling2D((2, 2), padding='same')(x ) encoder = Model(inputArray_, encrypted) inputArray_ 给出以下错误:TypeError: unhashable type: 'numpy.ndarray'
    • 现在是另一个错误。您正在向模型传递不正确的输入。我建议您查看 keras 文档中的示例,它们非常容易理解:keras.io/getting-started/functional-api-guide
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 2019-11-08
    相关资源
    最近更新 更多