生活随笔
收集整理的這篇文章主要介紹了
colab配合谷歌云盘使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
首先你得有一個谷歌賬號!!!
參考: https://zhuanlan.zhihu.com/p/149233850
https://blog.csdn.net/lumingha/article/details/104825702
1 將數(shù)據(jù)傳送至谷歌云盤(云盤地址:https://drive.google.com/drive/my-drive)
創(chuàng)建文件夾 上傳數(shù)據(jù)到stock_data
2 跳轉(zhuǎn)到colab(新建—>更多–> google Colaboratory)
3 掛載網(wǎng)盤+ 切換路徑
from google.colab import driveimport os# 掛載網(wǎng)盤drive.
mount('/content/drive/') # 切換路徑os.
chdir('/content/drive/MyDrive/rnn')
4. 查看文件
!ls -ll # 和linux終端用法差不多 但是!
(感嘆號
)不能少
5. 執(zhí)行代碼
我的代碼在本地是可以運行的
直接粘貼進去進行了,代碼如下(這里用的是py文件,最好使用ipy)
# -*-
coding: utf-8 -*-
import datetimeimport 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].valuestest_set = maotai.iloc[2426 - 300
:, 2
:3].valuestraining_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') # 損失函數(shù)用均方誤差return local_modeldef 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
)# 對預測數(shù)據(jù)還原---從(0,1)反歸一化到原始范圍predicted_stock_price = sc.inverse_
transform(predicted_stock_price
)# 對真實數(shù)據(jù)還原---從(0,1)反歸一化到原始范圍real_stock_price = sc.inverse_
transform(np.
reshape(y_test
, (y_test.shape[0]
, 1
)))# 畫出真實數(shù)據(jù)和預測數(shù)據(jù)的對比曲線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 driveimport os# 掛載網(wǎng)盤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(代碼執(zhí)行程序–> 更改運行時類型–>硬件加速器選gpu)
查看當前分配的硬件 可以使用!nvidia-smi
執(zhí)行結(jié)果如下
只是用來演示,本案例中運行速度并不比本人筆記本快多少
總結(jié)
以上是生活随笔為你收集整理的colab配合谷歌云盘使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。