colab配合谷歌云盘使用
COLA
🥤 COLA: Clean Object-oriented & Layered Architecture
项目地址:https://gitcode.com/gh_mirrors/col/COLA
免费下载资源
·
首先你得有一个谷歌账号!!!
参考: https://zhuanlan.zhihu.com/p/149233850
https://blog.csdn.net/lumingha/article/details/104825702
1 将数据传送至谷歌云盘(云盘地址:https://drive.google.com/drive/my-drive)
创建文件夹 上传数据到stock_data
2 跳转到colab(新建—>更多–> google Colaboratory)
3 挂载网盘+ 切换路径
from google.colab import drive
import os
# 挂载网盘
drive.mount('/content/drive/')
# 切换路径
os.chdir('/content/drive/MyDrive/rnn')
4. 查看文件
!ls -ll # 和linux终端用法差不多 但是!(感叹号)不能少
5. 执行代码
我的代码在本地是可以运行的
直接粘贴进去进行了,代码如下(这里用的是py文件,最好使用ipy)
# -*- coding: utf-8 -*-
import datetime
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dropout, Dense, SimpleRNN
import matplotlib.pyplot as plt
import os
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error, mean_absolute_error
import math
# 归一化
sc = MinMaxScaler(feature_range=(0, 1)) # 定义归一化:归一化到(0,1)之间
def get_stock_data(file_path):
maotai = pd.read_csv(file_path)
training_set = maotai.iloc[0:2426 - 300, 2:3].values
test_set = maotai.iloc[2426 - 300:, 2:3].values
training_set_scaled = sc.fit_transform(training_set)
test_set_scaled = sc.transform(test_set)
x_train = []
y_train = []
for i in range(60, len(training_set_scaled)):
x_train.append(training_set_scaled[i - 60:i, 0])
y_train.append(training_set_scaled[i, 0])
np.random.seed(7)
np.random.shuffle(x_train)
np.random.seed(7)
np.random.shuffle(y_train)
x_train = np.array(x_train)
y_train = np.array(y_train)
x_train = np.reshape(x_train, (x_train.shape[0], 60, 1))
x_test = []
y_test = []
for i in range(60, len(test_set_scaled)):
x_test.append(test_set_scaled[i - 60:i, 0])
y_test.append(test_set_scaled[i, 0])
x_test = np.array(x_test)
y_test = np.array(y_test)
x_test = np.reshape(x_test, (x_test.shape[0], 60, 1))
return (x_train, y_train), (x_test, y_test)
def load_local_model(model_path):
if os.path.exists(model_path + '/saved_model.pb'):
print(datetime.datetime.now())
local_model = tf.keras.models.load_model(model_path)
else:
local_model = tf.keras.Sequential([
SimpleRNN(80, return_sequences=True),
Dropout(0.2),
SimpleRNN(100),
Dropout(0.2),
Dense(1)
])
local_model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
loss='mean_squared_error') # 损失函数用均方误差
return local_model
def show_train_line(history):
loss = history.history['loss']
val_loss = history.history['val_loss']
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.title('Training and Validation Loss')
plt.legend()
plt.show()
def stock_predict(model, x_test, y_test):
# 测试集输入模型进行预测
predicted_stock_price = model.predict(x_test)
# 对预测数据还原---从(0,1)反归一化到原始范围
predicted_stock_price = sc.inverse_transform(predicted_stock_price)
# 对真实数据还原---从(0,1)反归一化到原始范围
real_stock_price = sc.inverse_transform(np.reshape(y_test, (y_test.shape[0], 1)))
# 画出真实数据和预测数据的对比曲线
plt.plot(real_stock_price, color='red', label='MaoTai Stock Price')
plt.plot(predicted_stock_price, color='blue', label='Predicted MaoTai Stock Price')
plt.title('MaoTai Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('MaoTai Stock Price')
plt.legend()
plt.show()
plt.savefig('./model/rnn/compare.jpg')
mse = mean_squared_error(predicted_stock_price, real_stock_price)
rmse = math.sqrt(mean_squared_error(predicted_stock_price, real_stock_price))
mae = mean_absolute_error(predicted_stock_price, real_stock_price)
print('均方误差: %.6f' % mse)
print('均方根误差: %.6f' % rmse)
print('平均绝对误差: %.6f' % mae)
if __name__ == '__main__':
from google.colab import drive
import os
# 挂载网盘
drive.mount('/content/drive/')
# 切换路径
os.chdir('/content/drive/MyDrive/rnn')
'''
# 查看当前路径
!pwd
# 查看当前路径下的文件夹
!ls -ll
# 查看分配的机器
!nvidia-smi
'''
file_path = './stock_data/SH600519.csv'
(x_train, y_train), (x_test, y_test) = get_stock_data(file_path)
model_path = "./model/rnn"
model = load_local_model(model_path)
history = model.fit(x_train, y_train, batch_size=64, epochs=100, validation_data=(x_test, y_test),validation_freq=1)
show_train_line(history)
model.summary()
model.save(model_path, save_format="tf")
stock_predict(model, x_test, y_test)
6.选择gpu(代码执行程序–> 更改运行时类型–>硬件加速器选gpu)
查看当前分配的硬件 可以使用!nvidia-smi
执行结果如下
只是用来演示,本案例中运行速度并不比本人笔记本快多少
GitHub 加速计划 / col / COLA
9
5
下载
🥤 COLA: Clean Object-oriented & Layered Architecture
最近提交(Master分支:4 个月前 )
bda50471 - 6 个月前
65e9e39d
bump org.apache.maven.plugins:maven-compiler-plugin from 3.11.0 to 3.13.0 (#496)
bump org.apache.maven.plugins:maven-surefire-plugin from 3.1.2 to 3.2.5 (#497)
bump org.springframework.boot:spring-boot-dependencies from 3.1.0 to 3.3.0.(#498)
bump org.apache.maven.plugins:maven-javadoc-plugin from 3.6.0 to 3.7.0 (#499)
bump commons-cli:commons-cli from 1.5.0 to 1.8.0 (#500)
bump org.jacoco:jacoco-maven-plugin from 0.8.10 to 0.8.12 (#501)
bump org.apache.maven.plugins:maven-source-plugin from 3.2.1 to 3.3.1 (#502)
bump org.wiremock:wiremock-standalone from 3.0.1 to 3.0.3 (#503)
6 个月前
更多推荐
已为社区贡献4条内容
所有评论(0)