In [4]:
import shutil
from sklearn import datasets
from sklearn import metrics
from sklearn.model_selection import train_test_split
import tensorflow as tf
learn = tf.contrib.learn
iris = datasets.load_iris()
x_train, x_test, y_train, y_test = train_test_split(
iris.data, iris.target, test_size=0.2, random_state=42)
x_train, x_val, y_train, y_val = train_test_split(
x_train, y_train, test_size=0.2, random_state=42)
val_monitor = learn.monitors.ValidationMonitor(
x_val, y_val, early_stopping_rounds=200)
model_dir = '/tmp/iris_model'
try:
shutil.rmtree(model_dir)
except OSError:
pass
classifier1 = learn.DNNClassifier(
feature_columns=learn.infer_real_valued_columns_from_input(x_train),
hidden_units=[10, 20, 10],
n_classes=3,
model_dir=model_dir)
classifier1.fit(x=x_train, y=y_train, steps=2000)
predictions1 = list(classifier1.predict(x_test, as_iterable=True))
score1 = metrics.accuracy_score(y_test, predictions1)
model_dir = '/tmp/iris_model_val'
try:
shutil.rmtree(model_dir)
except OSError:
pass
classifier2 = learn.DNNClassifier(
feature_columns=learn.infer_real_valued_columns_from_input(x_train),
hidden_units=[10, 20, 10],
n_classes=3,
model_dir=model_dir,
config=tf.contrib.learn.RunConfig(save_checkpoints_secs=1))
classifier2.fit(x=x_train, y=y_train, steps=2000, monitors=[val_monitor])
predictions2 = list(classifier2.predict(x_test, as_iterable=True))
score2 = metrics.accuracy_score(y_test, predictions2)
print('score1:',score1)
print('score2:',score2)
print('score2 > score1', score2 > score1)
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/monitors.py:322: BaseMonitor.__init__ (from tensorflow.contrib.learn.python.learn.monitors) is deprecated and will be removed after 2016-12-05.
Instructions for updating:
Monitors are deprecated. Please use tf.train.SessionRunHook.
WARNING:tensorflow:float64 is not supported by many models, consider casting to float32.
INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_keep_checkpoint_every_n_hours': 10000, '_save_checkpoints_steps': None, '_master': '', '_num_ps_replicas': 0, '_task_id': 0, '_tf_config': gpu_options {
per_process_gpu_memory_fraction: 1.0
}
, '_save_summary_steps': 100, '_is_chief': True, '_environment': 'local', '_evaluation_master': '', '_task_type': None, '_save_checkpoints_secs': 600, '_tf_random_seed': None, '_keep_checkpoint_max': 5, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x7f8f744706d8>}
WARNING:tensorflow:From <ipython-input-4-d1be09113dc4>:33: calling BaseEstimator.fit (from tensorflow.contrib.learn.python.learn.estimators.estimator) with x is deprecated and will be removed after 2016-12-01.
Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
est = Estimator(...) -> est = SKCompat(Estimator(...))
WARNING:tensorflow:From <ipython-input-4-d1be09113dc4>:33: calling BaseEstimator.fit (from tensorflow.contrib.learn.python.learn.estimators.estimator) with y is deprecated and will be removed after 2016-12-01.
Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
est = Estimator(...) -> est = SKCompat(Estimator(...))
WARNING:tensorflow:float64 is not supported by many models, consider casting to float32.
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:1362: scalar_summary (from tensorflow.python.ops.logging_ops) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please switch to tf.summary.scalar. Note that tf.summary.scalar uses the node name instead of the tag. This means that TensorFlow will automatically de-duplicate summary names based on the scope they are created in. Also, passing a tensor or list of tags to a scalar summary op is no longer supported.
/usr/local/lib/python3.5/dist-packages/tensorflow/python/util/deprecation.py:247: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
equality = a == b
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Saving checkpoints for 1 into /tmp/iris_model/model.ckpt.
INFO:tensorflow:loss = 1.995, step = 1
INFO:tensorflow:global_step/sec: 398.888
INFO:tensorflow:loss = 0.0556344, step = 101
INFO:tensorflow:global_step/sec: 129.189
INFO:tensorflow:loss = 0.0301557, step = 201
INFO:tensorflow:global_step/sec: 419.981
INFO:tensorflow:loss = 0.0212613, step = 301
INFO:tensorflow:global_step/sec: 200.647
INFO:tensorflow:loss = 0.0159204, step = 401
INFO:tensorflow:global_step/sec: 242.59
INFO:tensorflow:loss = 0.0129824, step = 501
INFO:tensorflow:global_step/sec: 154.387
INFO:tensorflow:loss = 0.0109287, step = 601
INFO:tensorflow:global_step/sec: 489.103
INFO:tensorflow:loss = 0.00940481, step = 701
INFO:tensorflow:global_step/sec: 415.599
INFO:tensorflow:loss = 0.00822424, step = 801
INFO:tensorflow:global_step/sec: 386.302
INFO:tensorflow:loss = 0.00728025, step = 901
INFO:tensorflow:global_step/sec: 471.553
INFO:tensorflow:loss = 0.00650725, step = 1001
INFO:tensorflow:global_step/sec: 491.744
INFO:tensorflow:loss = 0.00586252, step = 1101
INFO:tensorflow:global_step/sec: 445.735
INFO:tensorflow:loss = 0.00531728, step = 1201
INFO:tensorflow:global_step/sec: 409.596
INFO:tensorflow:loss = 0.00485022, step = 1301
INFO:tensorflow:global_step/sec: 485.849
INFO:tensorflow:loss = 0.00444582, step = 1401
INFO:tensorflow:global_step/sec: 308.093
INFO:tensorflow:loss = 0.00409275, step = 1501
INFO:tensorflow:global_step/sec: 258.719
INFO:tensorflow:loss = 0.00378207, step = 1601
INFO:tensorflow:global_step/sec: 409.537
INFO:tensorflow:loss = 0.0035069, step = 1701
INFO:tensorflow:global_step/sec: 464.613
INFO:tensorflow:loss = 0.00326178, step = 1801
INFO:tensorflow:global_step/sec: 462.353
INFO:tensorflow:loss = 0.00304244, step = 1901
INFO:tensorflow:Saving checkpoints for 2000 into /tmp/iris_model/model.ckpt.
INFO:tensorflow:Loss for final step: 0.00284715.
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/dnn.py:374: calling BaseEstimator.predict (from tensorflow.contrib.learn.python.learn.estimators.estimator) with x is deprecated and will be removed after 2016-12-01.
Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
est = Estimator(...) -> est = SKCompat(Estimator(...))
WARNING:tensorflow:float64 is not supported by many models, consider casting to float32.
WARNING:tensorflow:float64 is not supported by many models, consider casting to float32.
INFO:tensorflow:Using config: {'_keep_checkpoint_every_n_hours': 10000, '_save_checkpoints_steps': None, '_master': '', '_num_ps_replicas': 0, '_task_id': 0, '_tf_config': gpu_options {
per_process_gpu_memory_fraction: 1.0
}
, '_save_summary_steps': 100, '_is_chief': True, '_environment': 'local', '_evaluation_master': '', '_task_type': None, '_save_checkpoints_secs': 1, '_tf_random_seed': None, '_keep_checkpoint_max': 5, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x7f8fcc487a90>}
WARNING:tensorflow:From <ipython-input-4-d1be09113dc4>:49: calling BaseEstimator.fit (from tensorflow.contrib.learn.python.learn.estimators.estimator) with x is deprecated and will be removed after 2016-12-01.
Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
est = Estimator(...) -> est = SKCompat(Estimator(...))
WARNING:tensorflow:From <ipython-input-4-d1be09113dc4>:49: calling BaseEstimator.fit (from tensorflow.contrib.learn.python.learn.estimators.estimator) with y is deprecated and will be removed after 2016-12-01.
Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
est = Estimator(...) -> est = SKCompat(Estimator(...))
WARNING:tensorflow:float64 is not supported by many models, consider casting to float32.
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:1362: scalar_summary (from tensorflow.python.ops.logging_ops) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please switch to tf.summary.scalar. Note that tf.summary.scalar uses the node name instead of the tag. This means that TensorFlow will automatically de-duplicate summary names based on the scope they are created in. Also, passing a tensor or list of tags to a scalar summary op is no longer supported.
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Saving checkpoints for 1 into /tmp/iris_model_val/model.ckpt.
INFO:tensorflow:loss = 1.29196, step = 1
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/monitors.py:712: calling BaseEstimator.evaluate (from tensorflow.contrib.learn.python.learn.estimators.estimator) with x is deprecated and will be removed after 2016-12-01.
Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
est = Estimator(...) -> est = SKCompat(Estimator(...))
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/monitors.py:712: calling BaseEstimator.evaluate (from tensorflow.contrib.learn.python.learn.estimators.estimator) with y is deprecated and will be removed after 2016-12-01.
Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
est = Estimator(...) -> est = SKCompat(Estimator(...))
WARNING:tensorflow:float64 is not supported by many models, consider casting to float32.
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:1362: scalar_summary (from tensorflow.python.ops.logging_ops) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please switch to tf.summary.scalar. Note that tf.summary.scalar uses the node name instead of the tag. This means that TensorFlow will automatically de-duplicate summary names based on the scope they are created in. Also, passing a tensor or list of tags to a scalar summary op is no longer supported.
INFO:tensorflow:Starting evaluation at 2017-03-08-15:57:24
INFO:tensorflow:Finished evaluation at 2017-03-08-15:57:24
INFO:tensorflow:Saving dict for global step 1: accuracy = 0.208333, auc = 0.519965, global_step = 1, loss = 1.09549
WARNING:tensorflow:Skipping summary for global_step, must be a float or np.float32.
INFO:tensorflow:Validation (step 100): loss = 1.09549, global_step = 1, auc = 0.519965, accuracy = 0.208333
INFO:tensorflow:global_step/sec: 179.802
INFO:tensorflow:loss = 0.0726665, step = 101
INFO:tensorflow:global_step/sec: 406.432
INFO:tensorflow:loss = 0.0346194, step = 201
INFO:tensorflow:Saving checkpoints for 257 into /tmp/iris_model_val/model.ckpt.
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/monitors.py:712: calling BaseEstimator.evaluate (from tensorflow.contrib.learn.python.learn.estimators.estimator) with x is deprecated and will be removed after 2016-12-01.
Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
est = Estimator(...) -> est = SKCompat(Estimator(...))
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/monitors.py:712: calling BaseEstimator.evaluate (from tensorflow.contrib.learn.python.learn.estimators.estimator) with y is deprecated and will be removed after 2016-12-01.
Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
est = Estimator(...) -> est = SKCompat(Estimator(...))
WARNING:tensorflow:float64 is not supported by many models, consider casting to float32.
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:1362: scalar_summary (from tensorflow.python.ops.logging_ops) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please switch to tf.summary.scalar. Note that tf.summary.scalar uses the node name instead of the tag. This means that TensorFlow will automatically de-duplicate summary names based on the scope they are created in. Also, passing a tensor or list of tags to a scalar summary op is no longer supported.
INFO:tensorflow:Starting evaluation at 2017-03-08-15:57:25
INFO:tensorflow:Finished evaluation at 2017-03-08-15:57:25
INFO:tensorflow:Saving dict for global step 257: accuracy = 0.916667, auc = 0.981771, global_step = 257, loss = 0.313334
WARNING:tensorflow:Skipping summary for global_step, must be a float or np.float32.
INFO:tensorflow:Validation (step 300): loss = 0.313334, global_step = 257, auc = 0.981771, accuracy = 0.916667
INFO:tensorflow:global_step/sec: 149.344
INFO:tensorflow:loss = 0.0235893, step = 301
INFO:tensorflow:global_step/sec: 411.455
INFO:tensorflow:loss = 0.0181535, step = 401
INFO:tensorflow:global_step/sec: 570.049
INFO:tensorflow:loss = 0.0163982, step = 501
INFO:tensorflow:Saving checkpoints for 522 into /tmp/iris_model_val/model.ckpt.
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/monitors.py:712: calling BaseEstimator.evaluate (from tensorflow.contrib.learn.python.learn.estimators.estimator) with x is deprecated and will be removed after 2016-12-01.
Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
est = Estimator(...) -> est = SKCompat(Estimator(...))
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/monitors.py:712: calling BaseEstimator.evaluate (from tensorflow.contrib.learn.python.learn.estimators.estimator) with y is deprecated and will be removed after 2016-12-01.
Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
est = Estimator(...) -> est = SKCompat(Estimator(...))
WARNING:tensorflow:float64 is not supported by many models, consider casting to float32.
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:1362: scalar_summary (from tensorflow.python.ops.logging_ops) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please switch to tf.summary.scalar. Note that tf.summary.scalar uses the node name instead of the tag. This means that TensorFlow will automatically de-duplicate summary names based on the scope they are created in. Also, passing a tensor or list of tags to a scalar summary op is no longer supported.
INFO:tensorflow:Starting evaluation at 2017-03-08-15:57:26
INFO:tensorflow:Finished evaluation at 2017-03-08-15:57:26
INFO:tensorflow:Saving dict for global step 522: accuracy = 0.916667, auc = 0.963542, global_step = 522, loss = 0.353829
WARNING:tensorflow:Skipping summary for global_step, must be a float or np.float32.
INFO:tensorflow:Validation (step 600): loss = 0.353829, global_step = 522, auc = 0.963542, accuracy = 0.916667
INFO:tensorflow:Stopping. Best step: 300 with loss = 0.3133341073989868.
INFO:tensorflow:Saving checkpoints for 600 into /tmp/iris_model_val/model.ckpt.
INFO:tensorflow:Loss for final step: 0.0123381.
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/dnn.py:374: calling BaseEstimator.predict (from tensorflow.contrib.learn.python.learn.estimators.estimator) with x is deprecated and will be removed after 2016-12-01.
Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
est = Estimator(...) -> est = SKCompat(Estimator(...))
WARNING:tensorflow:float64 is not supported by many models, consider casting to float32.
score1: 0.966666666667
score2: 0.966666666667
score2 > score1 False
In [ ]:
Content source: LogicWang/ml
Similar notebooks: