tensorflow CNN架构问题

tv6aics1  于 2023-01-13  发布在  其他
关注(0)|答案(1)|浏览(106)

我试图实现一个CNN架构,但是,输出的形状有一个问题。集合的形状如下:

x_train.shape, y_train.shape, x_test.shape, y_test.shape
((1203, 162, 1), (1203, 7), (402, 162, 1), (402, 7))

体系结构的设置如下:

input_x = tf.keras.layers.Input(shape = (x_train.shape[1],1))
conv_1 = tf.keras.layers.Conv1D(filters=16,kernel_size=3,padding="same",activation="relu")(input_x)
pool_1 = tf.keras.layers.MaxPooling1D(2)(conv_1)
conv_2 = tf.keras.layers.Conv1D(filters=32,kernel_size=3,padding="same",activation="relu")(pool_1)
pool_2  = tf.keras.layers.MaxPooling1D(2)(conv_2)

flatten = tf.keras.layers.Flatten()(pool_2)
dense = tf.keras.layers.Dense(512, activation="relu")(flatten)
fb = tf.keras.layers.Dropout(0.4)(dense)
fb = tf.keras.layers.Dense(512, activation="relu")(fb)
fb = tf.keras.layers.Dropout(0.4)(fb)

output = tf.keras.layers.Dense(8, activation="softmax")(fb)
model_branching_summed = tf.keras.models.Model(inputs=input_x, outputs=output)
model_branching_summed.summary()
model_branching_summed.compile(optimizer=SGD(learning_rate=0.01 , momentum=0.8), loss='categorical_crossentropy', metrics= ['accuracy'])

历史=模型_分支_求和.拟合(x_训练,y_训练,批次_大小= 128,时期= 100,验证_数据=(x_测试,y_测试),回调=[rlrp])
但是当我运行这个模型时,它给了我下面的错误:
值错误追溯(最近调用最后)单元格输入[192],第5行1 rlrp = ReduceLROnPlateau(监视器="丢失",因子= 0.4,详细信息= 0,耐心= 2,最小lr = 0.0001)2 #(最小lr = 0.000001)----〉5历史=模型分支求和拟合(x_训练,y_训练,批次大小= 128,时间点= 100,确认数据=(x_测试,y_测试),回调=[rlrp])
ValueError:形状(None,7)和(None,8)不兼容
有人能帮我知道错误在哪里吗?

wb1gzix0

wb1gzix01#

您看到有一个形状为(None,8)的输出层,但您正在尝试计算形状为(None,7)的y_train矩阵上的损耗。
尝试更改此行:
output = tf.keras.layers.Dense(8, activation="softmax")(fb)

output = tf.keras.layers.Dense(7, activation="softmax")(fb)

相关问题