简体   繁体   English

CNN 架构问题

[英]Issue With CNN architecture

I'm trying to implement a CNN architecture, but, there are an issue with the shapes of the output. The shapes of the sets are as follow:我正在尝试实现 CNN 架构,但是 output 的形状存在问题。集合的形状如下:

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

The architecture's setting is as follows:架构的设置如下:

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'])

history=model_branching_summed.fit(x_train, y_train, batch_size=128, epochs=100, validation_data=(x_test, y_test), callbacks=[rlrp]) history=model_branching_summed.fit(x_train, y_train, batch_size=128, epochs=100, validation_data=(x_test, y_test), 回调=[rlrp])

But when I run the model, it give me the follow error:但是当我运行 model 时,它给我以下错误:

ValueError Traceback (most recent call last) Cell In[192], line 5 1 rlrp = ReduceLROnPlateau(monitor='loss', factor=0.4, verbose=0, patience=2,min_lr=0.0001) 2 #(min_lr=0.000001) ----> 5 history=model_branching_summed.fit(x_train, y_train, batch_size=128, epochs=100, validation_data=(x_test, y_test), callbacks=[rlrp]) ValueError Traceback(最后一次调用)Cell In[192],第 5 行 1 rlrp = ReduceLROnPlateau(monitor='loss', factor=0.4, verbose=0, patience=2,min_lr=0.0001) 2 #(min_lr=0.000001) ----> 5 history=model_branching_summed.fit(x_train, y_train, batch_size=128, epochs=100, validation_data=(x_test, y_test), 回调=[rlrp])

ValueError: Shapes (None, 7) and (None, 8) are incompatible ValueError:形状(无,7)和(无,8)不兼容

Can someone help me to know where is the error?有人可以帮我知道错误在哪里吗?

You seen to have an output layer in the shape of (None, 8) but you are trying to calculate loss on a y_train matrix of shape (None, 7).您看到有一个形状为 (None, 8) 的 output 层,但您正在尝试计算形状为 (None, 7) 的y_train矩阵的损失。

Try changing this line:尝试更改此行:

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM