#!/usr/bin/python
# -*- coding:utf-8 -*-
# @Author: DingQinbin
# @Time : 2020/10/26 9:39
import numpy as np
from keras.layers.core import Dense, Activation, Dropout
from keras.layers import LSTM
from keras.models import Sequential, load_model
from keras.callbacks import Callback
import keras.backend.tensorflow_backend as KTF
import tensorflow as tf
import pandas as pd
import os
import keras.callbacks
import matplotlib.pyplot as plt
# 设定为自增长
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config)
KTF.set_session(session)
def create_dataset(data, n_predictions, n_next):
'''
对数据进行处理
'''
dim = data.shape[1]
train_X, train_Y = [], []
for i in range(data.shape[0] - n_predictions - n_next - 1):
a = data[i:(i + n_predictions), :]
train_X.append(a)
tempb = data[(i + n_predictions):(i + n_predictions + n_next), :]
b = []
for j in range(len(tempb)):
for k in range(dim):
b.append(tempb[j, k])
train_Y.append(b)
train_X = np.array(train_X, dtype='float64')
train_Y = np.array(train_Y, dtype='float64')
test_X, test_Y = [], []
i = data.shape[0] - n_predictions - n_next - 1
a = data[i:(i + n_predictions), :]
test_X.append(a)
tempb = data[(i + n_predictions):(i + n_predictions + n_next), :]
b = []
for j in range(len(tempb)):
for k in range(dim):
b.append(tempb[j, k])
test_Y.append(b)
test_X = np.array(test_X, dtype='float64')
test_Y = np.array(test_Y, dtype='float64')
return train_X, train_Y, test_X, test_Y
def NormalizeMult(data, set_range):
'''
返回归一化后的数据和最大最小值
'''
normalize = np.arange(2 * data.shape[1], dtype='float64')
normalize = normalize.reshape(data.shape[1], 2)
for i in range(0, data.shape[1]):
if set_range == True:
list = data[:, i]
listlow, listhigh = np.percentile(list, [0, 100])
else:
if i == 0:
listlow = -90
listhigh = 90
else:
listlow = -180
listhigh = 180
normalize[i, 0] = listlow
normalize[i, 1] = listhigh
delta = listhigh - listlow
if delta != 0:
for j in range(0, data.shape[0]):
data[j, i] = (data[j, i] - listlow) / delta
return data, normalize
def trainModel(train_X, train_Y):
'''
trainX,trainY: 训练LSTM模型所需要的数据
'''
model = Sequential()
model.add(LSTM(
120,
input_shape=(train_X.shape[1], train_X.shape[2]),
return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(
120,
return_sequences=False))
model.add(Dropout(0.3))
model.add(Dense(
train_Y.shape[1]))
model.add(Activation("relu"))
model.compile(loss='mse', optimizer='adam', metrics=['acc'])
model.fit(train_X, train_Y, epochs=100, batch_size=64, verbose=1)
model.summary()
return model
if __name__ == "__main__":
train_num = 6
per_num = 1
# set_range = False
set_range = True
# 读入时间序列的文件数据
data = pd.read_csv('20080403010747.txt', sep=',').iloc[:, 0:2].values
print("样本数:{0},维度:{1}".format(data.shape[0], data.shape[1]))
# print(data)
# 画样本数据库
plt.scatter(data[:, 1], data[:, 0], c='b', marker='o', label='traj_A')
plt.legend(loc='upper left')
plt.grid()
plt.show()
# 归一化
data, normalize = NormalizeMult(data, set_range)
# print(normalize)
# 生成训练数据
train_X, train_Y, test_X, test_Y = create_dataset(data, train_num, per_num)
print("x\n", train_X.shape)
print("y\n", train_Y.shape)
# 训练模型
model = trainModel(train_X, train_Y)
loss, acc = model.evaluate(train_X, train_Y, verbose=2)
print('Loss : {}, Accuracy: {}'.format(loss, acc * 100))
# 保存模型
np.save("./traj_model_trueNorm.npy", normalize)
model.save("./traj_model_120.h5")
yava_free
- 粉丝: 5181
- 资源: 1980
最新资源
- mmexport1735633332505.png
- 【Web网页设计制作-毕业设计期末大作业源码】全屏大气的工业企业网站html5动画模板.zip
- 【Web网页设计制作-毕业设计期末大作业源码】七彩背景图库摄影网页模板5616.zip
- 【Web网页设计制作-毕业设计期末大作业源码】平面设计师个人案例展示html5模板5412.zip
- 【Web网页设计制作-毕业设计期末大作业源码】全屏大图背景App开发者服务网站模板4987.zip
- 【Web网页设计制作-毕业设计期末大作业源码】名片印刷制作公司企业网站模板5028.zip
- 【Web网页设计制作-毕业设计期末大作业源码】全屏大图复古艺术插画整站模板5013.zip
- 【Web网页设计制作-毕业设计期末大作业源码】墨蓝色设计案例展示响应式企业模板5171.zip
- 【Web网页设计制作-毕业设计期末大作业源码】墨绿色时尚家居装修企业网页模板5225.zip
- HTML5实现好看的cosplay拍照摄影网站源码.zip
- HTML5实现好看的App UI设计公司网站模板.zip
- HTML5实现好看的app产品开发软件网页模板.zip
- HTML5实现好看的IT科技公司响应式模板.zip
- HTML5实现好看的IOS、Android手机App推广网站模板.zip
- HTML5实现好看的IT电子产品商城网页源码.zip
- HTML5实现好看的Pizza美食响应式网站源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈