我在哪里可以在Jupyter中放置启动脚本?

时间:2017-08-22 13:06:43

标签: python jupyter-notebook startup

我正在寻找一种方法将我的启动脚本放在Jupyter中。在IPython中,我把它放在# Larger LSTM Network to Generate Text for Alice in Wonderland import numpy from keras.models import Sequential from keras.layers import Dense from keras.layers import Dropout from keras.layers import LSTM from keras.callbacks import ModelCheckpoint from keras.utils import np_utils # load ascii text and covert to lowercase filename = "wonderland.txt" raw_text = open(filename).read() raw_text = raw_text.lower() # create mapping of unique chars to integers chars = sorted(list(set(raw_text))) char_to_int = dict((c, i) for i, c in enumerate(chars)) # summarize the loaded data n_chars = len(raw_text) n_vocab = len(chars) print ("Total Characters: ", n_chars) print ("Total Vocab: ", n_vocab) # prepare the dataset of input to output pairs encoded as integers seq_length = 100 dataX = [] dataY = [] for i in range(0, n_chars - seq_length, 1): seq_in = raw_text[i:i + seq_length] seq_out = raw_text[i + seq_length] dataX.append([char_to_int[char] for char in seq_in]) dataY.append(char_to_int[seq_out]) n_patterns = len(dataX) print ("Total Patterns: ", n_patterns) # reshape X to be [samples, time steps, features] X = numpy.reshape(dataX, (n_patterns, seq_length, 1)) # normalize X = X / float(n_vocab) # one hot encode the output variable y = np_utils.to_categorical(dataY) # define the LSTM model model = Sequential() model.add(LSTM(256, input_shape=(X.shape[1], X.shape[2]), return_sequences=True)) model.add(Dropout(0.2)) model.add(LSTM(256)) model.add(Dropout(0.2)) model.add(Dense(y.shape[1], activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam') # define the checkpoint filepath="weights-improvement-{epoch:02d}-{loss:.4f}-bigger.hdf5" checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min') callbacks_list = [checkpoint] # fit the model model.fit(X, y, epochs=50, batch_size=64, callbacks=callbacks_list) 下面。

在Jupyter中,配置文件似乎应该是$IPYTHON_DIR/profile_default/startup/。但是,我想使用我的启动文件,它会在内核启动时导入大量的Python库。

我在哪里可以将这样的文件放在Jupyter中?

1 个答案:

答案 0 :(得分:5)

您可以在jupyter笔记本中获取默认的启动脚本文件夹:

get_ipython().profile_dir.startup_dir

在我的Windows PC上,该文件夹为:C:\Users\myusername\.ipython\profile_default\startup

并阅读文件夹下的README文件。

    Files will be run in lexicographical order, so you can control
the execution order of files with a prefix, e.g.::

    00-first.py
    50-middle.py
    99-last.ipy

因此,您可以将名为00-xx.py的文件放在该文件夹下。