| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent fd32b84 commit b3cd413
13 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -150,7 +150,7 @@ public static (Dictionary<string, IVariableV1>, ITensorOrOperation[]) import_sco | |||
| 150 | 150 | var variables = graph.get_collection<IVariableV1>(tf.GraphKeys.GLOBAL_VARIABLES, | |
| 151 | 151 | scope: scope_to_prepend_to_names); | |
| 152 | 152 | var var_list = new Dictionary<string, IVariableV1>(); | |
| 153 | - variables.ForEach(v => var_list[ops.strip_name_scope(v.Name, scope_to_prepend_to_names)] = v); | ||
| 153 | + // variables.ForEach(v => var_list[ops.strip_name_scope(v.Name, scope_to_prepend_to_names)] = v); | ||
| 154 | 154 | ||
| 155 | 155 | return (var_list, imported_return_elements); | |
| 156 | 156 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | using NumSharp; | |
| 2 | 2 | using System; | |
| 3 | 3 | using Tensorflow.Keras.ArgsDefinition; | |
| 4 | + using Tensorflow.Keras.Losses; | ||
| 4 | 5 | using Tensorflow.Keras.Optimizers; | |
| 5 | 6 | ||
| 6 | 7 | namespace Tensorflow.Keras.Engine | |
@@ -42,6 +43,11 @@ public void compile(string optimizerName, string lossName) | |||
| 42 | 43 | // Prepare list of loss functions, same size of model outputs. | |
| 43 | 44 | } | |
| 44 | 45 | ||
| 46 | + public void compile(string optimizerName, ILossFunc lossName) | ||
| 47 | + { | ||
| 48 | + throw new NotImplementedException(""); | ||
| 49 | + } | ||
| 50 | + | ||
| 45 | 51 | /// <summary> | |
| 46 | 52 | /// Generates output predictions for the input samples. | |
| 47 | 53 | /// </summary> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,6 +7,7 @@ | |||
| 7 | 7 | using Tensorflow.Keras.Datasets; | |
| 8 | 8 | using Tensorflow.Keras.Engine; | |
| 9 | 9 | using Tensorflow.Keras.Layers; | |
| 10 | + using Tensorflow.Keras.Losses; | ||
| 10 | 11 | using static Tensorflow.Binding; | |
| 11 | 12 | ||
| 12 | 13 | namespace Tensorflow | |
@@ -16,6 +17,7 @@ public class KerasApi | |||
| 16 | 17 | public KerasDataset datasets { get; } = new KerasDataset(); | |
| 17 | 18 | public Initializers initializers { get; } = new Initializers(); | |
| 18 | 19 | public LayersApi layers { get; } = new LayersApi(); | |
| 20 | + public LossesApi losses { get; } = new LossesApi(); | ||
| 19 | 21 | public Activations activations { get; } = new Activations(); | |
| 20 | 22 | public Preprocessing preprocessing { get; } = new Preprocessing(); | |
| 21 | 23 | public BackendImpl backend { get; } = new BackendImpl(); | |
@@ -69,52 +71,5 @@ public Tensor Input(TensorShape shape = null, | |||
| 69 | 71 | ||
| 70 | 72 | return layer.InboundNodes[0].Outputs; | |
| 71 | 73 | } | |
| 72 | - | ||
| 73 | - public class LayersApi | ||
| 74 | - { | ||
| 75 | - public Rescaling Rescaling(float scale, | ||
| 76 | - float offset = 0, | ||
| 77 | - TensorShape input_shape = null) | ||
| 78 | - => new Rescaling(new RescalingArgs | ||
| 79 | - { | ||
| 80 | - Scale = scale, | ||
| 81 | - Offset = offset, | ||
| 82 | - InputShape = input_shape | ||
| 83 | - }); | ||
| 84 | - | ||
| 85 | - public Dense Dense(int units, | ||
| 86 | - Activation activation = null, | ||
| 87 | - TensorShape input_shape = null) | ||
| 88 | - => new Dense(new DenseArgs | ||
| 89 | - { | ||
| 90 | - Units = units, | ||
| 91 | - Activation = activation ?? tf.keras.activations.Linear, | ||
| 92 | - InputShape = input_shape | ||
| 93 | - }); | ||
| 94 | - | ||
| 95 | - /// <summary> | ||
| 96 | - /// Turns positive integers (indexes) into dense vectors of fixed size. | ||
| 97 | - /// </summary> | ||
| 98 | - /// <param name="input_dim"></param> | ||
| 99 | - /// <param name="output_dim"></param> | ||
| 100 | - /// <param name="embeddings_initializer"></param> | ||
| 101 | - /// <param name="mask_zero"></param> | ||
| 102 | - /// <returns></returns> | ||
| 103 | - public Embedding Embedding(int input_dim, | ||
| 104 | - int output_dim, | ||
| 105 | - IInitializer embeddings_initializer = null, | ||
| 106 | - bool mask_zero = false, | ||
| 107 | - TensorShape input_shape = null, | ||
| 108 | - int input_length = -1) | ||
| 109 | - => new Embedding(new EmbeddingArgs | ||
| 110 | - { | ||
| 111 | - InputDim = input_dim, | ||
| 112 | - OutputDim = output_dim, | ||
| 113 | - MaskZero = mask_zero, | ||
| 114 | - InputShape = input_shape ?? input_length, | ||
| 115 | - InputLength = input_length, | ||
| 116 | - EmbeddingsInitializer = embeddings_initializer | ||
| 117 | - }); | ||
| 118 | - } | ||
| 119 | 74 | } | |
| 120 | 75 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,95 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Text; | ||
| 4 | + using Tensorflow.Keras.ArgsDefinition; | ||
| 5 | + using Tensorflow.Keras.Engine; | ||
| 6 | + using static Tensorflow.Binding; | ||
| 7 | + | ||
| 8 | + namespace Tensorflow.Keras.Layers | ||
| 9 | + { | ||
| 10 | + public class LayersApi | ||
| 11 | + { | ||
| 12 | + public Conv2D Conv2D(int filters, | ||
| 13 | + TensorShape kernel_size = null, | ||
| 14 | + string padding = "valid", | ||
| 15 | + string activation = "relu") | ||
| 16 | + => new Conv2D(new Conv2DArgs | ||
| 17 | + { | ||
| 18 | + Filters = filters, | ||
| 19 | + KernelSize = kernel_size, | ||
| 20 | + Padding = padding, | ||
| 21 | + Activation = GetActivationByName(activation) | ||
| 22 | + }); | ||
| 23 | + | ||
| 24 | + | ||
| 25 | + public Dense Dense(int units, | ||
| 26 | + string activation = "linear", | ||
| 27 | + TensorShape input_shape = null) | ||
| 28 | + => new Dense(new DenseArgs | ||
| 29 | + { | ||
| 30 | + Units = units, | ||
| 31 | + Activation = GetActivationByName(activation), | ||
| 32 | + InputShape = input_shape | ||
| 33 | + }); | ||
| 34 | + | ||
| 35 | + /// <summary> | ||
| 36 | + /// Turns positive integers (indexes) into dense vectors of fixed size. | ||
| 37 | + /// </summary> | ||
| 38 | + /// <param name="input_dim"></param> | ||
| 39 | + /// <param name="output_dim"></param> | ||
| 40 | + /// <param name="embeddings_initializer"></param> | ||
| 41 | + /// <param name="mask_zero"></param> | ||
| 42 | + /// <returns></returns> | ||
| 43 | + public Embedding Embedding(int input_dim, | ||
| 44 | + int output_dim, | ||
| 45 | + IInitializer embeddings_initializer = null, | ||
| 46 | + bool mask_zero = false, | ||
| 47 | + TensorShape input_shape = null, | ||
| 48 | + int input_length = -1) | ||
| 49 | + => new Embedding(new EmbeddingArgs | ||
| 50 | + { | ||
| 51 | + InputDim = input_dim, | ||
| 52 | + OutputDim = output_dim, | ||
| 53 | + MaskZero = mask_zero, | ||
| 54 | + InputShape = input_shape ?? input_length, | ||
| 55 | + InputLength = input_length, | ||
| 56 | + EmbeddingsInitializer = embeddings_initializer | ||
| 57 | + }); | ||
| 58 | + | ||
| 59 | + public Flatten Flatten(string data_format = null) | ||
| 60 | + => new Flatten(new FlattenArgs | ||
| 61 | + { | ||
| 62 | + DataFormat = data_format | ||
| 63 | + }); | ||
| 64 | + | ||
| 65 | + public MaxPooling2D MaxPooling2D(TensorShape pool_size = null, | ||
| 66 | + TensorShape strides = null, | ||
| 67 | + string padding = "valid") | ||
| 68 | + => new MaxPooling2D(new MaxPooling2DArgs | ||
| 69 | + { | ||
| 70 | + PoolSize = pool_size ?? (2, 2), | ||
| 71 | + Strides = strides, | ||
| 72 | + Padding = padding | ||
| 73 | + }); | ||
| 74 | + | ||
| 75 | + public Rescaling Rescaling(float scale, | ||
| 76 | + float offset = 0, | ||
| 77 | + TensorShape input_shape = null) | ||
| 78 | + => new Rescaling(new RescalingArgs | ||
| 79 | + { | ||
| 80 | + Scale = scale, | ||
| 81 | + Offset = offset, | ||
| 82 | + InputShape = input_shape | ||
| 83 | + }); | ||
| 84 | + | ||
| 85 | + Activation GetActivationByName(string name) | ||
| 86 | + => name switch | ||
| 87 | + { | ||
| 88 | + "linear" => tf.keras.activations.Linear, | ||
| 89 | + "relu" => tf.keras.activations.Relu, | ||
| 90 | + "sigmoid" => tf.keras.activations.Sigmoid, | ||
| 91 | + "tanh" => tf.keras.activations.Tanh, | ||
| 92 | + _ => tf.keras.activations.Linear | ||
| 93 | + }; | ||
| 94 | + } | ||
| 95 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,7 +30,7 @@ public Pooling2D(Pooling2DArgs args) | |||
| 30 | 30 | { | |
| 31 | 31 | this.args = args; | |
| 32 | 32 | args.PoolSize = conv_utils.normalize_tuple(args.PoolSize, 2, "pool_size"); | |
| 33 | - args.Strides = conv_utils.normalize_tuple(args.Strides, 2, "strides"); | ||
| 33 | + args.Strides = conv_utils.normalize_tuple(args.Strides ?? args.PoolSize, 2, "strides"); | ||
| 34 | 34 | args.Padding = conv_utils.normalize_padding(args.Padding); | |
| 35 | 35 | args.DataFormat = conv_utils.normalize_data_format(args.DataFormat); | |
| 36 | 36 | input_spec = new InputSpec(ndim: 4); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,7 +23,8 @@ public Rescaling(RescalingArgs args) : base(args) | |||
| 23 | 23 | protected override Tensor call(Tensor inputs, bool is_training = false, Tensor state = null) | |
| 24 | 24 | { | |
| 25 | 25 | scale = math_ops.cast(args.Scale, args.DType); | |
| 26 | - throw new NotImplementedException(""); | ||
| 26 | + offset = math_ops.cast(args.Offset, args.DType); | ||
| 27 | + return math_ops.cast(inputs, args.DType) * scale + offset; | ||
| 27 | 28 | } | |
| 28 | 29 | } | |
| 29 | 30 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,10 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Text; | ||
| 4 | + | ||
| 5 | + namespace Tensorflow.Keras.Losses | ||
| 6 | + { | ||
| 7 | + public interface ILossFunc | ||
| 8 | + { | ||
| 9 | + } | ||
| 10 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,29 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Text; | ||
| 4 | + | ||
| 5 | + namespace Tensorflow.Keras.Losses | ||
| 6 | + { | ||
| 7 | + /// <summary> | ||
| 8 | + /// Loss base class. | ||
| 9 | + /// </summary> | ||
| 10 | + public abstract class Loss | ||
| 11 | + { | ||
| 12 | + protected string reduction; | ||
| 13 | + protected string name; | ||
| 14 | + bool _allow_sum_over_batch_size; | ||
| 15 | + string _name_scope; | ||
| 16 | + | ||
| 17 | + public Loss(string reduction = ReductionV2.AUTO, string name = null) | ||
| 18 | + { | ||
| 19 | + this.reduction = reduction; | ||
| 20 | + this.name = name; | ||
| 21 | + _allow_sum_over_batch_size = false; | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + void _set_name_scope() | ||
| 25 | + { | ||
| 26 | + _name_scope = name; | ||
| 27 | + } | ||
| 28 | + } | ||
| 29 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,20 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Text; | ||
| 4 | + | ||
| 5 | + namespace Tensorflow.Keras.Losses | ||
| 6 | + { | ||
| 7 | + public class LossFunctionWrapper : Loss | ||
| 8 | + { | ||
| 9 | + Action fn; | ||
| 10 | + | ||
| 11 | + public LossFunctionWrapper(Action fn, | ||
| 12 | + string reduction = ReductionV2.AUTO, | ||
| 13 | + string name = null) | ||
| 14 | + : base(reduction: reduction, | ||
| 15 | + name: name) | ||
| 16 | + { | ||
| 17 | + this.fn = fn; | ||
| 18 | + } | ||
| 19 | + } | ||
| 20 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,12 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Text; | ||
| 4 | + | ||
| 5 | + namespace Tensorflow.Keras.Losses | ||
| 6 | + { | ||
| 7 | + public class LossesApi | ||
| 8 | + { | ||
| 9 | + public ILossFunc SparseCategoricalCrossentropy(bool from_logits = false) | ||
| 10 | + => new SparseCategoricalCrossentropy(from_logits: from_logits); | ||
| 11 | + } | ||
| 12 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments