-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
53 lines (38 loc) · 1.59 KB
/
Copy pathutils.py
File metadata and controls
53 lines (38 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import math
import logging
import pandas as pd
import numpy as np
import keras.backend as K
# Formats Position
format_position = lambda price: ('-$' if price < 0 else '+$') + '{0:.2f}'.format(abs(price))
# Formats Currency
format_currency = lambda price: '${0:.2f}'.format(abs(price))
def show_train_result(result, val_position, initial_offset):
""" Displays training results
"""
if val_position == initial_offset or val_position == 0.0:
logging.info('Episode {}/{} - Train Position: {} Val Position: USELESS Train Loss: {:.4f}'
.format(result[0], result[1], format_position(result[2]), result[3]))
else:
logging.info('Episode {}/{} - Train Position: {} Val Position: {} Train Loss: {:.4f})'
.format(result[0], result[1], format_position(result[2]), format_position(val_position), result[3],))
def show_eval_result(model_name, profit, initial_offset):
""" Displays eval results
"""
if profit == initial_offset or profit == 0.0:
logging.info('{}: USELESS\n'.format(model_name))
else:
logging.info('{}: {}\n'.format(model_name, format_position(profit)))
def get_stock_data(stock_file):
"""Reads stock data from csv file
"""
df = pd.read_csv(stock_file)
return list(df['Adj Close'])
def switch_k_backend_device():
""" Switches `keras` backend from GPU to CPU if required.
Faster computation on CPU (if using tensorflow-gpu).
"""
if K.backend() == "tensorflow":
logging.debug("switching to TensorFlow for CPU")
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"