pywatts/pywatts/neural.py

48 lines
1.9 KiB
Python
Raw Normal View History

2018-05-29 15:34:05 +02:00
import tensorflow as tf
2018-06-11 13:47:02 +02:00
2018-06-23 15:00:16 +02:00
def pywatts_input_fn(X, y=None, num_epochs=None, shuffle=True, batch_size=1):
# Create dictionary for features in hour 0 ... 335
features = {str(idx): [] for idx in range(336)}
2018-08-06 13:28:27 +02:00
dc_values = X['dc']
2018-06-23 15:00:16 +02:00
# Iterate the empty dictionary always adding the idx-th element from the dc_values list
for idx, value_list in features.items():
value_list.extend(dc_values[int(idx)::336])
labels = None
if y is not None:
2018-08-06 13:28:27 +02:00
labels = y['dc']
2018-06-23 15:00:16 +02:00
if labels is None:
dataset = tf.data.Dataset.from_tensor_slices(dict(features))
else:
dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))
2018-09-10 19:44:42 +02:00
if num_epochs is not None:
return dataset.batch(len(features['0']))
2018-06-23 15:40:45 +02:00
if shuffle:
2018-08-14 22:20:40 +02:00
return dataset.shuffle(len(features['0']*len(features)*4)).repeat().batch(batch_size)
2018-08-13 14:19:39 +02:00
else:
return dataset.batch(batch_size)
2018-05-29 15:57:50 +02:00
2018-05-29 15:34:05 +02:00
class Net:
__regressor = None
2018-09-12 17:52:05 +02:00
__feature_cols = [tf.feature_column.numeric_column(col) for col in ['dc']]
2018-05-29 15:34:05 +02:00
def __init__(self, feature_cols=__feature_cols):
self.__regressor = tf.estimator.DNNRegressor(feature_columns=feature_cols,
2018-09-12 17:52:05 +02:00
hidden_units=[64, 128, 64],
model_dir='tf_pywatts_model')
2018-05-29 15:34:05 +02:00
2018-06-23 15:40:45 +02:00
def train(self, training_data, training_results, batch_size, steps):
self.__regressor.train(input_fn=lambda: pywatts_input_fn(training_data, y=training_results, num_epochs=None, shuffle=True, batch_size=batch_size), steps=steps)
2018-05-29 15:34:05 +02:00
2018-08-06 13:28:27 +02:00
def evaluate(self, eval_data, eval_results, batch_size=1):
return self.__regressor.evaluate(input_fn=lambda: pywatts_input_fn(eval_data, y=eval_results, num_epochs=1, shuffle=False, batch_size=batch_size), steps=1)
2018-05-29 15:34:05 +02:00
2018-06-11 16:42:13 +02:00
def predict1h(self, predict_data):
2018-06-23 15:00:16 +02:00
return self.__regressor.predict(input_fn=lambda: pywatts_input_fn(predict_data, num_epochs=1, shuffle=False))