Class BaseModel
Implements
Inherited Members
Namespace: Keras.Models
Assembly: Keras.dll
Syntax
public class BaseModel : Base, IDisposable
Methods
| Improve this Doc View SourceCompile(StringOrInstance, String, String[], Single[], String, String[], NDarray[])
Configures the model for training.
Declaration
public void Compile(StringOrInstance optimizer, string loss, string[] metrics = null, float[] loss_weights = null, string sample_weight_mode = "None", string[] weighted_metrics = null, NDarray[] target_tensors = null)
Parameters
Type | Name | Description |
---|---|---|
StringOrInstance | optimizer | String (name of optimizer) or optimizer instance. See optimizers. |
System.String | loss | String (name of objective function) or objective function. See losses. If the model has multiple outputs, you can use a different loss on each output by passing a dictionary or a list of losses. The loss value that will be minimized by the model will then be the sum of all individual losses. |
System.String[] | metrics | List of metrics to be evaluated by the model during training and testing. Typically you will use metrics=['accuracy']. To specify different metrics for different outputs of a multi-output model, you could also pass a dictionary, such as metrics={'output_a': 'accuracy'}. |
System.Single[] | loss_weights | Optional list or dictionary specifying scalar coefficients (Python floats) to weight the loss contributions of different model outputs. The loss value that will be minimized by the model will then be the weighted sum of all individual losses, weighted by the loss_weightscoefficients. If a list, it is expected to have a 1:1 mapping to the model's outputs. If a tensor, it is expected to map output names (strings) to scalar coefficients. |
System.String | sample_weight_mode | If you need to do timestep-wise sample weighting (2D weights), set this to "temporal". None defaults to sample-wise weights (1D). If the model has multiple outputs, you can use a different sample_weight_mode on each output by passing a dictionary or a list of modes. |
System.String[] | weighted_metrics | List of metrics to be evaluated and weighted by sample_weight or class_weight during training and testing. |
Numpy.NDarray[] | target_tensors | By default, Keras will create placeholders for the model's target, which will be fed with the target data during training. If instead you would like to use your own target tensors (in turn, Keras will not expect external Numpy data for these targets at training time), you can specify them via the target_tensors argument. It can be a single tensor (for a single-output model), a list of tensors, or a dict mapping output names to target tensors. |
Evaluate(NDarray, NDarray, Nullable<Int32>, Int32, NDarray, Nullable<Int32>, Callback[])
Declaration
public double[] Evaluate(NDarray x, NDarray y, int? batch_size = default(int? ), int verbose = 1, NDarray sample_weight = null, int? steps = default(int? ), Callback[] callbacks = null)
Parameters
Type | Name | Description |
---|---|---|
Numpy.NDarray | x | |
Numpy.NDarray | y | |
System.Nullable<System.Int32> | batch_size | |
System.Int32 | verbose | |
Numpy.NDarray | sample_weight | |
System.Nullable<System.Int32> | steps | |
Callback[] | callbacks |
Returns
Type | Description |
---|---|
System.Double[] |
Fit(NDarray, NDarray, Nullable<Int32>, Int32, Int32, Callback[], Single, NDarray[], Boolean, Dictionary<Int32, Single>, NDarray, Int32, Nullable<Int32>, Nullable<Int32>)
Trains the model for a given number of epochs (iterations on a dataset).
Declaration
public History Fit(NDarray x, NDarray y, int? batch_size = default(int? ), int epochs = 1, int verbose = 1, Callback[] callbacks = null, float validation_split = 0F, NDarray[] validation_data = null, bool shuffle = true, Dictionary<int, float> class_weight = null, NDarray sample_weight = null, int initial_epoch = 0, int? steps_per_epoch = default(int? ), int? validation_steps = default(int? ))
Parameters
Type | Name | Description |
---|---|---|
Numpy.NDarray | x | Numpy array of training data (if the model has a single input), or list of Numpy arrays (if the model has multiple inputs). If input layers in the model are named, you can also pass a dictionary mapping input names to Numpy arrays. x can be None (default) if feeding from framework-native tensors (e.g. TensorFlow data tensors). |
Numpy.NDarray | y | Numpy array of target (label) data (if the model has a single output), or list of Numpy arrays (if the model has multiple outputs). If output layers in the model are named, you can also pass a dictionary mapping output names to Numpy arrays. y can be None (default) if feeding from framework-native tensors (e.g. TensorFlow data tensors). |
System.Nullable<System.Int32> | batch_size | Integer or None. Number of samples per gradient update. If unspecified, batch_sizewill default to 32. |
System.Int32 | epochs | Integer. Number of epochs to train the model. An epoch is an iteration over the entire x and y data provided. Note that in conjunction with initial_epoch, epochs is to be understood as "final epoch". The model is not trained for a number of iterations given by epochs, but merely until the epoch of index epochs is reached. |
System.Int32 | verbose | Integer. 0, 1, or 2. Verbosity mode. 0 = silent, 1 = progress bar, 2 = one line per epoch. |
Callback[] | callbacks | List of keras.callbacks.Callback instances. List of callbacks to apply during training and validation (if ). See callbacks. |
System.Single | validation_split | Float between 0 and 1. Fraction of the training data to be used as validation data. The model will set apart this fraction of the training data, will not train on it, and will evaluate the loss and any model metrics on this data at the end of each epoch. The validation data is selected from the last samples in the x and y data provided, before shuffling. |
Numpy.NDarray[] | validation_data | tuple (x_val, y_val) or tuple (x_val, y_val, val_sample_weights) on which to evaluate the loss and any model metrics at the end of each epoch. The model will not be trained on this data. validation_data will override validation_split. |
System.Boolean | shuffle | Boolean (whether to shuffle the training data before each epoch) or str (for 'batch'). 'batch' is a special option for dealing with the limitations of HDF5 data; it shuffles in batch-sized chunks. Has no effect when steps_per_epoch is not None. |
System.Collections.Generic.Dictionary<System.Int32, System.Single> | class_weight | Optional dictionary mapping class indices (integers) to a weight (float) value, used for weighting the loss function (during training only). This can be useful to tell the model to "pay more attention" to samples from an under-represented class. |
Numpy.NDarray | sample_weight | Optional Numpy array of weights for the training samples, used for weighting the loss function (during training only). You can either pass a flat (1D) Numpy array with the same length as the input samples (1:1 mapping between weights and samples), or in the case of temporal data, you can pass a 2D array with shape (samples, sequence_length), to apply a different weight to every timestep of every sample. In this case you should make sure to specifysample_weight_mode="temporal" in compile(). |
System.Int32 | initial_epoch | Integer. Epoch at which to start training (useful for resuming a previous training run). |
System.Nullable<System.Int32> | steps_per_epoch | Integer or None. Total number of steps (batches of samples) before declaring one epoch finished and starting the next epoch. When training with input tensors such as TensorFlow data tensors, the default None is equal to the number of samples in your dataset divided by the batch size, or 1 if that cannot be determined. |
System.Nullable<System.Int32> | validation_steps | Only relevant if steps_per_epoch is specified. Total number of steps (batches of samples) to validate before stopping. |
Returns
Type | Description |
---|---|
History | A History object. Its History.history attribute is a record of training loss values and metrics values at successive epochs, as well as validation loss values and validation metrics values (if applicable). |
LoadModel(String)
Loads the model.
Declaration
public static BaseModel LoadModel(string path)
Parameters
Type | Name | Description |
---|---|---|
System.String | path | The path. |
Returns
Type | Description |
---|---|
BaseModel |
LoadWeight(String)
Loads the weight to the model from a file.
Declaration
public void LoadWeight(string path)
Parameters
Type | Name | Description |
---|---|---|
System.String | path | The path of of the weight file. |
ModelFromJson(String)
Load the model from json.
Declaration
public static BaseModel ModelFromJson(string json_string)
Parameters
Type | Name | Description |
---|---|---|
System.String | json_string | The json string. |
Returns
Type | Description |
---|---|
BaseModel | The model |
ModelFromYaml(String)
Load the model from yaml.
Declaration
public static BaseModel ModelFromYaml(string json_string)
Parameters
Type | Name | Description |
---|---|---|
System.String | json_string | The json string. |
Returns
Type | Description |
---|---|
BaseModel | The model |
Predict(NDarray, Nullable<Int32>, Int32, Nullable<Int32>, Callback[])
Generates output predictions for the input samples. Computation is done in batches.
Declaration
public NDarray Predict(NDarray x, int? batch_size = default(int? ), int verbose = 1, int? steps = default(int? ), Callback[] callbacks = null)
Parameters
Type | Name | Description |
---|---|---|
Numpy.NDarray | x | The input data, as a Numpy array (or list of Numpy arrays if the model has multiple inputs). |
System.Nullable<System.Int32> | batch_size | Integer. If unspecified, it will default to 32. |
System.Int32 | verbose | Verbosity mode, 0 or 1. |
System.Nullable<System.Int32> | steps | Total number of steps (batches of samples) before declaring the prediction round finished. Ignored with the default value of None. |
Callback[] | callbacks | List of keras.callbacks.Callback instances. List of callbacks to apply during prediction. See callbacks. |
Returns
Type | Description |
---|---|
Numpy.NDarray | Numpy array(s) of predictions. |
PredictOnBatch(NDarray)
Returns predictions for a single batch of samples.
Declaration
public NDarray PredictOnBatch(NDarray x)
Parameters
Type | Name | Description |
---|---|---|
Numpy.NDarray | x | Input samples, as a Numpy array. |
Returns
Type | Description |
---|---|
Numpy.NDarray | Numpy array(s) of predictions. |
Save(String)
Save the model to h5 file
Declaration
public void Save(string path)
Parameters
Type | Name | Description |
---|---|---|
System.String | path | The path with filename eg: model.h5. |
SaveOnnx(String)
Saves keras model to onnx.
Declaration
public void SaveOnnx(string filePath)
Parameters
Type | Name | Description |
---|---|---|
System.String | filePath | The file path. |
SaveTensorflowJSFormat(String, Boolean)
Saves the tensorflow js format.
Declaration
public void SaveTensorflowJSFormat(string artifacts_dir, bool quantize = false)
Parameters
Type | Name | Description |
---|---|---|
System.String | artifacts_dir | The artifacts dir. |
System.Boolean | quantize | if set to |
SaveWeight(String)
Saves the weight of the trained model to a file.
Declaration
public void SaveWeight(string path)
Parameters
Type | Name | Description |
---|---|---|
System.String | path | The path of the weight to save. |
Summary(Nullable<Int32>, Single[])
Summaries the specified line length.
Declaration
public void Summary(int? line_length = default(int? ), float[] positions = null)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.Int32> | line_length | Length of the line. |
System.Single[] | positions | The positions. |
TestOnBatch(NDarray, NDarray, NDarray)
Tests the on batch.
Declaration
public double[] TestOnBatch(NDarray x, NDarray y, NDarray sample_weight = null)
Parameters
Type | Name | Description |
---|---|---|
Numpy.NDarray | x | Numpy array of test data, or list of Numpy arrays if the model has multiple inputs. If all inputs in the model are named, you can also pass a dictionary mapping input names to Numpy arrays. |
Numpy.NDarray | y | Numpy array of target data, or list of Numpy arrays if the model has multiple outputs. If all outputs in the model are named, you can also pass a dictionary mapping output names to Numpy arrays. |
Numpy.NDarray | sample_weight | Optional array of the same length as x, containing weights to apply to the model's loss for each sample. In the case of temporal data, you can pass a 2D array with shape (samples, sequence_length), to apply a different weight to every timestep of every sample. In this case you should make sure to specify sample_weight_mode="temporal" in compile(). |
Returns
Type | Description |
---|---|
System.Double[] | Scalar test loss (if the model has a single output and no metrics) or list of scalars (if the model has multiple outputs and/or metrics). The attribute model.metrics_names will give you the display labels for the scalar outputs. |
ToJson()
Converts the model to json.
Declaration
public string ToJson()
Returns
Type | Description |
---|---|
System.String |
TrainOnBatch(NDarray, NDarray, NDarray, Dictionary<Int32, Single>)
Runs a single gradient update on a single batch of data.
Declaration
public double[] TrainOnBatch(NDarray x, NDarray y, NDarray sample_weight = null, Dictionary<int, float> class_weight = null)
Parameters
Type | Name | Description |
---|---|---|
Numpy.NDarray | x | Numpy array of training data, or list of Numpy arrays if the model has multiple inputs. If all inputs in the model are named, you can also pass a dictionary mapping input names to Numpy arrays. |
Numpy.NDarray | y | Numpy array of target data, or list of Numpy arrays if the model has multiple outputs. If all outputs in the model are named, you can also pass a dictionary mapping output names to Numpy arrays. |
Numpy.NDarray | sample_weight | Optional array of the same length as x, containing weights to apply to the model's loss for each sample. In the case of temporal data, you can pass a 2D array with shape (samples, sequence_length), to apply a different weight to every timestep of every sample. In this case you should make sure to specify sample_weight_mode="temporal" in compile(). |
System.Collections.Generic.Dictionary<System.Int32, System.Single> | class_weight | Optional dictionary mapping class indices (integers) to a weight (float) to apply to the model's loss for the samples from this class during training. This can be useful to tell the model to "pay more attention" to samples from an under-represented class. |
Returns
Type | Description |
---|---|
System.Double[] | Scalar training loss (if the model has a single output and no metrics) or list of scalars (if the model has multiple outputs and/or metrics). The attribute model.metrics_names will give you the display labels for the scalar outputs. |