import numpy as np import matplotlib.pyplot as plt # 生成sin函数数据 import pip import pydot x = np.arange(0, 2*np.pi, 0.1) y = np.sin(x) # 可视化sin函数 plt.plot(x, y) plt.show() from keras.models import Sequential from keras.layers import Dense, SimpleRNN # 准备数据 dataX, dataY = [], [] for i in range(len(y)-1): dataX.append(y[i:i+1]) dataY.append(y[i+1]) dataX = np.array(dataX) dataY = np.array(dataY) # 划分训练集和测试集 train_size = int(len(dataY) * 0.7) test_size = len(dataY) - train_size trainX, testX = np.array(dataX[0:train_size]), np.array(dataX[train_size:len(dataX)]) trainY, testY = np.array(dataY[0:train_size]), np.array(dataY[train_size:len(dataY)]) # 调整输入数据的形状 trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1])) testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1])) # 定义模型结构 model = Sequential() model.add(SimpleRNN(units=10, input_shape=(1, 1))) model.add(Dense(units=1)) # 编译模型 model.compile(optimizer='adam', loss='mse') # 训练模型 history = model.fit(trainX, trainY, epochs=100, validation_data=(testX, testY)) # 可视化损失函数 plt.plot(history.history['loss']) plt.plot(history.history['val_loss']) plt.title('Model Loss') plt.ylabel('Loss') plt.xlabel('Epoch') plt.legend(['Train', 'Test'], loc='upper right') plt.show() #预测结果 trainPredict = model.predict(trainX) testPredict = model.predict(testX) # 可视化预测结果 plt.plot(y) plt.plot(np.concatenate((trainPredict, testPredict))) plt.show()有哪些模型可视化
时间: 2024-03-25 10:39:16 浏览: 72
import numpy as np import matplotlib.pyplot as plt import math
可以使用TensorBoard来可视化模型和训练过程。在代码中添加以下代码即可:
```
from keras.callbacks import TensorBoard
tensorboard = TensorBoard(log_dir='./logs', histogram_freq=0, write_graph=True, write_images=False)
```
然后在训练模型时添加callbacks参数:
```
history = model.fit(trainX, trainY, epochs=100, validation_data=(testX, testY), callbacks=[tensorboard])
```
在训练完成后,可以在命令行中运行以下命令来启动TensorBoard:
```
tensorboard --logdir=./logs
```
然后在浏览器中打开生成的链接即可查看可视化结果。
阅读全文