forked from AlbertoEsc/marketing_results_prediction
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensorflow_data_processing.py
More file actions
71 lines (57 loc) · 2.53 KB
/
tensorflow_data_processing.py
File metadata and controls
71 lines (57 loc) · 2.53 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
########################################################################
# Routines that simplify the use of tensorflow estimators #
# #
# Author: Alberto N. Escalante B. #
# Date: 19.09.2018 #
# E-mail: alberto.escalante@ini.rub.de #
# #
########################################################################
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import tensorflow as tf
import numpy as np
def train_input_fn(features, labels, batch_size):
"""An input function for training"""
# Convert the inputs to a Dataset.
dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))
# Shuffle, repeat, and batch the examples.
dataset = dataset.shuffle(1000).repeat().batch(batch_size)
return dataset
def eval_input_fn(features, labels, batch_size):
"""An input function for evaluation or prediction"""
features = dict(features)
if labels is None:
# No labels, use only features.
inputs = features
else:
inputs = (features, labels)
# Convert the inputs to a Dataset.
dataset = tf.data.Dataset.from_tensor_slices(inputs)
# Batch the examples
assert batch_size is not None, "batch_size must not be None"
dataset = dataset.batch(batch_size)
return dataset
def extract_pred_and_prob_from_estimator_predictions(predictions):
"""From a prediction object computed by the estimator, this function
extracts the prediction (class id) and probabilities and returns them
as two numpy arrays. """
pred = np.array([])
prob = np.array([])
for prediction in predictions:
pred = np.append(pred, prediction['class_ids'][0])
prob = np.append(prob, prediction['probabilities'])
num_samples = len(pred)
prob = prob.reshape((num_samples, -1))
return pred, prob
def extract_pred_from_estimator_predictions(predictions):
"""From a prediction object computed by the estimator, this function
extracts the prediction ('prediction') them
as a numpy array. """
# print('predictions:', predictions)
pred = np.array([])
for prediction in predictions:
pred = np.append(pred, prediction['predictions'])
num_samples = len(pred)
pred = pred.reshape((num_samples, ))
return pred