ãTensorFlow 2.0 Complete Course - Python Neural Networks for Beginners Tutorialãããã¼ã¹ã«èªåç¨ã«èª¬æ追å ãããã®ã«ãªãã¾ãã
ãã®åç»ã¯ï¼æéãããã®ã§ã注æ
ãªããå®è¡ããã人ã¯ä¸è¨ã®NoteBookããå®è¡ã§ãã¾ãï¼è¦googleã¢ã«ã¦ã³ãï¼
https://colab.research.google.com/drive/1Paw5kceuTaOrH2USp2AuPzWKgbgudh_8
åºæ¬çãªæ©æ¢°å¦ç¿ã¢ã«ã´ãªãºã ã®
- åé¡
- ã¯ã©ã¹ã¿ãªã³ã°
ã¤ãã¦èª¬æãã¾ãã
åé¡ï¼Classificationï¼
å帰ã使ç¨ãã¦æ°å¤ãäºæ¸¬ããå ´åãåé¡ã¯ããã¼ã¿ãã¤ã³ããç°ãªãã©ãã«ã®ã¯ã©ã¹ã«åé¢ããããã«ä½¿ç¨ããã¾ãããã®ä¾ã§ã¯ãTensorFlow Estimatorã使ç¨ãã¦è±ãåé¡ãã¾ãã
åã«æ¨å®å¨ã®åä½ã«è§¦ããã®ã§ããã®ä¾ãããå°ãæ©ã説æãã¾ãã
ãã®ã»ã¯ã·ã§ã³ã¯ãTensorFlowã¦ã§ããµã¤ãã®æ¬¡ã®ã¬ã¤ãã«åºã¥ãã¦ãã¾ãã https://www.tensorflow.org/tutorials/estimator/premade
Imports ã¨ãã»ããã¢ãã
%tensorflow_version 2.x # ã³ã©ãã®ã¿ã§ã from __future__ import absolute_import, division, print_function, unicode_literals import tensorflow as tf import pandas as pd
ãã¼ã¿ã»ãã
ãã®ç¹å®ã®ãã¼ã¿ã»ããã¯ãè±ã3ã¤ã®ç°ãªãã¯ã©ã¹ã®ç¨®ã«åãã¾ãã
- çµ¹ã®ãããª
- ãã¼ã·ã«ã©ã¼
- ãã¼ã¸ãã«
åè±ã«é¢ããæ å ±ã¯ä»¥ä¸ã®éãã§ãã
- ããçã®é·ã
- ããçã®å¹
- è±ã³ãã®é·ã
- è±ã³ãã®å¹
# å¾ã§ã¤ããã®è¨å®ãã CSV_COLUMN_NAMES = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth', 'Species'] SPECIES = ['Setosa', 'Versicolor', 'Virginica'] # ããã§ã¯ keras (TensorFlowå ã®ã¢ã¸ã¥ã¼ã«) ã使ç¨ãã¦ãã¼ã¿ã»ãããåå¾ãpandaãã¼ã¿ãã¬ã¼ã ã«åãè¾¼ã train_path = tf.keras.utils.get_file( "iris_training.csv", "https://storage.googleapis.com/download.tensorflow.org/data/iris_training.csv") test_path = tf.keras.utils.get_file( "iris_test.csv", "https://storage.googleapis.com/download.tensorflow.org/data/iris_test.csv") train = pd.read_csv(train_path, names=CSV_COLUMN_NAMES, header=0) test = pd.read_csv(test_path, names=CSV_COLUMN_NAMES, header=0)
ãã¼ã¿ã®å 容ï¼ä¸ä½ï¼ä»¶ï¼
train.head()
種ã®åãããããã¦ããããã©ãã«ã¨ãã¦ä½¿ç¨
train_y = train.pop('Species') test_y = test.pop('Species') train.head() # the species column is now gone train.shape # (120, 4)
å ¥åæ©è½
以åã«ä½æããåä»ãªå ¥åé¢æ°ãæãåºãã¦ãã ãããããã§å¥ã®ãã®ãä½æããå¿ è¦ãããã¾ãï¼å¹¸ãã«ããããã¯å°ãæ¶åããããã§ãã
def input_fn(features, labels, training=True, batch_size=256): # å ¥åããã¼ã¿ã»ããã«å¤æãã¾ãã dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels)) # ãã¬ã¼ãã³ã°ã¢ã¼ãã®å ´åã¯ãã·ã£ããã«ãã¦ç¹°ãè¿ãã¾ãã if training: dataset = dataset.shuffle(1000).repeat() return dataset.batch(batch_size)
æ©è½å
my_feature_columns = [] for key in train.keys(): my_feature_columns.append(tf.feature_column.numeric_column(key=key)) print(my_feature_columns) # [ NumericColumn(key='SepalLength', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None) # , NumericColumn(key='SepalWidth', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None) # , NumericColumn(key='PetalLength', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None) # , NumericColumn(key='PetalWidth', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None)]
ã¢ãã«ã®æ§ç¯
ããã§ã¢ãã«ãé¸æããæºåãæ´ãã¾ãããåé¡ã¿ã¹ã¯ã«ã¯ããã¾ãã¾ãªæ¨å®å¨/ã¢ãã«ããé¸æã§ãã¾ããããã¤ãã®ãªãã·ã§ã³ã以ä¸ã«ç¤ºãã¾ãã
- DNNClassifier ï¼ãã£ã¼ããã¥ã¼ã©ã«ãããã¯ã¼ã¯ï¼
- LinearClassifier
ã©ã¡ãã®ã¢ãã«ãé¸æã§ãã¾ãããDNNãæè¯ã®é¸æã®ããã§ããããã¯ããã¼ã¿å ã§ç·å½¢å¯¾å¿é¢ä¿ãè¦ã¤ãããã¨ãã§ããªãå ´åãããããã§ãã
ããã§ã¯ãã¢ãã«ãä½æãã¾ãããï¼
# ããããï¼ï¼ããã³ï¼ï¼ã®é表示ãã¼ãããã¤ï¼ã¤ã®ã¬ã¤ã¤ã¼ãæã¤ãã£ã¼ããã¥ã¼ã©ã«ãããã¯ã¼ã¯ãæ§ç¯ãã¾ã. classifier = tf.estimator.DNNClassifier( feature_columns=my_feature_columns, # ããããï¼ï¼ããã³ï¼ï¼ãã¼ãã®ï¼ã¤ã¬ã¤ã¤ã¼. hidden_units=[30, 10], # ã¢ãã«ã¯ï¼ã¤ã®ã¯ã©ã¹ããé¸æããå¿ è¦ n_classes=3) # INFO:tensorflow:Using default config. # WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmpge34j5tq # INFO:tensorflow:Using config: # {'_model_dir': '/tmp/tmpge34j5tq', '_tf_random_seed': None, '_save_summary_steps' # : 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs' # : 600, '_session_config': allow_soft_placement: true # graph_options { # rewrite_options { # meta_optimizer_iterations: ONE # } # } # , '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000 # , '_log_step_count_steps': 100, '_train_distribute': None # , '_device_fn': None, '_protocol': None, '_eval_distribute': None # , '_experimental_distribute': None, '_experimental_max_worker_delay_secs': None # , '_session_creation_timeout_secs': 7200, '_service': None # , '_cluster_spec': ClusterSpec({}), '_task_type': 'worker' # , '_task_id': 0, '_global_id_in_cluster': 0, '_master': '' # , '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0 # , '_num_worker_replicas': 1}
ä»è¡ã£ãã®ã¯ã2ã¤ã®é ã層ãæã¤ãã£ã¼ããã¥ã¼ã©ã«ãããã¯ã¼ã¯ãä½æãããã¨ã§ãã ãããã®å±¤ã«ã¯ããããã30åã¨10åã®ãã¥ã¼ãã³ãããã¾ãã ããã¯TensorFlowå ¬å¼ãã¥ã¼ããªã¢ã«ã使ç¨ãããã¥ã¼ãã³ã®æ°ãªã®ã§ãããã«åºå·ãã¾ãã ãã ããé ãããã¥ã¼ãã³ã®æ°ã¯ä»»æã®æ°ã§ãããé常ããããã®å¤ã®æé©ãªé¸æã 決å®ããããã«å¤ãã®å®é¨ã¨ãã¹ããè¡ããããã¨ã«è¨åãã価å¤ãããã¾ãã é表示ã®ãã¥ã¼ãã³ã®æ°ã試ãã¦ã¿ã¦ãçµæãå¤åãããã©ããã確èªãã¦ãã ããã
ãã¬ã¼ãã³ã°
classifier.train( input_fn=lambda: input_fn(train, train_y, training=True), steps=10000) # If you intended to run this layer in float32, you can safely ignore this warning. If in doubt, this warning is likely only an issue if you are porting a TensorFlow 1.X model to TensorFlow 2. # To change all layers to have dtype float64 by default, call `tf.keras.backend.set_floatx('float64')`. To change just this layer, pass dtype='float64' to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor. # INFO:tensorflow:Done calling model_fn. # INFO:tensorflow:Create CheckpointSaverHook. # INFO:tensorflow:Graph was finalized. # INFO:tensorflow:Restoring parameters from /tmp/tmpge34j5tq/model.ckpt-5000 # WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/training/saver.py:1077: get_checkpoint_mtimes (from tensorflow.python.training.checkpoint_management) is deprecated and will be removed in a future version. # Instructions for updating: # Use standard file utilities to get mtimes. # INFO:tensorflow:Running local_init_op. # INFO:tensorflow:Done running local_init_op. # INFO:tensorflow:Calling checkpoint listeners before saving checkpoint 5000... # INFO:tensorflow:Saving checkpoints for 5000 into /tmp/tmpge34j5tq/model.ckpt. # INFO:tensorflow:Calling checkpoint listeners after saving checkpoint 5000... # INFO:tensorflow:loss = 0.48039886, step = 5000 # INFO:tensorflow:global_step/sec: 492.458 # INFO:tensorflow:loss = 0.47152814, step = 5100 (0.204 sec) # ã» # ã» # ã» # INFO:tensorflow:global_step/sec: 647.212 # INFO:tensorflow:loss = 0.3012737, step = 14800 (0.158 sec) # INFO:tensorflow:global_step/sec: 651.296 # INFO:tensorflow:loss = 0.30669963, step = 14900 (0.152 sec) # INFO:tensorflow:Calling checkpoint listeners before saving checkpoint 15000... # INFO:tensorflow:Saving checkpoints for 15000 into /tmp/tmpge34j5tq/model.ckpt. # INFO:tensorflow:Calling checkpoint listeners after saving checkpoint 15000... # INFO:tensorflow:Loss for final step: 0.3042315. # <tensorflow_estimator.python.estimator.canned.dnn.DNNClassifierV2 at 0x7f43e12a9390>
â»ï¼ååããã¦ããã®ã§5000ããã«ãªã£ã¦ãã¾ã
ããã§èª¬æããã®ã¯ãstepså¼æ°ã ãã§ããããã¯åã«5000ã¹ãããã§å®è¡ããããã«åé¡åã«ä¼ãã¾ãããããå¤æ´ãã¦ãçµæãå¤åãããã©ããã確èªãã¦ãã ãããå¤ãã»ã©è¯ãã¨ã¯éããªããã¨ã«æ³¨æãã¦ãã ããã
è©ä¾¡
eval_result = classifier.evaluate( input_fn=lambda: input_fn(test, test_y, training=False)) print('\nTest set accuracy: {accuracy:0.3f}\n'.format(**eval_result)) # To change all layers to have dtype float64 by default, call `tf.keras.backend.set_floatx('float64')`. To change just this layer, pass dtype='float64' to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor. # INFO:tensorflow:Done calling model_fn. # INFO:tensorflow:Starting evaluation at 2020-04-26T12:41:41Z # INFO:tensorflow:Graph was finalized. # INFO:tensorflow:Restoring parameters from /tmp/tmpge34j5tq/model.ckpt-15000 # INFO:tensorflow:Running local_init_op. # INFO:tensorflow:Done running local_init_op. # INFO:tensorflow:Inference Time : 0.21235s # INFO:tensorflow:Finished evaluation at 2020-04-26-12:41:41 # INFO:tensorflow:Saving dict for global step 15000: accuracy = 0.93333334, average_loss = 0.36485243, global_step = 15000, loss = 0.36485243 # INFO:tensorflow:Saving 'checkpoint_path' summary for global step 15000: /tmp/tmpge34j5tq/model.ckpt-15000 Test set accuracy: 0.933
ä»åã¯ã¹ãããæ°ãæå®ãã¦ããªããã¨ã«æ³¨æãã¦ãã ãããããã¯ãè©ä¾¡ä¸ã«ã¢ãã«ããã¹ããã¼ã¿ã1åããè¦ãªãããã§ãã
äºæ¸¬
è¨ç·´ãããã¢ãã«ãã§ããã®ã§ãããã使ç¨ãã¦äºæ¸¬ãè¡ãã¾ãã
以ä¸ã«ãè±ã®ç¹å¾´ãå
¥åãã¦ãã®ã¯ã©ã¹ã®äºæ¸¬ã
確èªã§ããå°ããªã¹ã¯ãªãããæ¸ãã¾ãã
def input_fn(features, batch_size=256): # å ¥åãã©ãã«ãªãã®ãã¼ã¿ã»ããã«å¤æãã¾ã return tf.data.Dataset.from_tensor_slices(dict(features)).batch(batch_size) features = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'] predict = {} print("Please type numeric values as prompted.") for feature in features: valid = True while valid: val = input(feature + ": ") if not val.isdigit(): valid = False predict[feature] = [float(val)] predictions = classifier.predict(input_fn=lambda: input_fn(predict)) for pred_dict in predictions: class_id = pred_dict['class_ids'][0] probability = pred_dict['probabilities'][class_id] print('Prediction is "{}" ({:.1f}%)'.format( SPECIES[class_id], 100 * probability)) # Please type numeric values as prompted. # SepalLength: 2.4 âãã®é¨åã¯å ¥åããé¨åã§ã # SepalWidth: 2.6ã âãã®é¨åã¯å ¥åããé¨åã§ã # PetalLength: 6.5ã âãã®é¨åã¯å ¥åããé¨åã§ã # PetalWidth: 6.3ã âãã®é¨åã¯å ¥åããé¨åã§ã # INFO:tensorflow:Calling model_fn. # INFO:tensorflow:Done calling model_fn. # INFO:tensorflow:Graph was finalized. # INFO:tensorflow:Restoring parameters from /tmp/tmpge34j5tq/model.ckpt-15000 # INFO:tensorflow:Running local_init_op. # INFO:tensorflow:Done running local_init_op. # Prediction is "Virginica" (99.8%)
ã¯ã©ã¹ã¿ãªã³ã°(Clustering)
ã¯ã©ã¹ã¿ãªã³ã°ã¯ããã¼ã¿ãã¤ã³ãã®ã°ã«ã¼ãåãä¼´ãæ©æ¢°å¦ç¿ææ³ã§ããçè«çã«ã¯ãåãã°ã«ã¼ãå
ã®ãã¼ã¿ãã¤ã³ãã¯é¡ä¼¼ã®ããããã£ãæ©è½ãæã¤å¿
è¦ãããã¾ãããç°ãªãã°ã«ã¼ãã®ãã¼ã¿ãã¤ã³ãã¯é常ã«ç°ãªãããããã£ãæ©è½ãæã¤å¿
è¦ãããã¾ãã
ï¼https://towardsdatascience.com/the-5-clustering-algorithms-data-scientists-need-to-know-a36d136ef68ï¼
æ®å¿µãªãããTensorFlowã®ç¾å¨ã®ãã¼ã¸ã§ã³ã¨KMeansã®å®è£ ã«ã¯åé¡ãããã¾ããã¤ã¾ããã¼ãããã¢ã«ã´ãªãºã ãä½æããªãã¨KMeansã使ç¨ã§ãã¾ãããã¾ã ãã®ã¬ãã«ã«ã¯éãã¦ããªãã®ã§ãããã§ã¯ã¯ã©ã¹ã¿ãªã³ã°ã®åºæ¬ã«ã¤ãã¦èª¬æãã¾ãã
åºæ¬ã¢ã«ã´ãªãºã ã
- ã¹ããã1ï¼Kç¹ãã©ã³ãã ã«é¸æãã¦Kã®å³å¿ãé ç½®ãã
- ã¹ããã2ï¼ãã¹ã¦ã®ãã¼ã¿ãã¤ã³ããè·é¢ã§éå¿ã«å²ãå½ã¦ã¾ãããã¤ã³ãã«æãè¿ãéå¿ã¯ããããå²ãå½ã¦ããã¦ãããã®ã§ãã
- ã¹ããã3ï¼åéå¿ã«å±ãããã¹ã¦ã®ãã¤ã³ããå¹³åãã¦ããããã®ã¯ã©ã¹ã¿ã¼ã®ä¸å¤®ï¼éå¿ï¼ãè¦ã¤ãã¾ãã対å¿ããå³å¿ããã®ä½ç½®ã«é ç½®ãã¾ãã
- ã¹ããã4ï¼ãã¹ã¦ã®ãã¤ã³ããæãè¿ãéå¿ã«å度å²ãå½ã¦ã¾ãã
- ã¹ããã5ï¼æå±ããå³å¿ãå¤åããªãç¹ã¾ã§ãã¹ããã3ã4ãç¹°ãè¿ãã¾ãã