python - ValueError:使用不是符号张量的输入调用了层 activation_1
问题描述
from keras.layers import AveragePooling2D
from keras.models import Sequential
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers.core import Activation
from keras.layers.core import Flatten
from keras.layers.core import Dropout
from keras.layers.core import Dense
from keras import backend as K
class SmallerVGGNet:
@staticmethod
def build(width, height, depth, classes, finalAct="softmax"):
x = (height, width, depth)
output = -1
# CONV => RELU => POOL
x = (Conv2D(16, (3, 3), padding="same", input_shape=x))
x = (Activation("relu")(x))
x = (BatchNormalization(axis=output)(x))
x = (MaxPooling2D(pool_size=(3, 3))(x))
x = (Conv2D(32, (3, 3), padding="same")(x))
x = (Activation("relu")(x))
x = (BatchNormalization(axis=output)(x))
x = (MaxPooling2D(pool_size=(3, 3))(x))
x = (BatchNormalization(axis=output)(x))
# (CONV => RELU) * 2 => POOL
x = (Conv2D(64, (3, 3), padding="same")(x))
x = (Activation("relu")(x))
x = (BatchNormalization(axis=output)(x))
x = (Conv2D(64, (3, 3), padding="same")(x))
x = (Activation("relu")(x))
x = (BatchNormalization(axis=output)(x))
x = (AveragePooling2D(pool_size=(2, 2))(x))
# (CONV => RELU) * 2 => POOL
x = (Conv2D(128, (3, 3), padding="same")(x))
x = (Activation("relu")(x))
x = (BatchNormalization(axis=output)(x))
x = (Conv2D(128, (3, 3))(x))
x = (Activation("relu")(x))
x = (BatchNormalization(axis=output)(x))
x = (MaxPooling2D(pool_size=(2, 2))(x))
# first (and only) set of FC => RELU layers
x = (Flatten()(x))
x = (Dense(128)(x))
x = (Activation("relu")(x))
x = (BatchNormalization()(x))
x = (Dropout(0.5)(x))
# softmax classifier
x = (Dense(classes)(x))
x = (Activation(finalAct)(x))
x.summary()
# return the constructed network architecture
[enter image description here][2]
为什么当我运行它说的代码时会出现这种情况 层激活是使用不是符号张量的输入调用的。请帮我解决这个问题
ValueError:使用不是符号张量的输入调用层 activation_1。接收类型:. 完整输入:[]。该层的所有输入都应该是张量。
解决方案
在x = (Conv2D(16, (3, 3), padding="same", input_shape=x))
您没有给出任何输入的行中。因此,当您运行代码并到达x = (Activation("relu")(x))
x 是层而不是张量的行时,它会给出上述错误。因此,如评论中所述,您必须将输入传递给第一层。编辑后的代码如下(注意我使用的是 tensorflow.keras 库而不是 keras)
from tensorflow.compat.v1.keras.layers import AveragePooling2D
from tensorflow.compat.v1.keras.models import Sequential, Model
from tensorflow.compat.v1.keras.layers import Input, Dense, Dropout, Flatten, Activation, BatchNormalization, Conv2D, MaxPooling2D
from tensorflow.compat.v1.keras import backend as K
class SmallerVGGNet:
@staticmethod
def build(width, height, depth, classes, finalAct="softmax"):
x = (height, width, depth)
output = -1
# CONV => RELU => POOL
inputs = Input(shape=x)
x = (Conv2D(16, (3, 3), padding="same", input_shape=x)(inputs))
x = (Activation("relu")(x))
x = (BatchNormalization(axis=output)(x))
x = (MaxPooling2D(pool_size=(3, 3))(x))
x = (Conv2D(32, (3, 3), padding="same")(x))
x = (Activation("relu")(x))
x = (BatchNormalization(axis=output)(x))
x = (MaxPooling2D(pool_size=(3, 3))(x))
x = (BatchNormalization(axis=output)(x))
# (CONV => RELU) * 2 => POOL
x = (Conv2D(64, (3, 3), padding="same")(x))
x = (Activation("relu")(x))
x = (BatchNormalization(axis=output)(x))
x = (Conv2D(64, (3, 3), padding="same")(x))
x = (Activation("relu")(x))
x = (BatchNormalization(axis=output)(x))
x = (AveragePooling2D(pool_size=(2, 2))(x))
# (CONV => RELU) * 2 => POOL
x = (Conv2D(128, (3, 3), padding="same")(x))
x = (Activation("relu")(x))
x = (BatchNormalization(axis=output)(x))
x = (Conv2D(128, (3, 3))(x))
x = (Activation("relu")(x))
x = (BatchNormalization(axis=output)(x))
x = (MaxPooling2D(pool_size=(2, 2))(x))
# first (and only) set of FC => RELU layers
x = (Flatten()(x))
x = (Dense(128)(x))
x = (Activation("relu")(x))
x = (BatchNormalization()(x))
x = (Dropout(0.5)(x))
# softmax classifier
x = (Dense(classes)(x))
x = (Activation(finalAct)(x))
model = Model(inputs,x)
model.summary()
a = SmallerVGGNet()
a.build(100,100,100,10)
推荐阅读
- c++ - 循环帮助 While 循环不能正常工作
- c - 在 Nagios 中创建自定义变量
- amazon-web-services - 亚马逊 s3 每小时计费
- reactjs - React Hooks + window.addEventListener in useEffect
- javascript - 选择元素内的html嵌套脚本
- angular - mat-dialog 在页面加载时未正确打开
- c++ - C ++如何刷新标准输入
- javascript - 如何根据容器的宽度显示隐藏滑动图标(左右) - Javascript / ReactJs
- html - How to xpath text() greater than number 1?
- javascript - 在已从对象数组中过滤的对象内添加属性