检查目标时发生错误:预期density_具有3维,但数组的形状为(100,4) [英] Error when checking target: expected dense_ to have 3 dimensions, but got array with shape (100, 4)

查看:81
本文介绍了检查目标时发生错误:预期density_具有3维,但数组的形状为(100,4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2输入4输出的网络.我建立了一个时间步长= 5的LSTM模型.

I have a network with 2 inputs and 4 outputs. I have built an LSTM model with time step =5.

import numpy as np
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten, LSTMD
from keras.layers import Input, LSTM, concatenate, Dense, Lambda
from keras.models import Model
from sklearn.metrics import mean_squared_error
from keras.models import load_model
from keras.utils.vis_utils import plot_model
from tensorflow.keras import layers
import keras
from keras_self_attention import SeqSelfAttention
from tensorflow.keras.layers import Attention

以下是输入和输出:

X = np.random.normal(size=(100,5,2)) #  input 
Y = np.random.normal(size=(100,4))

这是我的模特:

model = keras.models.Sequential()
model.add(keras.layers.LSTM(units = 50, return_sequences=True, input_shape=(X.shape[1],X.shape[2])))
model.add(SeqSelfAttention(attention_width=50,attention_activation='linear',name='Attention'))
model.add(keras.layers.Dense(4,  activation='linear'))
model.compile(optimizer = 'adam', loss = 'mean_squared_error',metrics = ['MAE'])
model.summary()
model.fit( X, Y, epochs = 1, batch_size = 500)
model.save('model.h5')

运行模型时,出现此错误:

When I run the model, I've got this error:

Error when checking target: expected dense_56 to have 3 dimensions, but got array with shape (100,4)

有人可以帮助我吗?谢谢

Could anyone can help me? thanks

推荐答案

尝试一下:

model.add(keras.layers.LSTM(units = 50, input_shape=(X.shape[1],X.shape[2])))
model.add(SeqSelfAttention(attention_width=50,attention_activation='linear',name='Attention'))
model.add(keras.layers.Dense(4,  activation='linear'))

这篇关于检查目标时发生错误:预期density_具有3维,但数组的形状为(100,4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆