Skip to content

Commit 9e615e1

Browse files
committed
Add ensemble test case.
1 parent fad6045 commit 9e615e1

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

tests/core.py

+31
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,34 @@ def test_eval(self):
6060
# no element in the diff array should be larger than 1e-7
6161
maxdiff = np.max(np.abs(rtf - rtd))
6262
self.assertLess(maxdiff, 1e-7)
63+
64+
def test_ensemble_eval(self):
65+
simple_model2 = td.Model()
66+
y2, sess2 = self.get("simple2", "y", "sess")
67+
simple_model2.add(y2, tf_sess=sess2)
68+
69+
simple_model2.get("input_1").name = "input:0"
70+
simple_model2.get("output_1").name = "output:0"
71+
simple_model2.get("keep_prob_1").name = "keep_prob:0"
72+
73+
simple_ensemble = td.Ensemble()
74+
simple_ensemble.models = [self.simple_model, simple_model2]
75+
76+
inp, outp, kp = simple_ensemble.get("input", "output", "keep_prob")
77+
78+
# create an input batch
79+
examples = np.random.rand(1000, 10).astype("float32")
80+
81+
# eval both models manually and build the mean
82+
x1, y1, keep_prob1 = self.simple_model.get("input", "output", "keep_prob")
83+
r1 = y1.eval({x1: examples, keep_prob1: 1.0})
84+
x2, y2, keep_prob2 = simple_model2.get("input", "output", "keep_prob")
85+
r2 = y2.eval({x2: examples, keep_prob2: 1.0})
86+
rm = np.add(r1, r2) / 2.
87+
88+
# then, eval the ensemble
89+
re = outp.eval({inp: examples, kp: 1.0})
90+
91+
# no element in the diff array should be larger than 1e-7
92+
maxdiff = np.max(np.abs(re - rm))
93+
self.assertLess(maxdiff, 1e-7)

tests/models/simple2.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
3+
4+
import tensorflow as tf
5+
6+
7+
sess = tf.Session()
8+
9+
x = tf.placeholder(tf.float32, shape=[None, 10], name="input")
10+
keep_prob = tf.placeholder(tf.float32, name="keep_prob")
11+
12+
W = tf.Variable(tf.truncated_normal([10, 5], stddev=0.05))
13+
b = tf.Variable(tf.zeros([5]))
14+
15+
W_drop = tf.nn.dropout(W, keep_prob)
16+
17+
y = tf.nn.softmax(tf.matmul(x, W_drop) + b, name="output")
18+
19+
sess.run(tf.initialize_all_variables())

0 commit comments

Comments
 (0)