今天专门花时间总结梳理一下jupyter的一些高级设置,jupyter我已经介绍过一次基本内容了,Setup and Linux | James Chen’s Blogs,尤其是如何在服务器运行jupyter并且在本地浏览器显示,简直是使用python进行机器学习、深度学习、大数据的工作者的巨大福音。作为一个重度python&jupyter使用者,我已经习惯于在jupyter上进行大量的实验以及一次性的小工作、作业,需要跑很久的代码才会在运行通过后用VSCode编辑一下提交上去跑。用jupyter写了很多脚本,尤其适合可视化、展示和教学。可以在我的GitHub找到很多用jupyter写的代码,事实上很多教程和实验大家也都习惯于jupyter做了,比如我在这篇Deep Learning Practice介绍的资源中就有大量用jupyter写的。
jupyter支持markdown,只需要将某个代码框选为markdown格式,使用table of contents插件,就会自动在左边栏生成目录。对于写的很长的代码,可以帮助整理思路,快速定位代码。大家用jupyter一般是做前期的各种各样的实验,思路可能比较发散,所以用table of contents可以帮忙梳理思路,也方便以后再寻找、理解代码
This extension adds a drop-down menu to the IPython toolbar that allows easy insertion of code snippet cells into the current notebook. The code snippets are defined in a JSON file in nbextensions/snippets/snippets.json and an example snippet is included with this extension
Snippets are specified by adding a new JSON block to the list of existing snippets in $(jupyter —data-dir)/nbextensions/snippets/snippets.json. (I put my customized json file in jupyter-notebooks directory in /Users/james/) For example, to add a new snippet that imports numpy, matplotlib, and a print statement, the JSON file should be modified。
先分别列一下各种情景下需要导入哪些库:
Basic science
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import argparse, sys, os, errno %pylab inline import numpy as np import pandas as pd import matplotlib.pyplot as plt plt.style.use('ggplot') import seaborn as sns import h5py import os from tqdm import tqdm import scipy import sklearn from scipy.stats import pearsonr import warnings warnings.filterwarnings('ignore')
Highlevel plot
1 2 3 4 5 6 7 8
import matplotlib.animation as animation from matplotlib import rc from IPython.display import HTML, Image rc('animation', html='html5') import plotly import plotly.offline as off import plotly.plotly as py import plotly.graph_objs as go
import keras from keras import backend as K from keras.callbacks import TensorBoard from keras.callbacks import EarlyStopping from keras.optimizers import Adam from keras.callbacks import ModelCheckpoint import tensorflow as tf from keras.models import Model from keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D,Lambda, Dot,average,add, concatenate from keras.layers.normalization import BatchNormalization from keras.layers.core import Dropout, Activation,Reshape from keras.layers.merge import concatenate from keras.callbacks import TensorBoard, EarlyStopping, ModelCheckpoint from keras.initializers import RandomNormal import os os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID' os.environ['CUDA_VISIBLE_DEVICES'] = '4' from keras.backend.tensorflow_backend import set_session config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.99 (tf.Session(config=config))
Pytorch
1 2 3 4
import torch import math import torch.nn as nn import torch.nn.functional as F