深度学习平台之Notebook基础操作

一:Notebook中使用Shell命令

1.查看当前挂载的数据集目录
!ls /home/aistudio/data/
2.显示当前路径
!pwd
3.使用pip来安装自己需要的package(但不支持apt-get)
使用百度飞桨平台必须的安装命令
#导入paddlex !pip install paddlex #配置PaddleDetection环境 #下面的命令可以根据不同的paddle文件,或者目录更换 !pip install -r work/PaddleDetection/requirements.txt
例如:
!pip install jupyterthemes
4.查看当前环境中安装的package
!pip list --format=columns
5.使用git命令来同步代码和linus命令跳转到指定目录下
#跳转到work目录下 %cd work/ #也可以使用github !git clone https://gitee.com/PaddlePaddle/Paddle.git #Paddle官方模型

二. Magic命令

1.显示全部可以使用的Magic命令
%lsmagic
2.Magic的样例使用
首先导入import random
样例1.计算运行时间:
%%timeit prize = 0 for i in range(100): roll = random.randint(1, 6) if roll%2 == 0: prize += roll else: prize -= 1 #99.5 µs ± 606 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

样例2直接嵌入可视化内容, 例如%matplotlib inline:
%matplotlib inline %config InlineBackend.figure_format = ‘retina’ import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,1,300) for w in range(2,6,2): plt.plot(x, np.sin(np.pix)np.sin(2wnp.pi*x))

结果:

重点:%%writefile and %pycat: 导出cell内容/显示外部脚本的内容
%%writefile magic可以把cell的内容保存到外部文件里。 而%pycat则可把外部文件展示在Cell中

代码:
%%writefile SaveToPythonCode.py
from math import sqrt
for i in range(2,10):
flag=1
k=int(sqrt(i))
for j in range(2,k+1):
if i%j==0:
flag=0
break
if(flag):
print(i)

猜你喜欢

转载自blog.csdn.net/qq_51269815/article/details/121726075