Skip to content

Commit

Permalink
fix: tf.mul -> tf.multiply, feat: allow NaN's in y_train
Browse files Browse the repository at this point in the history
  • Loading branch information
Avsecz committed Mar 13, 2017
1 parent 3578ab4 commit db6207e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
20 changes: 13 additions & 7 deletions concise/concise.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ def _get_var_initialization(self, graph, X_feat_train, y_train):

return var

# DONE
def _build_graph(self, graph, var):
with graph.as_default():
tf_X_seq = tf.placeholder(tf.float32, shape=[None, 1, None, self._num_channels])
Expand Down Expand Up @@ -348,7 +347,16 @@ def model(data, tf_X_feat, var):
y_pred = model(tf_X_seq, tf_X_feat, var)
# TODO: enable other loss functions like softmax, huber loss etc
# - how to include loss-paramteres like k in the case of huber_loss
loss = tf.reduce_mean(tf.square(y_pred - tf_y))

# allow for NA's in Y
# - use tf.is_nan(tf_y) - https://www.tensorflow.org/api_docs/python/tf/is_nan
# - sum only those values that are none NA
# - https://www.tensorflow.org/api_docs/python/tf/where
y_diff = tf.where(tf.is_nan(tf_y),
x=tf.zeros_like(y_pred),
y=tf_y - y_pred,
name="y_diff")
loss = tf.reduce_mean(tf.square(y_diff))

# add regularization
# regularization = motif_lamb * tf.nn.l2_loss(motif_base_weights) +
Expand Down Expand Up @@ -394,7 +402,7 @@ def model(data, tf_X_feat, var):
#
# http://www.subsubroutine.com/sub-subroutine/2016/11/12/painting-like-van-gogh-with-convolutional-neural-networks

init = tf.global_variables_initializer() #tf.initialize_all_variables()
init = tf.global_variables_initializer() # tf.initialize_all_variables()

other_var = {
"tf_X_feat": tf_X_feat,
Expand Down Expand Up @@ -727,7 +735,6 @@ def _train_lbfgs(self, X_feat_train, X_seq_train, y_train,
(best_performance_epoch, best_performance))
break


# get the test accuracies
train_accuracy_final = self._accuracy_in_session(sess, other_var,
X_feat_train, X_seq_train, y_train)
Expand Down Expand Up @@ -1083,11 +1090,10 @@ def from_dict(cls, obj_dict):

weights = obj_dict["output"]["weights"]


if weights is not None:
# fix the dimentionality of X_feat in case it was 0 dimentional
if weights["feature_weights"].shape == (0,):
weights["feature_weights"].shape = (0 , obj_dict["param"]["num_tasks"])
weights["feature_weights"].shape = (0, obj_dict["param"]["num_tasks"])
dc._set_var_res(weights)

return dc
Expand Down Expand Up @@ -1252,7 +1258,7 @@ def train(self, X_feat, X_seq, y, id_vec=None, n_folds=10, use_stored_folds=None
# overwrite n_epochs with the best average number of best epochs
dc._param["n_epochs"] = int(np.array(best_val_acc_epoch_l).mean())
print("tranining global model with n_epochs = " + str(dc._param["n_epochs"]))

dc.train(X_feat, X_seq, y,
n_cores=n_cores
)
Expand Down
5 changes: 4 additions & 1 deletion concise/math_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
# mean-squared error
import numpy as np

# ignore NA-values
def mse(x, y):
return ((x - y) ** 2).mean(axis=None)
# equivalent to TF representation
y_diff = np.where(np.isnan(y) | np.isnan(x), 0, x - y)
return ((y_diff) ** 2).mean(axis=None)

# exponentiated root-mean-squared error
def ermse(x, y):
Expand Down
2 changes: 1 addition & 1 deletion concise/tf_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def l1_loss(tensor, weight=1.0, scope=None):
weight = tf.convert_to_tensor(weight,
dtype=tensor.dtype.base_dtype,
name='loss_weight')
loss = tf.mul(weight, tf.reduce_sum(tf.abs(tensor)), name='value')
loss = tf.multiply(weight, tf.reduce_sum(tf.abs(tensor)), name='value')
return loss

def huber_loss(tensor, k=1, scope=None):
Expand Down
36 changes: 36 additions & 0 deletions tests/test_na_y.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
test when some values are non-NA
----------------------------------
"""
import pytest
import os
import numpy as np

from concise import concise
from concise import math_helper
from concise import helper
from tests.setup_concise_load_data import load_example_data

# replace some Y values with NA
def data_w_na():
data = load_example_data(num_tasks=3)
param, X_feat, X_seq, y, id_vec = data
y[0:51, 0] = np.NaN
y[51:101, 1] = np.NaN
y[102:300, 1] = np.NaN
data = (param, X_feat, X_seq, y, id_vec)
return data

def test_not_na():
param, X_feat, X_seq, y, id_vec = data_w_na()

c = concise.Concise(**param)
c.train(X_feat, X_seq, y,
X_feat_valid=X_feat, X_seq_valid=X_seq, y_valid=y)

# Not a single one can be Nan
assert not np.any(np.isnan(c.get_accuracy()["loss_history"]))
assert not np.any(np.isnan(c.get_accuracy()["train_acc_history"]))
assert not np.any(np.isnan(c.get_accuracy()["val_acc_history"]))

0 comments on commit db6207e

Please sign in to comment.