【问题标题】:Cannot convert a symbolic Keras input/output to a numpy array in a simple CNN无法将符号 Keras 输入/输出转换为简单 CNN 中的 numpy 数组
【发布时间】:2021-04-21 06:46:00
【问题描述】:

这是一个非常简单的问题,我无法解决。我是 tensorflow 新手,这是我第二次遇到这个问题。

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


x = tf.keras.Input(shape=(128, 128, 4))
conv = Conv2D(30, (3, 3), activation='relu',input_shape=(128, 128, 4))(x)
conv = Conv2D(12, (5,5))(conv)
conv = MaxPooling2D(pool_size=(2,2))(conv)
print(conv[2])
conv = np.array(conv[2]) # <---- here is the problem
input_mean = np.mean(conv[1:], axis=0)
input_std = np.std(conv, axis=0)
conv = (conv - input_mean) / input_std

conv = Flatten()(conv)
conv = Dense(157, activation='relu')(conv)
model = Model(inputs = x, outputs = conv)
#model.summary()

我得到的错误是,

Cannot convert a symbolic Keras input/output to a numpy array. This error may indicate that you're trying to pass a symbolic value to a NumPy call, which is not supported. Or, you may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model.

我的问题是,我将如何从我的 Maxpooling 层获取 输出 并获取每个传入通道的均值和标准差? mean 和 std 的输出将是一个张量,其中每个通道都被单独归一化。然后我会展平这个输出并将其发送到我的全连接密集层。

提前致谢。

【问题讨论】:

    标签: python tensorflow keras deep-learning conv-neural-network


    【解决方案1】:

    我遇到了类似的错误,我执行了以下操作:

    del model
    

    之前:

    model = Model(inputs = x, outputs = conv)
    

    它解决了我的问题。 我很想知道它是否也能解决你的问题:)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-17
      • 2021-03-29
      • 2021-11-01
      • 2021-11-26
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      相关资源
      最近更新 更多