Row 11699

Row ID: 11699 | Dataset Entry | Axioma AXP Content Repository

Content Data

This page contains data entry 11699 from the Axioma AXP content repository. The structured data below represents the complete record for this entry.

I am using TensorFlow 2.16 and Python3 for implementing an AutoEncoder and Self-Organizing Map for MNIST dataset. The entire code can be referred to [here](https://github.com/arjun-majumdar/Autoencoders_Experiments/blob/master/desom_tensorflow2.py). For brevity, the main code is:

# SOM hyper-params- map_height = 10 map_width = 10 gamma = 0.001 # Total number of train steps/iterations- total_iterations = len(train_dataset) * num_epochs # Temperature hyper-parm controlling radius of Gaussian neighborhood- Tmax = 10.0 Tmin = 0.1 class DESOM(Model): def __init__( self, map_height = 10, map_width = 10, latent_dim = 50, encoder_dims = [1, 500, 500, 100] ): super(DESOM, self).__init__() self.map_height = map_height self.map_width = map_width self.map_size = (self.map_height, self.map_width) self.latent_dim = latent_dim self.n_prototypes = self.map_size[0] * self.map_size[1] self.encoder_dims = encoder_dims self.encoder_dims.append(self.latent_dim) self.autoencoder, self.encoder, self.decoder = mlp_autoencoder( # encoder_dims = [X_train.shape[-1], 500, 500, 2000, latent_dim], encoder_dims = self.encoder_dims, act = 'relu', init = 'glorot_uniform', batchnorm = False ) # Initialize SOM layer- self.som_layer = SOMLayer( map_size = (self.map_height, self.map_width), name = 'SOM' )(self.encoder.output) # Create DESOM model self.model = Model( inputs = self.autoencoder.input, outputs = [self.autoencoder.output, self.som_layer] ) def compile(self, gamma:float = 0.001, optimizer:str = 'adam') -> None: """ Compile DESOM model Parameters ---------- gamma : float coefficient of SOM loss (hyperparameter) optimizer : str (default='adam') optimization algorithm """ self.model.compile( loss = {'decoder_0': 'mse', 'SOM': som_loss}, # loss_weights = [1, gamma], loss_weights = {'decoder_0': 1.0, 'SOM': gamma}, optimizer = optimizer ) return None def predict(self, x): """ Predict best-matching unit using the output of SOM layer Parameters ---------- x : array, shape = [n_samples, input_dim] or [n_samples, height, width, channels] input samples Returns ------- y_pred : array, shape = [n_samples] index of the best-matching unit """ _, d = self.model.predict(x, verbose = 0) return d.argmin(axis = 1) def map_dist(self, y_pred): """ Calculate pairwise Manhattan distances between cluster assignments and map prototypes (rectangular grid topology) Parameters ---------- y_pred : array, shape = [n_samples] cluster assignments Returns ------- d : array, shape = [n_samples, n_prototypes] pairwise distance matrix on the map """ # y_pred = tf.argmin(input = pairwise_squared_l2dist, axis = 1) labels = tf.range(self.n_prototypes) tmp = tf.cast( x = tf.expand_dims(input = y_pred, axis = 1), dtype = tf.dtypes.int32 ) # print(labels.dtype, tmp.dtype, y_pred.dtype) d_row = tf.abs(tmp - labels) // self.map_size[1] d_col = tf.abs(tmp % self.map_size[1] - labels % self.map_size[1]) # (d_row + d_col).dtype # tf.int32 d_row = tf.cast(x = d_row, dtype = tf.dtypes.float32) d_col = tf.cast(x = d_col, dtype = tf.dtypes.float32) return d_row + d_col def neighborhood_function( self, d, T, neighborhood = 'gaussian' ): """ SOM neighborhood function (Gaussian neighborhood) Parameters ---------- d : int distance on the map T : float temperature parameter (neighborhood radius) neighborhood : str type of neighborhood function ('gaussian' or 'window') Returns ------- w : float in [0, 1] neighborhood weights """ if neighborhood == 'gaussian': # return np.exp(-(d ** 2) / (T ** 2)) return tf.exp(-tf.square(d) / tf.square(T)) elif neighborhood == 'window': # return (d <= T).astype(np.float32) return tf.cast(x = (d <= T), dtype = tf.dtypes.float32) else: raise ValueError('invalid neighborhood function') # Initialize MLP AutoEncoder DESOM model- model = DESOM( map_height = map_height, map_width = map_width, latent_dim = latent_dim, encoder_dims = [784, 500, 500, 100] ) # Compile model- model.compile(gamma = gamma, optimizer = 'adam') # Required for computing temperature for current train step- # curr_iter = 1 curr_iter = tf.constant(1) total_iterations = tf.cast(x = total_iterations, dtype = tf.dtypes.int32) # Train loss- train_loss = list() for epoch in range(1, num_epochs + 1): for x, _ in train_dataset: # Compute bmu/cluster assignments for batch- # _, d = model.model.predict(x) _, d = model.model(x) # y_pred = d.argmin(axis = 1) y_pred = tf.argmin(input = d, axis = 1) y_pred = tf.cast(x = y_pred, dtype = tf.dtypes.float32) # y_pred.shape, d.shape # ((1024,), (1024, 100)) # Compute temperature for current train step- curr_T = tf.cast( x = Tmax * tf.pow((Tmin / Tmax), (curr_iter / total_iterations)), dtype = tf.dtypes.float32 ) # Compute topographic (neighborhood) weights for this batch- w_batch = model.neighborhood_function( d = model.map_dist(y_pred = y_pred), T = curr_T, neighborhood = 'gaussian' ) # Train on batch- loss = model.model.train_on_batch(x = x, y = [x, w_batch]) train_loss.append(loss.item()) curr_iter += 1

It gives me the Warning:

>

>

>

>

>

>

>

>

>

>

>

>

FieldValue
text I am using TensorFlow 2.16 and Python3 for implementing an AutoEncoder and Self-Organizing Map for MNIST dataset. The entire code can be referred to [here](https://github.com/arjun-majumdar/Autoencoders_Experiments/blob/master/desom_tensorflow2.py). For brevity, the main code is: # SOM hyper-params- map_height = 10 map_width = 10 gamma = 0.001 # Total number of train steps/iterations- total_iterations = len(train_dataset) * num_e…
label r/tensorflow
dataType post
communityName r/tensorflow
datetime 2024-05-20
username_encoded Z0FBQUFBQm5Lakw2UVhBekNWTXVIaVpOOVUxZ1czUUQ4Q2JldDU3aUI1dmQ0cEJqOEZXWnh3WlZROE03RkRoT1ZRQVpzLUVsczVQVDFXNkw3Q3hqNUdGVVhiemdRNVgxWFE9PQ==
url_encoded Z0FBQUFBQm5Lak9KdEdEWXN1d3UtelE2cjc3QWlhTkNMVGdyd1JlcEF6MlVCQUlMMXVzcEM4OENqMVNZZlNBdm0zSmctNXdZbzJma1A3bkppVHlyU0Y2X2lXbl9fWTRTV1pTUC1WZTFpT0sxZWF4Wnh2VHF2N1BpVXdHdjhYOUxNZG5QbU91Y2trblNhaXJYY2NTOG9qVWxFblpQeThoNmdyWmx0Rk9JcnBkVVBnaDVpV093d081b2g4cVgyTHctNlkxOU5WdE10X0xSblhLREkyM2w1TGdKV3VvN3BUM1Exdz09

Raw Record

{
  "text": "I am using TensorFlow 2.16 and Python3 for implementing an AutoEncoder and Self-Organizing Map for MNIST dataset. The entire code can be referred to [here](https://github.com/arjun-majumdar/Autoencoders_Experiments/blob/master/desom_tensorflow2.py). For brevity, the main code is:\n\n        # SOM hyper-params-\n        map_height = 10\n        map_width = 10\n        \n        gamma = 0.001\n        \n        # Total number of train steps/iterations-\n        total_iterations = len(train_dataset) * num_epochs\n        \n        # Temperature hyper-parm controlling radius of Gaussian neighborhood-\n        Tmax = 10.0\n        Tmin = 0.1\n       \n        \n        class DESOM(Model):\n            def __init__(\n                self, map_height = 10,\n                map_width = 10, latent_dim = 50,\n                encoder_dims = [1, 500, 500, 100]\n                ):\n                super(DESOM, self).__init__()\n                self.map_height = map_height\n                self.map_width = map_width\n                self.map_size = (self.map_height, self.map_width)\n                self.latent_dim = latent_dim\n                self.n_prototypes = self.map_size[0] * self.map_size[1]\n                self.encoder_dims = encoder_dims\n                self.encoder_dims.append(self.latent_dim)\n        \n                self.autoencoder, self.encoder, self.decoder = mlp_autoencoder(\n                    # encoder_dims = [X_train.shape[-1], 500, 500, 2000, latent_dim],\n                    encoder_dims = self.encoder_dims,\n                    act = 'relu', init = 'glorot_uniform',\n                    batchnorm = False\n                )\n        \n                # Initialize SOM layer-\n                self.som_layer = SOMLayer(\n                    map_size = (self.map_height, self.map_width), name = 'SOM'\n                )(self.encoder.output)\n        \n                # Create DESOM model\n                self.model = Model(\n                    inputs = self.autoencoder.input,\n                    outputs = [self.autoencoder.output, self.som_layer]\n                )\n        \n        \n            def compile(self, gamma:float = 0.001, optimizer:str = 'adam') -> None:\n                \"\"\"\n                Compile DESOM model\n        \n                Parameters\n                ----------\n                gamma : float\n                    coefficient of SOM loss (hyperparameter)\n                optimizer : str (default='adam')\n                    optimization algorithm\n                \"\"\"\n                self.model.compile(\n                    loss = {'decoder_0': 'mse', 'SOM': som_loss},\n                    # loss_weights = [1, gamma],\n                    loss_weights = {'decoder_0': 1.0, 'SOM': gamma},\n                    optimizer = optimizer\n                )\n        \n                return None\n        \n        \n            def predict(self, x):\n                \"\"\"\n                Predict best-matching unit using the output of SOM layer\n        \n                Parameters\n                ----------\n                x : array, shape = [n_samples, input_dim] or [n_samples, height, width, channels]\n                    input samples\n        \n                Returns\n                -------\n                y_pred : array, shape = [n_samples]\n                    index of the best-matching unit\n                \"\"\"\n                _, d = self.model.predict(x, verbose = 0)\n                return d.argmin(axis = 1)\n        \n        \n            def map_dist(self, y_pred):\n                \"\"\"\n                Calculate pairwise Manhattan distances between cluster assignments and map prototypes\n                (rectangular grid topology)\n        \n                Parameters\n                ----------\n                y_pred : array, shape = [n_samples]\n                    cluster assignments\n        \n                Returns\n                -------\n                d : array, shape = [n_samples, n_prototypes]\n                    pairwise distance matrix on the map\n                \"\"\"\n                \n                # y_pred = tf.argmin(input = pairwise_squared_l2dist, axis = 1)\n                labels = tf.range(self.n_prototypes)\n                tmp = tf.cast(\n                    x = tf.expand_dims(input = y_pred, axis = 1),\n                    dtype = tf.dtypes.int32\n                )\n                # print(labels.dtype, tmp.dtype, y_pred.dtype)\n                d_row = tf.abs(tmp - labels) // self.map_size[1]\n                d_col = tf.abs(tmp % self.map_size[1] - labels % self.map_size[1])\n        \n                # (d_row + d_col).dtype\n                # tf.int32\n        \n                d_row = tf.cast(x = d_row, dtype = tf.dtypes.float32)\n                d_col = tf.cast(x = d_col, dtype = tf.dtypes.float32)\n        \n                return d_row + d_col\n        \n        \n            def neighborhood_function(\n                self, d,\n                T, neighborhood = 'gaussian'\n            ):\n                \"\"\"\n                SOM neighborhood function (Gaussian neighborhood)\n        \n                Parameters\n                ----------\n                d : int\n                    distance on the map\n                T : float\n                    temperature parameter (neighborhood radius)\n                neighborhood : str\n                    type of neighborhood function ('gaussian' or 'window')\n        \n                Returns\n                -------\n                w : float in [0, 1]\n                    neighborhood weights\n                \"\"\"\n                if neighborhood == 'gaussian':\n                    # return np.exp(-(d ** 2) / (T ** 2))\n                    return tf.exp(-tf.square(d) / tf.square(T))\n                elif neighborhood == 'window':\n                    # return (d <= T).astype(np.float32)\n                    return tf.cast(x = (d <= T), dtype = tf.dtypes.float32)\n                else:\n                    raise ValueError('invalid neighborhood function')\n        \n        \n        # Initialize MLP AutoEncoder DESOM model-\n        model = DESOM(\n            map_height = map_height, map_width = map_width,\n            latent_dim = latent_dim,\n            encoder_dims = [784, 500, 500, 100]\n        )\n        \n        # Compile model-\n        model.compile(gamma = gamma, optimizer = 'adam')\n        \n        # Required for computing temperature for current train step-\n        # curr_iter = 1\n        curr_iter = tf.constant(1)\n        total_iterations = tf.cast(x = total_iterations, dtype = tf.dtypes.int32)\n        \n        # Train loss-\n        train_loss = list()\n        \n        \n        for epoch in range(1, num_epochs + 1):\n            for x, _ in train_dataset:\n        \n                # Compute bmu/cluster assignments for batch-\n                # _, d = model.model.predict(x)\n                _, d = model.model(x)\n                # y_pred = d.argmin(axis = 1)\n                y_pred = tf.argmin(input = d, axis = 1)\n                y_pred = tf.cast(x = y_pred, dtype = tf.dtypes.float32)\n        \n                # y_pred.shape, d.shape\n                # ((1024,), (1024, 100))\n        \n                # Compute temperature for current train step-\n                curr_T = tf.cast(\n                    x = Tmax * tf.pow((Tmin / Tmax), (curr_iter / total_iterations)),\n                    dtype = tf.dtypes.float32\n                    )\n        \n                # Compute topographic (neighborhood) weights for this batch-\n                w_batch = model.neighborhood_function(\n                    d = model.map_dist(y_pred = y_pred),\n                    T = curr_T, neighborhood = 'gaussian'\n                )\n        \n                # Train on batch-\n                loss = model.model.train_on_batch(x = x, y = [x, w_batch])\n                train_loss.append(loss.item())\n        \n                curr_iter += 1\n\nIt gives me the Warning:\n\n>\n\n>\n\n>\n\n>\n\n>\n\n>\n\n>\n\n>\n\n>\n\n>\n\n>\n\n>\n\n",
  "label": "r/tensorflow",
  "dataType": "post",
  "communityName": "r/tensorflow",
  "datetime": "2024-05-20",
  "username_encoded": "Z0FBQUFBQm5Lakw2UVhBekNWTXVIaVpOOVUxZ1czUUQ4Q2JldDU3aUI1dmQ0cEJqOEZXWnh3WlZROE03RkRoT1ZRQVpzLUVsczVQVDFXNkw3Q3hqNUdGVVhiemdRNVgxWFE9PQ==",
  "url_encoded": "Z0FBQUFBQm5Lak9KdEdEWXN1d3UtelE2cjc3QWlhTkNMVGdyd1JlcEF6MlVCQUlMMXVzcEM4OENqMVNZZlNBdm0zSmctNXdZbzJma1A3bkppVHlyU0Y2X2lXbl9fWTRTV1pTUC1WZTFpT0sxZWF4Wnh2VHF2N1BpVXdHdjhYOUxNZG5QbU91Y2trblNhaXJYY2NTOG9qVWxFblpQeThoNmdyWmx0Rk9JcnBkVVBnaDVpV093d081b2g4cVgyTHctNlkxOU5WdE10X0xSblhLREkyM2w1TGdKV3VvN3BUM1Exdz09"
}

Entry Information