From a9f91702eb1a344fefa1a79279c9fc947138a66a Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 1 Apr 2023 19:38:58 +0300 Subject: [PATCH 01/17] Remove python_version < "3.11" for tensorflow --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index a1d607df07e1..acfbc823e77f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,7 +15,7 @@ scikit-fuzzy scikit-learn statsmodels sympy -tensorflow; python_version < "3.11" +tensorflow texttable tweepy xgboost From 419b253747a9bea69c870dd8fb6dc84c4986ed36 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 1 Apr 2023 19:45:51 +0300 Subject: [PATCH 02/17] Reenable neural_network/input_data.py_tf --- neural_network/{input_data.py_tf => input_data.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename neural_network/{input_data.py_tf => input_data.py} (100%) diff --git a/neural_network/input_data.py_tf b/neural_network/input_data.py similarity index 100% rename from neural_network/input_data.py_tf rename to neural_network/input_data.py From 49e9ce677167e51096154cd479eedf1edacbf1b7 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 1 Apr 2023 16:46:11 +0000 Subject: [PATCH 03/17] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index c781b17bf05f..51cb2a2d1a12 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -685,6 +685,7 @@ * [2 Hidden Layers Neural Network](neural_network/2_hidden_layers_neural_network.py) * [Back Propagation Neural Network](neural_network/back_propagation_neural_network.py) * [Convolution Neural Network](neural_network/convolution_neural_network.py) + * [Input Data](neural_network/input_data.py) * [Perceptron](neural_network/perceptron.py) * [Simple Neural Network](neural_network/simple_neural_network.py) From e621deca28e4899963e65f78366692483f2880a5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 1 Apr 2023 16:46:35 +0000 Subject: [PATCH 04/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/input_data.py | 80 ++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/neural_network/input_data.py b/neural_network/input_data.py index 0e22ac0bcda5..64459c1d8dd9 100644 --- a/neural_network/input_data.py +++ b/neural_network/input_data.py @@ -46,16 +46,16 @@ def _read32(bytestream): def _extract_images(f): """Extract the images into a 4D uint8 numpy array [index, y, x, depth]. - Args: - f: A file object that can be passed into a gzip reader. + Args: + f: A file object that can be passed into a gzip reader. - Returns: - data: A 4D uint8 numpy array [index, y, x, depth]. + Returns: + data: A 4D uint8 numpy array [index, y, x, depth]. - Raises: - ValueError: If the bytestream does not start with 2051. + Raises: + ValueError: If the bytestream does not start with 2051. - """ + """ print("Extracting", f.name) with gzip.GzipFile(fileobj=f) as bytestream: magic = _read32(bytestream) @@ -86,17 +86,17 @@ def _dense_to_one_hot(labels_dense, num_classes): def _extract_labels(f, one_hot=False, num_classes=10): """Extract the labels into a 1D uint8 numpy array [index]. - Args: - f: A file object that can be passed into a gzip reader. - one_hot: Does one hot encoding for the result. - num_classes: Number of classes for the one hot encoding. + Args: + f: A file object that can be passed into a gzip reader. + one_hot: Does one hot encoding for the result. + num_classes: Number of classes for the one hot encoding. - Returns: - labels: a 1D uint8 numpy array. + Returns: + labels: a 1D uint8 numpy array. - Raises: - ValueError: If the bystream doesn't start with 2049. - """ + Raises: + ValueError: If the bystream doesn't start with 2049. + """ print("Extracting", f.name) with gzip.GzipFile(fileobj=f) as bytestream: magic = _read32(bytestream) @@ -115,8 +115,8 @@ def _extract_labels(f, one_hot=False, num_classes=10): class _DataSet: """Container class for a _DataSet (deprecated). - THIS CLASS IS DEPRECATED. - """ + THIS CLASS IS DEPRECATED. + """ @deprecated( None, @@ -135,21 +135,21 @@ def __init__( ): """Construct a _DataSet. - one_hot arg is used only if fake_data is true. `dtype` can be either - `uint8` to leave the input as `[0, 255]`, or `float32` to rescale into - `[0, 1]`. Seed arg provides for convenient deterministic testing. - - Args: - images: The images - labels: The labels - fake_data: Ignore inages and labels, use fake data. - one_hot: Bool, return the labels as one hot vectors (if True) or ints (if - False). - dtype: Output image dtype. One of [uint8, float32]. `uint8` output has - range [0,255]. float32 output has range [0,1]. - reshape: Bool. If True returned images are returned flattened to vectors. - seed: The random seed to use. - """ + one_hot arg is used only if fake_data is true. `dtype` can be either + `uint8` to leave the input as `[0, 255]`, or `float32` to rescale into + `[0, 1]`. Seed arg provides for convenient deterministic testing. + + Args: + images: The images + labels: The labels + fake_data: Ignore inages and labels, use fake data. + one_hot: Bool, return the labels as one hot vectors (if True) or ints (if + False). + dtype: Output image dtype. One of [uint8, float32]. `uint8` output has + range [0,255]. float32 output has range [0,1]. + reshape: Bool. If True returned images are returned flattened to vectors. + seed: The random seed to use. + """ seed1, seed2 = random_seed.get_seed(seed) # If op level seed is not set, use whatever graph level seed is returned numpy.random.seed(seed1 if seed is None else seed2) @@ -250,14 +250,14 @@ def next_batch(self, batch_size, fake_data=False, shuffle=True): def _maybe_download(filename, work_directory, source_url): """Download the data from source url, unless it's already here. - Args: - filename: string, name of the file in the directory. - work_directory: string, path to working directory. - source_url: url to download from if file doesn't exist. + Args: + filename: string, name of the file in the directory. + work_directory: string, path to working directory. + source_url: url to download from if file doesn't exist. - Returns: - Path to resulting file. - """ + Returns: + Path to resulting file. + """ if not gfile.Exists(work_directory): gfile.MakeDirs(work_directory) filepath = os.path.join(work_directory, filename) From a05c0d46298f0a5bbdda6c987177da0bbb6ef15a Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 1 Apr 2023 19:57:22 +0300 Subject: [PATCH 05/17] Try to fix ruff --- neural_network/input_data.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/neural_network/input_data.py b/neural_network/input_data.py index 0e22ac0bcda5..454f9b2b5e32 100644 --- a/neural_network/input_data.py +++ b/neural_network/input_data.py @@ -20,9 +20,9 @@ import collections import gzip +import numpy import os -import numpy from six.moves import urllib from six.moves import xrange # pylint: disable=redefined-builtin @@ -262,7 +262,7 @@ def _maybe_download(filename, work_directory, source_url): gfile.MakeDirs(work_directory) filepath = os.path.join(work_directory, filename) if not gfile.Exists(filepath): - urllib.request.urlretrieve(source_url, filepath) + urllib.request.urlretrieve(source_url, filepath) #nosec with gfile.GFile(filepath) as f: size = f.size() print("Successfully downloaded", filename, size, "bytes.") @@ -328,7 +328,8 @@ def fake(): if not 0 <= validation_size <= len(train_images): raise ValueError( - f"Validation size should be between 0 and {len(train_images)}. Received: {validation_size}." + f"Validation size should be between 0 and {len(train_images)}. " + f"Received: {validation_size}." ) validation_images = train_images[:validation_size] @@ -336,7 +337,7 @@ def fake(): train_images = train_images[validation_size:] train_labels = train_labels[validation_size:] - options = dict(dtype=dtype, reshape=reshape, seed=seed) + options = {'dtype': dtype, 'reshape': reshape, 'seed': seed} train = _DataSet(train_images, train_labels, **options) validation = _DataSet(validation_images, validation_labels, **options) From 7ba23c6ac7be20e4e6023fc435ec4718bfd3f954 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 1 Apr 2023 16:58:10 +0000 Subject: [PATCH 06/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/input_data.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/neural_network/input_data.py b/neural_network/input_data.py index e7a3a4dacaa3..82344a98ab44 100644 --- a/neural_network/input_data.py +++ b/neural_network/input_data.py @@ -262,7 +262,7 @@ def _maybe_download(filename, work_directory, source_url): gfile.MakeDirs(work_directory) filepath = os.path.join(work_directory, filename) if not gfile.Exists(filepath): - urllib.request.urlretrieve(source_url, filepath) #nosec + urllib.request.urlretrieve(source_url, filepath) # nosec with gfile.GFile(filepath) as f: size = f.size() print("Successfully downloaded", filename, size, "bytes.") @@ -337,7 +337,7 @@ def fake(): train_images = train_images[validation_size:] train_labels = train_labels[validation_size:] - options = {'dtype': dtype, 'reshape': reshape, 'seed': seed} + options = {"dtype": dtype, "reshape": reshape, "seed": seed} train = _DataSet(train_images, train_labels, **options) validation = _DataSet(validation_images, validation_labels, **options) From 2b8209b48876c13d5010c4352b699380a71abee3 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 1 Apr 2023 20:04:32 +0300 Subject: [PATCH 07/17] Try to fix ruff --- neural_network/input_data.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/neural_network/input_data.py b/neural_network/input_data.py index 82344a98ab44..b2c11f12584f 100644 --- a/neural_network/input_data.py +++ b/neural_network/input_data.py @@ -20,9 +20,8 @@ import collections import gzip -import numpy -import os +import numpy from six.moves import urllib from six.moves import xrange # pylint: disable=redefined-builtin @@ -31,6 +30,8 @@ from tensorflow.python.platform import gfile from tensorflow.python.util.deprecation import deprecated +import os + _Datasets = collections.namedtuple("_Datasets", ["train", "validation", "test"]) # CVDF mirror of http://yann.lecun.com/exdb/mnist/ @@ -262,7 +263,7 @@ def _maybe_download(filename, work_directory, source_url): gfile.MakeDirs(work_directory) filepath = os.path.join(work_directory, filename) if not gfile.Exists(filepath): - urllib.request.urlretrieve(source_url, filepath) # nosec + urllib.request.urlretrieve(source_url, filepath) # noqa: S310 with gfile.GFile(filepath) as f: size = f.size() print("Successfully downloaded", filename, size, "bytes.") From dae6b330473a547c5bf82b8bdcc01830c7d3e53f Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 1 Apr 2023 20:07:59 +0300 Subject: [PATCH 08/17] Try to fix ruff --- neural_network/input_data.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/neural_network/input_data.py b/neural_network/input_data.py index b2c11f12584f..4a85b3e9ff79 100644 --- a/neural_network/input_data.py +++ b/neural_network/input_data.py @@ -20,18 +20,15 @@ import collections import gzip +import os import numpy -from six.moves import urllib -from six.moves import xrange # pylint: disable=redefined-builtin +from six.moves import urllib, xrange -from tensorflow.python.framework import dtypes -from tensorflow.python.framework import random_seed +from tensorflow.python.framework import dtypes, random_seed from tensorflow.python.platform import gfile from tensorflow.python.util.deprecation import deprecated -import os - _Datasets = collections.namedtuple("_Datasets", ["train", "validation", "test"]) # CVDF mirror of http://yann.lecun.com/exdb/mnist/ From 8631e9d9f1555ba1375ee2bcfe48cab6eb67cf63 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 1 Apr 2023 20:09:19 +0300 Subject: [PATCH 09/17] Try to fix ruff --- neural_network/input_data.py | 1 - 1 file changed, 1 deletion(-) diff --git a/neural_network/input_data.py b/neural_network/input_data.py index 4a85b3e9ff79..dd48493ad91f 100644 --- a/neural_network/input_data.py +++ b/neural_network/input_data.py @@ -24,7 +24,6 @@ import numpy from six.moves import urllib, xrange - from tensorflow.python.framework import dtypes, random_seed from tensorflow.python.platform import gfile from tensorflow.python.util.deprecation import deprecated From 59cc927aa5204d5d4247257bc580a79f0a7cbcb6 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 1 Apr 2023 20:11:17 +0300 Subject: [PATCH 10/17] Try to fix pre-commit --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index acfbc823e77f..fbfdfa1e1971 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,5 +18,6 @@ sympy tensorflow texttable tweepy +types-six xgboost yulewalker From 1020d3d252e523feacf18ea202376f812f0a2c83 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 1 Apr 2023 20:20:00 +0300 Subject: [PATCH 11/17] Try to fix --- neural_network/input_data.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/neural_network/input_data.py b/neural_network/input_data.py index dd48493ad91f..2a32f0b82c37 100644 --- a/neural_network/input_data.py +++ b/neural_network/input_data.py @@ -21,9 +21,9 @@ import collections import gzip import os +import urllib import numpy -from six.moves import urllib, xrange from tensorflow.python.framework import dtypes, random_seed from tensorflow.python.platform import gfile from tensorflow.python.util.deprecation import deprecated @@ -203,8 +203,8 @@ def next_batch(self, batch_size, fake_data=False, shuffle=True): else: fake_label = 0 return ( - [fake_image for _ in xrange(batch_size)], - [fake_label for _ in xrange(batch_size)], + [fake_image for _ in range(batch_size)], + [fake_label for _ in range(batch_size)], ) start = self._index_in_epoch # Shuffle for the first epoch From 75386546ae6bb16c0184e87fb8bf9e9cc40f3010 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 1 Apr 2023 20:21:28 +0300 Subject: [PATCH 12/17] Fix --- neural_network/input_data.py | 2 +- requirements.txt | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/neural_network/input_data.py b/neural_network/input_data.py index 2a32f0b82c37..b9063bcb94b5 100644 --- a/neural_network/input_data.py +++ b/neural_network/input_data.py @@ -259,7 +259,7 @@ def _maybe_download(filename, work_directory, source_url): gfile.MakeDirs(work_directory) filepath = os.path.join(work_directory, filename) if not gfile.Exists(filepath): - urllib.request.urlretrieve(source_url, filepath) # noqa: S310 + urllib.request.urlretrieve(source_url, filepath) with gfile.GFile(filepath) as f: size = f.size() print("Successfully downloaded", filename, size, "bytes.") diff --git a/requirements.txt b/requirements.txt index fbfdfa1e1971..acfbc823e77f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,6 +18,5 @@ sympy tensorflow texttable tweepy -types-six xgboost yulewalker From 596df568123a4d80a94f79039196ddc857b9e9d3 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 1 Apr 2023 20:23:43 +0300 Subject: [PATCH 13/17] Fix --- neural_network/input_data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neural_network/input_data.py b/neural_network/input_data.py index b9063bcb94b5..2a32f0b82c37 100644 --- a/neural_network/input_data.py +++ b/neural_network/input_data.py @@ -259,7 +259,7 @@ def _maybe_download(filename, work_directory, source_url): gfile.MakeDirs(work_directory) filepath = os.path.join(work_directory, filename) if not gfile.Exists(filepath): - urllib.request.urlretrieve(source_url, filepath) + urllib.request.urlretrieve(source_url, filepath) # noqa: S310 with gfile.GFile(filepath) as f: size = f.size() print("Successfully downloaded", filename, size, "bytes.") From 60ca5334dac36018a585bce8c849c81710ddd742 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 1 Apr 2023 20:27:52 +0300 Subject: [PATCH 14/17] Reenable dynamic_programming/k_means_clustering_tensorflow.py_tf --- ...ustering_tensorflow.py_tf => k_means_clustering_tensorflow.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename dynamic_programming/{k_means_clustering_tensorflow.py_tf => k_means_clustering_tensorflow.py} (100%) diff --git a/dynamic_programming/k_means_clustering_tensorflow.py_tf b/dynamic_programming/k_means_clustering_tensorflow.py similarity index 100% rename from dynamic_programming/k_means_clustering_tensorflow.py_tf rename to dynamic_programming/k_means_clustering_tensorflow.py From 0c8c9fe109fb67f9e931b6bd90a2eefe642de761 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 1 Apr 2023 17:28:19 +0000 Subject: [PATCH 15/17] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 51cb2a2d1a12..34967082b359 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -309,6 +309,7 @@ * [Floyd Warshall](dynamic_programming/floyd_warshall.py) * [Integer Partition](dynamic_programming/integer_partition.py) * [Iterating Through Submasks](dynamic_programming/iterating_through_submasks.py) + * [K Means Clustering Tensorflow](dynamic_programming/k_means_clustering_tensorflow.py) * [Knapsack](dynamic_programming/knapsack.py) * [Longest Common Subsequence](dynamic_programming/longest_common_subsequence.py) * [Longest Common Substring](dynamic_programming/longest_common_substring.py) From 4dfa2a626fc7d7de6c92a3882ba6f1daa441c7b1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 1 Apr 2023 17:28:25 +0000 Subject: [PATCH 16/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/k_means_clustering_tensorflow.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/dynamic_programming/k_means_clustering_tensorflow.py b/dynamic_programming/k_means_clustering_tensorflow.py index 4fbcedeaa0dc..cd274d597ff8 100644 --- a/dynamic_programming/k_means_clustering_tensorflow.py +++ b/dynamic_programming/k_means_clustering_tensorflow.py @@ -30,7 +30,6 @@ def TFKMeansCluster(vectors, noofclusters): graph = tf.Graph() with graph.as_default(): - # SESSION OF COMPUTATION sess = tf.Session() @@ -96,7 +95,6 @@ def TFKMeansCluster(vectors, noofclusters): # iterations, instead of using a Stopping Criterion. noofiterations = 100 for iteration_n in range(noofiterations): - ##EXPECTATION STEP ##Based on the centroid locations till last iteration, compute ##the _expected_ centroid assignments. From c34b617a5200e390760b1595d1eb49a6c083c192 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 1 Apr 2023 20:31:02 +0300 Subject: [PATCH 17/17] Try to fix ruff --- dynamic_programming/k_means_clustering_tensorflow.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/dynamic_programming/k_means_clustering_tensorflow.py b/dynamic_programming/k_means_clustering_tensorflow.py index cd274d597ff8..8d3f6f0dfbcb 100644 --- a/dynamic_programming/k_means_clustering_tensorflow.py +++ b/dynamic_programming/k_means_clustering_tensorflow.py @@ -1,9 +1,10 @@ -import tensorflow as tf from random import shuffle + +import tensorflow as tf from numpy import array -def TFKMeansCluster(vectors, noofclusters): +def tf_k_means_cluster(vectors, noofclusters): """ K-Means Clustering using TensorFlow. 'vectors' should be a n*k 2-D NumPy array, where n is the number @@ -94,7 +95,7 @@ def TFKMeansCluster(vectors, noofclusters): # iterations. To keep things simple, we will only do a set number of # iterations, instead of using a Stopping Criterion. noofiterations = 100 - for iteration_n in range(noofiterations): + for _ in range(noofiterations): ##EXPECTATION STEP ##Based on the centroid locations till last iteration, compute ##the _expected_ centroid assignments.