import tensorflow as tf import numpy as np from keras import Model in_flow= np.load("X_in_30od.npy") out_flow= np.load("X_out_30od.npy") c1 = np.load("X_30od.npy") D1 = np.load("Y_30od.npy") print(c1.shape) print(D1.shape) max=np.max(out_flow) train_in_flow=in_flow[0:200]/max val_in_flow=in_flow[200:260]/max test_in_flow=out_flow[260:]/max train_out_flow=out_flow[0:200]/max val_out_flow=out_flow[200:260]/max test_out_flow=out_flow[260:]/max train_c1=c1[0:200]/max val_c1=c1[200:260]/max test_c1=c1[260:]/max train_D1=D1[0:200]/max val_D1=D1[200:260]/max test_D1=D1[260:]/max print(train_c1.shape, train_in_flow.shape, train_in_flow.shape, train_D1.shape) from keras.layers import * input_od=Input(shape=(5,109,109)) x1=Reshape((5,109,109,1),input_shape=(5,109,109))(input_od) x1=ConvLSTM2D(filters=64,kernel_size=(3,3),activation='relu',padding='same',input_shape=(5,109,109,1))(x1) x1=Dropout(0.2)(x1) x1=Dense(1)(x1) x1=Reshape((109,109))(x1) input_inflow=Input(shape=(5,109)) x2=Permute((2,1))(input_inflow) x2=LSTM(109,return_sequences=True,activation='sigmoid')(x2) x2=Dense(109,activation='sigmoid')(x2) x2=tf.multiply(x1,x2) x2=Dense(109,activation='sigmoid')(x2) input_inflow2=Input(shape=(5,109)) x3=Permute([2,1])(input_inflow2) x3=LSTM(109,return_sequences=True,activation='sigmoid')(x3) x3=Dense(109,activation='sigmoid')(x3) x3 = Reshape((109, 109))(x3) x3=tf.multiply(x1,x3) x3=Dense(109,activation='sigmoid')(x3) mix=Add()([x2,x3]) mix=Bidirectional(LSTM(109,return_sequences=True,activation='sigmoid'))(mix) mix=Dense(109,activation='sigmoid')(mix) model= Model(inputs=[input_od,input_inflow,input_inflow2],outputs=[mix]) model.compile(optimizer='adam', loss='mean_squared_error') history = model.fit([train_c1, train_in_flow,train_in_flow ],train_D1, validation_data=([val_c1,val_out_flow, val_in_flow], val_D1), epochs=100, batch_size=32) model.save("my_model.h10032") model.save_weights("my_model_weights.h10032") 根据上述程序利用保持好的模型预测并将预测结果可视化输出
时间: 2024-03-21 21:38:14 浏览: 103
以下是根据您提供的代码进行预测和可视化输出的示例代码:
```
import tensorflow as tf
import numpy as np
from keras import Model
from keras.models import load_model
import matplotlib.pyplot as plt
# 加载模型
model = load_model("my_model.h10032")
# 加载数据
in_flow = np.load("X_in_30od.npy")
out_flow = np.load("X_out_30od.npy")
c1 = np.load("X_30od.npy")
D1 = np.load("Y_30od.npy")
max_val = np.max(out_flow)
# 对输入数据进行预处理
test_in_flow = in_flow[260:] / max_val
test_out_flow = out_flow[260:] / max_val
test_c1 = c1[260:] / max_val
# 进行预测
pred_D1 = model.predict([test_c1, test_out_flow, test_in_flow])
# 可视化输出
plt.figure(figsize=(10, 10))
for i in range(5):
plt.subplot(3, 2, i+1)
plt.imshow(pred_D1[i], cmap='jet')
plt.title("Prediction of OD flow at time step " + str(i+1))
plt.colorbar()
plt.show()
```
注意,这里只输出了前5个时间步的预测结果,您可以根据需要进行修改。另外,这里使用了jet配色方案进行可视化,您可以根据需要进行修改。
阅读全文