オライリー社の"実践 時系列解析"という本で学習を進めています。
この本のgithubリンクです -> https://github.com/PracticalTimeSeriesAnalysis/BookRepo
一番下に添付したコードの通り、この本ではエポックごとに更新される重みと結果を記録する管理システムとしてperfライブラリの perf.write_val()
を用いているのですが、perfライブラリのインストールがうまくいきません。
まず、そのままimport perf
を実行すると、no module named
となるので
!pip install perf
とgoogle colab上で実行するのですが、その際
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
ERROR: Could not find a version that satisfies the requirement perf (from versions: none)
ERROR: No matching distribution found for perf
と表示されてしまい、インストールがうまく進みません。
pipのバージョンによるインストールの失敗かと考え、
python3 -m pip install --upgrade pip
と実行し最新のバージョンで再度試しましたが、うまくいきませんでした。
・実際に実行しようとしたコード
https://github.com/PracticalTimeSeriesAnalysis/BookRepo/blob/master/Ch10/ForecastingElectricity.ipynb
################
## TRAINING ##
################
def train(symbol, iter_train, valid_iter, iter_test,
data_names, label_names,
save_dir, gpu):
## save training information/results
if not os.path.exists(SAVE_DIR):
os.makedirs(SAVE_DIR)
printFile = open(os.path.join(SAVE_DIR, 'log.txt'), 'w')
def print_to_file(msg):
print(msg)
print(msg, file = printFile, flush = True)
## print_to_file(args) ## preserve configuation to enable hyperparameter optimization
## archiving results header
print_to_file('Epoch Training Cor Validation Cor')
## storing prior epoch's values to set an improvement threshold
## terminates early if progress slow
buf = RingBuffer(THRESHOLD_EPOCHS)
old_val = None
## mxnet boilerplate
## defaults to 1 gpu of which index is 0
##devs = [mx.gpu(gpu)]
devs = mx.cpu()
module = mx.mod.Module(symbol,
data_names=data_names,
label_names=label_names,
context=devs)
module.bind(data_shapes=iter_train.provide_data,
label_shapes=iter_train.provide_label)
module.init_params(mx.initializer.Uniform(0.1))
module.init_optimizer(optimizer='adam',
optimizer_params={'learning_rate': LR})
## training
for epoch in range( N_EPOCHS):
iter_train.reset()
iter_val.reset()
for batch in iter_train:
module.forward(batch, is_train=True) # compute predictions
module.backward() # compute gradients
module.update() # update parameters
## training results
train_pred = module.predict(iter_train).asnumpy()
train_label = iter_train.label[0][1].asnumpy()
train_perf = perf.write_eval(train_pred, train_label,
save_dir, 'train', epoch)
## validation results
val_pred = module.predict(iter_val).asnumpy()
val_label = iter_val.label[0][1].asnumpy()
val_perf = perf.write_eval(val_pred, val_label,
save_dir, 'valid', epoch)
print_to_file('%d %f %f ' % (epoch, train_perf['COR'], val_perf['COR']))
if epoch > 0: # if we don't yet have measures of improvement, skip
buf.append(val_perf['COR'] - old_val)
if epoch > 2: # if we do have measures of improvement, check them
vals = buf.get()
# print(vals)
# print(COR_THRESHOLD)
vals = [v for v in vals if v != 0]
if sum([v < COR_THRESHOLD for v in vals]) == len(vals):
print_to_file('EARLY EXIT')
break
old_val = val_perf['COR']
## testing
test_pred = module.predict(iter_test).asnumpy()
test_label = iter_test.label[0][1].asnumpy()
test_perf = perf.write_eval(test_pred, test_label, save_dir, 'tst', epoch)
print_to_file('\n TESTING PERFORMANCE')
print_to_file(test_perf)