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
__________________________
另一答案
我更新了您的代码,以得到与输入相同的输出形状。我们需要在模型的开头和结尾处添加Flatten
和Reshape
层。简单来说,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输出的主要内容,如果未能解决你的问题,请参考以下文章