|
1 | 1 | using Microsoft.VisualStudio.TestPlatform.Utilities;
|
2 | 2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
3 | 3 | using Newtonsoft.Json.Linq;
|
| 4 | +using System.Collections.Generic; |
4 | 5 | using System.Linq;
|
5 | 6 | using System.Xml.Linq;
|
6 | 7 | using Tensorflow.Keras.Engine;
|
@@ -129,6 +130,53 @@ public void TestModelBeforeTF2_5()
|
129 | 130 | }
|
130 | 131 |
|
131 | 132 |
|
| 133 | + [TestMethod] |
| 134 | + public void BiasRegularizerSaveAndLoad() |
| 135 | + { |
| 136 | + var savemodel = keras.Sequential(new List<ILayer>() |
| 137 | + { |
| 138 | + tf.keras.layers.InputLayer((227, 227, 3)), |
| 139 | + tf.keras.layers.Conv2D(96, (11, 11), (4, 4), activation:"relu", padding:"valid"), |
| 140 | + tf.keras.layers.BatchNormalization(), |
| 141 | + tf.keras.layers.MaxPooling2D((3, 3), strides:(2, 2)), |
| 142 | + |
| 143 | + tf.keras.layers.Conv2D(256, (5, 5), (1, 1), "same", activation: keras.activations.Relu, bias_regularizer:keras.regularizers.L1L2), |
| 144 | + tf.keras.layers.BatchNormalization(), |
| 145 | + |
| 146 | + tf.keras.layers.Conv2D(256, (5, 5), (1, 1), "same", activation: keras.activations.Relu, bias_regularizer:keras.regularizers.L2), |
| 147 | + tf.keras.layers.BatchNormalization(), |
| 148 | + |
| 149 | + tf.keras.layers.Conv2D(256, (5, 5), (1, 1), "same", activation: keras.activations.Relu, bias_regularizer:keras.regularizers.L1), |
| 150 | + tf.keras.layers.BatchNormalization(), |
| 151 | + tf.keras.layers.MaxPooling2D((3, 3), (2, 2)), |
| 152 | + |
| 153 | + tf.keras.layers.Flatten(), |
| 154 | + |
| 155 | + tf.keras.layers.Dense(1000, activation: "linear"), |
| 156 | + tf.keras.layers.Softmax(1) |
| 157 | + }); |
| 158 | + |
| 159 | + savemodel.compile(tf.keras.optimizers.Adam(), tf.keras.losses.SparseCategoricalCrossentropy(from_logits: true), new string[] { "accuracy" }); |
| 160 | + |
| 161 | + var num_epochs = 1; |
| 162 | + var batch_size = 8; |
| 163 | + |
| 164 | + var trainDataset = new RandomDataSet(new Shape(227, 227, 3), 16); |
| 165 | + |
| 166 | + savemodel.fit(trainDataset.Data, trainDataset.Labels, batch_size, num_epochs); |
| 167 | + |
| 168 | + savemodel.save(@"./bias_regularizer_save_and_load", save_format: "tf"); |
| 169 | + |
| 170 | + var loadModel = tf.keras.models.load_model(@"./bias_regularizer_save_and_load"); |
| 171 | + loadModel.summary(); |
| 172 | + |
| 173 | + loadModel.compile(tf.keras.optimizers.Adam(), tf.keras.losses.SparseCategoricalCrossentropy(from_logits: true), new string[] { "accuracy" }); |
| 174 | + |
| 175 | + var fitDataset = new RandomDataSet(new Shape(227, 227, 3), 16); |
| 176 | + |
| 177 | + loadModel.fit(fitDataset.Data, fitDataset.Labels, batch_size, num_epochs); |
| 178 | + } |
| 179 | + |
132 | 180 |
|
133 | 181 | [TestMethod]
|
134 | 182 | public void CreateConcatenateModelSaveAndLoad()
|
|
0 commit comments