|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +namespace Tensorflow.Clustering |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Creates the graph for k-means clustering. |
| 9 | + /// </summary> |
| 10 | + public class KMeans : Python |
| 11 | + { |
| 12 | + public const string CLUSTERS_VAR_NAME = "clusters"; |
| 13 | + |
| 14 | + public const string SQUARED_EUCLIDEAN_DISTANCE = "squared_euclidean"; |
| 15 | + public const string COSINE_DISTANCE = "cosine"; |
| 16 | + public const string RANDOM_INIT = "random"; |
| 17 | + public const string KMEANS_PLUS_PLUS_INIT = "kmeans_plus_plus"; |
| 18 | + public const string KMC2_INIT = "kmc2"; |
| 19 | + |
| 20 | + Tensor[] _inputs; |
| 21 | + int _num_clusters; |
| 22 | + IInitializer _initial_clusters; |
| 23 | + string _distance_metric; |
| 24 | + bool _use_mini_batch; |
| 25 | + int _mini_batch_steps_per_iteration; |
| 26 | + int _random_seed; |
| 27 | + int _kmeans_plus_plus_num_retries; |
| 28 | + int _kmc2_chain_length; |
| 29 | + |
| 30 | + public KMeans(Tensor inputs, |
| 31 | + int num_clusters, |
| 32 | + IInitializer initial_clusters = null, |
| 33 | + string distance_metric = SQUARED_EUCLIDEAN_DISTANCE, |
| 34 | + bool use_mini_batch = false, |
| 35 | + int mini_batch_steps_per_iteration = 1, |
| 36 | + int random_seed = 0, |
| 37 | + int kmeans_plus_plus_num_retries = 2, |
| 38 | + int kmc2_chain_length = 200) |
| 39 | + { |
| 40 | + _inputs = new Tensor[] { inputs }; |
| 41 | + _num_clusters = num_clusters; |
| 42 | + _initial_clusters = initial_clusters; |
| 43 | + _distance_metric = distance_metric; |
| 44 | + _use_mini_batch = use_mini_batch; |
| 45 | + _mini_batch_steps_per_iteration = mini_batch_steps_per_iteration; |
| 46 | + _random_seed = random_seed; |
| 47 | + _kmeans_plus_plus_num_retries = kmeans_plus_plus_num_retries; |
| 48 | + _kmc2_chain_length = kmc2_chain_length; |
| 49 | + } |
| 50 | + |
| 51 | + public object training_graph() |
| 52 | + { |
| 53 | + var initial_clusters = _initial_clusters; |
| 54 | + var num_clusters = ops.convert_to_tensor(_num_clusters); |
| 55 | + var inputs = _inputs; |
| 56 | + _create_variables(num_clusters); |
| 57 | + |
| 58 | + throw new NotImplementedException("KMeans training_graph"); |
| 59 | + } |
| 60 | + |
| 61 | + private RefVariable[] _create_variables(Tensor num_clusters) |
| 62 | + { |
| 63 | + var init_value = constant_op.constant(new float[0], dtype: TF_DataType.TF_FLOAT); |
| 64 | + var cluster_centers = tf.Variable(init_value, name: CLUSTERS_VAR_NAME, validate_shape: false); |
| 65 | + var cluster_centers_initialized = tf.Variable(false, dtype: TF_DataType.TF_BOOL, name: "initialized"); |
| 66 | + RefVariable update_in_steps = null; |
| 67 | + if (_use_mini_batch && _mini_batch_steps_per_iteration > 1) |
| 68 | + throw new NotImplementedException("KMeans._create_variables"); |
| 69 | + else |
| 70 | + { |
| 71 | + var cluster_centers_updated = cluster_centers; |
| 72 | + var cluster_counts = _use_mini_batch ? |
| 73 | + tf.Variable(array_ops.ones(new Tensor[] { num_clusters }, dtype: TF_DataType.TF_INT64)) : |
| 74 | + null; |
| 75 | + return new RefVariable[] |
| 76 | + { |
| 77 | + cluster_centers, |
| 78 | + cluster_centers_initialized, |
| 79 | + cluster_counts, |
| 80 | + cluster_centers_updated, |
| 81 | + update_in_steps |
| 82 | + }; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments