Keras 2D输出

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Keras 2D输出相关的知识,希望对你有一定的参考价值。

我需要制作一个模型,以2D二进制矩阵作为输入:(37,10),并返回一个与输入形状相同的真实2D矩阵。我写了这段代码,但是不确定(在输出层中)X应该等于什么。

model=Sequential()
model.add(Dense(32,activation='linear',input_shape=(37,10)))
model.add(Dense(32,activation='linear'))
model.add(Dense(X,activation='linear'))
model.compile(loss='mse',optimizer=Adam(lr=self.learning_rate),metrics=['accuracy'])

请让我知道,如果您认为我的模型正确无误,请写些什么,而不是X

谢谢

答案

[X将为10,即使最初将FC层用于2维数据可能不太适合,而且您也可以确保指标是准确的。

这是您的模型,输出形状正确。

from tensorflow.keras.layers import *
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.optimizers import Adam
import tensorflow as tf
import numpy as np

model=Sequential()
model.add(Dense(32,activation='linear',input_shape=(37,10)))
model.add(Dense(32,activation='linear'))
model.add(Dense(10,activation='linear'))
model.compile(loss='mse',optimizer=Adam(lr=.001),metrics=['accuracy'])
model.summary()
Model: "sequential_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_8 (Dense)              (None, 37, 32)            352       
_________________________________________________________________
dense_9 (Dense)              (None, 37, 32)            1056      
_________________________________________________________________
dense_10 (Dense)             (None, 37, 10)            330       
=================================================================
Total params: 1,738
Trainable params: 1,738
Non-trainable params: 0
__________________________
另一答案

我更新了您的代码,以得到与输入相同的输出形状。我们需要在模型的开头和结尾处添加FlattenReshape层。简单来说,X应该等于input_shape中的元素数。

from tensorflow.keras.layers import Dense, Flatten,Reshape
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam

input_shape=(37,10)
num_elm =input_shape[0]*input_shape[1]
model=Sequential()
model.add(Flatten(input_shape=input_shape))
model.add(Dense(32, activation='linear'))
model.add(Dense(32, activation='linear'))
model.add(Dense(num_elm, activation='linear'))
model.add(Reshape(input_shape))
model.compile(loss='mse',optimizer=Adam(),metrics=['accuracy'])

model.summary()

Model: "sequential_5"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten_4 (Flatten)          (None, 370)               0         
_________________________________________________________________
dense_14 (Dense)             (None, 32)                11872     
_________________________________________________________________
dense_15 (Dense)             (None, 32)                1056      
_________________________________________________________________
dense_16 (Dense)             (None, 370)               12210     
_________________________________________________________________
reshape (Reshape)            (None, 37, 10)            0         
=================================================================
Total params: 25,138
Trainable params: 25,138
Non-trainable params: 0
_________________________________________________________________

以上是关于Keras 2D输出的主要内容,如果未能解决你的问题,请参考以下文章

Keras Conv2D 和输入通道

keras 中的 Flatten() 和 GlobalAveragePooling2D() 有啥区别

Keras功能API的语法

返回状态Keras-多变量输出

Keras中Conv2D和Convolution2D的区别

分割层输出时,Keras 抛出“Tensor”对象没有属性“_keras_shape”