3636
3737def boston_input_fn (num_epochs = None ):
3838 boston = tf .contrib .learn .datasets .load_boston ()
39- features = tf .cast (
40- tf .reshape (tf .constant (boston .data ), [- 1 , _BOSTON_INPUT_DIM ]), tf .float32 )
39+ features = tf .reshape (tf .constant (boston .data ), [- 1 , _BOSTON_INPUT_DIM ])
4140 if num_epochs :
4241 features = tf .train .limit_epochs (features , num_epochs = num_epochs )
43- target = tf .cast (
44- tf .reshape (tf .constant (boston .target ), [- 1 , 1 ]), tf .float32 )
42+ target = tf .reshape (tf .constant (boston .target ), [- 1 , 1 ])
4543 return features , target
4644
4745
4846def iris_input_fn ():
4947 iris = tf .contrib .learn .datasets .load_iris ()
50- features = tf .cast (
51- tf .reshape (tf .constant (iris .data ), [- 1 , _IRIS_INPUT_DIM ]), tf .float32 )
52- target = tf .cast (
53- tf .reshape (tf .constant (iris .target ), [- 1 ]), tf .int32 )
48+ features = tf .reshape (tf .constant (iris .data ), [- 1 , _IRIS_INPUT_DIM ])
49+ target = tf .reshape (tf .constant (iris .target ), [- 1 ])
5450 return features , target
5551
5652
5753def boston_eval_fn ():
5854 boston = tf .contrib .learn .datasets .load_boston ()
5955 n_examples = len (boston .target )
60- features = tf .cast (
61- tf .reshape (tf .constant (boston .data ), [n_examples , _BOSTON_INPUT_DIM ]),
62- tf .float32 )
63- target = tf .cast (
64- tf .reshape (tf .constant (boston .target ), [n_examples , 1 ]), tf .float32 )
56+ features = tf .reshape (
57+ tf .constant (boston .data ), [n_examples , _BOSTON_INPUT_DIM ])
58+ target = tf .reshape (tf .constant (boston .target ), [n_examples , 1 ])
6559 return tf .concat (0 , [features , features ]), tf .concat (0 , [target , target ])
6660
6761
@@ -188,7 +182,7 @@ def testUntrained(self):
188182 with self .assertRaises (tf .contrib .learn .NotFittedError ):
189183 _ = est .evaluate (
190184 x = boston .data ,
191- y = boston .target .astype (np .float32 ))
185+ y = boston .target .astype (np .float64 ))
192186 with self .assertRaises (tf .contrib .learn .NotFittedError ):
193187 est .predict (x = boston .data )
194188
@@ -197,10 +191,11 @@ def testContinueTraining(self):
197191 output_dir = tempfile .mkdtemp ()
198192 est = tf .contrib .learn .Estimator (model_fn = linear_model_fn ,
199193 model_dir = output_dir )
200- est .fit (x = boston .data , y = boston .target .astype (np .float32 ), steps = 50 )
194+ float64_target = boston .target .astype (np .float64 )
195+ est .fit (x = boston .data , y = float64_target , steps = 50 )
201196 scores = est .evaluate (
202197 x = boston .data ,
203- y = boston . target . astype ( np . float32 ) ,
198+ y = float64_target ,
204199 metrics = {'MSE' : tf .contrib .metrics .streaming_mean_squared_error })
205200 del est
206201 # Create another estimator object with the same output dir.
@@ -210,35 +205,36 @@ def testContinueTraining(self):
210205 # Check we can evaluate and predict.
211206 scores2 = est2 .evaluate (
212207 x = boston .data ,
213- y = boston . target . astype ( np . float32 ) ,
208+ y = float64_target ,
214209 metrics = {'MSE' : tf .contrib .metrics .streaming_mean_squared_error })
215210 self .assertAllClose (scores2 ['MSE' ],
216211 scores ['MSE' ])
217212 predictions = est2 .predict (x = boston .data )
218- other_score = _sklearn .mean_squared_error (predictions , boston . target )
213+ other_score = _sklearn .mean_squared_error (predictions , float64_target )
219214 self .assertAllClose (other_score , scores ['MSE' ])
220215
221216 # Check we can keep training.
222- est2 .fit (x = boston .data , y = boston . target . astype ( np . float32 ) , steps = 100 )
217+ est2 .fit (x = boston .data , y = float64_target , steps = 100 )
223218 scores3 = est2 .evaluate (
224219 x = boston .data ,
225- y = boston . target . astype ( np . float32 ) ,
220+ y = float64_target ,
226221 metrics = {'MSE' : tf .contrib .metrics .streaming_mean_squared_error })
227222 self .assertLess (scores3 ['MSE' ], scores ['MSE' ])
228223
229224 def testEstimatorParams (self ):
230225 boston = tf .contrib .learn .datasets .load_boston ()
231226 est = tf .contrib .learn .Estimator (model_fn = linear_model_params_fn ,
232227 params = {'learning_rate' : 0.01 })
233- est .fit (x = boston .data , y = boston .target . astype ( np . float32 ) , steps = 100 )
228+ est .fit (x = boston .data , y = boston .target , steps = 100 )
234229
235230 def testBostonAll (self ):
236231 boston = tf .contrib .learn .datasets .load_boston ()
237232 est = tf .contrib .learn .Estimator (model_fn = linear_model_fn )
238- est .fit (x = boston .data , y = boston .target .astype (np .float32 ), steps = 100 )
233+ float64_target = boston .target .astype (np .float64 )
234+ est .fit (x = boston .data , y = float64_target , steps = 100 )
239235 scores = est .evaluate (
240236 x = boston .data ,
241- y = boston . target . astype ( np . float32 ) ,
237+ y = float64_target ,
242238 metrics = {'MSE' : tf .contrib .metrics .streaming_mean_squared_error })
243239 predictions = est .predict (x = boston .data )
244240 other_score = _sklearn .mean_squared_error (predictions , boston .target )
@@ -277,7 +273,7 @@ def testIrisIterator(self):
277273 iris = tf .contrib .learn .datasets .load_iris ()
278274 est = tf .contrib .learn .Estimator (model_fn = logistic_model_no_mode_fn )
279275 x_iter = itertools .islice (iris .data , 100 )
280- y_iter = itertools .islice (np . int32 ( iris .target ) , 100 )
276+ y_iter = itertools .islice (iris .target , 100 )
281277 est .fit (x_iter , y_iter , steps = 100 )
282278 _ = est .evaluate (input_fn = iris_input_fn , steps = 1 )
283279 predictions = est .predict (x = iris .data )['class' ]
@@ -374,19 +370,16 @@ def _assert_single_feature_column(
374370 '' : tf .FixedLenFeature (shape = expected_shape , dtype = expected_dtype )
375371 }, feature_column .config )
376372
377- # Note: See tf.contrib.learn.io.data_feeder for why int32 converts to float32.
378373 def testInt32Input (self ):
379374 feature_columns = tf .contrib .learn .infer_real_valued_columns_from_input (
380375 np .ones (shape = [7 , 8 ], dtype = np .int32 ))
381- self ._assert_single_feature_column ([8 ], tf .float32 , feature_columns )
376+ self ._assert_single_feature_column ([8 ], tf .int32 , feature_columns )
382377
383378 def testInt32InputFn (self ):
384379 feature_columns = tf .contrib .learn .infer_real_valued_columns_from_input_fn (
385380 lambda : (tf .ones (shape = [7 , 8 ], dtype = tf .int32 ), None ))
386381 self ._assert_single_feature_column ([8 ], tf .int32 , feature_columns )
387382
388- # Note: See tf.contrib.learn.io.data_feeder for why int64 doesn't convert to
389- # float64.
390383 def testInt64Input (self ):
391384 feature_columns = tf .contrib .learn .infer_real_valued_columns_from_input (
392385 np .ones (shape = [7 , 8 ], dtype = np .int64 ))
@@ -407,22 +400,21 @@ def testFloat32InputFn(self):
407400 lambda : (tf .ones (shape = [7 , 8 ], dtype = tf .float32 ), None ))
408401 self ._assert_single_feature_column ([8 ], tf .float32 , feature_columns )
409402
410- # Note: See tf.contrib.learn.io.data_feeder for why float64 converts to
411- # float32.
412403 def testFloat64Input (self ):
413404 feature_columns = tf .contrib .learn .infer_real_valued_columns_from_input (
414405 np .ones (shape = [7 , 8 ], dtype = np .float64 ))
415- self ._assert_single_feature_column ([8 ], tf .float32 , feature_columns )
406+ self ._assert_single_feature_column ([8 ], tf .float64 , feature_columns )
416407
417408 def testFloat64InputFn (self ):
418409 feature_columns = tf .contrib .learn .infer_real_valued_columns_from_input_fn (
419410 lambda : (tf .ones (shape = [7 , 8 ], dtype = tf .float64 ), None ))
420411 self ._assert_single_feature_column ([8 ], tf .float64 , feature_columns )
421412
422413 def testBoolInput (self ):
423- feature_columns = tf .contrib .learn .infer_real_valued_columns_from_input (
424- np .array ([[False for _ in xrange (8 )] for _ in xrange (7 )]))
425- self ._assert_single_feature_column ([8 ], tf .float32 , feature_columns )
414+ with self .assertRaisesRegexp (
415+ ValueError , 'on integer or non floating types are not supported' ):
416+ tf .contrib .learn .infer_real_valued_columns_from_input (
417+ np .array ([[False for _ in xrange (8 )] for _ in xrange (7 )]))
426418
427419 def testBoolInputFn (self ):
428420 with self .assertRaisesRegexp (
@@ -431,18 +423,12 @@ def testBoolInputFn(self):
431423 tf .contrib .learn .infer_real_valued_columns_from_input_fn (
432424 lambda : (tf .constant (False , shape = [7 , 8 ], dtype = tf .bool ), None ))
433425
434- def testInvalidStringInput (self ):
435- # pylint: disable=g-long-lambda
426+ def testStringInput (self ):
436427 with self .assertRaisesRegexp (
437- ValueError , 'could not convert string to float' ):
428+ ValueError , 'on integer or non floating types are not supported' ):
429+ # pylint: disable=g-long-lambda
438430 tf .contrib .learn .infer_real_valued_columns_from_input (
439- np .array ([['foo%d' % i for i in xrange (8 )] for _ in xrange (7 )]))
440-
441- def testStringInput (self ):
442- # pylint: disable=g-long-lambda
443- feature_columns = tf .contrib .learn .infer_real_valued_columns_from_input (
444- np .array ([['%d.0' % i for i in xrange (8 )] for _ in xrange (7 )]))
445- self ._assert_single_feature_column ([8 ], tf .float32 , feature_columns )
431+ np .array ([['%d.0' % i for i in xrange (8 )] for _ in xrange (7 )]))
446432
447433 def testStringInputFn (self ):
448434 with self .assertRaisesRegexp (
@@ -457,13 +443,13 @@ def testBostonInputFn(self):
457443 feature_columns = tf .contrib .learn .infer_real_valued_columns_from_input_fn (
458444 boston_input_fn )
459445 self ._assert_single_feature_column (
460- [_BOSTON_INPUT_DIM ], tf .float32 , feature_columns )
446+ [_BOSTON_INPUT_DIM ], tf .float64 , feature_columns )
461447
462448 def testIrisInputFn (self ):
463449 feature_columns = tf .contrib .learn .infer_real_valued_columns_from_input_fn (
464450 iris_input_fn )
465451 self ._assert_single_feature_column (
466- [_IRIS_INPUT_DIM ], tf .float32 , feature_columns )
452+ [_IRIS_INPUT_DIM ], tf .float64 , feature_columns )
467453
468454if __name__ == '__main__' :
469455 tf .test .main ()
0 commit comments