colab配合谷歌云盘使用
首先你得有一个谷歌账号!!!
参考: 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 driveimport os# 挂载网盘drive.mount('/content/drive/') # 切换路径os.chdir('/content/drive/MyDrive/rnn')
4. 查看文件
!ls -ll # 和linux终端用法差不多 但是!(感叹号)不能少
5. 执行代码
我的代码在本地是可以运行的
直接粘贴进去进行了,代码如下(这里用的是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') # 损失函数用均方误差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)# 对预测数据还原---从(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 driveimport 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
执行结果如下
只是用来演示,本案例中运行速度并不比本人笔记本快多少
colab配合谷歌云盘使用相关推荐
- google colab连接谷歌云盘
在做深度学习项目时,我们一定会需要一个服务器,有时候因为条件限制没有服务器,可以用google的colab来跑我们的程序,它最大的特点是有GPU支持,型号Tesla P100-PCIE-16GB GP ...
- colab 挂载谷歌云盘
import os from google.colab import drive drive.mount('/gdrive') os.symlink('/gdrive/My Drive', '/con ...
- 谷歌云盘Colaboratory如何载入文件
谷歌云的Colaboratory的项目的确不错,提供Tesla K80这块高级的GPU加速功能,但是也存在一个问题. 因为Colaboratory是完全云端的,所以,每次如果想让他访问谷歌云盘的内容, ...
- 【我的第一个目标检测课题】2、薅一把Google的羊毛!使用Colaboratory链接谷歌云盘在线进行网络训练
2020.12.30晚记 在上一篇中已经介绍了用自己的电脑配置了GPU,配置完后训练速度确实是大大提升,但是因为自己的轻薄本显存太少了,只有2G,而我们的数据集还挺大,图片分辨率也高,所以尽管把bat ...
- 2019 Google Drive Api 上传文件到谷歌云盘 获取分享下载链接
如果图片失效或者格式已乱,建议阅读原文 在[案例]搭建 Quizzes 网站,每天赚取 30-50 美元 提到下载站项目,因为整个项目代码加上一些说明,会导致内容太多,所以准备分成几部分,这样看起 ...
- google谷歌云盘_Google舞蹈综合症
google谷歌云盘 Good stuff-just the thing my buddy Adam Cogan is looking for as he dances to the top of t ...
- google谷歌云盘_如何(以及为什么)开始使用Google云打印
google谷歌云盘 Wouldn't it be wonderful if you could print from any of your devices (desktops, phones, t ...
- [Linux][Colab] Colab连接google云盘 | ssh连接Colab | 防止Colab断连
购买Colab Pro之后,界面多了终端显示: 1. Colab连接goole云盘 在代码命令行输入命令,实现colab和google云盘的连接: from google.colab import d ...
- google谷歌云盘_Google 12岁生日快乐
google谷歌云盘 It's Google's 12th Birthday. You'd probably realized that if you've visited the search en ...
- google谷歌云盘_Google诗歌中的冬天
google谷歌云盘 Seasonal decoration and localization have been discussion points around the SitePoint off ...
最新文章
- 成功解决SyntaxError: import * only allowed at module level
- ubuntu16.04下面使用graphviz
- 在ubuntu上搭建LNMP服务器
- cf 1512 E. Permutation by Sum
- 论文审稿意见太奇葩?NeurIPS 2021
- 【ICLR2020】通过强化学习和稀疏奖励进行模仿学习
- 锻造完美U盘小偷:活用消息机制
- 走近夜间灯光——教你平均灯光指数(ANLI)如何得到(超详细)
- python list转json对象_将列表转换为json对象
- 远程医疗监护系统开发
- Winlogon、LSASS、Userinit
- 矩阵键盘消抖 c语言,按键消抖,矩阵键盘原理和矩阵键盘的仿真模型
- OMNeT 例程 Tictoc13 学习笔记
- 厦门大学c语言2017,厦门大学2017年各专业录取分数线
- 什么是soft matting方法_建筑师学“交互”有什么意义?零基础如何展开?
- 根据正则表达式创建NFA的Thompson算法 python实现
- (转)802.1Q标准中TAG字段简单说明
- 机器人视觉测量与控制
- win10安装RabbitMQ教程
- mac系统怎么制作装系统的u盘?苹果电脑u盘启动盘制作教程