keras Timedistributed lstm+cnn

# univariate cnn-lstm example
from numpy import array
from keras.models import Sequential
from keras.layers import LSTM
from keras.models import Model
from keras.layers import Flatten
from keras.layers import TimeDistributed
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
from keras.layers import Input, Add, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten,Conv2D, AveragePooling2D, MaxPooling2D, GlobalMaxPooling2D
# define dataset
X = array([[10, 20, 30, 40], [20, 30, 40, 50], [30, 40, 50, 60], [40, 50, 60, 70]])
y = array([50, 60, 70, 80])
# reshape from [samples, timesteps] into [samples, subsequences, timesteps, features]
X = X.reshape((X.shape[0], 2, 2, 1))
# define model

def cnn():
    input_shape = (2, 2, 1)
    X_input = Input(input_shape)
    
    X=TimeDistributed(Conv1D(filters=64, kernel_size=1, activation='relu'), input_shape=(2, 2, 1))(X_input)
    X=TimeDistributed(MaxPooling1D(pool_size=2))(X)
    X=TimeDistributed(Flatten())(X)
#    X = Dense(5, activation = 'softmax', name = 'fc' + str(5))(X)
    X=LSTM(50, activation='relu')(X)
    X=Dense(1)(X)
    
    model = Model(inputs = X_input, outputs = X, name = 'ResNet50')
    return model


model=cnn()
model.compile(optimizer='adam', loss='mse')
model.fit(X, y, epochs=500, verbose=0)
x_input = array([50, 60, 70, 80])
x_input = x_input.reshape((1, 2, 2, 1))
yhat = model.predict(x_input, verbose=0)
print(yhat)
[[ 93.35614777]]
posted @   luoganttcc  阅读(379)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示