我正在尝试将CNN和LSTM结合起来进行图像分类。
我尝试了以下代码,但出现错误。我有4个课程要训练和测试。
以下是代码:
from keras.models import Sequential
from keras.layers import LSTM,Conv2D,MaxPooling2D,Dense,Dropout,Input,Bidirectional,Softmax,TimeDistributed
input_shape = (200,300,3)
Model = Sequential()
Model.add(TimeDistributed(Conv2D(
filters=16, kernel_size=(12, 16), activation='relu', input_shape=input_shape)))
Model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2),strides=2)))
Model.add(TimeDistributed(Conv2D(
filters=24, kernel_size=(8, 12), activation='relu')))
Model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2),strides=2)))
Model.add(TimeDistributed(Conv2D(
filters=32, kernel_size=(5, 7), activation='relu')))
Model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2),strides=2)))
Model.add(Bidirectional(LSTM((10),return_sequences=True)))
Model.add(Dense(64,activation='relu'))
Model.add(Dropout(0.5))
Model.add(Softmax(4))
Model.compile(loss='sparse_categorical_crossentropy',optimizer='adam')
Model.build(input_shape)
我遇到以下错误:
“输入张量必须为3、4或5,但为{}。”。format(n + 2)) ValueError:输入张量必须为3、4或5,但必须为2。
答案 0 :(得分:2)
我在代码中发现了很多问题:
<script>
var content = `<p>bla bla</p><script src="https://example"></script>`; //Uncaught SyntaxError: Unexpected end of input -> for: </script>
<script>
就可以了,不需要Conv2D
TimeDistributed
return_sequences=False
而不是categorical_crossentropy
,因为您的目标是单次编码的此处是完整的示例示例:
sparse_categorical_crossentropy